sift-r

Apportion objects / arrays into multiple buckets based on predicates / patterns

Usage no npm install needed!

<script type="module">
  import siftR from 'https://cdn.skypack.dev/sift-r';
</script>

README

sift-r 📥

MIT license npm bundle size Version

Apportion objects / arrays into multiple buckets based on a predicate / pattern.

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
//   }

Install / Use

npm 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 } = siftr
</script>

Documentation

sift() has several call-signatures (the above example is #4):

sift(input, optionalSchemaOrPattern)

// Working with plain-objects (pojos)
1: sift({ key: [pattern, value], key: [pattern, value], ... })
2: sift({ key: value }, { key: pattern })
3: sift({ key: value }, pattern)
4: sift({ key: value }, [pattern, pattern, ...])

// Working with arrays
5: sift([[pattern, value], [pattern, value], ...])
6: sift([value, value], [pattern, pattern])
7: sift([value, value], value-pattern)

pattern means either a predicate function, or something that the match-iz when() method supports, because sift uses match-iz internally.

  • When working with pojos, the pattern is applied to each key/value pair.

  • When working with arrays, the pattern is applied to each item in the array.

Here are call-signatures #1-3 (pojos):

// 1:
//
const [strValues, failed] = sift({
  title: [isString, 'header'],
  slug: [isString, 1],
  markdown: [isString, '# header'],
  footer: [isString, undefined]
})

// 2:
//
const [strValues, failed] = sift(
  {
    title: 'header',
    slug: 1,
    markdown: '# header',
    footer: undefined
  },
  {
    title: isString,
    slug: isString,
    markdown: isString,
    footer: isString
  }
)

// 3:
//
const [strValues, failed] = sift(
  {
    title: 'header',
    slug: 1,
    markdown: '# header',
    footer: undefined
  },
  isString
)

Call-signatures #5-7 (arrays):

// 5:
//
const [strValues, failed] = sift([
  [isString, 'header'],
  [isString, 1],
  [isString, '# header']
])

// 6:
//
const [strValues, failed] = sift(
  ['header', 1, '# header'],
  [isString, isString, isString]
)

// 7:
//
const [strValues, failed] = sift(
  ['header', 1, '# header'],
  isString
)

You can find more examples in the test suite.

Credits

sift-r was written by Conan Theobald.

I hope you found it useful! If so, I like coffee ☕️ :)

License

MIT licensed: See LICENSE