altair-graphql-core
Version:
Several of the core logic for altair graphql client
52 lines • 3.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const dot_notation_1 = require("./dot-notation");
(0, vitest_1.describe)('setByDotNotation', () => {
(0, vitest_1.it)('should correctly set value by dot notation', () => {
const obj = {};
const existingObj = { a: 1, b: { c: 2 } };
// set to empty path
(0, dot_notation_1.setByDotNotation)(obj, 'a.b', 3);
(0, vitest_1.expect)(obj).toEqual({ a: { b: 3 } });
// set to existing object in path
(0, dot_notation_1.setByDotNotation)(obj, 'a.c', 4);
(0, vitest_1.expect)(obj).toEqual({ a: { b: 3, c: 4 } });
// set to new array
(0, dot_notation_1.setByDotNotation)(obj, 'a.d.1', 5);
(0, vitest_1.expect)(obj).toEqual({ a: { b: 3, c: 4, d: [undefined, 5] } });
// set to existing array
(0, dot_notation_1.setByDotNotation)(obj, 'a.d.0', 6);
(0, vitest_1.expect)(obj).toEqual({ a: { b: 3, c: 4, d: [6, 5] } });
// set existing value
(0, dot_notation_1.setByDotNotation)(obj, 'a.c', 7);
(0, vitest_1.expect)(obj).toEqual({ a: { b: 3, c: 7, d: [6, 5] } });
// set new object inside array
(0, dot_notation_1.setByDotNotation)(obj, 'a.d.2.x', 8);
(0, vitest_1.expect)(obj).toEqual({ a: { b: 3, c: 7, d: [6, 5, { x: 8 }] } });
// set new object inside existing object
(0, dot_notation_1.setByDotNotation)(existingObj, 'b.e.f', 9);
(0, vitest_1.expect)(existingObj).toEqual({ a: 1, b: { c: 2, e: { f: 9 } } });
// set object inside existing object
(0, dot_notation_1.setByDotNotation)(existingObj, 'b.e', { g: 10 });
(0, vitest_1.expect)(existingObj).toEqual({ a: 1, b: { c: 2, e: { g: 10 } } });
// set object inside existing object with merge
(0, dot_notation_1.setByDotNotation)(existingObj, 'b.e', { h: 11 }, true);
(0, vitest_1.expect)(existingObj).toEqual({ a: 1, b: { c: 2, e: { g: 10, h: 11 } } });
// prevent prototype pollution
const polluted = {};
(0, dot_notation_1.setByDotNotation)(polluted, '__proto__.polluted', 'polluted');
(0, vitest_1.expect)(polluted.polluted).toBeUndefined();
(0, vitest_1.expect)({}.polluted).toBeUndefined();
(0, dot_notation_1.setByDotNotation)(polluted, 'constructor.prototype.polluted', 'polluted');
(0, vitest_1.expect)(polluted.polluted).toBeUndefined();
(0, vitest_1.expect)({}.polluted).toBeUndefined();
// ensure valid paths with substrings are not blocked
const validObj = {};
(0, dot_notation_1.setByDotNotation)(validObj, 'myconstructor', 'is-ok');
(0, vitest_1.expect)(validObj.myconstructor).toBe('is-ok');
(0, dot_notation_1.setByDotNotation)(validObj, 'some_prototype_value', 'is-ok');
(0, vitest_1.expect)(validObj.some_prototype_value).toBe('is-ok');
});
});
//# sourceMappingURL=dot-notation.spec.js.map