@scalar/json-magic 0.8.2 → 0.8.3
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/CHANGELOG.md +13 -0
- package/dist/bundle/index.d.ts +1 -0
- package/dist/bundle/index.d.ts.map +1 -1
- package/dist/bundle/index.js.map +1 -1
- package/dist/bundle/plugins/browser.js.map +1 -1
- package/dist/bundle/plugins/node.d.ts +1 -1
- package/dist/bundle/plugins/node.js +1 -1
- package/dist/bundle/plugins/node.js.map +1 -1
- package/dist/dereference/index.d.ts.map +1 -1
- package/dist/dereference/index.js.map +2 -2
- package/dist/diff/index.d.ts +1 -1
- package/dist/diff/index.d.ts.map +1 -1
- package/dist/diff/index.js +1 -1
- package/dist/diff/index.js.map +2 -2
- package/dist/helpers/escape-json-pointer.d.ts +1 -1
- package/dist/helpers/escape-json-pointer.js.map +1 -1
- package/dist/magic-proxy/index.d.ts.map +1 -1
- package/dist/magic-proxy/index.js.map +2 -2
- package/dist/magic-proxy/proxy.d.ts +0 -1
- package/dist/magic-proxy/proxy.d.ts.map +1 -1
- package/dist/magic-proxy/proxy.js +1 -2
- package/dist/magic-proxy/proxy.js.map +2 -2
- package/package.json +12 -13
- package/.turbo/turbo-build.log +0 -10
- package/esbuild.ts +0 -15
- package/src/bundle/bundle.test.ts +0 -2917
- package/src/bundle/bundle.ts +0 -916
- package/src/bundle/create-limiter.test.ts +0 -28
- package/src/bundle/create-limiter.ts +0 -52
- package/src/bundle/index.ts +0 -3
- package/src/bundle/plugins/browser.ts +0 -4
- package/src/bundle/plugins/fetch-urls/index.test.ts +0 -141
- package/src/bundle/plugins/fetch-urls/index.ts +0 -105
- package/src/bundle/plugins/node.ts +0 -5
- package/src/bundle/plugins/parse-json/index.test.ts +0 -24
- package/src/bundle/plugins/parse-json/index.ts +0 -32
- package/src/bundle/plugins/parse-yaml/index.test.ts +0 -26
- package/src/bundle/plugins/parse-yaml/index.ts +0 -34
- package/src/bundle/plugins/read-files/index.test.ts +0 -36
- package/src/bundle/plugins/read-files/index.ts +0 -58
- package/src/bundle/value-generator.test.ts +0 -165
- package/src/bundle/value-generator.ts +0 -143
- package/src/dereference/dereference.test.ts +0 -142
- package/src/dereference/dereference.ts +0 -84
- package/src/dereference/index.ts +0 -2
- package/src/diff/apply.test.ts +0 -262
- package/src/diff/apply.ts +0 -83
- package/src/diff/diff.test.ts +0 -328
- package/src/diff/diff.ts +0 -93
- package/src/diff/index.test.ts +0 -150
- package/src/diff/index.ts +0 -5
- package/src/diff/merge.test.ts +0 -1109
- package/src/diff/merge.ts +0 -136
- package/src/diff/trie.test.ts +0 -30
- package/src/diff/trie.ts +0 -113
- package/src/diff/utils.test.ts +0 -169
- package/src/diff/utils.ts +0 -111
- package/src/helpers/convert-to-local-ref.test.ts +0 -211
- package/src/helpers/convert-to-local-ref.ts +0 -43
- package/src/helpers/escape-json-pointer.test.ts +0 -13
- package/src/helpers/escape-json-pointer.ts +0 -8
- package/src/helpers/get-schemas.test.ts +0 -356
- package/src/helpers/get-schemas.ts +0 -80
- package/src/helpers/get-segments-from-path.test.ts +0 -17
- package/src/helpers/get-segments-from-path.ts +0 -17
- package/src/helpers/get-value-by-path.test.ts +0 -338
- package/src/helpers/get-value-by-path.ts +0 -44
- package/src/helpers/is-json-object.ts +0 -31
- package/src/helpers/is-object.test.ts +0 -27
- package/src/helpers/is-object.ts +0 -4
- package/src/helpers/is-yaml.ts +0 -18
- package/src/helpers/json-path-utils.test.ts +0 -57
- package/src/helpers/json-path-utils.ts +0 -50
- package/src/helpers/normalize.test.ts +0 -92
- package/src/helpers/normalize.ts +0 -35
- package/src/helpers/unescape-json-pointer.test.ts +0 -23
- package/src/helpers/unescape-json-pointer.ts +0 -9
- package/src/magic-proxy/index.ts +0 -2
- package/src/magic-proxy/proxy.test.ts +0 -1987
- package/src/magic-proxy/proxy.ts +0 -323
- package/src/types.ts +0 -1
- package/tsconfig.build.json +0 -12
- package/tsconfig.json +0 -16
- package/vite.config.ts +0 -8
package/src/diff/diff.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents the possible types of changes that can be made to a document.
|
|
3
|
-
* - 'add': A new property is added
|
|
4
|
-
* - 'update': An existing property's value is changed
|
|
5
|
-
* - 'delete': A property is removed
|
|
6
|
-
*/
|
|
7
|
-
type ChangeType = 'add' | 'update' | 'delete'
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Represents a single difference between two documents.
|
|
11
|
-
* @property path - Array of strings representing the path to the changed property
|
|
12
|
-
* @property changes - The new value for the property (for add/update) or the old value (for delete)
|
|
13
|
-
* @property type - The type of change that occurred
|
|
14
|
-
*/
|
|
15
|
-
export type Difference<_T> = { path: string[]; changes: any; type: ChangeType }
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Get the difference between two objects.
|
|
19
|
-
*
|
|
20
|
-
* This function performs a breadth-first comparison between two objects and returns
|
|
21
|
-
* a list of operations needed to transform the first object into the second.
|
|
22
|
-
*
|
|
23
|
-
* @param doc1 - The source object to compare from
|
|
24
|
-
* @param doc2 - The target object to compare to
|
|
25
|
-
* @returns A list of operations (add/update/delete) with their paths and changes
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* // Compare two simple objects
|
|
29
|
-
* const original = { name: 'John', age: 30 }
|
|
30
|
-
* const updated = { name: 'John', age: 31, city: 'New York' }
|
|
31
|
-
* const differences = diff(original, updated)
|
|
32
|
-
* // Returns:
|
|
33
|
-
* // [
|
|
34
|
-
* // { path: ['age'], changes: 31, type: 'update' },
|
|
35
|
-
* // { path: ['city'], changes: 'New York', type: 'add' }
|
|
36
|
-
* // ]
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* // Compare nested objects
|
|
40
|
-
* const original = {
|
|
41
|
-
* user: { name: 'John', settings: { theme: 'light' } }
|
|
42
|
-
* }
|
|
43
|
-
* const updated = {
|
|
44
|
-
* user: { name: 'John', settings: { theme: 'dark' } }
|
|
45
|
-
* }
|
|
46
|
-
* const differences = diff(original, updated)
|
|
47
|
-
* // Returns:
|
|
48
|
-
* // [
|
|
49
|
-
* // { path: ['user', 'settings', 'theme'], changes: 'dark', type: 'update' }
|
|
50
|
-
* // ]
|
|
51
|
-
*/
|
|
52
|
-
export const diff = <T extends Record<string, unknown>>(doc1: Record<string, unknown>, doc2: T) => {
|
|
53
|
-
const diff: Difference<T>[] = []
|
|
54
|
-
|
|
55
|
-
const bfs = (el1: unknown, el2: unknown, prefix = []) => {
|
|
56
|
-
// If the types are different, we know that the property has been added, deleted or updated
|
|
57
|
-
if (typeof el1 !== typeof el2) {
|
|
58
|
-
if (typeof el1 === 'undefined') {
|
|
59
|
-
diff.push({ path: prefix, changes: el2, type: 'add' })
|
|
60
|
-
return
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (typeof el2 === 'undefined') {
|
|
64
|
-
diff.push({ path: prefix, changes: el1, type: 'delete' })
|
|
65
|
-
return
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
diff.push({ path: prefix, changes: el2, type: 'update' })
|
|
69
|
-
return
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// We now can assume that el1 and el2 are of the same type
|
|
73
|
-
|
|
74
|
-
// For nested objects, we need to recursively check the properties
|
|
75
|
-
if (typeof el1 === 'object' && typeof el2 === 'object' && el1 !== null && el2 !== null) {
|
|
76
|
-
const keys = new Set([...Object.keys(el1), ...Object.keys(el2)])
|
|
77
|
-
|
|
78
|
-
for (const key of keys) {
|
|
79
|
-
bfs(el1[key], el2[key], [...prefix, key])
|
|
80
|
-
}
|
|
81
|
-
return
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// For primitives, we can just compare the values
|
|
85
|
-
if (el1 !== el2) {
|
|
86
|
-
diff.push({ path: prefix, changes: el2, type: 'update' })
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Run breadth-first search
|
|
91
|
-
bfs(doc1, doc2)
|
|
92
|
-
return diff
|
|
93
|
-
}
|
package/src/diff/index.test.ts
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import { apply } from '@/diff/apply'
|
|
2
|
-
import { diff } from '@/diff/diff'
|
|
3
|
-
import { describe, expect, test } from 'vitest'
|
|
4
|
-
|
|
5
|
-
describe('if we get list of operations we need to perform A -> B, when we apply them on A it should give us B', () => {
|
|
6
|
-
test.each([
|
|
7
|
-
[
|
|
8
|
-
{ name: 'John', age: 25 },
|
|
9
|
-
{ name: 'Jeremy', age: 25 },
|
|
10
|
-
],
|
|
11
|
-
[
|
|
12
|
-
{
|
|
13
|
-
name: 'John',
|
|
14
|
-
age: 25,
|
|
15
|
-
interest: {
|
|
16
|
-
cars: { classics: ['Ferrari 250 GTO', 'Chevrolet Camaro'] },
|
|
17
|
-
music: { pop: ['Bruno Mars', 'Justin Bieber'] },
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
name: 'John',
|
|
22
|
-
age: 25,
|
|
23
|
-
interest: {
|
|
24
|
-
cars: { classics: ['Ferrari 250 GTO'] },
|
|
25
|
-
music: { rock: ['Eagles', 'AC/DC'] },
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
],
|
|
29
|
-
[{ version: '1.1.0', name: '@scalar/json-diff' }, {}],
|
|
30
|
-
[
|
|
31
|
-
{
|
|
32
|
-
openapi: '3.0.0',
|
|
33
|
-
info: {
|
|
34
|
-
title: 'Simple API',
|
|
35
|
-
description: 'A small OpenAPI specification example',
|
|
36
|
-
version: '1.0.0',
|
|
37
|
-
},
|
|
38
|
-
paths: {
|
|
39
|
-
'/users': {
|
|
40
|
-
get: {
|
|
41
|
-
summary: 'Get a list of users',
|
|
42
|
-
operationId: 'getUsers',
|
|
43
|
-
responses: {
|
|
44
|
-
'200': {
|
|
45
|
-
description: 'A list of users',
|
|
46
|
-
content: {
|
|
47
|
-
'application/json': {
|
|
48
|
-
schema: {
|
|
49
|
-
type: 'array',
|
|
50
|
-
items: {
|
|
51
|
-
type: 'object',
|
|
52
|
-
properties: {
|
|
53
|
-
id: { type: 'integer' },
|
|
54
|
-
name: { type: 'string' },
|
|
55
|
-
},
|
|
56
|
-
},
|
|
57
|
-
},
|
|
58
|
-
},
|
|
59
|
-
},
|
|
60
|
-
},
|
|
61
|
-
},
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
'/users/{id}': {
|
|
65
|
-
get: {
|
|
66
|
-
summary: 'Get a user by ID',
|
|
67
|
-
operationId: 'getUserById',
|
|
68
|
-
parameters: [
|
|
69
|
-
{
|
|
70
|
-
name: 'id',
|
|
71
|
-
in: 'path',
|
|
72
|
-
required: true,
|
|
73
|
-
schema: { type: 'integer' },
|
|
74
|
-
},
|
|
75
|
-
],
|
|
76
|
-
responses: {
|
|
77
|
-
'200': {
|
|
78
|
-
description: 'User details',
|
|
79
|
-
content: {
|
|
80
|
-
'application/json': {
|
|
81
|
-
schema: {
|
|
82
|
-
type: 'object',
|
|
83
|
-
properties: {
|
|
84
|
-
id: { type: 'integer' },
|
|
85
|
-
name: { type: 'string' },
|
|
86
|
-
},
|
|
87
|
-
},
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
},
|
|
91
|
-
'404': {
|
|
92
|
-
description: 'User not found',
|
|
93
|
-
},
|
|
94
|
-
},
|
|
95
|
-
},
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
},
|
|
99
|
-
{
|
|
100
|
-
openapi: '3.0.0',
|
|
101
|
-
info: {
|
|
102
|
-
title: 'Simple API',
|
|
103
|
-
description: 'A big OpenAPI specification example',
|
|
104
|
-
version: '1.0.0',
|
|
105
|
-
},
|
|
106
|
-
paths: {
|
|
107
|
-
'/users': {
|
|
108
|
-
get: {
|
|
109
|
-
summary: 'Get a list of users',
|
|
110
|
-
operationId: 'getUsers',
|
|
111
|
-
responses: {
|
|
112
|
-
'200': {
|
|
113
|
-
description: 'A list of users',
|
|
114
|
-
content: {
|
|
115
|
-
'application/json': {
|
|
116
|
-
schema: {
|
|
117
|
-
type: 'array',
|
|
118
|
-
items: {
|
|
119
|
-
type: 'object',
|
|
120
|
-
properties: {
|
|
121
|
-
id: { type: 'integer' },
|
|
122
|
-
name: { type: 'string' },
|
|
123
|
-
},
|
|
124
|
-
},
|
|
125
|
-
},
|
|
126
|
-
},
|
|
127
|
-
},
|
|
128
|
-
},
|
|
129
|
-
},
|
|
130
|
-
},
|
|
131
|
-
},
|
|
132
|
-
},
|
|
133
|
-
},
|
|
134
|
-
],
|
|
135
|
-
[{}, {}],
|
|
136
|
-
[
|
|
137
|
-
{},
|
|
138
|
-
{
|
|
139
|
-
openapi: '3.0.0',
|
|
140
|
-
info: {
|
|
141
|
-
title: 'Simple API',
|
|
142
|
-
description: 'A big OpenAPI specification example',
|
|
143
|
-
version: '1.0.0',
|
|
144
|
-
},
|
|
145
|
-
},
|
|
146
|
-
],
|
|
147
|
-
])('apply(a, diff(a, b)) === b', (a, b) => {
|
|
148
|
-
expect(apply(a, diff(a, b))).toEqual(b)
|
|
149
|
-
})
|
|
150
|
-
})
|