Update checkProp.md

This commit is contained in:
Angelos Chalaris
2019-03-18 08:29:42 +02:00
committed by GitHub
parent 3d96e30092
commit 611c0132fe

View File

@ -1,30 +1,29 @@
### checkProp ### checkProp
Given a `predicate` function and a `prop` string this curried function will then take an `object` to inspect by calling the property and passing it to the predicate. Given a `predicate` function and a `prop` string, this curried function will then take an `object` to inspect by calling the property and passing it to the predicate.
It summons `prop` on `obj` and passes it to a provided `predicate` function and returns a masked boolean Summon `prop` on `obj`, pass it to a provided `predicate` function and return a masked boolean.
```js ```js
const checkProp = (predicate, prop) => obj => !!predicate(obj[prop]); const checkProp = (predicate, prop) => obj => !!predicate(obj[prop]);
``` ```
```js ```js
const lengthIs4 = checkProp(l => l === 4, 'length');
lengthIs4([]); // false
lengthIs4([1,2,3,4]); // true
lengthIs4(new Set([1,2,3,4])); // false (Set uses Size, not length)
const lengthIs4 = checkProp(l => l === 4, 'length') const session = { user: {} };
lengthIs4([]) // false const validUserSession = checkProps(u => u.active && !u.disabled, 'user');
lengthIs4([1,2,3,4]) // true
lengthIs4(new Set([1,2,3,4])) // false (Set uses Size, not length)
const session = { user: {} } validUserSession(session); // false
const validUserSession = checkProps(u => u.active && !u.disabled, 'user')
validUserSession(session) // false session.user.active = true;
validUserSession(session); // true
session.user.active = true const noLength(l => l === undefined, 'length');
validUserSession(session) // true noLength([]); // false
noLength({}); // true
const noLength(l => l === undefined, 'length') noLength(new Set()); // true
noLength([]) // false
noLength({}) // true
noLength(new Set()) // true
``` ```