Permalink
Browse files

Sort keys so deepEquals is key order agnostic

This is a combination of 4 commits.

* failing test for deepEqual key order
* deepEqual key order test now passes but isn't recursive yet
* failing test for recursive deepEqual key order
* deepEquals key sorting is now recursive, all tests pass
  • Loading branch information...
substack authored and isaacs committed Jan 6, 2012
1 parent 5f5c7ce commit 27c52281ff2a8b509f49cdbfa5ffcf2500376633
Showing with 27 additions and 1 deletion.
  1. +12 −1 lib/tap-assert.js
  2. +15 −0 test/deep.js
View
@@ -314,8 +314,19 @@ function selectFields (a, b) {
return ret
}
+function sortObject (obj) {
+ if (typeof obj !== 'object' || Array.isArray(obj) || obj === null) {
+ return obj
+ }
+
+ return Object.keys(obj).sort().reduce(function (acc, key) {
+ acc[key] = sortObject(obj[key])
+ return acc
+ }, {})
+}
+
function stringify (a) {
- return JSON.stringify(a, (function () {
+ return JSON.stringify(sortObject(a), (function () {
var seen = []
, keys = []
return function (key, val) {
View
@@ -0,0 +1,15 @@
+var tap = require("../")
+ , test = tap.test
+
+test("deepEquals shouldn't care about key order", function (t) {
+ t.deepEqual({ a : 1, b : 2 }, { b : 2, a : 1 })
+ t.end()
+})
+
+test("deepEquals shouldn't care about key order recursively", function (t) {
+ t.deepEqual(
+ { x : { a : 1, b : 2 }, y : { c : 3, d : 4 } },
+ { y : { d : 4, c : 3 }, x : { b : 2, a : 1 } }
+ )
+ t.end()
+})

0 comments on commit 27c5228

Please sign in to comment.