ts-prime
Version:
A utility library for JavaScript and Typescript.
28 lines (27 loc) • 1.26 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var setPath_1 = require("./setPath");
test('setPath', function () {
expect(setPath_1.setPath({}, 'a.b.c.d'.split('.'), 58)).toEqual({
a: { b: { c: { d: 58 } } },
});
expect(setPath_1.setPath({ a: { x: 4 } }, 'a.b.c.d'.split('.'), 58)).toEqual({
a: { x: 4, b: { c: { d: 58 } } },
});
var data = { data: 4, a: [1, 2, 3, 4, 5] };
expect(setPath_1.setPath(data, 'a.5'.split('.'), 58)).toEqual({
a: [1, 2, 3, 4, 5, 58],
data: 4,
});
expect(data).toEqual({ data: 4, a: [1, 2, 3, 4, 5] });
expect(setPath_1.setPath([], '0.a.b'.split('.'), 58)).toEqual([{ a: { b: 58 } }]);
});
test('setPath set existing value', function () {
expect(setPath_1.setPath({ a: { b: { c: { d: 58 } } } }, 'a.b.c.d'.split('.'), 22)).toEqual({ a: { b: { c: { d: 22 } } } });
});
test('setPath overwrite entire path', function () {
expect(setPath_1.setPath({ a: { b: { c: { d: 58 } } } }, 'a.b'.split('.'), 22)).toEqual({ a: { b: 22 } });
});
test('setPath overwrite entire path', function () {
expect(setPath_1.setPath({ a: { b: { c: { d: 58 } } } }, 'a.b'.split('.'), [1, 2, 3, 4, 5])).toEqual({ a: { b: [1, 2, 3, 4, 5] } });
});