@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.
Files changed (84) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/bundle/index.d.ts +1 -0
  3. package/dist/bundle/index.d.ts.map +1 -1
  4. package/dist/bundle/index.js.map +1 -1
  5. package/dist/bundle/plugins/browser.js.map +1 -1
  6. package/dist/bundle/plugins/node.d.ts +1 -1
  7. package/dist/bundle/plugins/node.js +1 -1
  8. package/dist/bundle/plugins/node.js.map +1 -1
  9. package/dist/dereference/index.d.ts.map +1 -1
  10. package/dist/dereference/index.js.map +2 -2
  11. package/dist/diff/index.d.ts +1 -1
  12. package/dist/diff/index.d.ts.map +1 -1
  13. package/dist/diff/index.js +1 -1
  14. package/dist/diff/index.js.map +2 -2
  15. package/dist/helpers/escape-json-pointer.d.ts +1 -1
  16. package/dist/helpers/escape-json-pointer.js.map +1 -1
  17. package/dist/magic-proxy/index.d.ts.map +1 -1
  18. package/dist/magic-proxy/index.js.map +2 -2
  19. package/dist/magic-proxy/proxy.d.ts +0 -1
  20. package/dist/magic-proxy/proxy.d.ts.map +1 -1
  21. package/dist/magic-proxy/proxy.js +1 -2
  22. package/dist/magic-proxy/proxy.js.map +2 -2
  23. package/package.json +12 -13
  24. package/.turbo/turbo-build.log +0 -10
  25. package/esbuild.ts +0 -15
  26. package/src/bundle/bundle.test.ts +0 -2917
  27. package/src/bundle/bundle.ts +0 -916
  28. package/src/bundle/create-limiter.test.ts +0 -28
  29. package/src/bundle/create-limiter.ts +0 -52
  30. package/src/bundle/index.ts +0 -3
  31. package/src/bundle/plugins/browser.ts +0 -4
  32. package/src/bundle/plugins/fetch-urls/index.test.ts +0 -141
  33. package/src/bundle/plugins/fetch-urls/index.ts +0 -105
  34. package/src/bundle/plugins/node.ts +0 -5
  35. package/src/bundle/plugins/parse-json/index.test.ts +0 -24
  36. package/src/bundle/plugins/parse-json/index.ts +0 -32
  37. package/src/bundle/plugins/parse-yaml/index.test.ts +0 -26
  38. package/src/bundle/plugins/parse-yaml/index.ts +0 -34
  39. package/src/bundle/plugins/read-files/index.test.ts +0 -36
  40. package/src/bundle/plugins/read-files/index.ts +0 -58
  41. package/src/bundle/value-generator.test.ts +0 -165
  42. package/src/bundle/value-generator.ts +0 -143
  43. package/src/dereference/dereference.test.ts +0 -142
  44. package/src/dereference/dereference.ts +0 -84
  45. package/src/dereference/index.ts +0 -2
  46. package/src/diff/apply.test.ts +0 -262
  47. package/src/diff/apply.ts +0 -83
  48. package/src/diff/diff.test.ts +0 -328
  49. package/src/diff/diff.ts +0 -93
  50. package/src/diff/index.test.ts +0 -150
  51. package/src/diff/index.ts +0 -5
  52. package/src/diff/merge.test.ts +0 -1109
  53. package/src/diff/merge.ts +0 -136
  54. package/src/diff/trie.test.ts +0 -30
  55. package/src/diff/trie.ts +0 -113
  56. package/src/diff/utils.test.ts +0 -169
  57. package/src/diff/utils.ts +0 -111
  58. package/src/helpers/convert-to-local-ref.test.ts +0 -211
  59. package/src/helpers/convert-to-local-ref.ts +0 -43
  60. package/src/helpers/escape-json-pointer.test.ts +0 -13
  61. package/src/helpers/escape-json-pointer.ts +0 -8
  62. package/src/helpers/get-schemas.test.ts +0 -356
  63. package/src/helpers/get-schemas.ts +0 -80
  64. package/src/helpers/get-segments-from-path.test.ts +0 -17
  65. package/src/helpers/get-segments-from-path.ts +0 -17
  66. package/src/helpers/get-value-by-path.test.ts +0 -338
  67. package/src/helpers/get-value-by-path.ts +0 -44
  68. package/src/helpers/is-json-object.ts +0 -31
  69. package/src/helpers/is-object.test.ts +0 -27
  70. package/src/helpers/is-object.ts +0 -4
  71. package/src/helpers/is-yaml.ts +0 -18
  72. package/src/helpers/json-path-utils.test.ts +0 -57
  73. package/src/helpers/json-path-utils.ts +0 -50
  74. package/src/helpers/normalize.test.ts +0 -92
  75. package/src/helpers/normalize.ts +0 -35
  76. package/src/helpers/unescape-json-pointer.test.ts +0 -23
  77. package/src/helpers/unescape-json-pointer.ts +0 -9
  78. package/src/magic-proxy/index.ts +0 -2
  79. package/src/magic-proxy/proxy.test.ts +0 -1987
  80. package/src/magic-proxy/proxy.ts +0 -323
  81. package/src/types.ts +0 -1
  82. package/tsconfig.build.json +0 -12
  83. package/tsconfig.json +0 -16
  84. package/vite.config.ts +0 -8
