source: node_modules/json-stable-stringify/test/cmp.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 936 bytes
Line 
1'use strict';
2
3var test = require('tape');
4var stringify = require('../');
5
6test('custom comparison function', function (t) {
7 t.plan(1);
8 var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
9 var s = stringify(obj, function (a, b) {
10 return a.key < b.key ? 1 : -1;
11 });
12 t.equal(s, '{"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}');
13});
14
15test('custom comparison function with get', function (t) {
16 t.plan(2);
17
18 stringify({ a: 1, b: 2 }, function (a, b) { // eslint-disable-line no-unused-vars
19 t.equal(arguments[2], undefined, 'comparator options not passed when not explicitly requested');
20 });
21
22 var obj = { c: 8, b: [{ z: 7, y: 6, x: 4, v: 2, '!v': 3 }, 7], a: 3 };
23 var s = stringify(obj, function (a, b, options) {
24 var get = options.get;
25 var v1 = (get('!' + a.key) || 0) + a.value;
26 var v2 = (get('!' + b.key) || 0) + b.value;
27 return v1 - v2;
28 });
29 t.equal(s, '{"c":8,"b":[{"!v":3,"x":4,"v":2,"y":6,"z":7},7],"a":3}');
30});
Note: See TracBrowser for help on using the repository browser.