| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e13d1df commit 22ae8c0
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -134,7 +134,7 @@ changes: | |||
| 134 | 134 | * `expected` {any} | |
| 135 | 135 | * `message` {any} | |
| 136 | 136 | ||
| 137 | - Generally identical to `assert.deepEqual()` with three exceptions: | ||
| 137 | + Generally identical to `assert.deepEqual()` with a few exceptions: | ||
| 138 | 138 | ||
| 139 | 139 | 1. Primitive values besides `NaN` are compared using the [Strict Equality | |
| 140 | 140 | Comparison][] ( `===` ). Set and Map values, Map keys and `NaN` are compared | |
@@ -143,6 +143,7 @@ Generally identical to `assert.deepEqual()` with three exceptions: | |||
| 143 | 143 | 2. [`[[Prototype]]`][prototype-spec] of objects are compared using | |
| 144 | 144 | the [Strict Equality Comparison][] too. | |
| 145 | 145 | 3. [Type tags][Object.prototype.toString()] of objects should be the same. | |
| 146 | + 4. [Object wrappers][] are compared both as objects and unwrapped values. | ||
| 146 | 147 | ||
| 147 | 148 | ```js | |
| 148 | 149 | const assert = require('assert'); | |
@@ -172,8 +173,14 @@ assert.deepEqual(date, fakeDate); | |||
| 172 | 173 | assert.deepStrictEqual(date, fakeDate); | |
| 173 | 174 | // AssertionError: 2017-03-11T14:25:31.849Z deepStrictEqual Date {} | |
| 174 | 175 | // Different type tags | |
| 176 | + | ||
| 175 | 177 | assert.deepStrictEqual(NaN, NaN); | |
| 176 | 178 | // OK, because of the SameValueZero comparison | |
| 179 | + | ||
| 180 | + assert.deepStrictEqual(new Number(1), new Number(2)); | ||
| 181 | + // Fails because the wrapped number is unwrapped and compared as well. | ||
| 182 | + assert.deepStrictEqual(new String('foo'), Object('foo')); | ||
| 183 | + // OK because the object and the string are identical when unwrapped. | ||
| 177 | 184 | ``` | |
| 178 | 185 | ||
| 179 | 186 | If the values are not equal, an `AssertionError` is thrown with a `message` | |
@@ -711,3 +718,4 @@ For more information, see | |||
| 711 | 718 | [enumerable "own" properties]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties | |
| 712 | 719 | [mdn-equality-guide]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness | |
| 713 | 720 | [prototype-spec]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots | |
| 721 | + [Object wrappers]: https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_in_JavaScript | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -215,13 +215,30 @@ function strictDeepEqual(actual, expected) { | |||
| 215 | 215 | if (!areSimilarTypedArrays(actual, expected)) { | |
| 216 | 216 | return false; | |
| 217 | 217 | } | |
| 218 | - | ||
| 219 | 218 | // Buffer.compare returns true, so actual.length === expected.length | |
| 220 | 219 | // if they both only contain numeric keys, we don't need to exam further | |
| 221 | 220 | if (Object.keys(actual).length === actual.length && | |
| 222 | 221 | Object.keys(expected).length === expected.length) { | |
| 223 | 222 | return true; | |
| 224 | 223 | } | |
| 224 | + } else if (typeof actual.valueOf === 'function') { | ||
| 225 | + const actualValue = actual.valueOf(); | ||
| 226 | + // Note: Boxed string keys are going to be compared again by Object.keys | ||
| 227 | + if (actualValue !== actual) { | ||
| 228 | + if (!innerDeepEqual(actualValue, expected.valueOf(), true)) | ||
| 229 | + return false; | ||
| 230 | + // Fast path for boxed primitives | ||
| 231 | + var lengthActual = 0; | ||
| 232 | + var lengthExpected = 0; | ||
| 233 | + if (typeof actualValue === 'string') { | ||
| 234 | + lengthActual = actual.length; | ||
| 235 | + lengthExpected = expected.length; | ||
| 236 | + } | ||
| 237 | + if (Object.keys(actual).length === lengthActual && | ||
| 238 | + Object.keys(expected).length === lengthExpected) { | ||
| 239 | + return true; | ||
| 240 | + } | ||
| 241 | + } | ||
| 225 | 242 | } | |
| 226 | 243 | } | |
| 227 | 244 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -489,4 +489,23 @@ assert.doesNotThrow(() => { assert.deepStrictEqual({ a: NaN }, { a: NaN }); }); | |||
| 489 | 489 | assert.doesNotThrow( | |
| 490 | 490 | () => { assert.deepStrictEqual([ 1, 2, NaN, 4 ], [ 1, 2, NaN, 4 ]); }); | |
| 491 | 491 | ||
| 492 | + // Handle boxed primitives | ||
| 493 | + { | ||
| 494 | + const boxedString = new String('test'); | ||
| 495 | + const boxedSymbol = Object(Symbol()); | ||
| 496 | + assertOnlyDeepEqual(new Boolean(true), Object(false)); | ||
| 497 | + assertOnlyDeepEqual(Object(true), new Number(1)); | ||
| 498 | + assertOnlyDeepEqual(new Number(2), new Number(1)); | ||
| 499 | + assertOnlyDeepEqual(boxedSymbol, Object(Symbol())); | ||
| 500 | + assertOnlyDeepEqual(boxedSymbol, {}); | ||
| 501 | + assertDeepAndStrictEqual(boxedSymbol, boxedSymbol); | ||
| 502 | + assertDeepAndStrictEqual(Object(true), Object(true)); | ||
| 503 | + assertDeepAndStrictEqual(Object(2), Object(2)); | ||
| 504 | + assertDeepAndStrictEqual(boxedString, Object('test')); | ||
| 505 | + boxedString.slow = true; | ||
| 506 | + assertNotDeepOrStrict(boxedString, Object('test')); | ||
| 507 | + boxedSymbol.slow = true; | ||
| 508 | + assertNotDeepOrStrict(boxedSymbol, {}); | ||
| 509 | + } | ||
| 510 | + | ||
| 492 | 511 | /* eslint-enable */ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments