@tdb/util
Version:
Shared helpers and utilities.
170 lines (169 loc) • 6.84 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var chai_1 = require("chai");
var _1 = require(".");
var object = _1.value.object;
describe('object.build', function () {
it('return default root object (no keyPath)', function () {
chai_1.expect(object.build('', {})).to.eql({});
chai_1.expect(object.build(' ', {})).to.eql({});
});
it('returns clone of the given root object', function () {
var obj = {};
chai_1.expect(object.build('', obj)).to.not.equal(obj);
});
it('adds single level', function () {
chai_1.expect(object.build('foo', {})).to.eql({ foo: {} });
chai_1.expect(object.build(' foo ', {})).to.eql({ foo: {} });
});
it('adds multi-levels (path)', function () {
var res = object.build('foo.bar', {});
chai_1.expect(res).to.eql({ foo: { bar: {} } });
});
it('adds multi-levels with custom value', function () {
var test = function (value) {
var res = object.build('foo.bar.baz', {}, value);
chai_1.expect(res.foo.bar.baz).to.eql(value);
};
test(0);
test(123);
test('hello');
test('');
test(' ');
test({});
});
it('does not replace existing object/value (cloned, single-level)', function () {
var obj = { foo: { bar: 123 } };
var res = object.build('foo', obj);
chai_1.expect(res).to.eql(obj);
chai_1.expect(res).to.not.equal(obj);
chai_1.expect(res.foo).to.not.equal(obj.foo);
});
it('throws if path overwrites value', function () {
var test = function (keyPath, obj) {
var fn = function () { return object.build(keyPath, obj); };
chai_1.expect(fn).to.throw();
};
test('foo.bar', { foo: { bar: 123 } });
test('foo.bar', { foo: { bar: 0 } });
test('foo.bar', { foo: { bar: null } });
test('foo.bar', { foo: { bar: '' } });
});
it('throws if starts/ends with period (.)', function () {
var test = function (keyPath) {
var fn = function () { return object.build(keyPath, {}); };
chai_1.expect(fn).to.throw();
};
test('foo.bar.');
test('foo.bar. ');
test('.foo.bar');
test(' .foo.bar ');
test('.foo.bar.');
});
it('appends existing object', function () {
var obj = { foo: { bar: 123 } };
var res = object.build('foo.baz', obj);
chai_1.expect(res).to.eql({ foo: { bar: 123, baz: {} } });
});
});
describe('object.pluck', function () {
it('returns [undefined] when no match', function () {
chai_1.expect(object.pluck('foo', {})).to.eql(undefined);
chai_1.expect(object.pluck('foo.bar', {})).to.eql(undefined);
chai_1.expect(object.pluck('foo.bar', { baz: 123 })).to.eql(undefined);
});
it('gets value', function () {
var test = function (keyPath, root, value) {
var res = object.pluck(keyPath, root);
chai_1.expect(res).to.eql(value, "The key-path \"" + keyPath + "\" should be [" + value + "]");
};
test('foo', { foo: 123 }, 123);
test('foo.bar', { foo: { bar: 123 } }, 123);
test(' foo.bar ', { foo: { bar: 123 } }, 123);
test(' foo. bar ', { foo: { bar: 123 } }, 123);
});
it('throws if starts/ends with period (.)', function () {
var test = function (key) {
var fn = function () { return object.pluck(key, {}); };
chai_1.expect(fn).to.throw();
};
test('foo.bar.');
test('foo.bar. ');
test('.foo.bar');
test(' .foo.bar ');
test('.foo.bar.');
});
});
describe('object.remove', function () {
var test = function (keyPath, root, expected) {
var result = object.remove(keyPath, root);
var msg = "keyPath: \"" + keyPath + "\"";
chai_1.expect(result).to.eql(expected, msg);
chai_1.expect(result).to.not.equal(root, msg);
};
it('removes nothing (no match)', function () {
test('', {}, {});
test('', { foo: 123 }, { foo: 123 });
test('foo', {}, {});
test('foo', { bar: 456 }, { bar: 456 });
test('foo.bar', {}, {});
test('foo.bar', { foo: 123 }, { foo: 123 });
test('foo.bar.baz', { foo: 123 }, { foo: 123 });
});
it('removes shallow path', function () {
test('foo', { foo: 123 }, {});
test('foo', { foo: 123, bar: 'hi' }, { bar: 'hi' });
});
it('removes deep path', function () {
test('foo.bar', { foo: { bar: 123 } }, { foo: {} });
test('foo.bar.baz', { foo: { bar: { baz: 456 } } }, { foo: { bar: {} } });
test('foo.bar', { foo: { bar: 123, baz: 456 } }, { foo: { baz: 456 } });
test('foo.bar', { foo: { bar: 123 }, baz: 456 }, { baz: 456, foo: {} });
});
it('removes wildcard (*)', function () {
test('foo.*', { foo: { bar: 123 } }, { foo: {} });
test('foo.*', { foo: { bar: 123 }, baz: 456 }, { baz: 456, foo: {} });
test('*', { foo: { bar: 123 }, baz: 456 }, {});
});
});
describe('object.prune', function () {
var test = function (keyPath, root, expected) {
var result = object.prune(keyPath, root);
var msg = "keyPath: \"" + keyPath + "\"";
chai_1.expect(result).to.eql(expected, msg);
chai_1.expect(result).to.not.equal(root, msg);
};
it('prunes nothing (no match)', function () {
test('', {}, {});
test('', { foo: 123 }, { foo: 123 });
test('foo', {}, {});
test('foo', { bar: 456 }, { bar: 456 });
test('foo.bar', {}, {});
test('foo.bar', { foo: 123 }, { foo: 123 });
test('foo.bar.baz', { foo: 123 }, { foo: 123 });
});
it('prunes nothing (child not empty)', function () {
test('foo', { foo: { bar: {} } }, { foo: { bar: {} } });
});
it('throws if wild card not at end of path', function () {
var fn = function () { return object.prune('*.bar', {}); };
chai_1.expect(fn).to.throw();
});
it('prunes wildcard (*)', function () {
test('foo.*', { foo: { bar: {}, baz: 123 } }, {});
test('foo.*', { foo: 123 }, {});
test('foo.*', {}, {});
test('*', {}, {});
test('*', { foo: 123, bar: {} }, {});
});
it('prunes shallow path', function () {
test('foo', { foo: 123 }, {});
test('foo', { foo: 123, bar: 'hi' }, { bar: 'hi' });
});
it('prunes deep path', function () {
test('foo.bar', { foo: { bar: 123 } }, {});
test('foo.bar.baz', { foo: { bar: { baz: 456 } } }, {});
test('foo.bar', { foo: { bar: 123, baz: 456 } }, { foo: { baz: 456 } });
test('foo.bar', { foo: { bar: 123 }, baz: 456 }, { baz: 456 });
});
});