不要怂,就是干,撸起袖子干!

Commit 19defcad by Jan Aagaard Meier

Merge pull request #4041 from dantman/master

Fix  #4040, do not call .clone method on plain objects or arrays.
2 parents 789c3dc9 a3d45b8b
Showing with 44 additions and 1 deletions
......@@ -90,8 +90,14 @@ var Utils = module.exports = {
},
cloneDeep: function(obj, fn) {
return lodash.cloneDeep(obj, function (elem) {
// Do not try to customize cloning of plain objects and strings
if (Array.isArray(elem) || lodash.isPlainObject(elem)) {
return undefined;
}
// Preserve special data-types like `fn` across clones. _.get() is used for checking up the prototype chain
if (elem && typeof elem.clone === 'function') {return elem.clone(); }
if (elem && typeof elem.clone === 'function') {
return elem.clone();
}
// Unfortunately, lodash.cloneDeep doesn't preserve Buffer.isBuffer, which we have to rely on for binary data
if (Buffer.isBuffer(elem)) { return elem; }
......
......@@ -109,6 +109,43 @@ describe(Support.getTestDialectTeaser('Utils'), function() {
});
});
describe('cloneDeep', function() {
it('should clone objects', function() {
var obj = {foo: 1}
, clone = Utils.cloneDeep(obj);
expect(obj).to.not.equal(clone);
});
it('should clone nested objects', function() {
var obj = {foo: {bar: 1}}
, clone = Utils.cloneDeep(obj);
expect(obj.foo).to.not.equal(clone.foo);
});
it('should not call clone methods on plain objects', function() {
expect(function() {
Utils.cloneDeep({
clone: function() {
throw new Error('clone method called');
}
});
}).to.not.throw();
});
it('should not call clone methods on arrays', function() {
expect(function() {
var arr = [];
arr.clone = function() {
throw new Error('clone method called');
};
Utils.cloneDeep(arr);
}).to.not.throw();
});
});
describe('validateParameter', function() {
describe('method signature', function() {
it('throws an error if the value is not defined', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!