merge-options
Advanced tools
Comparing version 1.0.1 to 2.0.0
68
index.js
'use strict'; | ||
const isOptionObject = require('is-plain-obj'); | ||
const hasOwnProperty = Object.prototype.hasOwnProperty; | ||
const propIsEnumerable = Object.propertyIsEnumerable; | ||
const {hasOwnProperty} = Object.prototype; | ||
const {propertyIsEnumerable} = Object; | ||
const defineProperty = (obj, name, value) => Object.defineProperty(obj, name, { | ||
@@ -15,3 +15,4 @@ value, | ||
const defaultMergeOpts = { | ||
concatArrays: false | ||
concatArrays: false, | ||
ignoreUndefined: false | ||
}; | ||
@@ -32,5 +33,5 @@ | ||
for (let i = 0; i < symbols.length; i++) { | ||
if (propIsEnumerable.call(value, symbols[i])) { | ||
keys.push(symbols[i]); | ||
for (const symbol of symbols) { | ||
if (propertyIsEnumerable.call(value, symbol)) { | ||
keys.push(symbol); | ||
} | ||
@@ -76,10 +77,17 @@ } | ||
/** | ||
* @param merged {already cloned} | ||
* @return {cloned Object} | ||
* @param {*} merged already cloned | ||
* @param {*} source something to merge | ||
* @param {string[]} keys keys to merge | ||
* @param {Object} config Config Object | ||
* @returns {*} cloned Object | ||
*/ | ||
const mergeKeys = (merged, source, keys, mergeOpts) => { | ||
const mergeKeys = (merged, source, keys, config) => { | ||
keys.forEach(key => { | ||
if (typeof source[key] === 'undefined' && config.ignoreUndefined) { | ||
return; | ||
} | ||
// Do not recurse into prototype chain of merged | ||
if (key in merged && merged[key] !== Object.getPrototypeOf(merged)) { | ||
defineProperty(merged, key, merge(merged[key], source[key], mergeOpts)); | ||
defineProperty(merged, key, merge(merged[key], source[key], config)); | ||
} else { | ||
@@ -94,8 +102,10 @@ defineProperty(merged, key, clone(source[key])); | ||
/** | ||
* @param merged {already cloned} | ||
* @return {cloned Object} | ||
* @param {*} merged already cloned | ||
* @param {*} source something to merge | ||
* @param {Object} config Config Object | ||
* @returns {*} cloned Object | ||
* | ||
* see [Array.prototype.concat ( ...arguments )](http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.concat) | ||
*/ | ||
const concatArrays = (merged, source, mergeOpts) => { | ||
const concatArrays = (merged, source, config) => { | ||
let result = merged.slice(0, 0); | ||
@@ -124,5 +134,3 @@ let resultIndex = 0; | ||
// Merge non-index keys | ||
result = mergeKeys(result, array, getEnumerableOwnPropertyKeys(array).filter(key => { | ||
return indices.indexOf(key) === -1; | ||
}), mergeOpts); | ||
result = mergeKeys(result, array, getEnumerableOwnPropertyKeys(array).filter(key => !indices.includes(key)), config); | ||
}); | ||
@@ -134,8 +142,10 @@ | ||
/** | ||
* @param merged {already cloned} | ||
* @return {cloned Object} | ||
* @param {*} merged already cloned | ||
* @param {*} source something to merge | ||
* @param {Object} config Config Object | ||
* @returns {*} cloned Object | ||
*/ | ||
function merge(merged, source, mergeOpts) { | ||
if (mergeOpts.concatArrays && Array.isArray(merged) && Array.isArray(source)) { | ||
return concatArrays(merged, source, mergeOpts); | ||
function merge(merged, source, config) { | ||
if (config.concatArrays && Array.isArray(merged) && Array.isArray(source)) { | ||
return concatArrays(merged, source, config); | ||
} | ||
@@ -147,12 +157,10 @@ | ||
return mergeKeys(merged, source, getEnumerableOwnPropertyKeys(source), mergeOpts); | ||
return mergeKeys(merged, source, getEnumerableOwnPropertyKeys(source), config); | ||
} | ||
module.exports = function () { | ||
const mergeOpts = merge(clone(defaultMergeOpts), (this !== globalThis && this) || {}, defaultMergeOpts); | ||
let merged = {foobar: {}}; | ||
module.exports = function (...options) { | ||
const config = merge(clone(defaultMergeOpts), (this !== globalThis && this) || {}, defaultMergeOpts); | ||
let merged = {_: {}}; | ||
for (let i = 0; i < arguments.length; i++) { | ||
const option = arguments[i]; | ||
for (const option of options) { | ||
if (option === undefined) { | ||
@@ -166,6 +174,6 @@ continue; | ||
merged = merge(merged, {foobar: option}, mergeOpts); | ||
merged = merge(merged, {_: option}, config); | ||
} | ||
return merged.foobar; | ||
return merged._; | ||
}; |
{ | ||
"name": "merge-options", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Merge Option Objects", | ||
@@ -9,7 +9,6 @@ "license": "MIT", | ||
"name": "Michael Mayer", | ||
"email": "michael@schnittstabil.de", | ||
"url": "schnittstabil.de" | ||
"email": "michael@schnittstabil.de" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
"node": ">=8" | ||
}, | ||
@@ -34,11 +33,11 @@ "scripts": { | ||
"devDependencies": { | ||
"ava": "^0.25", | ||
"coveralls": "^3.0", | ||
"nyc": "^11.7", | ||
"rimraf": "^2.5", | ||
"xo": "^0.20" | ||
"ava": "^2.4.0", | ||
"coveralls": "^3.0.3", | ||
"nyc": "^14.1.1", | ||
"rimraf": "^3.0.0", | ||
"xo": "^0.25.3" | ||
}, | ||
"dependencies": { | ||
"is-plain-obj": "^1.1" | ||
"is-plain-obj": "^2.0.0" | ||
} | ||
} |
@@ -29,2 +29,11 @@ # merge-options [![Build Status](https://travis-ci.org/schnittstabil/merge-options.svg?branch=master)](https://travis-ci.org/schnittstabil/merge-options) [![Coverage Status](https://coveralls.io/repos/schnittstabil/merge-options/badge.svg?branch=master&service=github)](https://coveralls.io/github/schnittstabil/merge-options?branch=master) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) | ||
### Usage with custom config | ||
```js | ||
const mergeOptions = require('merge-options').bind({ignoreUndefined: true}); | ||
mergeOptions({foo: 'bar'}, {foo: undefined}) | ||
//=> {foo: 'bar'} | ||
``` | ||
## API | ||
@@ -89,3 +98,22 @@ | ||
##### config.ignoreUndefined | ||
Type: `boolean`<br/>Default: `false` | ||
Ignore undefined values: | ||
```js | ||
mergeOptions({foo: 'bar'}, {foo: undefined}) | ||
//=> {foo: undefined} | ||
// Via call | ||
mergeOptions.call({ignoreUndefined: true}, {foo: 'bar'}, {foo: undefined}) | ||
//=> {foo: 'bar'} | ||
// Via apply | ||
mergeOptions.apply({ignoreUndefined: true}, [{foo: 'bar'}, {foo: undefined}]) | ||
//=> {foo: 'bar'} | ||
``` | ||
## Related | ||
@@ -92,0 +120,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9946
137
131
+ Addedis-plain-obj@2.1.0(transitive)
- Removedis-plain-obj@1.1.0(transitive)
Updatedis-plain-obj@^2.0.0