Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8ab93e2

Browse files
author
James Halliday
committed
comparison test now passes
1 parent 3af627d commit 8ab93e2

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

‎index.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ var json = typeof JSON !== 'undefined' ? JSON : require('jsonify');
33
module.exports = function (obj, opts) {
44
if (!opts) opts = {};
55
if (typeof opts === 'function') opts = { cmp: opts };
6-
var cmp = opts.cmp;
6+
var cmp = opts.cmp && (function (f) {
7+
return function (node) {
8+
return function (a, b) {
9+
var aobj = { key: a, value: node[a] };
10+
var bobj = { key: b, value: node[b] };
11+
return f(aobj, bobj);
12+
};
13+
};
14+
})(opts.cmp);
715

816
return (function stringify (node) {
917
if (typeof node !== 'object') {
@@ -17,7 +25,7 @@ module.exports = function (obj, opts) {
1725
return '[' + out.join(',') + ']';
1826
}
1927
else {
20-
var keys = objectKeys(node).sort(cmp);
28+
var keys = objectKeys(node).sort(cmp(node));
2129
var out = [];
2230
for (var i = 0; i < keys.length; i++) {
2331
var key = keys[i];

‎test/cmp.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ test('custom comparison function', function (t) {
55
t.plan(1);
66
var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
77
var s = stringify(obj, function (a, b) {
8-
return a.key < b.key ? -1 : 1;
8+
return a.key < b.key ? 1 : -1;
99
});
1010
t.equal(s, '{"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}');
1111
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /