Used to merge objects together. Like Object.assign. But it performs a deep merge.
Pass objects into mix to combine them. Mix creates a new object for you automatically. Nothing will get mutated.
let count = 0
const one = { one: 'one' }
const two = { two: 'two' }
const mixed = mix(one, two)
// Output
// {
// one: 'one',
// two: 'two',
// }- It copies accessors.
- It performs a deep merge so no mutation can happen
Mix copies accessors along with other properties. Most other deep merging libraries don't do this. Object.assign doesn't do this either.
let count = 0
const one = { one: 'one' }
const two = { two: 'two' }
const three = {
get count () { return count },
set count (value) { count = value }
}
const mixed = mix({}, one, two, three)
// Output
// {
// one: 'one',
// two: 'two',
// get count () { return count } ,
// set count (value) { count = value }
// }mix copies nested objects and arrays so you don't have to worry about mutation.
const one = {}
const two = { nested: { value: 'two' } }
const three = mix(one, two)
// Nested values do not get mutated
three.nested.value = 'three'
console.log(two.nested.value) // 'two'That's it!