chai-exclude
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -11,5 +11,6 @@ module.exports = (chai, utils) => { | ||
*/ | ||
function removeKeys (obj, props) { | ||
function removeKeys (obj, props, recursive = false) { | ||
const res = {} | ||
const keys = Object.keys(obj) | ||
const isRecursive = !!recursive | ||
@@ -20,3 +21,5 @@ for (let i = 0; i < keys.length; i++) { | ||
if (props.indexOf(key) === -1) { | ||
if (isRecursive && (val instanceof Object && !Array.isArray(val))) { | ||
res[key] = removeKeys(val, props, recursive) | ||
} else if (props.indexOf(key) === -1) { | ||
res[key] = val | ||
@@ -45,2 +48,19 @@ } | ||
}) | ||
Assertion.addMethod('excludingEvery', function (props) { | ||
utils.expectTypes(this, ['object']) | ||
// If exclude parameter(s) are not provided | ||
if (!props) { | ||
return this | ||
} | ||
if (typeof props === 'string') { | ||
props = [props] | ||
} | ||
this._obj = removeKeys(this._obj, props, true) | ||
return this | ||
}) | ||
} |
{ | ||
"name": "chai-exclude", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Exclude keys before a deep equal operation with expect", | ||
"main": "lib/chai-exclude.js", | ||
"types": "lib/chai-exclude.d.ts", | ||
"author": "Saugat Acharya <mesaugat@gmail.com>", | ||
@@ -28,2 +29,3 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@types/chai": "^4.0.4", | ||
"chai": "^4.1.2", | ||
@@ -30,0 +32,0 @@ "eslint": "^4.9.0", |
102
README.md
# chai-exclude | ||
[![npm](https://img.shields.io/npm/v/chai-exclude.svg)](https://www.npmjs.com/package/chai-exclude) | ||
[![npm](https://img.shields.io/npm/dt/chai-exclude.svg)](https://www.npmjs.com/package/chai-exclude) | ||
[![Build Status](https://travis-ci.org/mesaugat/chai-exclude.svg?branch=master)](https://travis-ci.org/mesaugat/chai-exclude) | ||
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) | ||
Exclude keys before a deep equal operation with [expect](http://chaijs.com/api/bdd/). | ||
Exclude keys before a deep equal operation with chaijs [expect](http://chaijs.com/api/bdd/). | ||
@@ -34,16 +37,105 @@ ## Why? | ||
1. Excluding a property from the object | ||
### a) excluding | ||
1. Excluding a property from an object | ||
```js | ||
expect({ a: 'a', b: 'b' }).excluding('a').to.equal({ b: 'b' }); | ||
expect({ a: 'a', b: 'b' }).excluding('a').to.deep.equal({ b: 'b' }) | ||
``` | ||
2. Excluding multiple properties from the object | ||
2. Excluding multiple top level properties from an object | ||
```js | ||
expect({ a: 'a', b: 'b', c: 'c' }).excluding(['a', 'b']).to.equal({ c: 'c' }); | ||
const obj = { | ||
a: 'a', | ||
b: 'b', | ||
c: { | ||
d: 'd', | ||
e: 'e' | ||
} | ||
} | ||
expect(obj).excluding(['a', 'c']).to.deep.equal({ b: 'b' }) | ||
``` | ||
### b) excludingEvery | ||
1. Excluding every property in a deeply nested object | ||
```js | ||
const actual = { | ||
a: 'a', | ||
b: 'b', | ||
c: { | ||
a: 'a', | ||
b: { | ||
a: 'a', | ||
d: { | ||
a: 'a', | ||
b: 'b', | ||
d: null | ||
} | ||
} | ||
}, | ||
d: ['a', 'c'] | ||
} | ||
const expected = { | ||
b: 'b', | ||
c: { | ||
b: { | ||
d: { | ||
b: 'b', | ||
d: null | ||
} | ||
} | ||
}, | ||
d: ['a', 'c'] | ||
} | ||
expect(actual).excludingEvery('a').to.deep.equal(expected) | ||
``` | ||
2. Excluding multiple properties in a deeply nested object | ||
```js | ||
const actual = { | ||
a: 'a', | ||
b: 'b', | ||
c: { | ||
a: 'a', | ||
b: { | ||
a: 'a', | ||
d: { | ||
a: 'a', | ||
b: 'b', | ||
d: null | ||
} | ||
} | ||
}, | ||
d: ['a', 'c'] | ||
} | ||
const expected = { | ||
b: 'b', | ||
c: { | ||
b: { | ||
d: { // d is not removed because it is an object | ||
b: 'b' | ||
} | ||
} | ||
} | ||
} | ||
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 | ||
Contributions are welcome. If you have any questions create an issue [here](https://github.com/mesaugat/chai-exclude/issues). | ||
## License | ||
[MIT](LICENSE) |
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
6370
5
60
141
9