defaults-deep-ts
Version:
_.defaultsDeep with proper typescript typing
92 lines (91 loc) • 3.93 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var index_1 = __importDefault(require("./index"));
var noop = function () { return undefined; };
describe('defaultsDeep', function () {
it('provides defaults', function () {
var object = {};
var source = { a: {} };
var actual = { a: {} };
expect((0, index_1.default)(object, source)).toEqual(actual);
});
it('doesn\'t override existing props', function () {
var object = { a: { b: 1 } };
var source = { a: {} };
var actual = { a: { b: 1 } };
expect((0, index_1.default)(object, source)).toEqual(actual);
});
it('extends props', function () {
var object = { a: { a: 1 } };
var source = { a: { a: 2, b: 2 } };
var actual = { a: { a: 1, b: 2 } };
expect((0, index_1.default)(object, source)).toEqual(actual);
});
it('dosn\'t override arrays', function () {
var object = { a: [] };
var source = { a: [2] };
var actual = { a: [] };
expect((0, index_1.default)(object, source)).toEqual(actual);
});
it('should deep assign source properties if missing on `object`', function () {
var object = { 'a': { 'b': 2 }, 'd': 4 }, source = { 'a': { 'b': 3, 'c': 3 }, 'e': 5 }, expected = { 'a': { 'b': 2, 'c': 3 }, 'd': 4, 'e': 5 };
expect((0, index_1.default)(object, source)).toEqual(expected);
});
it('should overwrite `null` values', function () {
var object = { 'a': { 'b': null } };
var source = { 'a': { 'b': 2 } };
var actual = (0, index_1.default)(object, source);
expect(actual.a.b).toBe(2);
});
it('should not overwrite regexp values', function () {
var object = { 'a': { 'b': /x/ } };
var source = { 'a': { 'b': /y/ } };
var actual = (0, index_1.default)(object, source);
expect(actual.a.b).toEqual(/x/);
});
it('should not convert function properties to objects', function () {
var actual = (0, index_1.default)({}, { 'a': noop });
expect(actual.a).toEqual(noop);
actual = (0, index_1.default)({}, { 'a': { 'b': noop } });
expect(actual.a.b).toEqual(noop);
});
it('should overwrite `undefined` values', function () {
var object = { 'a': { 'b': undefined } };
var source = { 'a': { 'b': 2 } };
var actual = (0, index_1.default)(object, source);
expect(actual.a.b).toEqual(2);
});
it('should assign `undefined` values', function () {
var source = { 'a': undefined, 'b': { 'c': undefined, 'd': 1 } };
var expected = { 'a': undefined, 'b': { 'c': undefined, 'd': 1 } };
var actual = (0, index_1.default)({}, source);
expect(actual).toEqual(expected);
});
it.skip('should merge sources containing circular references', function () {
var object = {
'foo': { 'b': { 'c': { 'd': {} } } },
'bar': { 'a': 2 }
};
var source = {
'foo': { 'b': { 'c': { 'd': {} } } },
'bar': {}
};
object.foo.b.c.d = object;
source.foo.b.c.d = source;
source.bar.b = source.foo.b;
var actual = (0, index_1.default)(object, source);
expect(actual.bar.b).toEqual(actual.foo.b);
expect(actual.foo.b.c.d).toEqual(actual.foo.b.c.d.foo.b.c.d);
});
it('should not attempt a merge of a string into an array', function () {
var actual = (0, index_1.default)({ 'a': ['abc'] }, { 'a': 'abc' });
expect(actual.a).toEqual(['abc']);
});
it('shouldn’t fail to compile with error TS2589', function () {
var z = (0, index_1.default)({}, {});
[z].find(function (_) { return true; });
});
});