source: imaps-frontend/node_modules/json-stable-stringify-without-jsonify/test/replacer.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.7 KB
RevLine 
[d565449]1var test = require('tape');
2var stringify = require('../');
3
4test('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
13test('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
26test('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
39test('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
51test('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
63test('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});
Note: See TracBrowser for help on using the repository browser.