Initial commit
[yaffs-website] / node_modules / json-stable-stringify / test / replacer.js
1 var test = require('tape');
2 var stringify = require('../');
3
4 test('replace root', function (t) {
5         t.plan(1);
6
7         var obj = { a: 1, b: 2, c: false };
8         var replacer = function(key, value) { return 'one'; };
9
10         t.equal(stringify(obj, { replacer: replacer }), '"one"');
11 });
12
13 test('replace numbers', function (t) {
14         t.plan(1);
15
16         var obj = { a: 1, b: 2, c: false };
17         var replacer = function(key, value) {
18                 if(value === 1) return 'one';
19                 if(value === 2) return 'two';
20                 return value;
21         };
22
23         t.equal(stringify(obj, { replacer: replacer }), '{"a":"one","b":"two","c":false}');
24 });
25
26 test('replace with object', function (t) {
27         t.plan(1);
28
29         var obj = { a: 1, b: 2, c: false };
30         var replacer = function(key, value) {
31                 if(key === 'b') return { d: 1 };
32                 if(value === 1) return 'one';
33                 return value;
34         };
35
36         t.equal(stringify(obj, { replacer: replacer }), '{"a":"one","b":{"d":"one"},"c":false}');
37 });
38
39 test('replace with undefined', function (t) {
40         t.plan(1);
41
42         var obj = { a: 1, b: 2, c: false };
43         var replacer = function(key, value) {
44                 if(value === false) return;
45                 return value;
46         };
47
48         t.equal(stringify(obj, { replacer: replacer }), '{"a":1,"b":2}');
49 });
50
51 test('replace with array', function (t) {
52         t.plan(1);
53
54         var obj = { a: 1, b: 2, c: false };
55         var replacer = function(key, value) {
56                 if(key === 'b') return ['one', 'two'];
57                 return value;
58         };
59
60         t.equal(stringify(obj, { replacer: replacer }), '{"a":1,"b":["one","two"],"c":false}');
61 });
62
63 test('replace array item', function (t) {
64         t.plan(1);
65
66         var obj = { a: 1, b: 2, c: [1,2] };
67         var replacer = function(key, value) {
68                 if(value === 1) return 'one';
69                 if(value === 2) return 'two';
70                 return value;
71         };
72
73         t.equal(stringify(obj, { replacer: replacer }), '{"a":"one","b":"two","c":["one","two"]}');
74 });