chai-exclude
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -5,9 +5,10 @@ module.exports = (chai, utils) => { | ||
/** | ||
* Remove specified properties from an object and return a new object. | ||
* Remove keys from an object and return a new object. | ||
* | ||
* @param {Object} obj The object to remove keys | ||
* @param {Array} props Array of keys to remove | ||
* @return {Object} | ||
* @param {Object} obj object to remove keys | ||
* @param {Array} props array of keys to remove | ||
* @param {Boolean} recursive true if property needs to be removed recursively | ||
* @returns {Object} | ||
*/ | ||
function removeKeys (obj, props, recursive = false) { | ||
function removeKeysFromObject (obj, props, recursive = false) { | ||
const res = {} | ||
@@ -21,5 +22,9 @@ const keys = Object.keys(obj) | ||
if (isRecursive && (val instanceof Object && !Array.isArray(val))) { | ||
res[key] = removeKeys(val, props, recursive) | ||
} else if (props.indexOf(key) === -1) { | ||
const hasKey = props.indexOf(key) === -1 | ||
if (isRecursive && hasKey && isObject(val)) { | ||
res[key] = removeKeysFromObject(val, props, true) | ||
} else if (isRecursive && hasKey && isArray(val)) { | ||
res[key] = removeKeysFromArray(val, props, true) | ||
} else if (hasKey) { | ||
res[key] = val | ||
@@ -33,6 +38,56 @@ } | ||
/** | ||
* Override of the standard assertEqual to also remove the keys from the | ||
* other part of the equation. | ||
* | ||
* @param {Object} _super | ||
* Remove keys from an object inside an array and return a new array. | ||
* | ||
* @param {Array} array array with objects | ||
* @param {Array} props array of keys to remove | ||
* @param {Boolean} recursive true if property needs to be removed recursively | ||
* @returns {Array} | ||
*/ | ||
function removeKeysFromArray (array, props, recursive = false) { | ||
const res = [] | ||
let val = {} | ||
if (!array.length) { | ||
return res | ||
} | ||
for (let i = 0; i < array.length; i++) { | ||
if (isObject(array[i])) { | ||
val = removeKeysFromObject(array[i], props, true) | ||
} else if (isArray(array[i])) { | ||
val = removeKeysFromArray(array[i], props, true) | ||
} else { | ||
val = array[i] | ||
} | ||
res.push(val) | ||
} | ||
return res | ||
} | ||
/** | ||
* Check if the argument is an array. | ||
* | ||
* @param {any} arg | ||
* @returns {Boolean} | ||
*/ | ||
function isArray (arg) { | ||
return Array.isArray(arg) | ||
} | ||
/** | ||
* Check if the argument is an object. | ||
* | ||
* @param {any} arg | ||
* @returns {Boolean} | ||
*/ | ||
function isObject (arg) { | ||
return arg instanceof Object && arg.constructor === Object | ||
} | ||
/** | ||
* Override standard assertEqual method to remove the keys from other part of the equation. | ||
* | ||
* @param {Object} _super | ||
* @returns {Function} | ||
@@ -44,7 +99,8 @@ */ | ||
if (utils.flag(this, 'excluding')) { | ||
val = removeKeys(val, utils.flag(this, 'excludingProps')) | ||
val = removeKeysFromObject(val, utils.flag(this, 'excludingProps')) | ||
} else if (utils.flag(this, 'excludingEvery')) { | ||
val = removeKeys(val, utils.flag(this, 'excludingProps'), true) | ||
val = removeKeysFromObject(val, utils.flag(this, 'excludingProps'), true) | ||
} | ||
} | ||
_super.apply(this, arguments) | ||
@@ -54,6 +110,11 @@ } | ||
/** | ||
* Add a new method 'excluding' to the assertion library. | ||
*/ | ||
Assertion.addMethod('excluding', function (props) { | ||
utils.expectTypes(this, ['object']) | ||
// If exclude parameter(s) are not provided | ||
const obj = this._obj | ||
// If exclude parameter is not provided | ||
if (!props) { | ||
@@ -67,3 +128,3 @@ return this | ||
this._obj = removeKeys(this._obj, props) | ||
this._obj = removeKeysFromObject(obj, props) | ||
@@ -76,6 +137,11 @@ utils.flag(this, 'excluding', true) | ||
/** | ||
* Add a new method 'excludingEvery' to the assertion library. | ||
*/ | ||
Assertion.addMethod('excludingEvery', function (props) { | ||
utils.expectTypes(this, ['object']) | ||
// If exclude parameter(s) are not provided | ||
const obj = this._obj | ||
// If exclude parameter is not provided | ||
if (!props) { | ||
@@ -89,3 +155,3 @@ return this | ||
this._obj = removeKeys(this._obj, props, true) | ||
this._obj = removeKeysFromObject(obj, props, true) | ||
@@ -92,0 +158,0 @@ utils.flag(this, 'excludingEvery', true) |
{ | ||
"name": "chai-exclude", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Exclude keys to compare from a deep equal operation with chai expect", | ||
@@ -5,0 +5,0 @@ "main": "lib/chai-exclude.js", |
@@ -12,3 +12,3 @@ # chai-exclude | ||
Sometimes you'll need to exclude object properties that generate unique values while doing a deep equal operation. This plugin makes it easier to remove those properties before comparison. | ||
Sometimes you'll need to exclude object properties that generate unique values while doing a deep equal operation. This plugin makes it easier to remove those properties from comparison. | ||
@@ -148,8 +148,4 @@ https://github.com/chaijs/chai/issues/885 | ||
b: { | ||
d: { // d is not removed because it is an object | ||
b: 'b' | ||
} | ||
} | ||
} | ||
} | ||
@@ -159,4 +155,2 @@ expect(actual).excludingEvery(['a', 'd']).to.deep.equal(expected) | ||
__Note: `excludingEvery` will not remove the property if it is an object in a deeply nested object.__ | ||
## Contributing | ||
@@ -163,0 +157,0 @@ |
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
9216
141
160