|
| 1 | + |
| 2 | +# Difference between JSON objects. |
| 3 | +# |
| 4 | +# @param [Object] a |
| 5 | +# @param [Object] b |
| 6 | +# @param [Array] stack Optional scope. |
| 7 | +# @param [Boolean] rename Internal |
| 8 | +# @param [Object] garbage Internal |
| 9 | +# |
| 10 | +# @return [Object] Difference between b - a JSON objects. |
| 11 | +# |
| 12 | +rusDiff = (a, b, stack = [], rename = true, garbage = {}) -> |
| 13 | + |
| 14 | + # Sorted lists of keys. |
| 15 | + aKeys = Object.keys(a).sort() |
| 16 | + bKeys = Object.keys(b).sort() |
| 17 | + |
| 18 | + # Number of keys. |
| 19 | + aN = aKeys.length |
| 20 | + bN = bKeys.length |
| 21 | + |
| 22 | + # Key indices. |
| 23 | + aI = 0 |
| 24 | + bI = 0 |
| 25 | + |
| 26 | + # Result. |
| 27 | + rus = |
| 28 | + $rename: {} |
| 29 | + $unset: {} |
| 30 | + $set: {} |
| 31 | + |
| 32 | + # Unset |
| 33 | + unsetA = (i) -> |
| 34 | + key = (stack.concat aKeys[i]).join('.') |
| 35 | + rus.$unset[key] = true |
| 36 | + (garbage[ a[aKeys[i]] ] ||= []).push key |
| 37 | + |
| 38 | + setB = (i) -> |
| 39 | + key = (stack.concat bKeys[i]).join('.') |
| 40 | + rus.$set[key] = b[bKeys[i]] |
| 41 | + |
| 42 | + while (aI < aN) and (bI < bN) |
| 43 | + aKey = aKeys[aI] |
| 44 | + bKey = bKeys[bI] |
| 45 | + |
| 46 | + if aKey is bKey |
| 47 | + aVal = a[aKey] |
| 48 | + bVal = b[bKey] |
| 49 | + if aVal isnt bVal |
| 50 | + if (typeof aVal is 'object') and (typeof bVal is 'object') |
| 51 | + for k, v of rusDiff(aVal, bVal, stack.concat([aKey]), false, garbage) |
| 52 | + |
| 53 | + # Merge changes |
| 54 | + for k2, v2 of v |
| 55 | + rus[k][k2] = v2 |
| 56 | + else |
| 57 | + |
| 58 | + # At least one is not an Object (hash), b overwrites a. |
| 59 | + # |
| 60 | + # NOTE: aVal doesn't go to garbage (as a potential rename) because MongoDB 2.4.x doesn't allow $set |
| 61 | + # and $rename for the same key paths giving MongoDB error 10150: "exception: Field name duplication |
| 62 | + # not allowed with modifiers" |
| 63 | + setB bI |
| 64 | + ++aI |
| 65 | + ++bI |
| 66 | + else |
| 67 | + if aKey < bKey |
| 68 | + unsetA aI |
| 69 | + ++aI |
| 70 | + else |
| 71 | + setB bI |
| 72 | + ++bI |
| 73 | + |
| 74 | + # Finish remaining a keys if any left. |
| 75 | + while aI < aN |
| 76 | + unsetA aI++ |
| 77 | + |
| 78 | + # Finish remaining b keys if any left. |
| 79 | + while bI < bN |
| 80 | + setB bI++ |
| 81 | + |
| 82 | + if rename |
| 83 | + |
| 84 | + # Diff has been completed, root invocation wants to do the rename, collect from garbage |
| 85 | + # whatever we can. |
| 86 | + collect = ([k, key] for k, v of rus.$set when garbage[v]? and (key = garbage[v].pop())) |
| 87 | + for e in collect |
| 88 | + [k, key] = e |
| 89 | + rus.$rename[key] = k |
| 90 | + delete rus.$unset[key] |
| 91 | + delete rus.$set[k] |
| 92 | + |
| 93 | + # Return non-empty modifications only. |
| 94 | + for k of rus |
| 95 | + if Object.keys(rus[k]).length is 0 |
| 96 | + delete rus[k] |
| 97 | + |
| 98 | + rus |
| 99 | + |
| 100 | +if module? and module.exports? |
| 101 | + module.exports.rusDiff = rusDiff |
0 commit comments