index.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // port of https://github.com/nodejs/node/blob/v6.3.0/lib/assert.js#L145-L248
  2. // http://wiki.commonjs.org/wiki/Unit_Testing/1.0
  3. //
  4. // Originally from narwhal.js (http://narwhaljs.org)
  5. // Copyright (c) 2009 Thomas Robinson <280north.com>
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the 'Software'), to
  9. // deal in the Software without restriction, including without limitation the
  10. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  11. // sell copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. 'use strict';
  24. var Buffer = require('buffer').Buffer;
  25. var compare = Buffer.compare;
  26. var indexOf = require('indexof');
  27. var filter = require('array-filter');
  28. var getPrototypeOf = Object.getPrototypeOf || function(obj) {
  29. return obj.__proto__ || (
  30. obj.constructor
  31. ? obj.constructor.prototype
  32. : Object.prototype
  33. );
  34. };
  35. function isEnumerable(obj, key) {
  36. return Object.prototype.propertyIsEnumerable.call(obj, key);
  37. };
  38. function pToString(obj) {
  39. return Object.prototype.toString.call(obj);
  40. };
  41. function isPrimitive(arg) {
  42. return arg === null ||
  43. typeof arg === 'boolean' ||
  44. typeof arg === 'number' ||
  45. typeof arg === 'string' ||
  46. typeof arg === 'symbol' || // ES6 symbol
  47. typeof arg === 'undefined';
  48. }
  49. function isObject(arg) {
  50. return typeof arg === 'object' && arg !== null;
  51. }
  52. function isDate(d) {
  53. return isObject(d) && pToString(d) === '[object Date]';
  54. }
  55. function isRegExp(re) {
  56. return isObject(re) && pToString(re) === '[object RegExp]';
  57. }
  58. var isArguments = (function () {
  59. function isArg(obj) {
  60. return isObject(obj) && pToString(obj) == '[object Arguments]';
  61. }
  62. // dealing with old IEs (There's no Arguments type)
  63. if (!isArg(arguments)) {
  64. return function(obj) {
  65. return isObject(obj) &&
  66. typeof obj.length === 'number' &&
  67. obj.length >= 0 &&
  68. pToString(obj) !== '[object Array]' &&
  69. pToString(obj.callee) === '[object Function]';
  70. };
  71. } else {
  72. return isArg;
  73. }
  74. })();
  75. function fromBufferSupport() {
  76. try {
  77. return typeof Buffer.from === 'function' && !!Buffer.from([0x62,0x75,0x66,0x66,0x65,0x72]);
  78. } catch (e) {
  79. // Buffer.from under Node 4.x causes `TypeError: this is not a typed array.`
  80. return false;
  81. }
  82. }
  83. var toBuffer = (function () {
  84. // check whether Buffer constructor accepts ArrayBuffer or not
  85. function isBufferConstructorAcceptsArrayBuffer() {
  86. try {
  87. return typeof Uint8Array === 'function' && (new Buffer(new Uint8Array([1]).buffer)[0] === 1);
  88. } catch (e) {
  89. return false;
  90. }
  91. }
  92. if (isBufferConstructorAcceptsArrayBuffer()) {
  93. // Node 4.x
  94. return function (ab) {
  95. return new Buffer(ab);
  96. };
  97. } else {
  98. // Node 0.10.x and 0.12.x
  99. return function (ab) {
  100. var buffer = new Buffer(ab.byteLength);
  101. var view = new Uint8Array(ab);
  102. for (var i = 0; i < buffer.length; ++i) {
  103. buffer[i] = view[i];
  104. }
  105. return buffer;
  106. };
  107. }
  108. })();
  109. var bufferFrom = fromBufferSupport() ? Buffer.from : toBuffer;
  110. var objectKeys = (function () {
  111. var OLD_V8_ARRAY_BUFFER_ENUM = ['BYTES_PER_ELEMENT','get','set','slice','subarray','buffer','length','byteOffset','byteLength'];
  112. var keys = Object.keys || require('object-keys');
  113. return function objectKeys(obj) {
  114. // avoid iterating enumerable properties of ArrayBuffer under old V8
  115. if (isEnumerable(obj, 'buffer') &&
  116. isEnumerable(obj, 'byteOffset') &&
  117. isEnumerable(obj, 'byteLength')) {
  118. return filter(keys(obj), function (k) {
  119. return indexOf(OLD_V8_ARRAY_BUFFER_ENUM, k) === -1;
  120. });
  121. } else {
  122. return keys(obj);
  123. }
  124. };
  125. })();
  126. function _deepEqual(actual, expected, strict, memos) {
  127. // 7.1. All identical values are equivalent, as determined by ===.
  128. if (actual === expected) {
  129. return true;
  130. } else if (actual instanceof Buffer && expected instanceof Buffer) {
  131. return compare(actual, expected) === 0;
  132. // 7.2. If the expected value is a Date object, the actual value is
  133. // equivalent if it is also a Date object that refers to the same time.
  134. } else if (isDate(actual) && isDate(expected)) {
  135. return actual.getTime() === expected.getTime();
  136. // 7.3 If the expected value is a RegExp object, the actual value is
  137. // equivalent if it is also a RegExp object with the same source and
  138. // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
  139. } else if (isRegExp(actual) && isRegExp(expected)) {
  140. return actual.source === expected.source &&
  141. actual.global === expected.global &&
  142. actual.multiline === expected.multiline &&
  143. actual.lastIndex === expected.lastIndex &&
  144. actual.ignoreCase === expected.ignoreCase;
  145. // 7.4. Other pairs that do not both pass typeof value == 'object',
  146. // equivalence is determined by ==.
  147. } else if ((actual === null || typeof actual !== 'object') &&
  148. (expected === null || typeof expected !== 'object')) {
  149. return strict ? actual === expected : actual == expected;
  150. // If both values are instances of typed arrays, wrap their underlying
  151. // ArrayBuffers in a Buffer each to increase performance
  152. // This optimization requires the arrays to have the same type as checked by
  153. // Object.prototype.toString (aka pToString). Never perform binary
  154. // comparisons for Float*Arrays, though, since e.g. +0 === -0 but their
  155. // bit patterns are not identical.
  156. } else if (typeof ArrayBuffer === 'function' && typeof ArrayBuffer.isView === 'function' &&
  157. ArrayBuffer.isView(actual) && ArrayBuffer.isView(expected) &&
  158. pToString(actual) === pToString(expected) &&
  159. !(actual instanceof Float32Array ||
  160. actual instanceof Float64Array)) {
  161. return compare(bufferFrom(actual.buffer),
  162. bufferFrom(expected.buffer)) === 0;
  163. // 7.5 For all other Object pairs, including Array objects, equivalence is
  164. // determined by having the same number of owned properties (as verified
  165. // with Object.prototype.hasOwnProperty.call), the same set of keys
  166. // (although not necessarily the same order), equivalent values for every
  167. // corresponding key, and an identical 'prototype' property. Note: this
  168. // accounts for both named and indexed properties on Arrays.
  169. } else {
  170. memos = memos || {actual: [], expected: []};
  171. var actualIndex = indexOf(memos.actual, actual);
  172. if (actualIndex !== -1) {
  173. if (actualIndex === indexOf(memos.expected, expected)) {
  174. return true;
  175. }
  176. }
  177. memos.actual.push(actual);
  178. memos.expected.push(expected);
  179. return objEquiv(actual, expected, strict, memos);
  180. }
  181. }
  182. function objEquiv(a, b, strict, actualVisitedObjects) {
  183. if (a === null || a === undefined || b === null || b === undefined)
  184. return false;
  185. // if one is a primitive, the other must be same
  186. if (isPrimitive(a) || isPrimitive(b))
  187. return a === b;
  188. if (strict && getPrototypeOf(a) !== getPrototypeOf(b))
  189. return false;
  190. var aIsArgs = isArguments(a),
  191. bIsArgs = isArguments(b);
  192. if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs))
  193. return false;
  194. var ka = objectKeys(a),
  195. kb = objectKeys(b),
  196. key, i;
  197. // having the same number of owned properties (keys incorporates
  198. // hasOwnProperty)
  199. if (ka.length != kb.length)
  200. return false;
  201. //the same set of keys (although not necessarily the same order),
  202. ka.sort();
  203. kb.sort();
  204. //~~~cheap key test
  205. for (i = ka.length - 1; i >= 0; i--) {
  206. if (ka[i] != kb[i])
  207. return false;
  208. }
  209. //equivalent values for every corresponding key, and
  210. //~~~possibly expensive deep test
  211. for (i = ka.length - 1; i >= 0; i--) {
  212. key = ka[i];
  213. if (!_deepEqual(a[key], b[key], strict, actualVisitedObjects))
  214. return false;
  215. }
  216. return true;
  217. }
  218. module.exports = _deepEqual;