@sopkit/json 1.0.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/README.md +51 -0
- package/dist/index.cjs +82 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +48 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/package.json +32 -0
- package/src/index.ts +100 -0
- package/tsconfig.json +14 -0
- package/tsup.config.ts +11 -0
package/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# `@sopkit/json`
|
|
2
|
+
|
|
3
|
+
Premium, lightweight JSON formatter, validator, and minifier for both Browser and Node.js. Part of the SopKit utility ecosystem.
|
|
4
|
+
|
|
5
|
+
## Online Interactive Tool
|
|
6
|
+
You can use the browser-based interactive version of this tool at [SopKit JSON Formatter](https://sopkit.github.io/json-formatter/).
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
- Robust JSON validation with detailed syntax error reporting (including line and column numbers).
|
|
10
|
+
- Beautiful JSON formatting (custom spaces or tabs).
|
|
11
|
+
- Fast JSON minification (collapses whitespaces).
|
|
12
|
+
- Zero dependencies.
|
|
13
|
+
- ESM and CommonJS support.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
```bash
|
|
17
|
+
npm install @sopkit/json
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
### ESM
|
|
23
|
+
```typescript
|
|
24
|
+
import { validate, format, minify } from "@sopkit/json";
|
|
25
|
+
|
|
26
|
+
const rawJson = '{"name": "SopKit", "active": true}';
|
|
27
|
+
|
|
28
|
+
// Validation
|
|
29
|
+
const res = validate(rawJson);
|
|
30
|
+
if (res.valid) {
|
|
31
|
+
console.log("Valid!", res.data);
|
|
32
|
+
} else {
|
|
33
|
+
console.log(`Failed at line ${res.line}, col ${res.column}: ${res.error}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Formatting
|
|
37
|
+
const pretty = format(rawJson, { space: 4 });
|
|
38
|
+
|
|
39
|
+
// Minification
|
|
40
|
+
const mini = minify(pretty); // '{"name":"SopKit","active":true}'
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### CommonJS
|
|
44
|
+
```javascript
|
|
45
|
+
const { validate, format } = require("@sopkit/json");
|
|
46
|
+
|
|
47
|
+
const pretty = format('{"status":"ok"}');
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## License
|
|
51
|
+
MIT © [SopKit](https://sopkit.github.io/)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
format: () => format,
|
|
24
|
+
minify: () => minify,
|
|
25
|
+
validate: () => validate
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
function validate(jsonStr) {
|
|
29
|
+
if (typeof jsonStr !== "string") {
|
|
30
|
+
return { valid: false, error: "Input must be a string" };
|
|
31
|
+
}
|
|
32
|
+
if (jsonStr.trim() === "") {
|
|
33
|
+
return { valid: false, error: "Input string is empty" };
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
const data = JSON.parse(jsonStr);
|
|
37
|
+
return { valid: true, data };
|
|
38
|
+
} catch (e) {
|
|
39
|
+
const message = e.message;
|
|
40
|
+
let line = void 0;
|
|
41
|
+
let column = void 0;
|
|
42
|
+
const lineMatch = message.match(/line\s+(\d+)/i);
|
|
43
|
+
const colMatch = message.match(/column\s+(\d+)/i);
|
|
44
|
+
const posMatch = message.match(/position\s+(\d+)/i);
|
|
45
|
+
if (lineMatch) line = parseInt(lineMatch[1], 10);
|
|
46
|
+
if (colMatch) column = parseInt(colMatch[1], 10);
|
|
47
|
+
if (posMatch && !line && !column) {
|
|
48
|
+
const pos = parseInt(posMatch[1], 10);
|
|
49
|
+
const linesBefore = jsonStr.slice(0, pos).split("\n");
|
|
50
|
+
line = linesBefore.length;
|
|
51
|
+
column = linesBefore[linesBefore.length - 1].length + 1;
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
valid: false,
|
|
55
|
+
error: message,
|
|
56
|
+
line,
|
|
57
|
+
column
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function format(jsonStr, options = {}) {
|
|
62
|
+
const { space = 2 } = options;
|
|
63
|
+
const validation = validate(jsonStr);
|
|
64
|
+
if (!validation.valid) {
|
|
65
|
+
throw new Error(`Invalid JSON: ${validation.error}`);
|
|
66
|
+
}
|
|
67
|
+
return JSON.stringify(validation.data, null, space);
|
|
68
|
+
}
|
|
69
|
+
function minify(jsonStr) {
|
|
70
|
+
const validation = validate(jsonStr);
|
|
71
|
+
if (!validation.valid) {
|
|
72
|
+
throw new Error(`Invalid JSON: ${validation.error}`);
|
|
73
|
+
}
|
|
74
|
+
return JSON.stringify(validation.data);
|
|
75
|
+
}
|
|
76
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
77
|
+
0 && (module.exports = {
|
|
78
|
+
format,
|
|
79
|
+
minify,
|
|
80
|
+
validate
|
|
81
|
+
});
|
|
82
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * SopKit JSON Utilities\n * Premium, zero-dependency JSON utility for parsing, validating, formatting, and minification.\n * Link: https://sopkit.github.io/json-formatter/\n */\n\nexport interface FormatOptions {\n /**\n * Spacing size or indent character. Defaults to 2.\n */\n space?: number | string;\n}\n\nexport interface ValidationResult {\n /**\n * Whether the input is valid JSON.\n */\n valid: boolean;\n /**\n * The parsed JavaScript object/array/value if valid.\n */\n data?: any;\n /**\n * Error message if validation failed.\n */\n error?: string;\n /**\n * Line number of the syntax error if validation failed.\n */\n line?: number;\n /**\n * Column number of the syntax error if validation failed.\n */\n column?: number;\n}\n\n/**\n * Validates a string to check if it's a valid JSON representation.\n * Returns a detailed ValidationResult object.\n */\nexport function validate(jsonStr: string): ValidationResult {\n if (typeof jsonStr !== \"string\") {\n return { valid: false, error: \"Input must be a string\" };\n }\n if (jsonStr.trim() === \"\") {\n return { valid: false, error: \"Input string is empty\" };\n }\n try {\n const data = JSON.parse(jsonStr);\n return { valid: true, data };\n } catch (e: any) {\n const message = e.message;\n let line = undefined;\n let column = undefined;\n\n const lineMatch = message.match(/line\\s+(\\d+)/i);\n const colMatch = message.match(/column\\s+(\\d+)/i);\n const posMatch = message.match(/position\\s+(\\d+)/i);\n\n if (lineMatch) line = parseInt(lineMatch[1], 10);\n if (colMatch) column = parseInt(colMatch[1], 10);\n\n if (posMatch && !line && !column) {\n const pos = parseInt(posMatch[1], 10);\n const linesBefore = jsonStr.slice(0, pos).split(\"\\n\");\n line = linesBefore.length;\n column = linesBefore[linesBefore.length - 1].length + 1;\n }\n\n return {\n valid: false,\n error: message,\n line,\n column\n };\n }\n}\n\n/**\n * Formats (beautifies) a JSON string with optional custom indentation.\n */\nexport function format(jsonStr: string, options: FormatOptions = {}): string {\n const { space = 2 } = options;\n const validation = validate(jsonStr);\n if (!validation.valid) {\n throw new Error(`Invalid JSON: ${validation.error}`);\n }\n return JSON.stringify(validation.data, null, space);\n}\n\n/**\n * Minifies a JSON string, stripping all whitespace and unnecessary formatting.\n */\nexport function minify(jsonStr: string): string {\n const validation = validate(jsonStr);\n if (!validation.valid) {\n throw new Error(`Invalid JSON: ${validation.error}`);\n }\n return JSON.stringify(validation.data);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAwCO,SAAS,SAAS,SAAmC;AAC1D,MAAI,OAAO,YAAY,UAAU;AAC/B,WAAO,EAAE,OAAO,OAAO,OAAO,yBAAyB;AAAA,EACzD;AACA,MAAI,QAAQ,KAAK,MAAM,IAAI;AACzB,WAAO,EAAE,OAAO,OAAO,OAAO,wBAAwB;AAAA,EACxD;AACA,MAAI;AACF,UAAM,OAAO,KAAK,MAAM,OAAO;AAC/B,WAAO,EAAE,OAAO,MAAM,KAAK;AAAA,EAC7B,SAAS,GAAQ;AACf,UAAM,UAAU,EAAE;AAClB,QAAI,OAAO;AACX,QAAI,SAAS;AAEb,UAAM,YAAY,QAAQ,MAAM,eAAe;AAC/C,UAAM,WAAW,QAAQ,MAAM,iBAAiB;AAChD,UAAM,WAAW,QAAQ,MAAM,mBAAmB;AAElD,QAAI,UAAW,QAAO,SAAS,UAAU,CAAC,GAAG,EAAE;AAC/C,QAAI,SAAU,UAAS,SAAS,SAAS,CAAC,GAAG,EAAE;AAE/C,QAAI,YAAY,CAAC,QAAQ,CAAC,QAAQ;AAChC,YAAM,MAAM,SAAS,SAAS,CAAC,GAAG,EAAE;AACpC,YAAM,cAAc,QAAQ,MAAM,GAAG,GAAG,EAAE,MAAM,IAAI;AACpD,aAAO,YAAY;AACnB,eAAS,YAAY,YAAY,SAAS,CAAC,EAAE,SAAS;AAAA,IACxD;AAEA,WAAO;AAAA,MACL,OAAO;AAAA,MACP,OAAO;AAAA,MACP;AAAA,MACA;AAAA,IACF;AAAA,EACF;AACF;AAKO,SAAS,OAAO,SAAiB,UAAyB,CAAC,GAAW;AAC3E,QAAM,EAAE,QAAQ,EAAE,IAAI;AACtB,QAAM,aAAa,SAAS,OAAO;AACnC,MAAI,CAAC,WAAW,OAAO;AACrB,UAAM,IAAI,MAAM,iBAAiB,WAAW,KAAK,EAAE;AAAA,EACrD;AACA,SAAO,KAAK,UAAU,WAAW,MAAM,MAAM,KAAK;AACpD;AAKO,SAAS,OAAO,SAAyB;AAC9C,QAAM,aAAa,SAAS,OAAO;AACnC,MAAI,CAAC,WAAW,OAAO;AACrB,UAAM,IAAI,MAAM,iBAAiB,WAAW,KAAK,EAAE;AAAA,EACrD;AACA,SAAO,KAAK,UAAU,WAAW,IAAI;AACvC;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SopKit JSON Utilities
|
|
3
|
+
* Premium, zero-dependency JSON utility for parsing, validating, formatting, and minification.
|
|
4
|
+
* Link: https://sopkit.github.io/json-formatter/
|
|
5
|
+
*/
|
|
6
|
+
interface FormatOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Spacing size or indent character. Defaults to 2.
|
|
9
|
+
*/
|
|
10
|
+
space?: number | string;
|
|
11
|
+
}
|
|
12
|
+
interface ValidationResult {
|
|
13
|
+
/**
|
|
14
|
+
* Whether the input is valid JSON.
|
|
15
|
+
*/
|
|
16
|
+
valid: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* The parsed JavaScript object/array/value if valid.
|
|
19
|
+
*/
|
|
20
|
+
data?: any;
|
|
21
|
+
/**
|
|
22
|
+
* Error message if validation failed.
|
|
23
|
+
*/
|
|
24
|
+
error?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Line number of the syntax error if validation failed.
|
|
27
|
+
*/
|
|
28
|
+
line?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Column number of the syntax error if validation failed.
|
|
31
|
+
*/
|
|
32
|
+
column?: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Validates a string to check if it's a valid JSON representation.
|
|
36
|
+
* Returns a detailed ValidationResult object.
|
|
37
|
+
*/
|
|
38
|
+
declare function validate(jsonStr: string): ValidationResult;
|
|
39
|
+
/**
|
|
40
|
+
* Formats (beautifies) a JSON string with optional custom indentation.
|
|
41
|
+
*/
|
|
42
|
+
declare function format(jsonStr: string, options?: FormatOptions): string;
|
|
43
|
+
/**
|
|
44
|
+
* Minifies a JSON string, stripping all whitespace and unnecessary formatting.
|
|
45
|
+
*/
|
|
46
|
+
declare function minify(jsonStr: string): string;
|
|
47
|
+
|
|
48
|
+
export { type FormatOptions, type ValidationResult, format, minify, validate };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SopKit JSON Utilities
|
|
3
|
+
* Premium, zero-dependency JSON utility for parsing, validating, formatting, and minification.
|
|
4
|
+
* Link: https://sopkit.github.io/json-formatter/
|
|
5
|
+
*/
|
|
6
|
+
interface FormatOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Spacing size or indent character. Defaults to 2.
|
|
9
|
+
*/
|
|
10
|
+
space?: number | string;
|
|
11
|
+
}
|
|
12
|
+
interface ValidationResult {
|
|
13
|
+
/**
|
|
14
|
+
* Whether the input is valid JSON.
|
|
15
|
+
*/
|
|
16
|
+
valid: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* The parsed JavaScript object/array/value if valid.
|
|
19
|
+
*/
|
|
20
|
+
data?: any;
|
|
21
|
+
/**
|
|
22
|
+
* Error message if validation failed.
|
|
23
|
+
*/
|
|
24
|
+
error?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Line number of the syntax error if validation failed.
|
|
27
|
+
*/
|
|
28
|
+
line?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Column number of the syntax error if validation failed.
|
|
31
|
+
*/
|
|
32
|
+
column?: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Validates a string to check if it's a valid JSON representation.
|
|
36
|
+
* Returns a detailed ValidationResult object.
|
|
37
|
+
*/
|
|
38
|
+
declare function validate(jsonStr: string): ValidationResult;
|
|
39
|
+
/**
|
|
40
|
+
* Formats (beautifies) a JSON string with optional custom indentation.
|
|
41
|
+
*/
|
|
42
|
+
declare function format(jsonStr: string, options?: FormatOptions): string;
|
|
43
|
+
/**
|
|
44
|
+
* Minifies a JSON string, stripping all whitespace and unnecessary formatting.
|
|
45
|
+
*/
|
|
46
|
+
declare function minify(jsonStr: string): string;
|
|
47
|
+
|
|
48
|
+
export { type FormatOptions, type ValidationResult, format, minify, validate };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function validate(jsonStr) {
|
|
3
|
+
if (typeof jsonStr !== "string") {
|
|
4
|
+
return { valid: false, error: "Input must be a string" };
|
|
5
|
+
}
|
|
6
|
+
if (jsonStr.trim() === "") {
|
|
7
|
+
return { valid: false, error: "Input string is empty" };
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
const data = JSON.parse(jsonStr);
|
|
11
|
+
return { valid: true, data };
|
|
12
|
+
} catch (e) {
|
|
13
|
+
const message = e.message;
|
|
14
|
+
let line = void 0;
|
|
15
|
+
let column = void 0;
|
|
16
|
+
const lineMatch = message.match(/line\s+(\d+)/i);
|
|
17
|
+
const colMatch = message.match(/column\s+(\d+)/i);
|
|
18
|
+
const posMatch = message.match(/position\s+(\d+)/i);
|
|
19
|
+
if (lineMatch) line = parseInt(lineMatch[1], 10);
|
|
20
|
+
if (colMatch) column = parseInt(colMatch[1], 10);
|
|
21
|
+
if (posMatch && !line && !column) {
|
|
22
|
+
const pos = parseInt(posMatch[1], 10);
|
|
23
|
+
const linesBefore = jsonStr.slice(0, pos).split("\n");
|
|
24
|
+
line = linesBefore.length;
|
|
25
|
+
column = linesBefore[linesBefore.length - 1].length + 1;
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
valid: false,
|
|
29
|
+
error: message,
|
|
30
|
+
line,
|
|
31
|
+
column
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function format(jsonStr, options = {}) {
|
|
36
|
+
const { space = 2 } = options;
|
|
37
|
+
const validation = validate(jsonStr);
|
|
38
|
+
if (!validation.valid) {
|
|
39
|
+
throw new Error(`Invalid JSON: ${validation.error}`);
|
|
40
|
+
}
|
|
41
|
+
return JSON.stringify(validation.data, null, space);
|
|
42
|
+
}
|
|
43
|
+
function minify(jsonStr) {
|
|
44
|
+
const validation = validate(jsonStr);
|
|
45
|
+
if (!validation.valid) {
|
|
46
|
+
throw new Error(`Invalid JSON: ${validation.error}`);
|
|
47
|
+
}
|
|
48
|
+
return JSON.stringify(validation.data);
|
|
49
|
+
}
|
|
50
|
+
export {
|
|
51
|
+
format,
|
|
52
|
+
minify,
|
|
53
|
+
validate
|
|
54
|
+
};
|
|
55
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * SopKit JSON Utilities\n * Premium, zero-dependency JSON utility for parsing, validating, formatting, and minification.\n * Link: https://sopkit.github.io/json-formatter/\n */\n\nexport interface FormatOptions {\n /**\n * Spacing size or indent character. Defaults to 2.\n */\n space?: number | string;\n}\n\nexport interface ValidationResult {\n /**\n * Whether the input is valid JSON.\n */\n valid: boolean;\n /**\n * The parsed JavaScript object/array/value if valid.\n */\n data?: any;\n /**\n * Error message if validation failed.\n */\n error?: string;\n /**\n * Line number of the syntax error if validation failed.\n */\n line?: number;\n /**\n * Column number of the syntax error if validation failed.\n */\n column?: number;\n}\n\n/**\n * Validates a string to check if it's a valid JSON representation.\n * Returns a detailed ValidationResult object.\n */\nexport function validate(jsonStr: string): ValidationResult {\n if (typeof jsonStr !== \"string\") {\n return { valid: false, error: \"Input must be a string\" };\n }\n if (jsonStr.trim() === \"\") {\n return { valid: false, error: \"Input string is empty\" };\n }\n try {\n const data = JSON.parse(jsonStr);\n return { valid: true, data };\n } catch (e: any) {\n const message = e.message;\n let line = undefined;\n let column = undefined;\n\n const lineMatch = message.match(/line\\s+(\\d+)/i);\n const colMatch = message.match(/column\\s+(\\d+)/i);\n const posMatch = message.match(/position\\s+(\\d+)/i);\n\n if (lineMatch) line = parseInt(lineMatch[1], 10);\n if (colMatch) column = parseInt(colMatch[1], 10);\n\n if (posMatch && !line && !column) {\n const pos = parseInt(posMatch[1], 10);\n const linesBefore = jsonStr.slice(0, pos).split(\"\\n\");\n line = linesBefore.length;\n column = linesBefore[linesBefore.length - 1].length + 1;\n }\n\n return {\n valid: false,\n error: message,\n line,\n column\n };\n }\n}\n\n/**\n * Formats (beautifies) a JSON string with optional custom indentation.\n */\nexport function format(jsonStr: string, options: FormatOptions = {}): string {\n const { space = 2 } = options;\n const validation = validate(jsonStr);\n if (!validation.valid) {\n throw new Error(`Invalid JSON: ${validation.error}`);\n }\n return JSON.stringify(validation.data, null, space);\n}\n\n/**\n * Minifies a JSON string, stripping all whitespace and unnecessary formatting.\n */\nexport function minify(jsonStr: string): string {\n const validation = validate(jsonStr);\n if (!validation.valid) {\n throw new Error(`Invalid JSON: ${validation.error}`);\n }\n return JSON.stringify(validation.data);\n}\n"],"mappings":";AAwCO,SAAS,SAAS,SAAmC;AAC1D,MAAI,OAAO,YAAY,UAAU;AAC/B,WAAO,EAAE,OAAO,OAAO,OAAO,yBAAyB;AAAA,EACzD;AACA,MAAI,QAAQ,KAAK,MAAM,IAAI;AACzB,WAAO,EAAE,OAAO,OAAO,OAAO,wBAAwB;AAAA,EACxD;AACA,MAAI;AACF,UAAM,OAAO,KAAK,MAAM,OAAO;AAC/B,WAAO,EAAE,OAAO,MAAM,KAAK;AAAA,EAC7B,SAAS,GAAQ;AACf,UAAM,UAAU,EAAE;AAClB,QAAI,OAAO;AACX,QAAI,SAAS;AAEb,UAAM,YAAY,QAAQ,MAAM,eAAe;AAC/C,UAAM,WAAW,QAAQ,MAAM,iBAAiB;AAChD,UAAM,WAAW,QAAQ,MAAM,mBAAmB;AAElD,QAAI,UAAW,QAAO,SAAS,UAAU,CAAC,GAAG,EAAE;AAC/C,QAAI,SAAU,UAAS,SAAS,SAAS,CAAC,GAAG,EAAE;AAE/C,QAAI,YAAY,CAAC,QAAQ,CAAC,QAAQ;AAChC,YAAM,MAAM,SAAS,SAAS,CAAC,GAAG,EAAE;AACpC,YAAM,cAAc,QAAQ,MAAM,GAAG,GAAG,EAAE,MAAM,IAAI;AACpD,aAAO,YAAY;AACnB,eAAS,YAAY,YAAY,SAAS,CAAC,EAAE,SAAS;AAAA,IACxD;AAEA,WAAO;AAAA,MACL,OAAO;AAAA,MACP,OAAO;AAAA,MACP;AAAA,MACA;AAAA,IACF;AAAA,EACF;AACF;AAKO,SAAS,OAAO,SAAiB,UAAyB,CAAC,GAAW;AAC3E,QAAM,EAAE,QAAQ,EAAE,IAAI;AACtB,QAAM,aAAa,SAAS,OAAO;AACnC,MAAI,CAAC,WAAW,OAAO;AACrB,UAAM,IAAI,MAAM,iBAAiB,WAAW,KAAK,EAAE;AAAA,EACrD;AACA,SAAO,KAAK,UAAU,WAAW,MAAM,MAAM,KAAK;AACpD;AAKO,SAAS,OAAO,SAAyB;AAC9C,QAAM,aAAa,SAAS,OAAO;AACnC,MAAI,CAAC,WAAW,OAAO;AACrB,UAAM,IAAI,MAAM,iBAAiB,WAAW,KAAK,EAAE;AAAA,EACrD;AACA,SAAO,KAAK,UAAU,WAAW,IAAI;AACvC;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sopkit/json",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Premium, lightweight JSON utility for browser and Node.js supporting formatting, minification, and strict syntax validation.",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"require": "./dist/index.cjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"sopkit",
|
|
21
|
+
"json",
|
|
22
|
+
"format",
|
|
23
|
+
"beautify",
|
|
24
|
+
"minify",
|
|
25
|
+
"validate",
|
|
26
|
+
"parse",
|
|
27
|
+
"lint"
|
|
28
|
+
],
|
|
29
|
+
"author": "SopKit",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"homepage": "https://sopkit.github.io/json-formatter/"
|
|
32
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SopKit JSON Utilities
|
|
3
|
+
* Premium, zero-dependency JSON utility for parsing, validating, formatting, and minification.
|
|
4
|
+
* Link: https://sopkit.github.io/json-formatter/
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export interface FormatOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Spacing size or indent character. Defaults to 2.
|
|
10
|
+
*/
|
|
11
|
+
space?: number | string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ValidationResult {
|
|
15
|
+
/**
|
|
16
|
+
* Whether the input is valid JSON.
|
|
17
|
+
*/
|
|
18
|
+
valid: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* The parsed JavaScript object/array/value if valid.
|
|
21
|
+
*/
|
|
22
|
+
data?: any;
|
|
23
|
+
/**
|
|
24
|
+
* Error message if validation failed.
|
|
25
|
+
*/
|
|
26
|
+
error?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Line number of the syntax error if validation failed.
|
|
29
|
+
*/
|
|
30
|
+
line?: number;
|
|
31
|
+
/**
|
|
32
|
+
* Column number of the syntax error if validation failed.
|
|
33
|
+
*/
|
|
34
|
+
column?: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Validates a string to check if it's a valid JSON representation.
|
|
39
|
+
* Returns a detailed ValidationResult object.
|
|
40
|
+
*/
|
|
41
|
+
export function validate(jsonStr: string): ValidationResult {
|
|
42
|
+
if (typeof jsonStr !== "string") {
|
|
43
|
+
return { valid: false, error: "Input must be a string" };
|
|
44
|
+
}
|
|
45
|
+
if (jsonStr.trim() === "") {
|
|
46
|
+
return { valid: false, error: "Input string is empty" };
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
const data = JSON.parse(jsonStr);
|
|
50
|
+
return { valid: true, data };
|
|
51
|
+
} catch (e: any) {
|
|
52
|
+
const message = e.message;
|
|
53
|
+
let line = undefined;
|
|
54
|
+
let column = undefined;
|
|
55
|
+
|
|
56
|
+
const lineMatch = message.match(/line\s+(\d+)/i);
|
|
57
|
+
const colMatch = message.match(/column\s+(\d+)/i);
|
|
58
|
+
const posMatch = message.match(/position\s+(\d+)/i);
|
|
59
|
+
|
|
60
|
+
if (lineMatch) line = parseInt(lineMatch[1], 10);
|
|
61
|
+
if (colMatch) column = parseInt(colMatch[1], 10);
|
|
62
|
+
|
|
63
|
+
if (posMatch && !line && !column) {
|
|
64
|
+
const pos = parseInt(posMatch[1], 10);
|
|
65
|
+
const linesBefore = jsonStr.slice(0, pos).split("\n");
|
|
66
|
+
line = linesBefore.length;
|
|
67
|
+
column = linesBefore[linesBefore.length - 1].length + 1;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
valid: false,
|
|
72
|
+
error: message,
|
|
73
|
+
line,
|
|
74
|
+
column
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Formats (beautifies) a JSON string with optional custom indentation.
|
|
81
|
+
*/
|
|
82
|
+
export function format(jsonStr: string, options: FormatOptions = {}): string {
|
|
83
|
+
const { space = 2 } = options;
|
|
84
|
+
const validation = validate(jsonStr);
|
|
85
|
+
if (!validation.valid) {
|
|
86
|
+
throw new Error(`Invalid JSON: ${validation.error}`);
|
|
87
|
+
}
|
|
88
|
+
return JSON.stringify(validation.data, null, space);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Minifies a JSON string, stripping all whitespace and unnecessary formatting.
|
|
93
|
+
*/
|
|
94
|
+
export function minify(jsonStr: string): string {
|
|
95
|
+
const validation = validate(jsonStr);
|
|
96
|
+
if (!validation.valid) {
|
|
97
|
+
throw new Error(`Invalid JSON: ${validation.error}`);
|
|
98
|
+
}
|
|
99
|
+
return JSON.stringify(validation.data);
|
|
100
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true
|
|
12
|
+
},
|
|
13
|
+
"include": ["src/**/*"]
|
|
14
|
+
}
|