From 50778af957163b1c58eb677957d46c0c018d6e93 Mon Sep 17 00:00:00 2001 From: fn-andrw Date: Thu, 18 Aug 2016 21:56:13 +0300 Subject: [PATCH 1/2] Make lib cross-compatible --- lib/deep-extend.js | 214 ++++++++++++++++++++++++--------------------- 1 file changed, 115 insertions(+), 99 deletions(-) diff --git a/lib/deep-extend.js b/lib/deep-extend.js index 522461d..d505fd0 100644 --- a/lib/deep-extend.js +++ b/lib/deep-extend.js @@ -5,7 +5,8 @@ * * The MIT License (MIT) * - * Copyright (c) 2013-2015 Viacheslav Lotsmanov + * Copyright (c) 2013-2016 Viacheslav Lotsmanov + * Copyright (c) 2016 Andrew Fatkulin * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in @@ -25,120 +26,135 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -'use strict'; - -function isSpecificValue(val) { - return ( - val instanceof Buffer - || val instanceof Date - || val instanceof RegExp - ) ? true : false; -} - -function cloneSpecificValue(val) { - if (val instanceof Buffer) { - var x = new Buffer(val.length); - val.copy(x); - return x; - } else if (val instanceof Date) { - return new Date(val.getTime()); - } else if (val instanceof RegExp) { - return new RegExp(val); - } else { - throw new Error('Unexpected situation'); - } -} -/** - * Recursive cloning array. - */ -function deepCloneArray(arr) { - var clone = []; - arr.forEach(function (item, index) { - if (typeof item === 'object' && item !== null) { - if (Array.isArray(item)) { - clone[index] = deepCloneArray(item); - } else if (isSpecificValue(item)) { - clone[index] = cloneSpecificValue(item); - } else { - clone[index] = deepExtend({}, item); - } - } else { - clone[index] = item; - } - }); - return clone; -} -/** - * Extening object that entered in first argument. - * - * Returns extended object or false if have no target object or incorrect type. - * - * If you wish to clone source object (without modify it), just use empty new - * object as first argument, like this: - * deepExtend({}, yourObj_1, [yourObj_N]); - */ -var deepExtend = module.exports = function (/*obj_1, [obj_2], [obj_N]*/) { - if (arguments.length < 1 || typeof arguments[0] !== 'object') { - return false; - } +(function (name, definition) { - if (arguments.length < 2) { - return arguments[0]; - } + if (typeof module !== 'undefined' && module.exports) module.exports = definition(); + else if (typeof define !== 'undefined' && typeof define == 'function' && typeof define.amd == 'object') define(definition); + else window[name] = definition(); - var target = arguments[0]; +})('deepExtend', function () { - // convert arguments to array and cut off target object - var args = Array.prototype.slice.call(arguments, 1); + 'use strict'; - var val, src, clone; + function isSpecificValue(val) { + return ( + typeof Buffer !== 'undefined' && val instanceof Buffer + || val instanceof Date + || val instanceof RegExp + ) ? true : false; + } - args.forEach(function (obj) { - // skip argument if it is array or isn't object - if (typeof obj !== 'object' || Array.isArray(obj)) { - return; + function cloneSpecificValue(val) { + if (typeof Buffer !== 'undefined' && val instanceof Buffer) { + var x = new Buffer(val.length); + val.copy(x); + return x; + } else if (val instanceof Date) { + return new Date(val.getTime()); + } else if (val instanceof RegExp) { + return new RegExp(val); + } else { + throw new Error('Unexpected situation'); } + } - Object.keys(obj).forEach(function (key) { - src = target[key]; // source value - val = obj[key]; // new value + /** + * Recursive cloning array. + */ + function deepCloneArray(arr) { + var clone = []; + arr.forEach(function (item, index) { + if (typeof item === 'object' && item !== null) { + if (Array.isArray(item)) { + clone[index] = deepCloneArray(item); + } else if (isSpecificValue(item)) { + clone[index] = cloneSpecificValue(item); + } else { + clone[index] = deepExtend({}, item); + } + } else { + clone[index] = item; + } + }); + return clone; + } - // recursion prevention - if (val === target) { - return; + /** + * Extening object that entered in first argument. + * + * Returns extended object or false if have no target object or incorrect type. + * + * If you wish to clone source object (without modify it), just use empty new + * object as first argument, like this: + * deepExtend({}, yourObj_1, [yourObj_N]); + */ + var deepExtend = function (/*obj_1, [obj_2], [obj_N]*/) { + if (arguments.length < 1 || typeof arguments[0] !== 'object') { + return false; + } - /** - * if new value isn't object then just overwrite by new value - * instead of extending. - */ - } else if (typeof val !== 'object' || val === null) { - target[key] = val; - return; + if (arguments.length < 2) { + return arguments[0]; + } - // just clone arrays (and recursive clone objects inside) - } else if (Array.isArray(val)) { - target[key] = deepCloneArray(val); - return; + var target = arguments[0]; - // custom cloning and overwrite for specific objects - } else if (isSpecificValue(val)) { - target[key] = cloneSpecificValue(val); - return; + // convert arguments to array and cut off target object + var args = Array.prototype.slice.call(arguments, 1); - // overwrite by new value if source isn't object or array - } else if (typeof src !== 'object' || src === null || Array.isArray(src)) { - target[key] = deepExtend({}, val); - return; + var val, src, clone; - // source value and new value is objects both, extending... - } else { - target[key] = deepExtend(src, val); + args.forEach(function (obj) { + // skip argument if it is array or isn't object + if (typeof obj !== 'object' || Array.isArray(obj)) { return; } + + Object.keys(obj).forEach(function (key) { + src = target[key]; // source value + val = obj[key]; // new value + + // recursion prevention + if (val === target) { + return; + + /** + * if new value isn't object then just overwrite by new value + * instead of extending. + */ + } else if (typeof val !== 'object' || val === null) { + target[key] = val; + return; + + // just clone arrays (and recursive clone objects inside) + } else if (Array.isArray(val)) { + target[key] = deepCloneArray(val); + return; + + // custom cloning and overwrite for specific objects + } else if (isSpecificValue(val)) { + target[key] = cloneSpecificValue(val); + return; + + // overwrite by new value if source isn't object or array + } else if (typeof src !== 'object' || src === null || Array.isArray(src)) { + target[key] = deepExtend({}, val); + return; + + // source value and new value is objects both, extending... + } else { + target[key] = deepExtend(src, val); + return; + } + }); }); - }); - return target; -} + return target; + } + + return deepExtend; + + +}); From 0cbaf06fd6d17e813fca4eb428286034fb24095f Mon Sep 17 00:00:00 2001 From: fn-andrw Date: Fri, 19 Aug 2016 12:03:00 +0300 Subject: [PATCH 2/2] Fix code style --- lib/deep-extend.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/deep-extend.js b/lib/deep-extend.js index d505fd0..f328442 100644 --- a/lib/deep-extend.js +++ b/lib/deep-extend.js @@ -30,9 +30,13 @@ (function (name, definition) { - if (typeof module !== 'undefined' && module.exports) module.exports = definition(); - else if (typeof define !== 'undefined' && typeof define == 'function' && typeof define.amd == 'object') define(definition); - else window[name] = definition(); + if (typeof module !== 'undefined' && module.exports) { + module.exports = definition(); + } else if (typeof define !== 'undefined' && typeof define === 'function' && typeof define.amd === 'object') { + define(definition); + } else { + window[name] = definition(); + } })('deepExtend', function () { @@ -40,7 +44,7 @@ function isSpecificValue(val) { return ( - typeof Buffer !== 'undefined' && val instanceof Buffer + (typeof Buffer !== 'undefined' && val instanceof Buffer) || val instanceof Date || val instanceof RegExp ) ? true : false;