-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from streamich/flat
Flat
- Loading branch information
Showing
8 changed files
with
204 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import tests_json from '../../__tests__/tests.json'; | ||
import spec_json from '../../__tests__/spec.json'; | ||
import { Operation } from '../../types'; | ||
import {applyPatch as v1} from '../v1'; | ||
import {applyPatch as v2} from '../v2'; | ||
import {applyPatch as v3} from '../v3'; | ||
import {applyPatch as v4} from '../v4'; | ||
import {validateOperation} from '../../validate'; | ||
import { deepClone } from '../../../../es6/json-patch/util'; | ||
|
||
const testSuites = [ | ||
{ | ||
name: 'tests.json', | ||
tests: tests_json, | ||
}, | ||
{ | ||
name: 'spec.json', | ||
tests: spec_json, | ||
}, | ||
]; | ||
|
||
const versions = {v1, v3, v4}; | ||
// const versions = {v3}; | ||
|
||
for (const name in versions) { | ||
const applyPatch = (versions as any)[name]; | ||
describe(`applyPatch ${name}`, () => { | ||
testSuites.forEach((s) => { | ||
const suite = deepClone(s) as any; | ||
describe(suite.name, () => { | ||
suite.tests.forEach((test: any) => { | ||
if (test.disabled) return; | ||
const testName = test.comment || test.error || JSON.stringify(test.patch); | ||
if (test.expected) { | ||
it('should succeed: ' + testName, () => { | ||
test.patch.forEach(validateOperation); | ||
const {doc} = applyPatch(test.doc, test.patch, true); | ||
expect(doc).toEqual(test.expected); | ||
}); | ||
} else if (test.error || test.patch[0].op === 'test') { | ||
it('should throw an error: ' + testName, () => { | ||
expect(() => { | ||
test.patch.forEach(validateOperation); | ||
applyPatch(test.doc, test.patch, true); | ||
}).toThrow(); | ||
}); | ||
} else throw new Error('invalid test case'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from './v1'; | ||
export * from './v4'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import {deepClone} from '../util'; | ||
import {Operation} from '../types'; | ||
import {Op, operationToOp} from '../op'; | ||
|
||
export interface OpResult { | ||
doc: unknown; | ||
old?: unknown; | ||
} | ||
|
||
export interface PatchResult { | ||
doc: unknown; | ||
res: readonly OpResult[]; | ||
} | ||
|
||
export function applyOp(doc: unknown, op: Op, mutate: boolean): OpResult { | ||
if (!mutate) doc = deepClone(doc); | ||
return op.apply(doc); | ||
} | ||
|
||
export function applyOps(doc: unknown, ops: readonly Op[], mutate: boolean): PatchResult { | ||
if (!mutate) doc = deepClone(doc); | ||
const res: OpResult[] = []; | ||
const length = ops.length; | ||
for (let i = 0; i < length; i++) { | ||
const opResult = ops[i].apply(doc); | ||
doc = opResult.doc; | ||
res.push(opResult); | ||
} | ||
return {doc, res}; | ||
} | ||
|
||
export function applyPatch(doc: unknown, patch: readonly Operation[], mutate: boolean): PatchResult { | ||
if (!mutate) doc = deepClone(doc); | ||
const res: OpResult[] = []; | ||
const length = patch.length; | ||
for (let i = 0; i < length; i++) { | ||
const op = operationToOp(patch[i]); | ||
const opResult = op.apply(doc); | ||
doc = opResult.doc; | ||
res.push(opResult); | ||
} | ||
return {doc, res}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters