Super Mixer
Mixes/merges/extends your object in multiple ways.
Unlike underscore/lodash utility methods this module allows you to:
- mix or deep merge objects' prototype chain. Regular mixin/extend/assign implementations can't do that.
- mix or deep merge unique properties only. I.e. data will not be overwritten if a property already exists.
- filter each individual property by target value, source value, and key. See API.
- transform each value by resulting value, source value, and key. See API.
Install
$ npm install supermixer
var mixer = ;
API
NB! All functions always mutate the first argument.
supermixer(opts = {})
The opts
:
* @param {Object} opts
* @param {Function} opts.filter Function which filters value and key.
* @param {Function} opts.transform Function which transforms each value.
* @param {Boolean} opts.chain Loop through prototype properties too.
* @param {Boolean} opts.deep Deep looping through the nested properties.
* @param {Boolean} opts.noOverwrite Do not overwrite any existing data (aka first one wins).
Usage:
const mix = ; const johnStream =
Object.assign
, aka $.extend
.
Regular mixin, aka // the regular Object.assign functionvar extend = ;// ORextend = supermixermixin; ;// { a: 1, b: 2 }
Mixin functions only.
// assigns own functions onlyvar functionMixer = ;// ORfunctionMixer = supermixermixinFunctions; ;// { b() }
Mixin functions including prototype chain.
// assigns functions only, but traverse through the prototype chainvar chainFunctionMixer = ;// ORchainFunctionMixer = supermixermixinChainFunctions; ;// { on(), off(), emit(), ... }
Deep merge
// deep merge own propertiesvar mergeDeep = ;// ORmergeDeep = supermixermerge; ;// { url: { host: "example.com", port: 81 } }
Deep merge but do not overwrite existing values.
// deep merge own propertiesvar mergeDeep = ;// ORmergeUnique = supermixermergeUnique; ;// { url: { host: "example.com" } }
Deep merge non functions including prototype chain.
// deeply merges data properties, traversing through prototype chainvar mergeChainData = ;// ORmergeChainData = supermixermergeChainNonFunctions; EventEmitterprototypehello = "world";;// { hello: "world" }
Want to contribute?
This project is Open Open Source. This means whoever submits an accepted PR will receive write permissions to the project.