Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

chai-exclude

Package Overview
Dependencies
Maintainers
1
Versions
21
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.12
to
2.0.0
+205
chai-exclude.js
const fclone = require('fclone')
function chaiExclude (chai, utils) {
const assert = chai.assert
const Assertion = chai.Assertion
/**
* 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 === Object(arg) && Object.prototype.toString.call(arg) !== '[object Array]'
}
/**
* Remove keys from an object or an array.
*
* @param {Object|Array} val object or array 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 removeKeysFrom (val, props, recursive = false) {
// Replace circular values with '[Circular]'
const obj = fclone(val)
if (isObject(obj)) {
return removeKeysFromObject(obj, props, recursive)
}
return removeKeysFromArray(obj, props, recursive)
}
/**
* Remove keys from an object and return a new 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 removeKeysFromObject (obj, props, recursive = false) {
const res = {}
const keys = Object.keys(obj)
const isRecursive = !!recursive
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
const val = obj[key]
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
}
}
return res
}
/**
* 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, recursive)
} else if (isArray(array[i])) {
val = removeKeysFromArray(array[i], props, recursive)
} else {
val = array[i]
}
res.push(val)
}
return res
}
/**
* Override standard assertEqual method to remove the keys from other part of the equation.
*
* @param {Object} _super
* @returns {Function}
*/
function assertEqual (_super) {
return function (val) {
const props = utils.flag(this, 'excludingProps')
if (utils.flag(this, 'excluding')) {
val = removeKeysFrom(val, props)
} else if (utils.flag(this, 'excludingEvery')) {
val = removeKeysFrom(val, props, true)
}
// In case of 'use strict' and babelified code
arguments[0] = val
_super.apply(this, arguments)
}
}
/**
* Add a new method 'deepEqualExcluding' to 'chai.assert'.
*/
utils.addMethod(assert, 'deepEqualExcluding', function (actual, expected, props, message) {
new Assertion(actual, message).excluding(props).to.deep.equal(expected)
})
/**
* Add a new method `deepEqualExcludingEvery` to 'chai.assert'.
*/
utils.addMethod(assert, 'deepEqualExcludingEvery', function (actual, expected, props, message) {
new Assertion(actual, message).excludingEvery(props).to.deep.equal(expected)
})
/**
* Add a new method 'excluding' to the assertion library.
*/
Assertion.addMethod('excluding', function (props) {
utils.expectTypes(this, ['object', 'array'])
const obj = this._obj
// If exclude parameter is not provided
if (!props) {
return this
}
if (typeof props === 'string') {
props = [props]
}
if (!isArray(props)) {
throw new Error('Excluding params should be either a string or an array')
}
this._obj = removeKeysFrom(obj, props)
utils.flag(this, 'excluding', true)
utils.flag(this, 'excludingProps', props)
})
/**
* Add a new method 'excludingEvery' to the assertion library.
*/
Assertion.addMethod('excludingEvery', function (props) {
utils.expectTypes(this, ['object', 'array'])
const obj = this._obj
// If exclude parameter is not provided
if (!props) {
return this
}
if (typeof props === 'string') {
props = [props]
}
if (!isArray(props)) {
throw new Error('Excluding params should be either a string or an array')
}
this._obj = removeKeysFrom(obj, props, true)
utils.flag(this, 'excludingEvery', true)
utils.flag(this, 'excludingProps', props)
})
Assertion.overwriteMethod('eq', assertEqual)
Assertion.overwriteMethod('equal', assertEqual)
Assertion.overwriteMethod('equals', assertEqual)
}
module.exports = chaiExclude
module.exports.default = chaiExclude // for Typescript
/// <reference types="@types/chai" />
declare module 'chai-exclude' {
export default function chaiExclude(chai: any, utils: any): void;
}
declare namespace Chai {
interface Assertion extends LanguageChains, NumericComparison, TypeComparison {
excluding(props: string | string[]): Assertion;
excludingEvery(props: string | string[]): Assertion;
}
interface Assert {
/**
* Asserts that actual is deeply equal to expected excluding some top level properties.
*
* @param actual Actual value.
* @param expected Expected value.
* @param props Properties or keys to exclude.
* @param message Message to display on error.
*/
deepEqualExcluding<T>(actual: T | T[], expected: T | T[], props: keyof T | (keyof T)[], message?: string): void;
/**
* Asserts that actual is deeply equal to expected excluding properties any level deep.
*
* @param actual Actual value.
* @param expected Expected value.
* @param props Properties or keys to exclude.
* @param message Message to display on error.
*/
deepEqualExcludingEvery<T>(actual: T | T[], expected: T | T[], props: keyof T | (keyof T)[], message?: string): void;
}
}
+10
-7
{
"name": "chai-exclude",
"version": "1.0.12",
"version": "2.0.0",
"description": "Exclude keys to compare from a deep equal operation with chai expect and assert",
"main": "lib/chai-exclude.js",
"types": "lib/chai-exclude.d.ts",
"main": "chai-exclude.js",
"types": "index.d.ts",
"author": "Saugat Acharya <mesaugat@gmail.com>",

@@ -11,7 +11,9 @@ "license": "MIT",

"files": [
"lib"
"chai-exclude.js",
"index.d.ts"
],
"scripts": {
"test": "mocha",
"lint": "standard --fix"
"test": "mocha --reporter spec --recursive --colors *.test.js && tsd-check",
"lint": "standard --fix",
"typecheck": "tsd-check"
},

@@ -37,3 +39,4 @@ "keywords": [

"mocha": "^5.0.0",
"standard": "^12.0.1"
"standard": "^12.0.1",
"tsd-check": "^0.3.0"
},

@@ -40,0 +43,0 @@ "dependencies": {

@@ -42,6 +42,6 @@ # chai-exclude

```js
import { use } from 'chai';
import chai from 'chai';
import chaiExclude from 'chai-exclude';
use(chaiExclude);
chai.use(chaiExclude);
```

@@ -52,6 +52,6 @@

```js
import { use } from 'chai';
import chaiExclude = require('chai-exclude');
import * as chai from 'chai';
import chaiExclude from 'chai-exclude';
use(chaiExclude);
chai.use(chaiExclude);

@@ -58,0 +58,0 @@ // The typings for chai-exclude are included with the package itself.

/// <reference types="chai" />
declare module "chai-exclude" {
function chaiExclude(chai: any, utils: any): void;
export = chaiExclude;
}
declare namespace Chai {
interface Assertion extends LanguageChains, NumericComparison, TypeComparison {
excluding(props: string | string[]): Assertion;
excludingEvery(props: string | string[]): Assertion;
}
interface Assert {
/**
* Asserts that actual is deeply equal to expected excluding some top level properties.
*
* @param actual Actual value.
* @param expected Expected value.
* @param props Properties or keys to exclude.
* @param message Message to display on error.
*/
deepEqualExcluding<T>(actual: T | T[], expected: T | T[], props: keyof T | (keyof T)[], message?: string): void;
/**
* Asserts that actual is deeply equal to expected excluding properties any level deep.
*
* @param actual Actual value.
* @param expected Expected value.
* @param props Properties or keys to exclude.
* @param message Message to display on error.
*/
deepEqualExcludingEvery<T>(actual: T | T[], expected: T | T[], props: keyof T | (keyof T)[], message?: string): void;
}
}
const fclone = require('fclone')
module.exports = function (chai, utils) {
const assert = chai.assert
const Assertion = chai.Assertion
/**
* 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 === Object(arg) && Object.prototype.toString.call(arg) !== '[object Array]'
}
/**
* Remove keys from an object or an array.
*
* @param {Object|Array} val object or array 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 removeKeysFrom (val, props, recursive = false) {
// Replace circular values with '[Circular]'
const obj = fclone(val)
if (isObject(obj)) {
return removeKeysFromObject(obj, props, recursive)
}
return removeKeysFromArray(obj, props, recursive)
}
/**
* Remove keys from an object and return a new 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 removeKeysFromObject (obj, props, recursive = false) {
const res = {}
const keys = Object.keys(obj)
const isRecursive = !!recursive
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
const val = obj[key]
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
}
}
return res
}
/**
* 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, recursive)
} else if (isArray(array[i])) {
val = removeKeysFromArray(array[i], props, recursive)
} else {
val = array[i]
}
res.push(val)
}
return res
}
/**
* Override standard assertEqual method to remove the keys from other part of the equation.
*
* @param {Object} _super
* @returns {Function}
*/
function assertEqual (_super) {
return function (val) {
const props = utils.flag(this, 'excludingProps')
if (utils.flag(this, 'excluding')) {
val = removeKeysFrom(val, props)
} else if (utils.flag(this, 'excludingEvery')) {
val = removeKeysFrom(val, props, true)
}
// In case of 'use strict' and babelified code
arguments[0] = val;
_super.apply(this, arguments)
}
}
/**
* Add a new method 'deepEqualExcluding' to 'chai.assert'.
*/
utils.addMethod(assert, 'deepEqualExcluding', function (actual, expected, props, message) {
new Assertion(actual, message).excluding(props).to.deep.equal(expected)
})
/**
* Add a new method `deepEqualExcludingEvery` to 'chai.assert'.
*/
utils.addMethod(assert, 'deepEqualExcludingEvery', function (actual, expected, props, message) {
new Assertion(actual, message).excludingEvery(props).to.deep.equal(expected)
})
/**
* Add a new method 'excluding' to the assertion library.
*/
Assertion.addMethod('excluding', function (props) {
utils.expectTypes(this, ['object', 'array'])
const obj = this._obj
// If exclude parameter is not provided
if (!props) {
return this
}
if (typeof props === 'string') {
props = [props]
}
if (!isArray(props)) {
throw new Error('Excluding params should be either a string or an array')
}
this._obj = removeKeysFrom(obj, props)
utils.flag(this, 'excluding', true)
utils.flag(this, 'excludingProps', props)
})
/**
* Add a new method 'excludingEvery' to the assertion library.
*/
Assertion.addMethod('excludingEvery', function (props) {
utils.expectTypes(this, ['object', 'array'])
const obj = this._obj
// If exclude parameter is not provided
if (!props) {
return this
}
if (typeof props === 'string') {
props = [props]
}
if (!isArray(props)) {
throw new Error('Excluding params should be either a string or an array')
}
this._obj = removeKeysFrom(obj, props, true)
utils.flag(this, 'excludingEvery', true)
utils.flag(this, 'excludingProps', props)
})
Assertion.overwriteMethod('eq', assertEqual)
Assertion.overwriteMethod('equal', assertEqual)
Assertion.overwriteMethod('equals', assertEqual)
}