Comparing version 1.2.2 to 1.3.0
37
index.js
@@ -7,5 +7,15 @@ const prettyFormat = require('pretty-format'); | ||
highlight: true, | ||
min: true | ||
}; | ||
const format = o => prettyFormat(o, formatOptions); | ||
const format = ({keyStr, v1, v2}, maxLen = Math.floor((process.stdout.columns - 3) / 2)) => { | ||
const k = keyStr.length > 20 ? '…' + keyStr.slice(-19) : keyStr; | ||
const s1 = v1 === undefined ? '' : prettyFormat(v1, formatOptions); | ||
const s2 = v2 === undefined ? '' : prettyFormat(v2, formatOptions); | ||
return { | ||
keyStr: k, | ||
v1: k.length + 1 + s1.length > maxLen ? s1.slice(0, maxLen - k.length - 3) + '…' : s1, | ||
v2: k.length + 1 + s2.length > maxLen ? s2.slice(0, maxLen - k.length - 3) + '…' : s2 | ||
}; | ||
}; | ||
@@ -19,9 +29,5 @@ class AssertionError extends Error { | ||
const leftDiff = (keyStr, v2) => `${chalk.bold.red('\n → ' + keyStr)}: ${chalk.magentaBright(format(v2))}`; | ||
const rightDiff = (keyStr, v1) => `${chalk.bold.red('\n ← ' + keyStr)}: ${chalk.magenta(format(v1))}`; | ||
const bothDiff = (keyStr, v1, v2) => `${chalk.bold.red('\n ↔ ' + keyStr)}: ${chalk.magenta(format(v1))} !== ${chalk.magentaBright(format(v2))}`; | ||
const deepEqual = (o1, o2) => { | ||
const diffs = []; | ||
const deepEq = (o1, o2, path = []) => { | ||
@@ -32,3 +38,3 @@ const o1IsObj = typeof o1 === 'object' && o1 !== null; | ||
if (o1 !== o2) { | ||
diffs.push(bothDiff(path.join('.'), o1, o2)); | ||
diffs.push(format({keyStr: path.join('.'), v1: o1, v2: o2})); | ||
} | ||
@@ -44,9 +50,9 @@ return; | ||
} else if (v1 === undefined) { | ||
diffs.push(leftDiff(keyStr, v2)); | ||
diffs.push(format({keyStr, v2})); | ||
} else if (v2 === undefined) { | ||
diffs.push(rightDiff(keyStr, v1)); | ||
diffs.push(format({keyStr, v1})); | ||
} else if (v1 && typeof v1 === 'object' && v2 && typeof v2 === 'object') { | ||
deepEq(v1, v2, path.concat(key)); | ||
} else if (v1 !== v2) { | ||
diffs.push(bothDiff(keyStr, v1, v2)); | ||
diffs.push(format({keyStr, v1, v2})); | ||
} | ||
@@ -58,5 +64,14 @@ }); | ||
throw new AssertionError(diffs.join('')); | ||
const r1Width = Math.max(...diffs.map(o => o.v1 ? o.keyStr.length + 1 + o.v1.length : 0)); | ||
const arr = diffs.map(({keyStr: k, v1, v2}) => { | ||
const s1 = (v1 && k ? chalk.bold.red(k) + ' ' : '')+ chalk.magenta(v1); | ||
const s2 = (v2 && k ? chalk.bold.red(k) + ' ' : '') + chalk.magentaBright(v2); | ||
const pad1 = v1 ? r1Width - k.length - 1 - v1.length : r1Width; | ||
return `\n${s1}${' '.repeat(pad1)}\t${s2}`; | ||
}); | ||
throw new AssertionError(arr.join('')); | ||
}; | ||
module.exports = deepEqual; |
{ | ||
"name": "deep-eq", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "deepEqual assertion with pretty diff", | ||
"main": "index.js", | ||
"files": ["index.js"], | ||
"files": [ | ||
"index.js" | ||
], | ||
"scripts": { | ||
@@ -8,0 +10,0 @@ "test": "nyc node test", |
@@ -6,2 +6,5 @@ # deep-eq | ||
## Why? | ||
- `assert.deepEqual` [doesn't display well](https://github.com/nodejs/node/issues/15696) the diff between 2 objects | ||
- simple: no transpiling needed | ||
@@ -14,10 +17,11 @@ ```js | ||
outputs: (colors not shown) | ||
outputs: (colors not shown, key and values (left/right) have different colors) | ||
```sh | ||
AssertionError: | ||
↔ foo.ok: 1 !== 2 | ||
→ foo.u: 0 | ||
at .... | ||
foo.ok 1 foo.ok 2 | ||
foo.u 0 | ||
at ....stacktrace... | ||
``` | ||
[npm-image]: https://img.shields.io/npm/v/deep-eq.svg?style=flat-square | ||
@@ -24,0 +28,0 @@ [npm-url]: https://www.npmjs.com/package/deep-eq |
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
4042
63
31