1 | export class Patch {
|
2 | }
|
3 | Patch.toPatchSet = (forward, backward) => {
|
4 | return {
|
5 | prev: backward ? toPatches(backward) : [],
|
6 | next: forward ? toPatches(forward) : [],
|
7 | };
|
8 | };
|
9 | Patch.isEmpty = (input) => {
|
10 | if (input === null || typeof input !== 'object') {
|
11 | return true;
|
12 | }
|
13 | else {
|
14 | const isEmptyArray = (input) => (Array.isArray(input) ? input.length === 0 : true);
|
15 | return isEmptyArray(input.prev) && isEmptyArray(input.next);
|
16 | }
|
17 | };
|
18 | const toPatch = (input) => {
|
19 | const hasForwardSlash = input.path.some((part) => {
|
20 | return typeof part === 'string' ? part.includes('/') : false;
|
21 | });
|
22 | if (hasForwardSlash) {
|
23 | const path = input.path
|
24 | .map((part) => (typeof part === 'string' ? `'${part}'` : part))
|
25 | .join(',');
|
26 | const err = `Property names cannot contain the "/" character. op: '${input.op}' path: [${path}]`;
|
27 | throw new Error(err);
|
28 | }
|
29 | return Object.assign(Object.assign({}, input), { path: input.path.join('/') });
|
30 | };
|
31 | const toPatches = (input) => {
|
32 | const patches = Array.isArray(input) ? input : [input];
|
33 | return patches.filter((p) => Boolean(p)).map((p) => toPatch(p));
|
34 | };
|