-
Notifications
You must be signed in to change notification settings - Fork 8
MergeChain
James edited this page Jun 24, 2017
·
3 revisions
deeply merge all data values on a chain with ease
type Mergeable = Obj | Arr
export interface DopeMergeOptions {
arrayMerge?: Fn
stringToArray?: boolean = true
boolToArray?: boolean = false
ignoreTypes?: string[] = ['null', 'undefined', 'NaN']
debug?: boolean = undefined
}
function dopemerge(obj1: Mergeable, obj2: Mergeable, opts?: DopeMergeOptions): Mergeable
class MergeChain extends ChainedMapBase {
public onValue(fn?: Fn): MergeChain
public onExisting(fn?: Fn): MergeChain
public obj(obj: Obj): MergeChain
public merge(objToMerge: Obj): MergeChain
}
- a simplified, optimized
.merge
which works similar, but does not deeply merge objects unless the properties are chainable instances - by default, overrides existing values or sets them initially
- mainly used for hydrating or transferring (for example, to-from localStorage, to-from webworker)
const chain = new ChainedMap()
chain.merge({ehOh: true}) // same as chain.set('ehOh', true)
chain.entries() === {ehOh: true}
const Chain = require('chain-able')
class Canada extends Chain {
static init(parent) { return new Canada(parent) }
constructor(parent) {
super('parent')
this.extend(['eh'])
}
}
const ls = {
get(key) { return JSON.parse(window.localStorage.getItem(key)) },
set(key, value) { window.localStorage.setItem(key, JSON.stringify(value)) }
}
const canada = Canada.init()
.eh('eh!')
.merge({canada: true})
.tap('canada', value => '🇨🇦')
.setIfEmpty('ooo', 'ahh')
ls.set('canada', canada)
const hydrated = new Canada().from(ls.get('canada'))
- in ChainedSet
arg
is anIteratable
(e.g.Array
) - in ChainedMap
arg
is anObject
- iterate over the object keys:
- if there an instance property matching the key
- default/fallback
- if existing value, deeply value- merge
.set