UNPKG

1.15 kBtext/coffeescriptView Raw
1# this is kind of like angular.extend(), except that if an attribute
2# on newObject (or any of its children) is equivalent to the same
3# attribute on oldObject, we won't overwrite it. This is useful if
4# you are trying to keep track of deeply nested references to a
5# resource's attributes from different scopes, for example.
6module.exports = modifyObjectInPlace = (oldObject, newObject, cachedObject) ->
7 # TODO the `when` clauses below are horrible hacks that need to be fixed
8 for key in Object.keys(oldObject) when key[0] isnt '$'
9 localChange = cachedObject and not cachedObject[key]?
10 delete oldObject[key] unless newObject[key]? or localChange
11
12 for key in Object.keys(newObject) when key[0] isnt '$'
13 if angular.isObject(oldObject[key]) and angular.isObject(newObject[key])
14 modifyObjectInPlace(oldObject[key], newObject[key], cachedObject?[key])
15 else
16 localChanges = cachedObject and not angular.equals(oldObject[key], cachedObject[key])
17 unless angular.equals(oldObject[key], newObject[key]) or localChanges
18 oldObject[key] = newObject[key]
19
20 oldObject.length = newObject.length if newObject.length?
21