Merge pull request #940 from skatcat31/master

[FEATURE] add checkProp utility function for inspecting objects
This commit is contained in:
Robert Mennell
2019-03-17 08:10:07 -07:00
committed by GitHub
2 changed files with 30 additions and 0 deletions

29
snippets/checkProp.md Normal file
View File

@ -0,0 +1,29 @@
### 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.
It summons `prop` on `obj` and passes it to a provided `predicate` function and returns a masked boolean
```js
const checkProp = (predicate, prop) => obj => !!predicate(obj[prop])
```
```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 session = { user: {} }
const validUserSession = checkProps(u => u.active && !u.disabled, 'user')
validUserSession(session) // false
session.user.active = true
validUserSession(session) // true
const noLength(l => l === undefined, 'length')
noLength([]) // false
noLength({}) // true
noLength(new Set()) // true
```

View File

@ -23,6 +23,7 @@ capitalize:string,array,intermediate
capitalizeEveryWord:string,regexp,intermediate
castArray:utility,array,type,beginner
chainAsync:function,intermediate
checkProp:function,object,utility,beginner
chunk:array,intermediate
clampNumber:math,beginner
cloneRegExp:utility,regexp,intermediate