-
Notifications
You must be signed in to change notification settings - Fork 0
Working with arrays
Conan edited this page May 6, 2022
·
4 revisions
import { sift, byPattern } from 'sift-r'
import { byPattern } from 'sift-r'
const filtered = [
{ user: 'barney', age: 36, active: false },
{ user: 'fred', age: 40, active: true },
{ user: 'pebbles', age: 1, active: false }
].filter(byPattern({ age: 36 }))
// filtered ===
// [
// { user: 'barney', age: 36, active: false }
// ]
import { pluck, isNumber } from 'match-iz'
const mapped = [
{ user: 'barney', age: 36, active: false },
{ user: 'fred', age: 40, active: true },
{ user: 'pebbles', age: 1, active: false },
{ user: '', age: 'not-a-number', active: true }
].map(byPattern({ age: pluck(isNumber) }))
// mapped === [36, 40, 1, undefined]
// 5:
//
const [strValues, theRest] = sift([
[isString, 'header'],
[isString, 1],
[isString, '# header']
])
// 6:
//
const [strValues, theRest] = sift(
['header', 1, '# header'],
[isString, isString, isString]
)
// 7:
//
const [strValues, theRest] = sift(
['header', 1, '# header'],
isString
)
// 8:
//
const [strValues, numValues, theRest] = sift(
['header', 1, '# header', 2, false, true],
isString,
isNumber
)
sift-r
📥 | on npm | docs home | top of page