addon-sdk/source/lib/diffpatcher/diff.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 "use strict";
     3 var method = require("../method/core")
     5 // Method is designed to work with data structures representing application
     6 // state. Calling it with a state should return object representing `delta`
     7 // that has being applied to a previous state to get to a current state.
     8 //
     9 // Example
    10 //
    11 // diff(state) // => { "item-id-1": { title: "some title" } "item-id-2": null }
    12 var diff = method("diff@diffpatcher")
    14 // diff between `null` / `undefined` to any hash is a hash itself.
    15 diff.define(null, function(from, to) { return to })
    16 diff.define(undefined, function(from, to) { return to })
    17 diff.define(Object, function(from, to) {
    18   return calculate(from, to || {}) || {}
    19 })
    21 function calculate(from, to) {
    22   var diff = {}
    23   var changes = 0
    24   Object.keys(from).forEach(function(key) {
    25     changes = changes + 1
    26     if (!(key in to) && from[key] != null) diff[key] = null
    27     else changes = changes - 1
    28   })
    29   Object.keys(to).forEach(function(key) {
    30     changes = changes + 1
    31     var previous = from[key]
    32     var current = to[key]
    33     if (previous === current) return (changes = changes - 1)
    34     if (typeof(current) !== "object") return diff[key] = current
    35     if (typeof(previous) !== "object") return diff[key] = current
    36     var delta = calculate(previous, current)
    37     if (delta) diff[key] = delta
    38     else changes = changes - 1
    39   })
    40   return changes ? diff : null
    41 }
    43 diff.calculate = calculate
    45 module.exports = diff

mercurial