Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chai-exclude

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chai-exclude - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

102

lib/chai-exclude.js

@@ -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)

2

package.json
{
"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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc