object-concat

0.2.9 • Public • Published

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

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.2.9
    7
    • latest

Version History

Package Sidebar

Install

npm i object-concat

Weekly Downloads

7

Version

0.2.9

License

MIT

Last publish

Collaborators

  • wilmoore