@scalar/json-magic 0.1.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/.turbo/turbo-build.log +9 -0
- package/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +356 -0
- package/dist/bundle/bundle.d.ts +292 -0
- package/dist/bundle/bundle.d.ts.map +1 -0
- package/dist/bundle/bundle.js +259 -0
- package/dist/bundle/bundle.js.map +7 -0
- package/dist/bundle/create-limiter.d.ts +21 -0
- package/dist/bundle/create-limiter.d.ts.map +1 -0
- package/dist/bundle/create-limiter.js +31 -0
- package/dist/bundle/create-limiter.js.map +7 -0
- package/dist/bundle/index.d.ts +2 -0
- package/dist/bundle/index.d.ts.map +1 -0
- package/dist/bundle/index.js +5 -0
- package/dist/bundle/index.js.map +7 -0
- package/dist/bundle/plugins/browser.d.ts +4 -0
- package/dist/bundle/plugins/browser.d.ts.map +1 -0
- package/dist/bundle/plugins/browser.js +9 -0
- package/dist/bundle/plugins/browser.js.map +7 -0
- package/dist/bundle/plugins/fetch-urls/index.d.ts +39 -0
- package/dist/bundle/plugins/fetch-urls/index.d.ts.map +1 -0
- package/dist/bundle/plugins/fetch-urls/index.js +48 -0
- package/dist/bundle/plugins/fetch-urls/index.js.map +7 -0
- package/dist/bundle/plugins/node.d.ts +5 -0
- package/dist/bundle/plugins/node.d.ts.map +1 -0
- package/dist/bundle/plugins/node.js +11 -0
- package/dist/bundle/plugins/node.js.map +7 -0
- package/dist/bundle/plugins/parse-json/index.d.ts +13 -0
- package/dist/bundle/plugins/parse-json/index.d.ts.map +1 -0
- package/dist/bundle/plugins/parse-json/index.js +22 -0
- package/dist/bundle/plugins/parse-json/index.js.map +7 -0
- package/dist/bundle/plugins/parse-yaml/index.d.ts +13 -0
- package/dist/bundle/plugins/parse-yaml/index.d.ts.map +1 -0
- package/dist/bundle/plugins/parse-yaml/index.js +23 -0
- package/dist/bundle/plugins/parse-yaml/index.js.map +7 -0
- package/dist/bundle/plugins/read-files/index.d.ts +29 -0
- package/dist/bundle/plugins/read-files/index.d.ts.map +1 -0
- package/dist/bundle/plugins/read-files/index.js +30 -0
- package/dist/bundle/plugins/read-files/index.js.map +7 -0
- package/dist/bundle/value-generator.d.ts +79 -0
- package/dist/bundle/value-generator.d.ts.map +1 -0
- package/dist/bundle/value-generator.js +55 -0
- package/dist/bundle/value-generator.js.map +7 -0
- package/dist/dereference/dereference.d.ts +45 -0
- package/dist/dereference/dereference.d.ts.map +1 -0
- package/dist/dereference/dereference.js +37 -0
- package/dist/dereference/dereference.js.map +7 -0
- package/dist/dereference/index.d.ts +2 -0
- package/dist/dereference/index.d.ts.map +1 -0
- package/dist/dereference/index.js +5 -0
- package/dist/dereference/index.js.map +7 -0
- package/dist/diff/apply.d.ts +35 -0
- package/dist/diff/apply.d.ts.map +1 -0
- package/dist/diff/apply.js +40 -0
- package/dist/diff/apply.js.map +7 -0
- package/dist/diff/diff.d.ts +56 -0
- package/dist/diff/diff.d.ts.map +1 -0
- package/dist/diff/diff.js +33 -0
- package/dist/diff/diff.js.map +7 -0
- package/dist/diff/index.d.ts +5 -0
- package/dist/diff/index.d.ts.map +1 -0
- package/dist/diff/index.js +9 -0
- package/dist/diff/index.js.map +7 -0
- package/dist/diff/merge.d.ts +43 -0
- package/dist/diff/merge.d.ts.map +1 -0
- package/dist/diff/merge.js +61 -0
- package/dist/diff/merge.js.map +7 -0
- package/dist/diff/trie.d.ts +64 -0
- package/dist/diff/trie.d.ts.map +1 -0
- package/dist/diff/trie.js +82 -0
- package/dist/diff/trie.js.map +7 -0
- package/dist/diff/utils.d.ts +63 -0
- package/dist/diff/utils.d.ts.map +1 -0
- package/dist/diff/utils.js +48 -0
- package/dist/diff/utils.js.map +7 -0
- package/dist/magic-proxy/index.d.ts +2 -0
- package/dist/magic-proxy/index.d.ts.map +1 -0
- package/dist/magic-proxy/index.js +6 -0
- package/dist/magic-proxy/index.js.map +7 -0
- package/dist/magic-proxy/proxy.d.ts +63 -0
- package/dist/magic-proxy/proxy.d.ts.map +1 -0
- package/dist/magic-proxy/proxy.js +108 -0
- package/dist/magic-proxy/proxy.js.map +7 -0
- package/dist/polyfills/index.d.ts +2 -0
- package/dist/polyfills/index.d.ts.map +1 -0
- package/dist/polyfills/index.js +25 -0
- package/dist/polyfills/index.js.map +7 -0
- package/dist/polyfills/path.d.ts +24 -0
- package/dist/polyfills/path.d.ts.map +1 -0
- package/dist/polyfills/path.js +174 -0
- package/dist/polyfills/path.js.map +7 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/types.js.map +7 -0
- package/dist/utils/escape-json-pointer.d.ts +7 -0
- package/dist/utils/escape-json-pointer.d.ts.map +1 -0
- package/dist/utils/escape-json-pointer.js +7 -0
- package/dist/utils/escape-json-pointer.js.map +7 -0
- package/dist/utils/get-segments-from-path.d.ts +5 -0
- package/dist/utils/get-segments-from-path.d.ts.map +1 -0
- package/dist/utils/get-segments-from-path.js +11 -0
- package/dist/utils/get-segments-from-path.js.map +7 -0
- package/dist/utils/is-json-object.d.ts +18 -0
- package/dist/utils/is-json-object.d.ts.map +1 -0
- package/dist/utils/is-json-object.js +16 -0
- package/dist/utils/is-json-object.js.map +7 -0
- package/dist/utils/is-object.d.ts +5 -0
- package/dist/utils/is-object.d.ts.map +1 -0
- package/dist/utils/is-object.js +5 -0
- package/dist/utils/is-object.js.map +7 -0
- package/dist/utils/is-yaml.d.ts +17 -0
- package/dist/utils/is-yaml.d.ts.map +1 -0
- package/dist/utils/is-yaml.js +7 -0
- package/dist/utils/is-yaml.js.map +7 -0
- package/dist/utils/json-path-utils.d.ts +23 -0
- package/dist/utils/json-path-utils.d.ts.map +1 -0
- package/dist/utils/json-path-utils.js +16 -0
- package/dist/utils/json-path-utils.js.map +7 -0
- package/dist/utils/normalize.d.ts +5 -0
- package/dist/utils/normalize.d.ts.map +1 -0
- package/dist/utils/normalize.js +28 -0
- package/dist/utils/normalize.js.map +7 -0
- package/dist/utils/unescape-json-pointer.d.ts +8 -0
- package/dist/utils/unescape-json-pointer.d.ts.map +1 -0
- package/dist/utils/unescape-json-pointer.js +7 -0
- package/dist/utils/unescape-json-pointer.js.map +7 -0
- package/esbuild.ts +13 -0
- package/package.json +65 -0
- package/src/bundle/bundle.test.ts +1843 -0
- package/src/bundle/bundle.ts +758 -0
- package/src/bundle/create-limiter.test.ts +28 -0
- package/src/bundle/create-limiter.ts +52 -0
- package/src/bundle/index.ts +2 -0
- package/src/bundle/plugins/browser.ts +4 -0
- package/src/bundle/plugins/fetch-urls/index.test.ts +147 -0
- package/src/bundle/plugins/fetch-urls/index.ts +94 -0
- package/src/bundle/plugins/node.ts +5 -0
- package/src/bundle/plugins/parse-json/index.test.ts +22 -0
- package/src/bundle/plugins/parse-json/index.ts +30 -0
- package/src/bundle/plugins/parse-yaml/index.test.ts +24 -0
- package/src/bundle/plugins/parse-yaml/index.ts +31 -0
- package/src/bundle/plugins/read-files/index.test.ts +35 -0
- package/src/bundle/plugins/read-files/index.ts +55 -0
- package/src/bundle/value-generator.test.ts +166 -0
- package/src/bundle/value-generator.ts +147 -0
- package/src/dereference/dereference.test.ts +137 -0
- package/src/dereference/dereference.ts +84 -0
- package/src/dereference/index.ts +2 -0
- package/src/diff/apply.test.ts +262 -0
- package/src/diff/apply.ts +78 -0
- package/src/diff/diff.test.ts +328 -0
- package/src/diff/diff.ts +94 -0
- package/src/diff/index.test.ts +150 -0
- package/src/diff/index.ts +5 -0
- package/src/diff/merge.test.ts +1109 -0
- package/src/diff/merge.ts +136 -0
- package/src/diff/trie.test.ts +30 -0
- package/src/diff/trie.ts +113 -0
- package/src/diff/utils.test.ts +169 -0
- package/src/diff/utils.ts +113 -0
- package/src/magic-proxy/index.ts +2 -0
- package/src/magic-proxy/proxy.test.ts +145 -0
- package/src/magic-proxy/proxy.ts +225 -0
- package/src/polyfills/index.ts +12 -0
- package/src/polyfills/path.ts +248 -0
- package/src/types.ts +1 -0
- package/src/utils/escape-json-pointer.test.ts +13 -0
- package/src/utils/escape-json-pointer.ts +8 -0
- package/src/utils/get-segments-from-path.test.ts +17 -0
- package/src/utils/get-segments-from-path.ts +17 -0
- package/src/utils/is-json-object.ts +31 -0
- package/src/utils/is-object.test.ts +27 -0
- package/src/utils/is-object.ts +4 -0
- package/src/utils/is-yaml.ts +18 -0
- package/src/utils/json-path-utils.test.ts +13 -0
- package/src/utils/json-path-utils.ts +38 -0
- package/src/utils/normalize.test.ts +91 -0
- package/src/utils/normalize.ts +34 -0
- package/src/utils/unescape-json-pointer.test.ts +23 -0
- package/src/utils/unescape-json-pointer.ts +9 -0
- package/tsconfig.build.json +12 -0
- package/tsconfig.json +16 -0
- package/vite.config.ts +8 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { normalize } from '@/utils/normalize'
|
|
2
|
+
import { describe, expect, it } from 'vitest'
|
|
3
|
+
|
|
4
|
+
describe('normalize', () => {
|
|
5
|
+
it('returns undefined if the document is null', () => {
|
|
6
|
+
expect(normalize(null)).toEqual(undefined)
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
it('should parse JSON string specifications', () => {
|
|
10
|
+
const jsonString = '{"foo": "bar"}'
|
|
11
|
+
expect(normalize(jsonString)).toEqual({ foo: 'bar' })
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should parse YAML string specifications', () => {
|
|
15
|
+
const yamlString = 'foo: bar\nbar: foo'
|
|
16
|
+
expect(normalize(yamlString)).toEqual({ foo: 'bar', bar: 'foo' })
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('should handle invalid YAML with custom maxAliasCount', () => {
|
|
20
|
+
const yamlString = `
|
|
21
|
+
aliases: &ref
|
|
22
|
+
- item1
|
|
23
|
+
- item2
|
|
24
|
+
items: *ref
|
|
25
|
+
`
|
|
26
|
+
expect(() => normalize(yamlString)).not.toThrow()
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('should return the same object if specification is already an object', () => {
|
|
30
|
+
const obj = { foo: 'bar' }
|
|
31
|
+
expect(normalize(obj)).toBe(obj)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it("doesn't freak out on invalid JSON strings ", () => {
|
|
35
|
+
// Missing quotes around property name
|
|
36
|
+
const malformedJson = '{ foo: "bar" }'
|
|
37
|
+
expect(normalize(malformedJson)).toEqual(undefined)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('should handle empty string input', () => {
|
|
41
|
+
expect(normalize('')).toEqual(undefined)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('should handle whitespace-only string input', () => {
|
|
45
|
+
expect(normalize(' ')).toEqual(undefined)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('should handle complex nested structures', () => {
|
|
49
|
+
const complex = {
|
|
50
|
+
nested: {
|
|
51
|
+
array: [1, 2, 3],
|
|
52
|
+
object: {
|
|
53
|
+
foo: 'bar',
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
expect(normalize(complex)).toEqual(complex)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('should handle invalid JSON and YAML strings gracefully', () => {
|
|
61
|
+
const invalidString = 'not a valid json or yaml'
|
|
62
|
+
expect(() => normalize(invalidString)).not.toThrow()
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it('should handle non-string, non-object inputs', () => {
|
|
66
|
+
expect(normalize(42)).toEqual(42)
|
|
67
|
+
expect(normalize(true)).toEqual(true)
|
|
68
|
+
expect(normalize([1, 2, 3])).toEqual([1, 2, 3])
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('should handle deeply nested structures', () => {
|
|
72
|
+
const nested = { a: { b: { c: { d: { e: 'deep' } } } } }
|
|
73
|
+
expect(normalize(nested)).toEqual(nested)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('should handle large input strings', () => {
|
|
77
|
+
const largeJson = JSON.stringify({ foo: 'bar'.repeat(10000) })
|
|
78
|
+
expect(normalize(largeJson)).toEqual({ foo: 'bar'.repeat(10000) })
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('should handle circular references', () => {
|
|
82
|
+
const circularObj: any = {}
|
|
83
|
+
circularObj.self = circularObj
|
|
84
|
+
expect(() => normalize(circularObj)).not.toThrow()
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('should handle special characters in strings', () => {
|
|
88
|
+
const specialCharJson = '{"foo": "bar\\n"}'
|
|
89
|
+
expect(normalize(specialCharJson)).toEqual({ foo: 'bar\n' })
|
|
90
|
+
})
|
|
91
|
+
})
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { parse } from 'yaml'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Normalize a string (YAML, JSON, object) to a JavaScript datatype.
|
|
5
|
+
*/
|
|
6
|
+
export function normalize(content: any) {
|
|
7
|
+
if (content === null) {
|
|
8
|
+
return undefined
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (typeof content === 'string') {
|
|
12
|
+
if (content.trim() === '') {
|
|
13
|
+
return undefined
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse(content)
|
|
18
|
+
} catch (_error) {
|
|
19
|
+
// Does it look like YAML?
|
|
20
|
+
const hasColon = /^[^:]+:/.test(content)
|
|
21
|
+
const isJson = content.slice(0, 50).trimStart().startsWith('{')
|
|
22
|
+
|
|
23
|
+
if (!hasColon || isJson) {
|
|
24
|
+
return undefined
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return parse(content, {
|
|
28
|
+
maxAliasCount: 10000,
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return content
|
|
34
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { unescapeJsonPointer } from './unescape-json-pointer'
|
|
4
|
+
|
|
5
|
+
describe('unescapeJsonPointer', async () => {
|
|
6
|
+
it('unescapes a slash', () => {
|
|
7
|
+
expect(unescapeJsonPointer('/foo~1bar~1baz')).toBe('/foo/bar/baz')
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('unescapes multiple slashes', () => {
|
|
11
|
+
expect(unescapeJsonPointer('#/paths/~1upload/post/responses/401/content/application~1problem+json/schema')).toBe(
|
|
12
|
+
'#/paths//upload/post/responses/401/content/application/problem+json/schema',
|
|
13
|
+
)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('unescapes a tilde', () => {
|
|
17
|
+
expect(unescapeJsonPointer('/foo~0bar~0baz')).toBe('/foo~bar~baz')
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('unescapes a space', () => {
|
|
21
|
+
expect(unescapeJsonPointer('foo%20bar')).toBe('foo bar')
|
|
22
|
+
})
|
|
23
|
+
})
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
/** We need to remove these :) */
|
|
5
|
+
"strictNullChecks": false,
|
|
6
|
+
"noImplicitAny": false,
|
|
7
|
+
"useUnknownInCatchVariables": false,
|
|
8
|
+
"exactOptionalPropertyTypes": false,
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
"paths": {
|
|
11
|
+
"@/*": ["./src/*"],
|
|
12
|
+
"@test/*": ["./test/*"]
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"include": ["src", "tests", "rollup.config.ts"]
|
|
16
|
+
}
|