@isdk/json-repair 0.1.3 → 0.3.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/CHANGELOG.md +65 -9
- package/README.cn.md +143 -9
- package/README.md +181 -12
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +828 -41
- package/dist/index.d.ts +828 -41
- package/dist/index.js +1 -1
- package/package.json +30 -7
- package/.prettierignore +0 -12
- package/.prettierrc +0 -8
- package/.versionrc +0 -36
- package/TODO +0 -0
- package/docs/README.md +0 -246
- package/docs/_media/README.cn.md +0 -214
- package/docs/classes/RepairParser.md +0 -64
- package/docs/classes/SchemaWalker.md +0 -241
- package/docs/functions/jsonRepair.md +0 -48
- package/docs/globals.md +0 -24
- package/docs/interfaces/RepairOptions.md +0 -96
- package/docs/type-aliases/JSONPath.md +0 -14
- package/eslint.config.mjs +0 -65
- package/setupVitest.mjs +0 -15
- package/tsconfig.json +0 -20
- package/tsconfig.release.json +0 -21
- package/tsup.config.ts +0 -38
- package/typedoc.config.cjs +0 -9
- package/vite.config.mjs +0 -21
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
[**@isdk/json-repair**](../README.md)
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
[@isdk/json-repair](../globals.md) / RepairOptions
|
|
6
|
-
|
|
7
|
-
# Interface: RepairOptions
|
|
8
|
-
|
|
9
|
-
Defined in: [types.ts:4](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/types.ts#L4)
|
|
10
|
-
|
|
11
|
-
Options for the JSON repair process.
|
|
12
|
-
|
|
13
|
-
## Properties
|
|
14
|
-
|
|
15
|
-
### additionalProperties?
|
|
16
|
-
|
|
17
|
-
> `optional` **additionalProperties?**: `"keep"` \| `"remove"`
|
|
18
|
-
|
|
19
|
-
Defined in: [types.ts:70](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/types.ts#L70)
|
|
20
|
-
|
|
21
|
-
Defines how to handle properties not defined in the Schema.
|
|
22
|
-
|
|
23
|
-
- `'keep'` (default): Keeps these extra properties in the output.
|
|
24
|
-
- `'remove'`: Drops them. Only applies at a level whose Schema declares `properties`;
|
|
25
|
-
a free-form level has no "extra" keys and is left untouched.
|
|
26
|
-
|
|
27
|
-
This option is independent from the JSON Schema keyword `additionalProperties: false`,
|
|
28
|
-
which is respected on its own when validating incoming keys.
|
|
29
|
-
|
|
30
|
-
***
|
|
31
|
-
|
|
32
|
-
### coerceTypes?
|
|
33
|
-
|
|
34
|
-
> `optional` **coerceTypes?**: `boolean`
|
|
35
|
-
|
|
36
|
-
Defined in: [types.ts:58](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/types.ts#L58)
|
|
37
|
-
|
|
38
|
-
Whether to coerce values to the type the Schema declares. Enabled by default.
|
|
39
|
-
|
|
40
|
-
- `true` (default): `"Approx. 1,200.50 USD"` becomes `1200.5`, `"yes"` becomes `true`,
|
|
41
|
-
`"Processing!"` becomes the enum entry it fuzzy matches, `12` becomes `"12"` when the
|
|
42
|
-
Schema says `type: 'string'`.
|
|
43
|
-
- `false`: values are returned exactly as they were parsed.
|
|
44
|
-
|
|
45
|
-
Coercion needs the Schema of the property the value belongs to; a level that declares
|
|
46
|
-
no types is left alone.
|
|
47
|
-
|
|
48
|
-
Coercing to a declared `string` reuses the text the document wrote, not the number it
|
|
49
|
-
parsed to, so a leading zero survives: `{ id: 01234 }` becomes `"01234"`.
|
|
50
|
-
`null` and structures (`{}` / `[]`) are never stringified - `"null"` and
|
|
51
|
-
`"[object Object]"` are not what a `type: 'string'` declaration asks for.
|
|
52
|
-
|
|
53
|
-
***
|
|
54
|
-
|
|
55
|
-
### keyMatching?
|
|
56
|
-
|
|
57
|
-
> `optional` **keyMatching?**: `boolean` \| ((`key`, `properties`) => `string` \| `undefined`)
|
|
58
|
-
|
|
59
|
-
Defined in: [types.ts:21](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/types.ts#L21)
|
|
60
|
-
|
|
61
|
-
Fuzzy matching of property keys against the Schema's declared `properties`.
|
|
62
|
-
|
|
63
|
-
- `false` (default): Only an exactly declared key counts as a match.
|
|
64
|
-
- `true`: Case-insensitive and separator-insensitive matching, so `user_name`,
|
|
65
|
-
`userName`, `USER-NAME` and `user name` all resolve to the declared key.
|
|
66
|
-
- `(key: string, properties: string[]) => string | undefined`: Custom mapping logic;
|
|
67
|
-
it receives the incoming key and the declared names, and returns the name to use
|
|
68
|
-
(or `undefined` to fall back to the incoming key).
|
|
69
|
-
|
|
70
|
-
A match renames the key to the Schema's name in the output and makes nested Schema
|
|
71
|
-
lookups (and therefore type coercion) work for it.
|
|
72
|
-
It also decides where greedy capture stops: with `true` a separator or case variant of a
|
|
73
|
-
declared key (`user_name:` for `userName`) ends the value in front of it.
|
|
74
|
-
When you reuse a `SchemaWalker`, pass this option to `SchemaWalker.create()`.
|
|
75
|
-
|
|
76
|
-
***
|
|
77
|
-
|
|
78
|
-
### stringCaptureStrategy?
|
|
79
|
-
|
|
80
|
-
> `optional` **stringCaptureStrategy?**: `"conservative"` \| `"aggressive"`
|
|
81
|
-
|
|
82
|
-
Defined in: [types.ts:40](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/types.ts#L40)
|
|
83
|
-
|
|
84
|
-
Strategy for greedy string capture when the strict JSON path failed for a value.
|
|
85
|
-
|
|
86
|
-
- `'aggressive'` (default): When the Schema declares property names for the current
|
|
87
|
-
level, the captured value only ends at one of those declared keys. Text that merely
|
|
88
|
-
looks like `"some words":` stays inside the value.
|
|
89
|
-
- `'conservative'`: Any text matching the `"key":` / `key:` pattern ends the value,
|
|
90
|
-
which keeps the structure of the document at the cost of cutting values short.
|
|
91
|
-
|
|
92
|
-
A whitespace-containing key the Schema declares is a breakpoint in both strategies even
|
|
93
|
-
when the document left it unquoted (`{ Capital: Berlin, Official language: Italian }`),
|
|
94
|
-
because it can only be recognised from the declaration.
|
|
95
|
-
Neither strategy affects values that were parsed by the strict JSON path; the
|
|
96
|
-
boundary rules described in the README apply to those unchanged.
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
[**@isdk/json-repair**](../README.md)
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
[@isdk/json-repair](../globals.md) / JSONPath
|
|
6
|
-
|
|
7
|
-
# Type Alias: JSONPath
|
|
8
|
-
|
|
9
|
-
> **JSONPath** = (`string` \| `number`)[]
|
|
10
|
-
|
|
11
|
-
Defined in: [types.ts:77](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/types.ts#L77)
|
|
12
|
-
|
|
13
|
-
Represents the current path in a JSON structure.
|
|
14
|
-
A list of strings (keys) and numbers (indices).
|
package/eslint.config.mjs
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import antfu from '@antfu/eslint-config'
|
|
2
|
-
import tsdoc from 'eslint-plugin-tsdoc'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* ESLint 只负责「正确性」,代码风格交给 prettier(见 package.json 的 format 脚本与 .prettierrc)。
|
|
6
|
-
*
|
|
7
|
-
* 这里沿用 ../ai-tool 的做法:用 @antfu 的配置作为底座,但整体关闭风格类与教条类插件层,
|
|
8
|
-
* 只保留指向真实 bug 的核心规则。想重新启用某一层,把对应的 `false` 改成 `true` 即可。
|
|
9
|
-
*/
|
|
10
|
-
const config = await antfu(
|
|
11
|
-
{
|
|
12
|
-
gitignore: true,
|
|
13
|
-
ignores: ['web/**', 'docs/**', 'dist/**', '.cache/**'],
|
|
14
|
-
|
|
15
|
-
// 风格类规则全部交给 prettier
|
|
16
|
-
stylistic: false,
|
|
17
|
-
// 以下插件层噪音太大,先整体关闭
|
|
18
|
-
regexp: false,
|
|
19
|
-
unicorn: false,
|
|
20
|
-
test: false,
|
|
21
|
-
imports: false,
|
|
22
|
-
jsonc: false,
|
|
23
|
-
yaml: false,
|
|
24
|
-
toml: false,
|
|
25
|
-
markdown: false,
|
|
26
|
-
},
|
|
27
|
-
{
|
|
28
|
-
plugins: { tsdoc },
|
|
29
|
-
rules: {
|
|
30
|
-
'tsdoc/syntax': 'off',
|
|
31
|
-
'antfu/no-top-level-await': 'off', // 测试文件里 top-level await 是正常写法
|
|
32
|
-
|
|
33
|
-
// 偏好类:不产生真实 bug,也不制造 diff
|
|
34
|
-
'prefer-const': 'off',
|
|
35
|
-
'prefer-template': 'off',
|
|
36
|
-
'object-shorthand': 'off',
|
|
37
|
-
'no-cond-assign': 'off', // 沿用旧配置:`while ((m = re.exec(s)))` 是惯用写法
|
|
38
|
-
'no-lone-blocks': 'off', // `case 'x': { … }; break;` 的花括号是刻意的
|
|
39
|
-
'unused-imports/no-unused-vars': 'off', // 未使用的函数参数多是签名占位
|
|
40
|
-
|
|
41
|
-
// TS 风格类
|
|
42
|
-
'ts/consistent-type-imports': 'off',
|
|
43
|
-
'ts/consistent-type-definitions': 'off',
|
|
44
|
-
'ts/no-use-before-define': 'off',
|
|
45
|
-
'ts/ban-ts-comment': 'off',
|
|
46
|
-
'ts/no-unused-expressions': 'off',
|
|
47
|
-
'ts/no-explicit-any': 'off', // 动态入参/出参本来就无静态类型(见 README 的 any 说明)
|
|
48
|
-
},
|
|
49
|
-
}
|
|
50
|
-
)
|
|
51
|
-
|
|
52
|
-
// ESLint 9 的 rules 通配符(如 `perfectionist/*`)只在 global config 生效,这里手动关掉整组噪音规则
|
|
53
|
-
const NOISE_GROUPS = ['perfectionist/', 'jsdoc/', 'node/', 'import/']
|
|
54
|
-
for (const item of config) {
|
|
55
|
-
if (!item.rules) {
|
|
56
|
-
continue
|
|
57
|
-
}
|
|
58
|
-
for (const name of Object.keys(item.rules)) {
|
|
59
|
-
if (NOISE_GROUPS.some((group) => name.startsWith(group))) {
|
|
60
|
-
item.rules[name] = 'off'
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export default config
|
package/setupVitest.mjs
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
// 这个 fetchMock 没有传递abort 中的 reason!
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
import createFetchMock from 'vitest-fetch-mock';
|
|
5
|
-
import { vi } from 'vitest';
|
|
6
|
-
// import './fetch-polyfill.mjs';
|
|
7
|
-
|
|
8
|
-
const fetchMocker = createFetchMock(vi);
|
|
9
|
-
|
|
10
|
-
// sets globalThis.fetch and globalThis.fetchMock to our mocked version
|
|
11
|
-
fetchMocker.enableMocks();
|
|
12
|
-
|
|
13
|
-
// changes default behavior of fetchMock to use the real 'fetch' implementation and not mock responses
|
|
14
|
-
fetchMocker.dontMock();
|
|
15
|
-
*/
|
package/tsconfig.json
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2022",
|
|
4
|
-
"module": "NodeNext",
|
|
5
|
-
"moduleResolution": "NodeNext",
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"strict": true,
|
|
8
|
-
"esModuleInterop": true,
|
|
9
|
-
"skipLibCheck": true,
|
|
10
|
-
"forceConsistentCasingInFileNames": true,
|
|
11
|
-
|
|
12
|
-
// 这份 tsconfig 只给编辑器 / 类型检查用(构建走 tsup + tsconfig.release.json),
|
|
13
|
-
// 所以这里 noEmit,并且不要把 outDir 之类的产物配置放进来。
|
|
14
|
-
"noEmit": true
|
|
15
|
-
},
|
|
16
|
-
// 开发/编辑器视角:测试文件也要在 program 里,否则 VS Code 会把它们
|
|
17
|
-
// 当成 invisible file 放进 inferred project,这份配置对它们无效。
|
|
18
|
-
"include": ["src/**/*", "test/**/*"],
|
|
19
|
-
"exclude": ["node_modules", "dist", "web", "docs"]
|
|
20
|
-
}
|
package/tsconfig.release.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
// 发包专用:tsup --dts / typedoc 用这份配置,排除测试文件。
|
|
3
|
-
// 编辑器与类型检查请用根目录的 tsconfig.json。
|
|
4
|
-
"extends": "./tsconfig.json",
|
|
5
|
-
"compilerOptions": {
|
|
6
|
-
// tsup --dts / typedoc 需要真正产出 .d.ts
|
|
7
|
-
"noEmit": false,
|
|
8
|
-
"declaration": true,
|
|
9
|
-
"outDir": "./dist"
|
|
10
|
-
},
|
|
11
|
-
"exclude": [
|
|
12
|
-
"node_modules",
|
|
13
|
-
"dist",
|
|
14
|
-
"web",
|
|
15
|
-
"docs",
|
|
16
|
-
"**/*.test.ts",
|
|
17
|
-
"**/*.spec.ts",
|
|
18
|
-
"**/*.test.js",
|
|
19
|
-
"**/*.spec.js"
|
|
20
|
-
]
|
|
21
|
-
}
|
package/tsup.config.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from 'tsup'
|
|
2
|
-
|
|
3
|
-
export default defineConfig({
|
|
4
|
-
// 发包用 release 配置(排除测试文件);
|
|
5
|
-
// 根目录 tsconfig.json 是编辑器/类型检查用的(含测试文件)。
|
|
6
|
-
tsconfig: 'tsconfig.release.json',
|
|
7
|
-
entry: ['src/index.ts'],
|
|
8
|
-
format: ['cjs', 'esm'],
|
|
9
|
-
// Inject cjs and esm shims:https://tsup.egoist.dev/#inject-cjs-and-esm-shims
|
|
10
|
-
shims: true,
|
|
11
|
-
// splitting: true,
|
|
12
|
-
// sourcemap: true,
|
|
13
|
-
// clean: true,
|
|
14
|
-
minify: 'terser',
|
|
15
|
-
terserOptions: {
|
|
16
|
-
// compress: {
|
|
17
|
-
// drop_console: true,
|
|
18
|
-
// drop_debugger: true,
|
|
19
|
-
// },
|
|
20
|
-
// https://terser.org/docs/options/#mangle-options
|
|
21
|
-
mangle: {
|
|
22
|
-
properties: {
|
|
23
|
-
regex: /^_[$]/,
|
|
24
|
-
// "undeclared": true, // Mangle those names when they are accessed as properties of known top level variables but their declarations are never found in input code.
|
|
25
|
-
},
|
|
26
|
-
toplevel: true,
|
|
27
|
-
reserved: [
|
|
28
|
-
// # expected names in web-extension content
|
|
29
|
-
'WeakSet',
|
|
30
|
-
'Set',
|
|
31
|
-
// # expected names in 3rd-party extensions' contents
|
|
32
|
-
'requestIdleCallback',
|
|
33
|
-
// # content global names:
|
|
34
|
-
'browser',
|
|
35
|
-
],
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
})
|
package/typedoc.config.cjs
DELETED
package/vite.config.mjs
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from 'vite'
|
|
2
|
-
import tsconfigPaths from 'vite-tsconfig-paths'
|
|
3
|
-
|
|
4
|
-
export default defineConfig({
|
|
5
|
-
plugins: [
|
|
6
|
-
tsconfigPaths({
|
|
7
|
-
projects: ['./tsconfig.json'],
|
|
8
|
-
}),
|
|
9
|
-
],
|
|
10
|
-
test: {
|
|
11
|
-
include: [
|
|
12
|
-
'src/**/*.spec.ts',
|
|
13
|
-
'src/**/*.test.ts',
|
|
14
|
-
'test/*.spec.ts',
|
|
15
|
-
'test/*.test.ts',
|
|
16
|
-
],
|
|
17
|
-
testTimeout: 80000,
|
|
18
|
-
globals: true,
|
|
19
|
-
setupFiles: ['./setupVitest.mjs'],
|
|
20
|
-
},
|
|
21
|
-
})
|