Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

xdiff instead of adiff

  • Loading branch information...
commit a9c548924362b50fd1a5b12c07dcf6398e9839a5 1 parent e2f4daa
Dominic Tarr authored April 20, 2012

Showing 1 changed file with 0 additions and 79 deletions. Show diff stats Hide diff stats

  1. 79  xdiff.js
79  xdiff.js
... ...
@@ -1,79 +0,0 @@
1  
-var a = require('adiff')
2  
-
3  
-function map(obj, itr) {
4  
-  var r = {}
5  
-  for (var i in obj)
6  
-    r[i] = itr(obj[i], i, obj)
7  
-  return r
8  
-}
9  
-/*
10  
-  to expand this into a real "database",
11  
-  also need {} objects.
12  
-
13  
-  there are alot more possibilities when those get added to the mix.
14  
-
15  
-  consider this:
16  
-  var older = 
17  
-    { a: {},
18  
-    , b: { hello: 'birthday message' } }
19  
-  var newer = 
20  
-    { a: { hello: 'birthday message' },
21  
-    , b: {} }
22  
-
23  
-  what has happened here?
24  
-  have we set a.hello = 'birthday message'
25  
-  and b = {}
26  
-  or have we set a = b; b = {}
27  
-
28  
-  detecting a move is computationally expensive, and ambigious.
29  
-
30  
-  thinking about this, i've decided that a reasonable compromise is to only
31  
-  handle moves when the object in question has an id (of some kind), and otherwise,
32  
-  to assume each change to an object is a set.
33  
-
34  
-*/
35  
-exports = module.exports = function (deps, exports) { 
36  
-  exports = exports || {}
37  
-  var a = require('adiff')(deps)
38  
-  console.log(a)
39  
-  exports.diff =
40  
-    function (older, newer) {
41  
-      var didChange = false
42  
-      var change = map(newer, function (n, k){
43  
-        var ch = a.diff(older[k] || [], n)
44  
-        if(ch.length)
45  
-          didChange = true
46  
-        return ch
47  
-      })
48  
-      if(!didChange) return null
49  
-      return change
50  
-    }
51  
-  exports.diff3 =
52  
-    function (branches) { //mine, concestor, yours,...
53  
-      if(arguments.length > 1)
54  
-        branches = [].slice.call(arguments)
55  
-      //collect the keys
56  
-      var keys = []
57  
-      branches.forEach(function (e) {
58  
-        for (var k in e) 
59  
-          if(!~keys.indexOf(k)) keys.push(k)
60  
-      })
61  
-      var changes = {}
62  
-      keys.forEach(function (k) {
63  
-        var collect = branches.map(function (o) {
64  
-          return o[k] || []
65  
-        })
66  
-        changes[k] = a.diff3(collect)
67  
-      })
68  
-      return changes
69  
-    }
70  
-  exports.patch = 
71  
-    function (obj, changes) {
72  
-      return map(changes, function (change, key) {
73  
-        return a.patch(obj[key] || [], change)
74  
-      })
75  
-    }
76  
-  return exports
77  
-}
78  
-
79  
-exports(null, exports)

0 notes on commit a9c5489

Please sign in to comment.
Something went wrong with that request. Please try again.