@helpers4/json 0.0.0-stage → 3.1.2-alpha.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/LICENSE.md +160 -0
- package/README.md +96 -2
- package/lib/index.d.ts +463 -0
- package/lib/index.js +928 -0
- package/lib/index.js.map +1 -0
- package/llms.txt +699 -0
- package/meta/api.json +662 -0
- package/meta/category.json +6 -0
- package/meta/examples.json +185 -0
- package/meta/licenses.json +4 -0
- package/package.json +53 -4
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"category": "json",
|
|
3
|
+
"label": "JSON",
|
|
4
|
+
"smallDescription": "JSON parsing and serialization, with its common variants",
|
|
5
|
+
"description": "Parse and stringify JSON and its variants — JSONC (HuJSON, JWCC), JSON5, NDJSON (JSON Lines), JSON text sequences (RFC 7464) and canonical JSON (RFC 8785). Every `parse*` throws on invalid input and has a `safe*` twin that returns `null` or a fallback instead."
|
|
6
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
{
|
|
2
|
+
"category": "json",
|
|
3
|
+
"functions": [
|
|
4
|
+
{
|
|
5
|
+
"name": "parse",
|
|
6
|
+
"examples": [
|
|
7
|
+
{
|
|
8
|
+
"title": "Parse a config file with comments",
|
|
9
|
+
"description": "Reads JSONC — the tsconfig.json / settings.json dialect — as well as strict JSON.",
|
|
10
|
+
"code": "parse(`{\n // where the build goes\n \"outDir\": \"dist\",\n}`)\n// => { outDir: 'dist' }"
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
"title": "Invalid input throws",
|
|
14
|
+
"description": "The SyntaxError names the line and column of the problem.",
|
|
15
|
+
"code": "parse('{ my-key: 1 }')\n// throws SyntaxError: a key with a dash needs quotes"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"title": "Parse JSON5",
|
|
19
|
+
"description": "Unquoted keys, single quotes, hexadecimal and leading-dot numbers, trailing commas.",
|
|
20
|
+
"code": "parse(\"{ name: 'app', retries: 0x3, ratio: .5, }\")\n// => { name: 'app', retries: 3, ratio: 0.5 }"
|
|
21
|
+
}
|
|
22
|
+
]
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"name": "parseLines",
|
|
26
|
+
"examples": [
|
|
27
|
+
{
|
|
28
|
+
"title": "Read NDJSON / JSON Lines",
|
|
29
|
+
"description": "One JSON value per line; a trailing line break is optional.",
|
|
30
|
+
"code": "parseLines('{\"id\":1}\\n{\"id\":2}\\n')\n// => [{ id: 1 }, { id: 2 }]"
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"title": "Skip a truncated last line",
|
|
34
|
+
"description": "With invalidLines: \"skip\", a line cut by a crash is dropped instead of failing the whole parse.",
|
|
35
|
+
"code": "parseLines('{\"id\":1}\\n{\"id\":', { invalidLines: 'skip' })\n// => [{ id: 1 }]"
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"title": "Fail on an invalid line by default",
|
|
39
|
+
"description": "The SyntaxError names the line.",
|
|
40
|
+
"code": "parseLines('1\\n{\\n3')\n// throws SyntaxError: parseLines: line 2: ..."
|
|
41
|
+
}
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"name": "parseSequence",
|
|
46
|
+
"examples": [
|
|
47
|
+
{
|
|
48
|
+
"title": "Read a JSON text sequence",
|
|
49
|
+
"description": "Each record starts with the record separator U+001E, as jq --seq writes it.",
|
|
50
|
+
"code": "parseSequence('\\u001E{\"id\":1}\\n\\u001E{\"id\":2}\\n')\n// => [{ id: 1 }, { id: 2 }]"
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
"title": "Recover from a truncated log",
|
|
54
|
+
"description": "RFC 7464 recommends skipping invalid records; ask for it with invalidRecords: \"skip\".",
|
|
55
|
+
"code": "parseSequence('\\u001E{\"id\":1}\\n\\u001E{\"id', { invalidRecords: 'skip' })\n// => [{ id: 1 }]"
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"name": "parseStrict",
|
|
61
|
+
"examples": [
|
|
62
|
+
{
|
|
63
|
+
"title": "Parse strict JSON",
|
|
64
|
+
"description": "Exactly what JSON.parse accepts, typed with a generic.",
|
|
65
|
+
"code": "parseStrict<{ id: number }>('{\"id\":1}')\n// => { id: 1 }"
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
"title": "Reject anything that is not strict JSON",
|
|
69
|
+
"description": "Comments and trailing commas throw a SyntaxError: use it to validate an API payload.",
|
|
70
|
+
"code": "parseStrict('{\"id\":1,}')\n// throws SyntaxError"
|
|
71
|
+
}
|
|
72
|
+
]
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"name": "safeParse",
|
|
76
|
+
"examples": [
|
|
77
|
+
{
|
|
78
|
+
"title": "Read JSONC or JSON5 without throwing",
|
|
79
|
+
"description": "Same parsing as parse, but returns null on invalid input.",
|
|
80
|
+
"code": "safeParse('{\"a\": 1, /* note */}') // => { a: 1 }\nsafeParse('{a: }') // => null"
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
"title": "Fall back to defaults",
|
|
84
|
+
"description": "Pass a fallback to get a usable value even when the file is broken.",
|
|
85
|
+
"code": "safeParse('{ broken', {})\n// => {}"
|
|
86
|
+
}
|
|
87
|
+
]
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
"name": "safeParseLines",
|
|
91
|
+
"examples": [
|
|
92
|
+
{
|
|
93
|
+
"title": "Read NDJSON without throwing",
|
|
94
|
+
"description": "Returns null if any line is invalid.",
|
|
95
|
+
"code": "safeParseLines('1\\n2\\n') // => [1, 2]\nsafeParseLines('1\\n{') // => null"
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
"title": "Options without a fallback",
|
|
99
|
+
"description": "Pass null as the fallback to give options.",
|
|
100
|
+
"code": "safeParseLines('1\\n\\n2', null, { blankLines: 'fail' })\n// => null (JSON Lines forbids blank lines)"
|
|
101
|
+
}
|
|
102
|
+
]
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
"name": "safeParseSequence",
|
|
106
|
+
"examples": [
|
|
107
|
+
{
|
|
108
|
+
"title": "Read a sequence without throwing",
|
|
109
|
+
"description": "Returns null if any record is invalid.",
|
|
110
|
+
"code": "safeParseSequence('\\u001E1\\n') // => [1]\nsafeParseSequence('\\u001E{\\n') // => null"
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
"title": "Provide a fallback",
|
|
114
|
+
"description": "The fallback is returned on invalid input.",
|
|
115
|
+
"code": "safeParseSequence('\\u001E{\\n', [])\n// => []"
|
|
116
|
+
}
|
|
117
|
+
]
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
"name": "safeParseStrict",
|
|
121
|
+
"examples": [
|
|
122
|
+
{
|
|
123
|
+
"title": "Parse without throwing",
|
|
124
|
+
"description": "Returns null instead of throwing on invalid input.",
|
|
125
|
+
"code": "safeParseStrict('{\"id\":1}') // => { id: 1 }\nsafeParseStrict('{\"id\":1,}') // => null"
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
"title": "Provide a fallback",
|
|
129
|
+
"description": "The fallback is returned on invalid input, so the result is never null.",
|
|
130
|
+
"code": "safeParseStrict('oops', { id: 0 })\n// => { id: 0 }"
|
|
131
|
+
}
|
|
132
|
+
]
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
"name": "stringify",
|
|
136
|
+
"examples": [
|
|
137
|
+
{
|
|
138
|
+
"title": "Pretty-print by default",
|
|
139
|
+
"description": "Indented with 2 spaces, like a hand-written config file.",
|
|
140
|
+
"code": "stringify({ name: 'app', tags: ['a'] })\n// => '{\\n \"name\": \"app\",\\n \"tags\": [\\n \"a\"\\n ]\\n}'"
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
"title": "Compact output",
|
|
144
|
+
"description": "indent: 0 gives a single line, like JSON.stringify(value).",
|
|
145
|
+
"code": "stringify({ b: 1, a: 2 }, { indent: 0 })\n// => '{\"b\":1,\"a\":2}'"
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
"title": "Canonical JSON (RFC 8785)",
|
|
149
|
+
"description": "Sorted keys and no whitespace: equal values always give the same bytes, ready to hash or sign.",
|
|
150
|
+
"code": "stringify({ b: 1, a: 2 }, 'canonical')\n// => '{\"a\":2,\"b\":1}'"
|
|
151
|
+
}
|
|
152
|
+
]
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
"name": "stringifyLines",
|
|
156
|
+
"examples": [
|
|
157
|
+
{
|
|
158
|
+
"title": "Write NDJSON / JSON Lines",
|
|
159
|
+
"description": "One compact value per line, each followed by a line break.",
|
|
160
|
+
"code": "stringifyLines([{ id: 1 }, { id: 2 }])\n// => '{\"id\":1}\\n{\"id\":2}\\n'"
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
"title": "Canonical lines",
|
|
164
|
+
"description": "Each line in canonical form, so a log can be hashed or diffed line by line.",
|
|
165
|
+
"code": "stringifyLines([{ b: 1, a: 2 }], 'canonical')\n// => '{\"a\":2,\"b\":1}\\n'"
|
|
166
|
+
}
|
|
167
|
+
]
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
"name": "stringifySequence",
|
|
171
|
+
"examples": [
|
|
172
|
+
{
|
|
173
|
+
"title": "Write a JSON text sequence",
|
|
174
|
+
"description": "Each value is framed as RS, JSON text, line feed (RFC 7464).",
|
|
175
|
+
"code": "stringifySequence([{ id: 1 }, 2])\n// => '\\u001E{\"id\":1}\\n\\u001E2\\n'"
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
"title": "Canonical records",
|
|
179
|
+
"description": "Each record in canonical form.",
|
|
180
|
+
"code": "stringifySequence([{ b: 1, a: 2 }], 'canonical')\n// => '\\u001E{\"a\":2,\"b\":1}\\n'"
|
|
181
|
+
}
|
|
182
|
+
]
|
|
183
|
+
}
|
|
184
|
+
]
|
|
185
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helpers4/json",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "3.1.2-alpha.0",
|
|
4
|
+
"description": "A set of helpers in TS/JS, compatible with tree-shaking, for json.",
|
|
5
|
+
"author": "baxyz <craft@baxyz.dev>",
|
|
6
|
+
"license": "LGPL-3.0",
|
|
7
|
+
"homepage": "https://helpers4.dev/typescript/categories/json/",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/helpers4/typescript.git"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"main": "lib/index.js",
|
|
15
|
+
"types": "lib/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./lib/index.d.ts",
|
|
19
|
+
"import": "./lib/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./meta/api.json": "./meta/api.json",
|
|
22
|
+
"./meta/examples.json": "./meta/examples.json",
|
|
23
|
+
"./meta/licenses.json": "./meta/licenses.json",
|
|
24
|
+
"./llms.txt": "./llms.txt",
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"helpers",
|
|
29
|
+
"json",
|
|
30
|
+
"parse",
|
|
31
|
+
"parseLines",
|
|
32
|
+
"parseSequence",
|
|
33
|
+
"parseStrict",
|
|
34
|
+
"safeParse",
|
|
35
|
+
"safeParseLines",
|
|
36
|
+
"safeParseSequence",
|
|
37
|
+
"safeParseStrict",
|
|
38
|
+
"stringify",
|
|
39
|
+
"stringifyLines",
|
|
40
|
+
"stringifySequence"
|
|
41
|
+
],
|
|
42
|
+
"files": [
|
|
43
|
+
"lib/index.js",
|
|
44
|
+
"lib/index.d.ts",
|
|
45
|
+
"lib/index.js.map",
|
|
46
|
+
"meta/",
|
|
47
|
+
"llms.txt",
|
|
48
|
+
"LICENSE.md",
|
|
49
|
+
"package.json",
|
|
50
|
+
"README.md"
|
|
51
|
+
],
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=26.0.0"
|
|
54
|
+
}
|
|
55
|
+
}
|