first commit

This commit is contained in:
s.golasch
2023-08-01 13:49:46 +02:00
commit 1fc239fd54
20238 changed files with 3112246 additions and 0 deletions

View File

@@ -0,0 +1,111 @@
var _ = require('lodash');
var should = require('should');
_.defaultsDeep = require('../');
/**
* Purpose:
* This test exists to make sure I didn't break anything when
* using mergeDefaults to override `_.defaults`.
*/
// From Lodash core tests:
// https://github.com/lodash/lodash/blob/master/test/test.js#L1843
describe('Test that _.mergeDefaults is backwards compatible with _.defaults \n', function() {
it('should assign properties of a source object if missing on the destination object', function() {
deepEqual(_.defaultsDeep({
'a': 1
}, {
'a': 2,
'b': 2
}), {
'a': 1,
'b': 2
});
});
it('should assign own source properties', function() {
function Foo() {
this.a = 1;
this.c = 3;
}
Foo.prototype.b = 2;
deepEqual(_.defaultsDeep({
'c': 2
}, new Foo()), {
'a': 1,
'c': 2
});
});
it('should accept multiple source objects', function() {
var expected = {
'a': 1,
'b': 2,
'c': 3
};
deepEqual(_.defaultsDeep({
'a': 1,
'b': 2
}, {
'b': 3
}, {
'c': 3
}), expected);
deepEqual(_.defaultsDeep({
'a': 1,
'b': 2
}, {
'b': 3,
'c': 3
}, {
'c': 2
}), expected);
});
it('should not overwrite `null` values', function() {
var actual = _.defaultsDeep({
'a': null
}, {
'a': 1
});
strictEqual(actual.a, null);
});
it('should overwrite `undefined` values', function() {
var actual = _.defaultsDeep({
'a': undefined
}, {
'a': 1
});
strictEqual(actual.a, 1);
});
it('should not error on `null` or `undefined` sources (test in IE < 9)', function() {
try {
deepEqual(_.defaultsDeep({
'a': 1
}, null, undefined, {
'a': 2,
'b': 2
}), {
'a': 1,
'b': 2
});
} catch (e) {
throw e;
}
});
});
// helper methods
function strictEqual(x, y) {
return should(x).equal(y);
}
function deepEqual(x, y) {
return should(x).eql(y);
}

View File

@@ -0,0 +1,96 @@
var assert = require('assert');
var _ = require('lodash');
_.mergeDefaults = require('../');
describe('mergeDefaults', function() {
describe('dont merge arrays', function() {
var X, Y, result;
beforeEach(function() {
X = {
z: 1,
a: 2,
b: 3,
d: {},
e: []
};
Y = {
a: 1,
b: 22,
c: 33,
d: {
x: 10
},
e: ['a','b']
};
result = _.mergeDefaults(X, Y);
});
it('should return an object', function() {
assert(typeof result === 'object');
});
it('should NOT MERGE ARRAYS in sub-objects', function() {
assert.deepEqual(result.e, []);
});
});
describe('complex (recursive)', function() {
var X, Y, result;
beforeEach(function() {
X = {
views: {
foo: {},
blueprints: {
someArray: ['z'],
enabled: true
}
},
connections: {},
z: 1,
a: 2,
b: 3,
d: {}
};
Y = {
views: {
locales: ['en', 'es'],
},
controllers: {
foo: {
bar: 'asdf'
},
blueprints: {
someArray: ['a','b'],
enabled: false
}
},
connections: {
mysql: {
host: 'localhost',
port: 1835913851
}
},
a: 1,
b: 22,
c: 33,
d: {
x: 10
}
};
result = _.mergeDefaults(X, Y);
});
it('should return an object', function() {
assert(typeof result === 'object');
});
it('should NOT MERGE ARRAYS in sub-objects', function() {
assert.deepEqual(result.views.blueprints.someArray, ['z']);
});
});
});

View File

@@ -0,0 +1,213 @@
var assert = require('assert');
var should = require('should');
var _ = require('lodash');
_.mergeDefaults = require('../');
describe('mergeDefaults', function() {
describe('basic (recursive)', function() {
var X, Y, result;
beforeEach(function() {
X = {
z: 1,
a: 2,
b: 3,
d: {}
};
Y = {
a: 1,
b: 22,
c: 33,
d: {
x: 10
}
};
result = _.mergeDefaults(X, Y);
});
it('should return an object', function() {
should(result).be.an.Object;
});
it('should have the expected values from first arg', function() {
should(result).have.property('z', 1);
should(result).have.property('a', 2);
should(result).have.property('b', 3);
});
it('should have the expected values from second arg', function() {
should(result).have.property('c', 33);
});
it('should have recursively merged the sub-objects', function() {
should(result).have
.property('d').with.a
.property('x', 10);
});
});
describe('complex (recursive)', function() {
var X, Y, result;
beforeEach(function() {
X = {
views: {
foo: {},
blueprints: {
enabled: true
}
},
connections: {},
z: 1,
a: 2,
b: 3,
d: {}
};
Y = {
views: {
locales: ['en', 'es'],
},
controllers: {
foo: {
bar: 'asdf'
},
blueprints: {
enabled: false
}
},
connections: {
mysql: {
host: 'localhost',
port: 1835913851
}
},
a: 1,
b: 22,
c: 33,
d: {
x: 10
}
};
result = _.mergeDefaults(X, Y);
});
it('should return an object', function() {
should(result).be.an.Object;
});
it('should have the expected values from first arg', function() {
should(result).have.property('z', 1);
should(result).have.property('a', 2);
should(result).have.property('b', 3);
});
it('should have the expected values from second arg', function() {
should(result).have.property('c', 33);
});
it('should have recursively merged the sub-objects', function() {
should(result).have
.property('d').with.a
.property('x', 10);
});
it('should still work even when shit gets crazy', function() {
// .views.locales
should(result).have
.property('views').with.a
.property('locales');
// .views.blueprints.enabled = true
should(result).have
.property('views').with
.property('blueprints').with
.property('enabled', true);
// controllers.foo.bar
should(result).have
.property('controllers').with
.property('foo').with
.property('bar');
// connections.mysql.host = 'localhost'
should(result).have
.property('connections').with
.property('mysql').with
.property('host', 'localhost');
});
});
describe('3-level-deep merge', function() {
var X = {
foo: {
a:1,
b:2,
bar: {
a:1,
b:2
}
}
};
var Y = {
foo: {
a:100,
c:3,
bar: {
a:10,
c:3
}
}
};
var result = _.mergeDefaults(X, Y);
it('should retain the values in X (first arg)', function (){
assert.equal(result.foo.a, 1);
assert.equal(result.foo.bar.a, 1);
assert.equal(result.foo.bar.b, 2);
});
it('should receive new values from Y (second arg)', function () {
assert.equal(result.foo.bar.c, 3);
});
});
describe('4-level-deep merge', function() {
var X = {
baz: {
foo: {
a:1,
b:2,
bar: {
a:1,
b:2
}
}
}
};
var Y = {
baz: {
foo: {
a:100,
c:3,
bar: {
a:10,
c:3
}
}
}
};
var result = _.mergeDefaults(X, Y);
it('should retain the values in X (first arg)', function (){
assert.equal(result.baz.foo.a, 1);
assert.equal(result.baz.foo.bar.a, 1);
assert.equal(result.baz.foo.bar.b, 2);
});
it('should receive new values from Y (second arg)', function () {
assert.equal(result.baz.foo.bar.c, 3);
});
});
});