1 | 'use strict';
|
---|
2 |
|
---|
3 | var test = require('tape');
|
---|
4 | var stringify = require('../');
|
---|
5 |
|
---|
6 | test('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 |
|
---|
15 | test('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 | });
|
---|