summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/builtins/JSON/JSON.stringify-replacer.js
blob: 156edffee76fdb66887cdcf702f1ce6de4df4de8 (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
test("basic functionality", () => {
    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;
    });

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

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

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