@@ -1,80 +0,0 @@
1
- /**
2
- * Retrieves the $id property from the input object if it exists and is a string.
3
- *
4
- * @param input - The object to extract the $id from.
5
- * @returns The $id string if present, otherwise undefined.
6
- */
7
- export const getId = (input: unknown): string | undefined => {
8
- if (input && typeof input === 'object' && input['$id'] && typeof input['$id'] === 'string') {
9
- return input['$id']
10
- }
11
- return undefined
12
- }
13
-
14
- /**
15
- * Joins an array of path segments into a single string separated by '/'.
16
- *
17
- * @param segments - The array of path segments.
18
- * @returns The joined path string.
19
- */
20
- const getPath = (segments: string[]): string => {
21
- return segments.join('/')
22
- }
23
-
24
- /**
25
- * Recursively traverses the input object to collect all schemas identified by $id and $anchor properties.
26
- *
27
- * - If an object has a $id property, it is added to the map with its $id as the key.
28
- * - If an object has a $anchor property, it is added to the map with a key composed of the current base and the anchor.
29
- * - The function performs a depth-first search (DFS) through all nested objects.
30
- *
31
- * @param input - The input object to traverse.
32
- * @param base - The current base URI, used for resolving anchors.
33
- * @param map - The map collecting found schemas.
34
- * @returns A map of schema identifiers to their corresponding objects.
35
- */
36
- export const getSchemas = (
37
- input: unknown,
38
- base: string = '',
39
- segments: string[] = [],
40
- map = new Map<string, string>(),
41
- visited = new WeakSet(),
42
- ) => {
43
- // Only process non-null objects
44
- if (typeof input !== 'object' || input === null) {
45
- return map
46
- }
47
-
48
- // If the object has already been visited, return the map
49
- if (visited.has(input)) {
50
- return map
51
- }
52
-
53
- // Add the object to the visited set
54
- visited.add(input)
55
-
56
- // Attempt to get $id from the current object
57
- const id = getId(input)
58
-
59
- // If $id exists, add the object to the map with $id as the key
60
- if (id) {
61
- map.set(id, getPath(segments))
62
- }
63
-
64
- // Update the base for nested anchors
65
- const newBase = id ?? base
66
-
67
- // If $anchor exists, add the object to the map with base#anchor as the key
68
- if (input['$anchor'] && typeof input['$anchor'] === 'string') {
69
- map.set(`${newBase}#${input['$anchor']}`, getPath(segments))
70
- }
71
-
72
- // Recursively traverse all properties (DFS)
73
- for (const key in input) {
74
- if (typeof input[key] === 'object' && input[key] !== null) {
75
- getSchemas(input[key], newBase, [...segments, key], map, visited)
76
- }
77
- }
78
-
79
- return map
80
- }
@@ -1,17 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
-
3
- import { getSegmentsFromPath } from './get-segments-from-path'
4
-
5
- describe('getSegmentsFromPath', () => {
6
- it('returns path segments', () => {
7
- const result = getSegmentsFromPath('/paths/test')
8
-
9
- expect(result).toEqual(['paths', 'test'])
10
- })
11
-
12
- it('unescaped slashes', () => {
13
- const result = getSegmentsFromPath('/paths/~1test')
14
-
15
- expect(result).toEqual(['paths', '/test'])
16
- })
17
- })
@@ -1,17 +0,0 @@
1
- import { unescapeJsonPointer } from './unescape-json-pointer'
2
-
3
- /**
4
- * Translate `/paths/~1test` to `['paths', '/test']`
5
- */
6
- export function getSegmentsFromPath(path: string) {
7
- return (
8
- // /paths/~1test
9
- path
10
- // ['', 'paths', '~1test']
11
- .split('/')
12
- // ['paths', '~test']
13
- .slice(1)
14
- // ['paths', '/test']
15
- .map(unescapeJsonPointer)
16
- )
17
- }
@@ -1,338 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
-
3
- import { getValueByPath } from './get-value-by-path'
4
-
5
- describe('getValueByPath', () => {
6
- it('should return the value at a simple path', () => {
7
- const target = {
8
- foo: 'bar',
9
- }
10
- const result = getValueByPath(target, ['foo'])
11
-
12
- expect(result).toEqual({
13
- context: '',
14
- value: 'bar',
15
- })
16
- })
17
-
18
- it('should return the value at a nested path', () => {
19
- const target = {
20
- foo: {
21
- bar: {
22
- baz: 42,
23
- },
24
- },
25
- }
26
- const result = getValueByPath(target, ['foo', 'bar', 'baz'])
27
-
28
- expect(result).toEqual({
29
- context: '',
30
- value: 42,
31
- })
32
- })
33
-
34
- it('should return context with $id when found', () => {
35
- const target = {
36
- foo: {
37
- $id: 'https://example.com/schema',
38
- bar: {
39
- baz: 'value',
40
- },
41
- },
42
- }
43
- const result = getValueByPath(target, ['foo', 'bar', 'baz'])
44
-
45
- expect(result).toEqual({
46
- context: 'https://example.com/schema',
47
- value: 'value',
48
- })
49
- })
50
-
51
- it('should preserve context from parent when child has no $id', () => {
52
- const target = {
53
- foo: {
54
- $id: 'https://example.com/parent',
55
- bar: {
56
- baz: 'value',
57
- },
58
- },
59
- }
60
- const result = getValueByPath(target, ['foo', 'bar', 'baz'])
61
-
62
- expect(result).toEqual({
63
- context: 'https://example.com/parent',
64
- value: 'value',
65
- })
66
- })
67
-
68
- it('should update context when child has $id', () => {
69
- const target = {
70
- foo: {
71
- $id: 'https://example.com/parent',
72
- bar: {
73
- $id: 'https://example.com/child',
74
- baz: 'value',
75
- },
76
- },
77
- }
78
- const result = getValueByPath(target, ['foo', 'bar', 'baz'])
79
-
80
- expect(result).toEqual({
81
- context: 'https://example.com/child',
82
- value: 'value',
83
- })
84
- })
85
-
86
- it('should return undefined value when path does not exist', () => {
87
- const target = {
88
- foo: 'bar',
89
- }
90
- const result = getValueByPath(target, ['nonexistent'])
91
-
92
- expect(result).toEqual({
93
- context: '',
94
- value: undefined,
95
- })
96
- })
97
-
98
- it('should return undefined value when path partially exists', () => {
99
- const target = {
100
- foo: {
101
- bar: 'baz',
102
- },
103
- }
104
- const result = getValueByPath(target, ['foo', 'nonexistent'])
105
-
106
- expect(result).toEqual({
107
- context: '',
108
- value: undefined,
109
- })
110
- })
111
-
112
- it('should return undefined value when traversing through null', () => {
113
- const target = {
114
- foo: null,
115
- }
116
- const result = getValueByPath(target, ['foo', 'bar'])
117
-
118
- expect(result).toEqual({
119
- context: '',
120
- value: undefined,
121
- })
122
- })
123
-
124
- it('should return undefined value when traversing through primitive', () => {
125
- const target = {
126
- foo: 'string',
127
- }
128
- const result = getValueByPath(target, ['foo', 'bar'])
129
-
130
- expect(result).toEqual({
131
- context: '',
132
- value: undefined,
133
- })
134
- })
135
-
136
- it('should handle empty segments array', () => {
137
- const target = {
138
- foo: 'bar',
139
- }
140
- const result = getValueByPath(target, [])
141
-
142
- expect(result).toEqual({
143
- context: '',
144
- value: target,
145
- })
146
- })
147
-
148
- it('should handle array indices in path', () => {
149
- const target = {
150
- items: [{ name: 'first' }, { name: 'second' }],
151
- }
152
- const result = getValueByPath(target, ['items', '0', 'name'])
153
-
154
- expect(result).toEqual({
155
- context: '',
156
- value: 'first',
157
- })
158
- })
159
-
160
- it('should handle objects with numeric keys', () => {
161
- const target = {
162
- '0': 'zero',
163
- '1': 'one',
164
- }
165
- const result = getValueByPath(target, ['0'])
166
-
167
- expect(result).toEqual({
168
- context: '',
169
- value: 'zero',
170
- })
171
- })
172
-
173
- it('should handle deeply nested objects', () => {
174
- const target = {
175
- level1: {
176
- level2: {
177
- level3: {
178
- level4: {
179
- level5: {
180
- value: 'deep',
181
- },
182
- },
183
- },
184
- },
185
- },
186
- }
187
- const result = getValueByPath(target, ['level1', 'level2', 'level3', 'level4', 'level5', 'value'])
188
-
189
- expect(result).toEqual({
190
- context: '',
191
- value: 'deep',
192
- })
193
- })
194
-
195
- it('should handle mixed data types in path', () => {
196
- const target = {
197
- string: 'text',
198
- number: 42,
199
- boolean: true,
200
- null: null,
201
- array: [1, 2, 3],
202
- object: { nested: 'value' },
203
- }
204
-
205
- expect(getValueByPath(target, ['string'])).toEqual({ context: '', value: 'text' })
206
- expect(getValueByPath(target, ['number'])).toEqual({ context: '', value: 42 })
207
- expect(getValueByPath(target, ['boolean'])).toEqual({ context: '', value: true })
208
- expect(getValueByPath(target, ['null'])).toEqual({ context: '', value: null })
209
- expect(getValueByPath(target, ['array'])).toEqual({ context: '', value: [1, 2, 3] })
210
- expect(getValueByPath(target, ['object', 'nested'])).toEqual({ context: '', value: 'value' })
211
- })
212
-
213
- it('should handle $id with different formats', () => {
214
- const target = {
215
- schema1: {
216
- $id: 'https://example.com/schema1',
217
- value: 'test1',
218
- },
219
- schema2: {
220
- $id: 'urn:uuid:123e4567-e89b-12d3-a456-426614174000',
221
- value: 'test2',
222
- },
223
- schema3: {
224
- $id: 'relative/path',
225
- value: 'test3',
226
- },
227
- }
228
-
229
- expect(getValueByPath(target, ['schema1', 'value'])).toEqual({
230
- context: 'https://example.com/schema1',
231
- value: 'test1',
232
- })
233
- expect(getValueByPath(target, ['schema2', 'value'])).toEqual({
234
- context: 'urn:uuid:123e4567-e89b-12d3-a456-426614174000',
235
- value: 'test2',
236
- })
237
- expect(getValueByPath(target, ['schema3', 'value'])).toEqual({
238
- context: 'relative/path',
239
- value: 'test3',
240
- })
241
- })
242
-
243
- it('should ignore non-string $id values', () => {
244
- const target = {
245
- schema1: {
246
- $id: 123, // number
247
- value: 'test1',
248
- },
249
- schema2: {
250
- $id: { nested: 'object' }, // object
251
- value: 'test2',
252
- },
253
- schema3: {
254
- $id: null, // null
255
- value: 'test3',
256
- },
257
- }
258
-
259
- expect(getValueByPath(target, ['schema1', 'value'])).toEqual({
260
- context: '',
261
- value: 'test1',
262
- })
263
- expect(getValueByPath(target, ['schema2', 'value'])).toEqual({
264
- context: '',
265
- value: 'test2',
266
- })
267
- expect(getValueByPath(target, ['schema3', 'value'])).toEqual({
268
- context: '',
269
- value: 'test3',
270
- })
271
- })
272
-
273
- it('should handle context inheritance through multiple levels', () => {
274
- const target = {
275
- root: {
276
- $id: 'https://example.com/root',
277
- level1: {
278
- level2: {
279
- $id: 'https://example.com/level2',
280
- level3: {
281
- level4: {
282
- value: 'nested',
283
- },
284
- },
285
- },
286
- },
287
- },
288
- }
289
-
290
- // Should use root context when no intermediate $id
291
- expect(getValueByPath(target, ['root', 'level1', 'level2', 'level3', 'level4', 'value'])).toEqual({
292
- context: 'https://example.com/level2',
293
- value: 'nested',
294
- })
295
- })
296
-
297
- it('should handle edge case with empty string keys', () => {
298
- const target = {
299
- '': 'empty key',
300
- normal: 'normal key',
301
- }
302
-
303
- expect(getValueByPath(target, [''])).toEqual({
304
- context: '',
305
- value: 'empty key',
306
- })
307
- expect(getValueByPath(target, ['normal'])).toEqual({
308
- context: '',
309
- value: 'normal key',
310
- })
311
- })
312
-
313
- it('should handle special characters in keys', () => {
314
- const target = {
315
- 'key-with-dash': 'dash',
316
- 'key_with_underscore': 'underscore',
317
- 'key.with.dots': 'dots',
318
- 'key with spaces': 'spaces',
319
- }
320
-
321
- expect(getValueByPath(target, ['key-with-dash'])).toEqual({
322
- context: '',
323
- value: 'dash',
324
- })
325
- expect(getValueByPath(target, ['key_with_underscore'])).toEqual({
326
- context: '',
327
- value: 'underscore',
328
- })
329
- expect(getValueByPath(target, ['key.with.dots'])).toEqual({
330
- context: '',
331
- value: 'dots',
332
- })
333
- expect(getValueByPath(target, ['key with spaces'])).toEqual({
334
- context: '',
335
- value: 'spaces',
336
- })
337
- })
338
- })
@@ -1,44 +0,0 @@
1
- import { getId } from '@/helpers/get-schemas'
2
-
3
- /**
4
- * Traverses an object using an array of string segments (path keys) and returns
5
- * the value at the specified path along with its context (id if available).
6
- *
7
- * @param target - The root object to traverse.
8
- * @param segments - An array of string keys representing the path to traverse.
9
- * @returns An object containing the final context (id or previous context) and the value at the path.
10
- *
11
- * @example
12
- * const obj = {
13
- * foo: {
14
- * bar: {
15
- * baz: 42
16
- * }
17
- * }
18
- * };
19
- * // Returns: { context: '', value: 42 }
20
- * getValueByPath(obj, ['foo', 'bar', 'baz']);
21
- */
22
- export function getValueByPath(target: unknown, segments: string[]): { context: string; value: any } {
23
- return segments.reduce<{ context: string; value: unknown }>(
24
- (acc, key) => {
25
- // If the accumulator is undefined, the path does not exist
26
- if (acc.value === undefined) {
27
- return { context: '', value: undefined }
28
- }
29
- // If the accumulator is not an object or is null, stop traversal
30
- if (typeof acc.value !== 'object' || acc.value === null) {
31
- return { context: '', value: undefined }
32
- }
33
- // Attempt to get the id from the current value for context tracking
34
- const id = getId(acc.value)
35
-
36
- // Return the next context and value for the next iteration
37
- return { context: id ?? acc.context, value: acc.value?.[key] }
38
- },
39
- {
40
- context: '',
41
- value: target,
42
- },
43
- )
44
- }
@@ -1,31 +0,0 @@
1
- import { isObject } from '@/helpers/is-object'
2
-
3
- /**
4
- * Determines if a string represents a valid JSON object (i.e., a plain object, not an array, primitive, or null).
5
- * The function first checks if the string appears to start with an opening curly brace (ignoring leading whitespace),
6
- * which is a quick heuristic to rule out arrays, primitives, and most invalid JSON. If this check passes,
7
- * it attempts to parse the string with JSON.parse. The result is then checked to ensure it is a plain object
8
- * (not an array, null, or primitive) using the isObject utility.
9
- *
10
- * @param value - The string to evaluate
11
- * @returns true if the string is valid JSON and parses to a plain object, false otherwise
12
- *
13
- * @example
14
- * isJsonObject('{"foo": "bar"}') // true
15
- * isJsonObject('[1,2,3]') // false
16
- * isJsonObject('not json') // false
17
- * isJsonObject('42') // false
18
- */
19
- export function isJsonObject(value: string) {
20
- // Quickly rule out anything that doesn't start with an object brace
21
- if (!/^\s*(\{)/.test(value.slice(0, 500))) {
22
- return false
23
- }
24
-
25
- try {
26
- const val = JSON.parse(value)
27
- return isObject(val)
28
- } catch {
29
- return false
30
- }
31
- }
@@ -1,27 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
-
3
- import { isObject } from './is-object'
4
-
5
- describe('isObject', () => {
6
- it('returns true for an object', () => {
7
- const result = isObject({
8
- foo: 'bar',
9
- })
10
- expect(result).toBe(true)
11
- })
12
-
13
- it('returns true for an empty object', () => {
14
- const result = isObject({})
15
- expect(result).toBe(true)
16
- })
17
-
18
- it('returns false for a string', () => {
19
- const result = isObject('foo')
20
- expect(result).toBe(false)
21
- })
22
-
23
- it('returns false for an array', () => {
24
- const result = isObject([])
25
- expect(result).toBe(false)
26
- })
27
- })
@@ -1,4 +0,0 @@
1
- /**
2
- * Check if the given value is an object
3
- */
4
- export const isObject = (obj: any): obj is object => typeof obj === 'object' && !Array.isArray(obj) && obj !== null
@@ -1,18 +0,0 @@
1
- /**
2
- * Checks if a string appears to be YAML content.
3
- * This function uses a simple heuristic: it looks for a line that starts with an optional dash,
4
- * followed by a key (alphanumeric or dash), a colon, and a value, and then at least one more line.
5
- * This is not a full YAML parser, but works for basic detection.
6
- *
7
- * @param value - The string to check
8
- * @returns true if the string looks like YAML, false otherwise
9
- *
10
- * @example
11
- * isYaml('openapi: 3.0.0\ninfo:\n title: Example') // true
12
- * isYaml('{"openapi": "3.0.0", "info": {"title": "Example"}}') // false
13
- * isYaml('- name: value\n- name: value2') // true
14
- * isYaml('type: object') // false (only one line)
15
- */
16
- export function isYaml(value: string): boolean {
17
- return /^\s*(?:-\s*)?[\w\-]+\s*:\s*.+\n.*/.test(value)
18
- }
@@ -1,57 +0,0 @@
1
- import { describe, expect, test } from 'vitest'
2
-
3
- import { createPathFromSegments, parseJsonPointer } from './json-path-utils'
4
-
5
- describe('parseJsonPointer', () => {
6
- test.each([
7
- ['#/users/name', ['users', 'name']],
8
- ['#/', []],
9
- ['', []],
10
- ['users/name', ['users', 'name']],
11
- ])('should correctly parse json pointers', (a, b) => {
12
- expect(parseJsonPointer(a)).toEqual(b)
13
- })
14
- })
15
-
16
- describe('createPathFromSegments', () => {
17
- test('creates nested objects for non-numeric segments', () => {
18
- const obj: any = {}
19
- const leaf = createPathFromSegments(obj, ['components', 'schemas', 'User'])
20
-
21
- expect(obj).toEqual({ components: { schemas: { User: {} } } })
22
- expect(leaf).toBe(obj.components.schemas.User)
23
- })
24
-
25
- test('creates arrays for numeric segments', () => {
26
- const obj: any = {}
27
- const arr = createPathFromSegments(obj, ['items', '0'])
28
-
29
- expect(Array.isArray(obj.items['0'])).toBe(true)
30
- expect(arr).toBe(obj.items['0'])
31
- })
32
-
33
- test('does not overwrite existing values along the path', () => {
34
- const obj: any = { a: { b: { c: { existing: true } } } }
35
- const leaf = createPathFromSegments(obj, ['a', 'b', 'c'])
36
-
37
- expect(leaf).toEqual({ existing: true })
38
- expect(obj.a.b.c).toEqual({ existing: true })
39
- })
40
-
41
- test('returns root object when segments array is empty', () => {
42
- const obj: any = { pre: true }
43
- const result = createPathFromSegments(obj, [])
44
-
45
- expect(result).toBe(obj)
46
- expect(obj).toEqual({ pre: true })
47
- })
48
-
49
- test('creates nested arrays for consecutive numeric segments', () => {
50
- const obj: any = {}
51
- const leaf = createPathFromSegments(obj, ['arr', '0', '1'])
52
-
53
- expect(Array.isArray(obj.arr['0'])).toBe(true)
54
- expect(Array.isArray(obj.arr['0']['1'])).toBe(true)
55
- expect(leaf).toBe(obj.arr['0']['1'])
56
- })
57
- })
@@ -1,50 +0,0 @@
1
- /**
2
- * Parses a JSON Pointer string into an array of path segments
3
- *
4
- * @example
5
- * ```ts
6
- * parseJsonPointer('#/components/schemas/User')
7
- *
8
- * ['components', 'schemas', 'User']
9
- * ```
10
- */
11
- export function parseJsonPointer(pointer: string): string[] {
12
- return (
13
- pointer
14
- // Split on '/'
15
- .split('/')
16
- // Remove the leading '#' if present
17
- .filter((segment, index) => (index !== 0 || segment !== '#') && segment)
18
- )
19
- }
20
-
21
- /**
22
- * Creates a nested path in an object from an array of path segments.
23
- * Only creates intermediate objects/arrays if they don't already exist.
24
- *
25
- * @param obj - The target object to create the path in
26
- * @param segments - Array of path segments to create
27
- * @returns The final nested object/array at the end of the path
28
- *
29
- * @example
30
- * ```ts
31
- * const obj = {}
32
- * createPathFromSegments(obj, ['components', 'schemas', 'User'])
33
- * // Creates: { components: { schemas: { User: {} } } }
34
- *
35
- * createPathFromSegments(obj, ['items', '0', 'name'])
36
- * // Creates: { items: [{ name: {} }] }
37
- * ```
38
- */
39
- export function createPathFromSegments(obj: any, segments: string[]) {
40
- return segments.reduce((acc, part) => {
41
- if (acc[part] === undefined) {
42
- if (isNaN(Number(part))) {
43
- acc[part] = {}
44
- } else {
45
- acc[part] = []
46
- }
47
- }
48
- return acc[part]
49
- }, obj)
50
- }