object-concat

Assigns properties of source object(s) to a new object.

Usage no npm install needed!

<script type="module">
  import objectConcat from 'https://cdn.skypack.dev/object-concat';
</script>

README

object-concat

Assigns properties of source object(s) to a new object.

Build Status Code Climate js-standard-style

npm install object-concat --save
npm stats

npm NPM downloads Dependency Status

Example

basic
var concat = require('object-concat')

var defaults = { level: 1 }
var restored = { player: 'isaac', level: 5 }
var gamedata = concat(defaults, restored)

assert.equal(gamedata.player, 'isaac')
//=> undefined

assert.equal(gamedata.level, 5)
//=> undefined

assert.notDeepEqual(gamedata, defaults)
//=> undefined

assert.notDeepEqual(gamedata, restored)
//=> undefined
transform
var concat = require('object-concat')

var defaults = { level: 1 }
var restored = { player: 'isaac', level: 5 }
var gamedata = concat(defaults, restored, function (key, sourceVal, targetVal) {
  return key === 'player' ? sourceVal.toUpperCase() : sourceVal
})

assert.equal(gamedata.player, 'ISAAC')
//=> undefined

Features

  • Concatenative inheritance.
  • Return a new object instead of mutating a target object.
  • Subsequent source properties overwrite previous.
  • Supports optional iteratee function allowing transformation of target values.

Anti-Features

  • Will never make you seed your parameter list with an empty object:
    • No _.extend({}, source)
  • Will never mutate existing objects.
  • Will never overwrite native Object prototype methods (i.e. Object.assign polyfills).

API

concat([sources], [iteratee])

Arguments
  • [sources]: (…Object) The source objects.
  • [iteratee]: (Function) Function that produces desired target value (must be last parameter).
    • key: (String) Object key name.
    • sourceVal: (*) Object source value.
    • targetVal: (*) Object target value.
Returns
  • (Object) The new object.

Alternatives

Most, if not all of the alternatives listed have varying semantics so be careful which you choose for your own applications.

Licenses

GitHub license