@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/src/index.ts CHANGED
@@ -1,20 +1,8 @@
1
- export {diffPatch, diffItem} from './diffPatch.js'
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 {DiffMatchPatchOptions, DiffOptions, DocumentStub, PatchOptions} from './diffPatch.js'
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
- * @public
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
- * @public
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
- * @public
30
+ * @internal
34
31
  */
35
- export interface InsertAfterPatch {
32
+ export interface InsertPatch {
36
33
  op: 'insert'
37
- after: Path
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
- * @public
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 patch containing either a Sanity set, unset, insert or diffMatchPatch operation
52
+ * A `reorder` operation used to ...
53
+ *
54
+ * Note: NOT a serializable mutation.
56
55
  *
57
56
  * @public
58
57
  */
59
- export type Patch = SetPatch | UnsetPatch | InsertAfterPatch | DiffMatchPatch
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 SanitySetPatch {
68
- id: string
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 SanityUnsetPatch {
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 SanityInsertPatch {
90
- id: string
98
+ export interface SanityInsertPatchOperation {
91
99
  insert:
92
- | {before: string; items: any[]}
93
- | {after: string; items: any[]}
94
- | {replace: string; items: any[]}
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 SanityDiffMatchPatch {
104
- id: string
105
- diffMatchPatch: {[key: string]: string}
111
+ export interface SanityDiffMatchPatchOperation {
112
+ diffMatchPatch: Record<string, string>
106
113
  }
107
114
 
108
115
  /**
109
- * A patch containing either a set, unset, insert or diffMatchPatch operation
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 type SanityPatch =
114
- | SanitySetPatch
115
- | SanityUnsetPatch
116
- | SanityInsertPatch
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
- function isKeyedObject(obj: any): obj is KeyedSanityObject {
58
- return typeof obj === 'object' && typeof obj._key === 'string'
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
+ }