Skip to content
This repository was archived by the owner on Mar 6, 2018. It is now read-only.

Commit fe8bbca

Browse files
committed
ffff
1 parent 2123c7c commit fe8bbca

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ module.exports.unpack = require('./lib/unpack')
3131

3232
// Utilities
3333
module.exports.concat = require('./lib/concat')
34+
module.exports.rebase = require('./lib/rebase')

lib/rebase.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,31 @@ var decode = ooPointer.decode
88
function createContext (op) {
99
var context = undefined
1010
var path = decode(op.path)
11-
return {} // fix edge cases!!
12-
// console.log(path)
11+
return Object.create(null) // fix edge cases!!
1312
}
1413

15-
function rebase(patch) {
14+
module.exports = function rebase(patch) {
1615
if (patch.length < 2) return patch
1716

18-
var ctx = createContext(patch[0])
17+
var changes = Object.create(null)
18+
// var ctx = createContext(patch[0])
1919

2020
for (var i = 0, l = patch.length; i < l; i++) {
21+
var op = patch[i]
22+
if (op.op === 'remove') {
23+
for (var p in changes) {
24+
if (p.indexOf(op.path) === 0) {
25+
delete changes[p]
26+
}
27+
}
28+
} else {
29+
changes[op.path] = op
30+
}
31+
}
2132

22-
rebase[patch[i].path] = [patch[i].op, patch[i].value]
33+
var rebased = []
34+
for (var k in changes) {
35+
rebased.push(changes[k])
2336
}
37+
return rebased
2438
}
25-
26-
var patch = [
27-
{op: "add", value: "bar", path: "/foo/machin"},
28-
{op: "remove", path: "/foo"}
29-
]
30-
31-
console.log(rebase(patch))
32-

test/rebase.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
import assert from 'assert'
4+
import rebase from '../lib/rebase'
5+
// need a json8/assert module
6+
const tests = require('./rebase_tests.json')
7+
8+
describe('rebase', () => {
9+
tests.forEach((test) => {
10+
describe(test.description, () => {
11+
it('passes', () => { // BDD lulz
12+
assert.deepEqual(rebase(test.patch), test.expected)
13+
})
14+
})
15+
})
16+
})

test/rebase_tests.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[
2+
{
3+
"description": "empty patch",
4+
"patch": [],
5+
"expected": []
6+
},
7+
{
8+
"description": "add then remove at same path",
9+
"patch": [
10+
{"path": "/foo", "op": "add", "value": "bar"},
11+
{"path": "/foo", "op": "remove"}
12+
],
13+
"expected": []
14+
},
15+
{
16+
"description": "add child value then remove parent",
17+
"patch": [
18+
{"path": "/foo/bar", "op": "add", "value": "bar"},
19+
{"path": "/foo", "op": "remove"}
20+
],
21+
"expected": []
22+
}
23+
]

0 commit comments

Comments
 (0)