-
Notifications
You must be signed in to change notification settings - Fork 52
Make lib cross-compatible #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ponyloop
wants to merge
3
commits into
unclechu:master
Choose a base branch
from
ponyloop:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrap this line to |
||
|| 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; | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove empty lines here |
||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{}
and put each expression on new linewindow
as objecttypeof window === 'object'
and if we don't have throw exceptionnew Error('Unknown platform')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Good catch. Thanks.