deep-entries

node v10.24.1
version: 4.0.0
endpointsharetweet
const { deepEntries, deepEntriesIterator, delimitEntry, delimitEntryBy, rotateEntry, rotateEntryBy } = require('deep-entries@4.0.0')
A shape made up of both Objects or Arrays can be described in terms of deep entries. Only enumerable own-members will be returned and iteration will honour index and / or insertion order. The following examples will consume this input:
const input = { foo: 1, bar: { deep: { key: 2 } }, baz: [ 3, [4,5], { key: 6 } ] }
deepEntries(): Similiar to Object.entries() will return nested entries as arrays of varying length, with the value always trailing.
deepEntries(input)
deepEntries(): Will accept an optional map function as a second parameter.
// delimitEntry is shorthand for delimitEntryBy('.') deepEntries(input, delimitEntry)
deepEntries() is an alias that collects all entries from a deepEntriesIterator(), which is also exposed to aid in performant iteration of larger structures. The rotateEntry map function rotates the entry array by 1 (i.e. putting the value first), allowing for more convenient destructuring of an entry.
// rotateEntry is shorthand for rotateEntryBy(1) for(let [value, ...keys] of deepEntriesIterator(input, rotateEntry)) { console.log({ keys, value }) }
It's worth noting that objects can have assigned iterators too.
const { withIterator } = require('with-iterator') const withDeepEntriesIterator = withIterator(function*() { yield* deepEntriesIterator(this, delimitEntryBy(':')) }) withDeepEntriesIterator(input) Array.from(input)
The map functions passed to deepEntries() and deepEntriesIterator() can effectively filter entries by not returning them - i.e. returning "undefined".
const { last: getValue } = require('ramda') deepEntries(input, entry => getValue(entry) > 3 ? entry : undefined )
The map functions follow a pattern of returning "undefined" if passed "undefined" such that they may be composed with filters, without throwing errors.
const { pipe } = require('ramda') const atDepth = n => entry => { if (entry.length === 2 + n) return entry } deepEntries(input, pipe( atDepth(1), delimitEntry ))
Loading…

no comments

    sign in to comment