@sanity/diff-patch 5.0.0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +248 -95
- package/dist/index.cjs +180 -160
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +47 -151
- package/dist/index.d.ts +47 -151
- package/dist/index.js +182 -162
- package/dist/index.js.map +1 -1
- package/package.json +5 -1
- package/src/diffPatch.ts +458 -357
- package/src/index.ts +3 -15
- package/src/patches.ts +53 -32
- package/src/paths.ts +12 -4
- package/src/setOperations.ts +29 -0
package/src/index.ts
CHANGED
|
@@ -1,20 +1,8 @@
|
|
|
1
|
-
export {diffPatch,
|
|
1
|
+
export {diffPatch, diffValue} from './diffPatch.js'
|
|
2
2
|
export {DiffError} from './diffError.js'
|
|
3
3
|
|
|
4
|
-
export type {
|
|
5
|
-
DiffMatchPatch,
|
|
6
|
-
InsertAfterPatch,
|
|
7
|
-
Patch,
|
|
8
|
-
SanityDiffMatchPatch,
|
|
9
|
-
SanityInsertPatch,
|
|
10
|
-
SanityPatch,
|
|
11
|
-
SanityPatchMutation,
|
|
12
|
-
SanitySetPatch,
|
|
13
|
-
SanityUnsetPatch,
|
|
14
|
-
SetPatch,
|
|
15
|
-
UnsetPatch,
|
|
16
|
-
} from './patches.js'
|
|
4
|
+
export type {SanityPatch, SanityPatchMutation, SanityPatchOperations} from './patches.js'
|
|
17
5
|
|
|
18
|
-
export type {
|
|
6
|
+
export type {DocumentStub, PatchOptions} from './diffPatch.js'
|
|
19
7
|
|
|
20
8
|
export type {Path, PathSegment} from './paths.js'
|
package/src/patches.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import type {Path} from './paths.js'
|
|
1
|
+
import type {KeyedSanityObject, Path} from './paths.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A `set` operation
|
|
5
5
|
* Replaces the current path, does not merge
|
|
6
|
-
* Note: NOT a serializable mutation, see {@link SanitySetPatch} for that
|
|
7
6
|
*
|
|
8
|
-
* @
|
|
7
|
+
* @internal
|
|
9
8
|
*/
|
|
10
9
|
export interface SetPatch {
|
|
11
10
|
op: 'set'
|
|
@@ -16,9 +15,8 @@ export interface SetPatch {
|
|
|
16
15
|
/**
|
|
17
16
|
* A `unset` operation
|
|
18
17
|
* Unsets the entire value of the given path
|
|
19
|
-
* Note: NOT a serializable mutation, see {@link SanityUnsetPatch} for that
|
|
20
18
|
*
|
|
21
|
-
* @
|
|
19
|
+
* @internal
|
|
22
20
|
*/
|
|
23
21
|
export interface UnsetPatch {
|
|
24
22
|
op: 'unset'
|
|
@@ -28,22 +26,21 @@ export interface UnsetPatch {
|
|
|
28
26
|
/**
|
|
29
27
|
* A `insert` operation
|
|
30
28
|
* Inserts the given items _after_ the given path
|
|
31
|
-
* Note: NOT a serializable mutation, see {@link SanityInsertPatch} for that
|
|
32
29
|
*
|
|
33
|
-
* @
|
|
30
|
+
* @internal
|
|
34
31
|
*/
|
|
35
|
-
export interface
|
|
32
|
+
export interface InsertPatch {
|
|
36
33
|
op: 'insert'
|
|
37
|
-
|
|
34
|
+
position: 'before' | 'after' | 'replace'
|
|
35
|
+
path: Path
|
|
38
36
|
items: any[]
|
|
39
37
|
}
|
|
40
38
|
|
|
41
39
|
/**
|
|
42
40
|
* A `diffMatchPatch` operation
|
|
43
41
|
* Applies the given `value` (unidiff format) to the given path. Must be a string.
|
|
44
|
-
* Note: NOT a serializable mutation, see {@link SanityDiffMatchPatch} for that
|
|
45
42
|
*
|
|
46
|
-
* @
|
|
43
|
+
* @internal
|
|
47
44
|
*/
|
|
48
45
|
export interface DiffMatchPatch {
|
|
49
46
|
op: 'diffMatchPatch'
|
|
@@ -52,11 +49,25 @@ export interface DiffMatchPatch {
|
|
|
52
49
|
}
|
|
53
50
|
|
|
54
51
|
/**
|
|
55
|
-
* A
|
|
52
|
+
* A `reorder` operation used to ...
|
|
53
|
+
*
|
|
54
|
+
* Note: NOT a serializable mutation.
|
|
56
55
|
*
|
|
57
56
|
* @public
|
|
58
57
|
*/
|
|
59
|
-
export
|
|
58
|
+
export interface ReorderPatch {
|
|
59
|
+
op: 'reorder'
|
|
60
|
+
path: Path
|
|
61
|
+
snapshot: KeyedSanityObject[]
|
|
62
|
+
reorders: {sourceKey: string; targetKey: string}[]
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Internal patch representation used during diff generation
|
|
67
|
+
*
|
|
68
|
+
* @internal
|
|
69
|
+
*/
|
|
70
|
+
export type Patch = SetPatch | UnsetPatch | InsertPatch | DiffMatchPatch | ReorderPatch
|
|
60
71
|
|
|
61
72
|
/**
|
|
62
73
|
* A Sanity `set` patch mutation operation
|
|
@@ -64,9 +75,8 @@ export type Patch = SetPatch | UnsetPatch | InsertAfterPatch | DiffMatchPatch
|
|
|
64
75
|
*
|
|
65
76
|
* @public
|
|
66
77
|
*/
|
|
67
|
-
export interface
|
|
68
|
-
|
|
69
|
-
set: {[key: string]: any}
|
|
78
|
+
export interface SanitySetPatchOperation {
|
|
79
|
+
set: Record<string, unknown>
|
|
70
80
|
}
|
|
71
81
|
|
|
72
82
|
/**
|
|
@@ -75,8 +85,7 @@ export interface SanitySetPatch {
|
|
|
75
85
|
*
|
|
76
86
|
* @public
|
|
77
87
|
*/
|
|
78
|
-
export interface
|
|
79
|
-
id: string
|
|
88
|
+
export interface SanityUnsetPatchOperation {
|
|
80
89
|
unset: string[]
|
|
81
90
|
}
|
|
82
91
|
|
|
@@ -86,12 +95,11 @@ export interface SanityUnsetPatch {
|
|
|
86
95
|
*
|
|
87
96
|
* @public
|
|
88
97
|
*/
|
|
89
|
-
export interface
|
|
90
|
-
id: string
|
|
98
|
+
export interface SanityInsertPatchOperation {
|
|
91
99
|
insert:
|
|
92
|
-
| {before: string; items:
|
|
93
|
-
| {after: string; items:
|
|
94
|
-
| {replace: string; items:
|
|
100
|
+
| {before: string; items: unknown[]}
|
|
101
|
+
| {after: string; items: unknown[]}
|
|
102
|
+
| {replace: string; items: unknown[]}
|
|
95
103
|
}
|
|
96
104
|
|
|
97
105
|
/**
|
|
@@ -100,21 +108,34 @@ export interface SanityInsertPatch {
|
|
|
100
108
|
*
|
|
101
109
|
* @public
|
|
102
110
|
*/
|
|
103
|
-
export interface
|
|
104
|
-
|
|
105
|
-
diffMatchPatch: {[key: string]: string}
|
|
111
|
+
export interface SanityDiffMatchPatchOperation {
|
|
112
|
+
diffMatchPatch: Record<string, string>
|
|
106
113
|
}
|
|
107
114
|
|
|
108
115
|
/**
|
|
109
|
-
*
|
|
116
|
+
* Serializable patch operations that can be applied to a Sanity document.
|
|
117
|
+
*
|
|
118
|
+
* @public
|
|
119
|
+
*/
|
|
120
|
+
export type SanityPatchOperations = Partial<
|
|
121
|
+
SanitySetPatchOperation &
|
|
122
|
+
SanityUnsetPatchOperation &
|
|
123
|
+
SanityInsertPatchOperation &
|
|
124
|
+
SanityDiffMatchPatchOperation
|
|
125
|
+
>
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Meant to be used as the body of a {@link SanityPatchMutation}'s `patch` key.
|
|
129
|
+
*
|
|
130
|
+
* Contains additional properties to target a particular ID and optionally add
|
|
131
|
+
* an optimistic lock via [`ifRevisionID`](https://www.sanity.io/docs/content-lake/transactions#k29b2c75639d5).
|
|
110
132
|
*
|
|
111
133
|
* @public
|
|
112
134
|
*/
|
|
113
|
-
export
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
| SanityDiffMatchPatch
|
|
135
|
+
export interface SanityPatch extends SanityPatchOperations {
|
|
136
|
+
id: string
|
|
137
|
+
ifRevisionID?: string
|
|
138
|
+
}
|
|
118
139
|
|
|
119
140
|
/**
|
|
120
141
|
* A mutation containing a single patch
|
package/src/paths.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import type {KeyedSanityObject} from './diffPatch.js'
|
|
2
|
-
|
|
3
1
|
const IS_DOTTABLE_RE = /^[A-Za-z_][A-Za-z0-9_]*$/
|
|
4
2
|
|
|
5
3
|
/**
|
|
@@ -54,6 +52,16 @@ export function pathToString(path: Path): string {
|
|
|
54
52
|
}, '')
|
|
55
53
|
}
|
|
56
54
|
|
|
57
|
-
|
|
58
|
-
|
|
55
|
+
/**
|
|
56
|
+
* An object (record) that has a `_key` property
|
|
57
|
+
*
|
|
58
|
+
* @internal
|
|
59
|
+
*/
|
|
60
|
+
export interface KeyedSanityObject {
|
|
61
|
+
[key: string]: unknown
|
|
62
|
+
_key: string
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function isKeyedObject(obj: unknown): obj is KeyedSanityObject {
|
|
66
|
+
return typeof obj === 'object' && !!obj && '_key' in obj && typeof obj._key === 'string'
|
|
59
67
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/// <reference lib="esnext" />
|
|
2
|
+
|
|
3
|
+
export function difference<T>(source: Set<T>, target: Set<T>): Set<T> {
|
|
4
|
+
if ('difference' in Set.prototype) {
|
|
5
|
+
return source.difference(target)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const result = new Set<T>()
|
|
9
|
+
for (const item of source) {
|
|
10
|
+
if (!target.has(item)) {
|
|
11
|
+
result.add(item)
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return result
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function intersection<T>(source: Set<T>, target: Set<T>): Set<T> {
|
|
18
|
+
if ('intersection' in Set.prototype) {
|
|
19
|
+
return source.intersection(target)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const result = new Set<T>()
|
|
23
|
+
for (const item of source) {
|
|
24
|
+
if (target.has(item)) {
|
|
25
|
+
result.add(item)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return result
|
|
29
|
+
}
|