strict-extend
A shallow extend method that removes extra properties from the target object.
Inspired by jQuery's .extend()
and
Object.assign()
,
but any properties on the target argument that aren't on any of the source arguments will be removed.
Useful for filling in defaults for options parameters.
Installation
This package is available on npm as strict-extend
.
npm install strict-extend
Usage
Syntax
strictExtend(target, ...sources)
Parameters
target
: The target object.
sources
: The source object(s).
Description
strictExtend
will return a new object containing all of the properties from all of the sources
object(s).
Any properties already present on target
will not be overwritten.
However, any properties present on target
which are not present on any of the sources
object(s) will not be copied to the return object.
Values for properties which are on multiple sources
objects will be resolved by taking the value from the last source object.
Note that an error will not be thrown for null
or undefined
parameters.
Instead, an empty object will be returned.
Examples
Default Options
; // or const strictExtend = require('strictExtend'); const defaults = color: 'red' name: 'Clifford'; { return ;} console; // { color: 'red', name: 'Clifford' }console; // { color: 'blue', name: 'Sparky' }console; // { color: 'red', name: 'Rover' }// 'hasCollarOn' was ignored
Multiple Sources and Last In Behavior
const strictExtend = // or import strictExtend from 'strict-extend'; const target = kept: 'should not be overwritten' removed: 'should NOT be copied';const source1 = kept: 'this will be ignored' newProp1: 'this will be copied' newProp2: 'this value will be ignored due to the property on source2';const source2 = newProp2: 'last in wins!';console;// {// kept: 'should not be overwritten', (copied from target)// newProp1: 'this will be copied', (copied from source1)// newProp2: 'last in wins!', (copied from source2)// }// notice 'removed' from target not copied
null
/undefined
Behavior
;console; // {}console; // {}console; // {}console; // {}console; // {}
Development
Install
- Clone the repository.
- Run
npm install
in the directory you cloned into.
Tests
npm test
assumes you have Chrome and Firefox installed.
If that is not the case, you can run npm run test-ci
to only run tests with PhantomJS (which is a dev dependency).
Or, you can run the tests and specify which browsers you'd like to test with using the --browsers
option with karma start
.
For example, to test only with Firefox, you can run karma start --browsers Firefox
.
License
Licensed under the MIT License.