Last active
July 25, 2017 15:05
-
-
Save UnquietCode/f6c32488a8094174e1f6 to your computer and use it in GitHub Desktop.
Revisions
-
UnquietCode revised this gist
Dec 2, 2014 . 1 changed file with 12 additions and 1 deletion.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 charactersOriginal file line number Diff line number Diff line change @@ -28,8 +28,19 @@ mergeObject = (current, next) -> # change a property else if current[k] != v # array modifications if current[k] instanceof Array and v instanceof Array # check for special array append syntax if v.length == 1 and v[0] instanceof Array and v[0].length == 1 and v[0][0] instanceof Array current[k].push(x) for x in v[0][0] # plain old replace else copy() # recursive object copy else if not (v instanceof Array) and (typeof current[k]).toLowerCase() is 'object' and (typeof v).toLowerCase() is 'object' clone = valueOrCopy(current[k]) mergeObject(clone, v) current[k] = clone -
UnquietCode revised this gist
Nov 24, 2014 . 1 changed file with 3 additions and 0 deletions.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 charactersOriginal file line number Diff line number Diff line change @@ -38,6 +38,9 @@ mergeObject = (current, next) -> else copy() # if empty, then remove the key entirely if not current[k] then delete current[k] module.exports = (objects...) -> -
UnquietCode revised this gist
Nov 19, 2014 . 1 changed file with 17 additions and 12 deletions.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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,12 @@ valueOrCopy = (obj) -> if not obj return undefined else if obj instanceof Array newObj = [] newObj.push(x) for x in obj return newObj else if (typeof obj).toLowerCase() is 'object' newObj = {} newObj[k] = v for own k,v of obj return newObj @@ -12,26 +17,26 @@ valueOrCopy = (obj) -> mergeObject = (current, next) -> for own k,v of next copy = -> current[k] = valueOrCopy(v) # add or remove a property if not current[k] or not v copy() # change a property else if current[k] != v # recursive object copy if not (v instanceof Array) and (typeof current[k]).toLowerCase() is 'object' and (typeof v).toLowerCase() is 'object' clone = valueOrCopy(current[k]) mergeObject(clone, v) current[k] = clone # plain copy else copy() module.exports = (objects...) -> -
UnquietCode revised this gist
Nov 19, 2014 . 1 changed file with 7 additions and 8 deletions.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 charactersOriginal file line number Diff line number Diff line change @@ -1,15 +1,14 @@ valueOrCopy = (obj) -> if not obj return undefined if (typeof obj).toLowerCase() is 'object' newObj = {} newObj[k] = v for own k,v of obj return newObj else return obj mergeObject = (current, next) -> -
UnquietCode revised this gist
Nov 19, 2014 . 1 changed file with 31 additions and 17 deletions.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 charactersOriginal file line number Diff line number Diff line change @@ -1,32 +1,46 @@ valueOrCopy = (v) -> if not v return undefined else if (typeof v).toLowerCase() is 'object' v2 = {} v2[k] = v for own k,v of v return v2 else return v mergeObject = (current, next) -> for own k,v of next # add a property if not current[k] current[k] = valueOrCopy(v) # remove a property else if v == null current[k] = undefined # change a property else if current[k] != v # recursive copy if (typeof current[k]).toLowerCase() is 'object' and (typeof v).toLowerCase() is 'object' mergeObject(current[k], v) # plain copy else current[k] = valueOrCopy(v) module.exports = (objects...) -> # start a root root = {} # perform the merge for obj in objects mergeObject(root, obj) -
UnquietCode created this gist
Nov 18, 2014 .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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ module.exports = (objects...) -> mergeObject = (current, next) -> for own k,v of next # add a property if not current[k] current[k] = v # remove a property else if v == null current[k] = undefined # change a property else if current[k] != v # recursive copy if (typeof current[k]).toLowerCase() == 'object' and (typeof v).toLowerCase() == 'object' mergeObject(current[k], v) # plain copy else current[k] = v # start a root root = {} for obj in objects mergeObject(root, obj) root.toJson = -> JSON.stringify(this, null, 2) return root