summaryrefslogtreecommitdiffstats
path: root/toolkit/jetpack/diffpatcher/diff.js
diff options
context:
space:
mode:
authorMatt A. Tobin <email@mattatobin.com>2018-02-09 06:46:43 -0500
committerMatt A. Tobin <email@mattatobin.com>2018-02-09 06:46:43 -0500
commitac46df8daea09899ce30dc8fd70986e258c746bf (patch)
tree2750d3125fc253fd5b0671e4bd268eff1fd97296 /toolkit/jetpack/diffpatcher/diff.js
parent8cecf8d5208f3945b35f879bba3015bb1a11bec6 (diff)
downloadUXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.gz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.lz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.xz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.zip
Move Add-on SDK source to toolkit/jetpack
Diffstat (limited to 'toolkit/jetpack/diffpatcher/diff.js')
-rw-r--r--toolkit/jetpack/diffpatcher/diff.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/toolkit/jetpack/diffpatcher/diff.js b/toolkit/jetpack/diffpatcher/diff.js
new file mode 100644
index 000000000..967c137e6
--- /dev/null
+++ b/toolkit/jetpack/diffpatcher/diff.js
@@ -0,0 +1,45 @@
+"use strict";
+
+var method = require("../method/core")
+
+// Method is designed to work with data structures representing application
+// state. Calling it with a state should return object representing `delta`
+// that has being applied to a previous state to get to a current state.
+//
+// Example
+//
+// diff(state) // => { "item-id-1": { title: "some title" } "item-id-2": null }
+var diff = method("diff@diffpatcher")
+
+// diff between `null` / `undefined` to any hash is a hash itself.
+diff.define(null, function(from, to) { return to })
+diff.define(undefined, function(from, to) { return to })
+diff.define(Object, function(from, to) {
+ return calculate(from, to || {}) || {}
+})
+
+function calculate(from, to) {
+ var diff = {}
+ var changes = 0
+ Object.keys(from).forEach(function(key) {
+ changes = changes + 1
+ if (!(key in to) && from[key] != null) diff[key] = null
+ else changes = changes - 1
+ })
+ Object.keys(to).forEach(function(key) {
+ changes = changes + 1
+ var previous = from[key]
+ var current = to[key]
+ if (previous === current) return (changes = changes - 1)
+ if (typeof(current) !== "object") return diff[key] = current
+ if (typeof(previous) !== "object") return diff[key] = current
+ var delta = calculate(previous, current)
+ if (delta) diff[key] = delta
+ else changes = changes - 1
+ })
+ return changes ? diff : null
+}
+
+diff.calculate = calculate
+
+module.exports = diff