Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

readme.md

Mix

Used to merge objects together. Like Object.assign. But it performs a deep merge.

Here's why I created mix

Example

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

Features

  1. It copies accessors.
  2. It performs a deep merge so no mutation can happen

Copying accessors

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

Deep Merging

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!