summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/builtins/JSON/JSON.stringify-replacer.js
blob: e994d25ae4c17a4c9c31b4da909352be603fa680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
load("test-common.js");

try {
    let o = {
        var1: "foo",
        var2: 42,
        arr: [1, 2, {
            nested: {
                hello: "world",
            },
            get x() { return 10; }
        }],
        obj: {
            subarr: [3],
        },
    };

    let string = JSON.stringify(o, (key, value) => {
        if (key === "hello")
            return undefined;
        if (value === 10)
            return 20;
        if (key === "subarr")
            return [3, 4, 5];
        return value;
    });

    assert(string === '{"var1":"foo","var2":42,"arr":[1,2,{"nested":{},"x":20}],"obj":{"subarr":[3,4,5]}}');

    string = JSON.stringify(o, ["var1", "var1", "var2", "obj"]);
    assert(string == '{"var1":"foo","var2":42,"obj":{}}');

    string = JSON.stringify(o, ["var1", "var1", "var2", "obj", "subarr"]);
    assert(string == '{"var1":"foo","var2":42,"obj":{"subarr":[3]}}');

    console.log("PASS");
} catch (e) {
    console.log("FAIL: " + e);
}