Skip to content

Instantly share code, notes, and snippets.

@UnquietCode
Last active July 25, 2017 15:05
Show Gist options
  • Save UnquietCode/f6c32488a8094174e1f6 to your computer and use it in GitHub Desktop.
Save UnquietCode/f6c32488a8094174e1f6 to your computer and use it in GitHub Desktop.

Revisions

  1. UnquietCode revised this gist Dec 2, 2014. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion JsonMerge.coffee
    Original 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
    if not (v instanceof Array) and (typeof current[k]).toLowerCase() is 'object' and (typeof v).toLowerCase() is 'object'
    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
  2. UnquietCode revised this gist Nov 24, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions JsonMerge.coffee
    Original 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...) ->

  3. UnquietCode revised this gist Nov 19, 2014. 1 changed file with 17 additions and 12 deletions.
    29 changes: 17 additions & 12 deletions JsonMerge.coffee
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,12 @@ valueOrCopy = (obj) ->
    if not obj
    return undefined

    if (typeof obj).toLowerCase() is 'object'
    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

    # add a property
    if not current[k]
    current[k] = valueOrCopy(v)
    for own k,v of next
    copy = -> current[k] = valueOrCopy(v)

    # remove a property
    else if v == null
    current[k] = undefined
    # add or remove a property
    if not current[k] or not v
    copy()

    # 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)
    # 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
    current[k] = valueOrCopy(v)
    copy()


    module.exports = (objects...) ->
  4. UnquietCode revised this gist Nov 19, 2014. 1 changed file with 7 additions and 8 deletions.
    15 changes: 7 additions & 8 deletions JsonMerge.coffee
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,14 @@

    valueOrCopy = (v) ->
    if not v
    valueOrCopy = (obj) ->
    if not obj
    return undefined

    else if (typeof v).toLowerCase() is 'object'
    v2 = {}
    v2[k] = v for own k,v of v
    return v2
    if (typeof obj).toLowerCase() is 'object'
    newObj = {}
    newObj[k] = v for own k,v of obj
    return newObj

    else
    return v
    return obj


    mergeObject = (current, next) ->
  5. UnquietCode revised this gist Nov 19, 2014. 1 changed file with 31 additions and 17 deletions.
    48 changes: 31 additions & 17 deletions JsonMerge.coffee
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,46 @@
    module.exports = (objects...) ->

    mergeObject = (current, next) ->
    for own k,v of next
    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

    # add a property
    if not current[k]
    current[k] = v

    # remove a property
    else if v == null
    current[k] = undefined
    mergeObject = (current, next) ->
    for own k,v of next

    # change a property
    else if current[k] != v
    # add a property
    if not current[k]
    current[k] = valueOrCopy(v)

    # recursive copy
    if (typeof current[k]).toLowerCase() == 'object' and (typeof v).toLowerCase() == 'object'
    mergeObject(current[k], v)
    # remove a property
    else if v == null
    current[k] = undefined

    # plain copy
    else
    current[k] = v
    # 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)

  6. UnquietCode created this gist Nov 18, 2014.
    34 changes: 34 additions & 0 deletions JsonMerge.coffee
    Original 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