sift-r
v1.4.0
Published
Apportion objects / arrays into multiple buckets based on predicates / patterns
Downloads
5,199
Maintainers
Readme
A complement to match-iz for filtering objects, arrays, and Map
/Set
based on a match-iz
pattern.
- 🗺 byPattern() for map/filter
- 📥 sift() an object
- 📁 sift() an array
- 🗂 sift() an array into multiple buckets
- 🗺 sift() a Map or Set
- 🔢 sift() an iterable
- 📖 Documentation
- 📀 Install / Use
byPattern()
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]
sift()
an object
import { sift } from 'sift-r'
const isString = x => typeof x === 'string'
const isNumber = x => typeof x === 'number'
const [strValues, numValues, neither] = sift(
{
title: 'header',
slug: 1,
markdown: '# header',
footer: undefined
},
[isString, isNumber]
)
// strValues ===
// {
// title: 'header',
// markdown: '# header'
// }
// numValues ===
// {
// slug: 1
// }
// neither ===
// {
// footer: undefined
// }
sift()
an array
(+using match-iz pattern-helpers)
import { sift } from 'sift-r'
import { gte, allOf } from 'match-iz'
const users = [
{ user: 'barney', age: 36, active: false },
{ user: 'fred', age: 40, active: true },
{ user: 'pebbles', age: 1, active: true }
]
const isActive = { active: true }
const isGrownUp = { age: gte(18) }
const isActiveGrownUp = allOf(isActive, isGrownUp)
const [activeGrownUps, everyoneElse] = sift(users, isActiveGrownUp)
// activeGrownUps ===
// [
// { user: 'fred', age: 40, active: true }
// ]
// everyoneElse ===
// [
// { user: 'barney', age: 36, active: false },
// { user: 'pebbles', age: 1, active: true }
// ]
sift()
an array into multiple buckets
import { sift } from 'sift-r'
import { lt } from 'match-iz'
const [oneYearOlds, lessThan40, theRest] = sift(
[
{ user: 'barney', age: 36, active: false },
{ user: 'fred', age: 40, active: true },
{ user: 'pebbles', age: 1, active: false }
],
{ age: 1, active: false },
{ age: lt(40) }
)
// oneYearOlds ===
// [{ user: 'pebbles', age: 1, active: false }]
// lessThan40 ===
// [{ user: 'barney', age: 36, active: false }]
// theRest ===
// [{ user: 'fred', age: 40, active: true }]
sift()
a Map
or Set
Set
example:
import { sift } from 'sift-r'
import { lt } from 'match-iz'
const [oneYearOlds, lessThan40, theRest] = sift(
new Set([
{ user: 'barney', age: 36, active: false },
{ user: 'fred', age: 40, active: true },
{ user: 'pebbles', age: 1, active: false }
]),
{ age: 1, active: false },
{ age: lt(40) }
)
// oneYearOlds ===
// new Set([
// { user: 'pebbles', age: 1, active: false }
// ])
// lessThan40 ===
// new Set([
// { user: 'barney', age: 36, active: false }
// ])
// theRest ===
// new Set([
// { user: 'fred', age: 40, active: true }
// ])
Map
example:
import { sift } from 'sift-r'
import { lt } from 'match-iz'
const [oneYearOlds, lessThan40, theRest] = sift(
new Map([
[0, { user: 'barney', age: 36, active: false }],
[1, { user: 'fred', age: 40, active: true }],
[2, { user: 'pebbles', age: 1, active: false }]
]),
{ age: 1, active: false },
{ age: lt(40) }
)
// oneYearOlds ===
// new Map([
// [2, { user: 'pebbles', age: 1, active: false }]
// ])
// lessThan40 ===
// new Map([
// [0, { user: 'barney', age: 36, active: false }]
// ])
// theRest ===
// new Map([
// [1, { user: 'fred', age: 40, active: true }]
// ])
sift()
an iterable
import { sift } from 'sift-r'
import { lt } from 'match-iz'
function* flintstones() {
yield { user: 'barney', age: 36, active: false }
yield { user: 'fred', age: 40, active: true }
yield { user: 'pebbles', age: 1, active: false }
}
const [oneYearOlds, lessThan40, theRest] = sift(
flintstones(),
{ age: 1, active: false },
{ age: lt(40) }
)
// oneYearOlds ===
// [{ user: 'pebbles', age: 1, active: false }]
// lessThan40 ===
// [{ user: 'barney', age: 36, active: false }]
// theRest ===
// [{ user: 'fred', age: 40, active: true }]
Install / Use
$ pnpm i sift-r
Supports import
/require
for ESM/CJS.
Browser/UMD version here:
<script src="https://unpkg.com/sift-r/dist/browser/sift-r.browser.js"></script>
<script>
const { sift, byPattern } = siftr
</script>
Documentation
sift()
has several call-signatures, all of which are documented on the Wiki.
Credits
sift-r
was written by Conan Theobald.
I hope you found it useful! If so, I like coffee ☕️ :)
License
MIT licensed: See LICENSE