jsonh-ts 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.
Files changed (56) hide show
  1. package/build/index.d.ts +22 -0
  2. package/build/index.d.ts.map +1 -0
  3. package/build/index.js +22 -0
  4. package/build/index.js.map +1 -0
  5. package/build/json-token-type.d.ts +77 -0
  6. package/build/json-token-type.d.ts.map +1 -0
  7. package/build/json-token-type.js +79 -0
  8. package/build/json-token-type.js.map +1 -0
  9. package/build/jsonh-number-parser.d.ts +21 -0
  10. package/build/jsonh-number-parser.d.ts.map +1 -0
  11. package/build/jsonh-number-parser.js +174 -0
  12. package/build/jsonh-number-parser.js.map +1 -0
  13. package/build/jsonh-reader-options.d.ts +25 -0
  14. package/build/jsonh-reader-options.d.ts.map +1 -0
  15. package/build/jsonh-reader-options.js +26 -0
  16. package/build/jsonh-reader-options.js.map +1 -0
  17. package/build/jsonh-reader.d.ts +68 -0
  18. package/build/jsonh-reader.d.ts.map +1 -0
  19. package/build/jsonh-reader.js +1218 -0
  20. package/build/jsonh-reader.js.map +1 -0
  21. package/build/jsonh-token.d.ts +21 -0
  22. package/build/jsonh-token.d.ts.map +1 -0
  23. package/build/jsonh-token.js +36 -0
  24. package/build/jsonh-token.js.map +1 -0
  25. package/build/jsonh-version.d.ts +15 -0
  26. package/build/jsonh-version.d.ts.map +1 -0
  27. package/build/jsonh-version.js +17 -0
  28. package/build/jsonh-version.js.map +1 -0
  29. package/build/result-helpers.d.ts +6 -0
  30. package/build/result-helpers.d.ts.map +1 -0
  31. package/build/result-helpers.js +11 -0
  32. package/build/result-helpers.js.map +1 -0
  33. package/build/result.d.ts +48 -0
  34. package/build/result.d.ts.map +1 -0
  35. package/build/result.js +95 -0
  36. package/build/result.js.map +1 -0
  37. package/build/string-text-reader.d.ts +11 -0
  38. package/build/string-text-reader.d.ts.map +1 -0
  39. package/build/string-text-reader.js +33 -0
  40. package/build/string-text-reader.js.map +1 -0
  41. package/build/text-reader.d.ts +20 -0
  42. package/build/text-reader.d.ts.map +1 -0
  43. package/build/text-reader.js +19 -0
  44. package/build/text-reader.js.map +1 -0
  45. package/index.ts +21 -0
  46. package/json-token-type.ts +77 -0
  47. package/jsonh-number-parser.ts +191 -0
  48. package/jsonh-reader-options.ts +26 -0
  49. package/jsonh-reader.ts +1317 -0
  50. package/jsonh-token.ts +37 -0
  51. package/jsonh-version.ts +15 -0
  52. package/package.json +30 -0
  53. package/result.ts +97 -0
  54. package/string-text-reader.ts +35 -0
  55. package/text-reader.ts +30 -0
  56. package/tsconfig.json +38 -0
package/jsonh-token.ts ADDED
@@ -0,0 +1,37 @@
1
+ import JsonTokenType = require("./json-token-type");
2
+
3
+ /**
4
+ * A single JSONH token with a {@link JsonTokenType}.
5
+ */
6
+ class JsonhToken {
7
+ /**
8
+ * The type of the token.
9
+ */
10
+ #jsonType: JsonTokenType;
11
+ /**
12
+ * The type of the token.
13
+ */
14
+ get jsonType(): JsonTokenType {
15
+ return this.#jsonType;
16
+ }
17
+ /**
18
+ * The value of the token, or an empty string.
19
+ */
20
+ #value: string;
21
+ /**
22
+ * The value of the token, or an empty string.
23
+ */
24
+ get value(): string {
25
+ return this.#value;
26
+ }
27
+
28
+ /**
29
+ * Constructs a single JSONH token.
30
+ */
31
+ constructor(jsonType: JsonTokenType, value: string = "") {
32
+ this.#jsonType = jsonType;
33
+ this.#value = value;
34
+ }
35
+ }
36
+
37
+ export = JsonhToken;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The major versions of the JSONH specification.
3
+ */
4
+ enum JsonhVersion {
5
+ /**
6
+ * Indicates that the latest version should be used (currently {@link V1}).
7
+ */
8
+ Latest = 0,
9
+ /**
10
+ * Version 1 of the specification, released 2025/03/19.
11
+ */
12
+ V1 = 1,
13
+ }
14
+
15
+ export = JsonhVersion;
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "jsonh-ts",
3
+ "version": "1.0.0",
4
+ "description": "JSON for Humans in TypeScript/JavaScript.",
5
+ "type": "commonjs",
6
+ "main": "build/index.js",
7
+ "types": "build/index.d.ts",
8
+ "scripts": {
9
+ "build": "tsc"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/jsonh-org/JsonhTs.git"
14
+ },
15
+ "keywords": [
16
+ "JSONH",
17
+ "JSON for Humans",
18
+ "JSON",
19
+ "JSON5",
20
+ "HJSON",
21
+ "config",
22
+ "parser"
23
+ ],
24
+ "author": "Joyless",
25
+ "license": "MIT",
26
+ "bugs": {
27
+ "url": "https://github.com/jsonh-org/JsonhTs/issues"
28
+ },
29
+ "homepage": "https://github.com/jsonh-org/JsonhTs#readme"
30
+ }
package/result.ts ADDED
@@ -0,0 +1,97 @@
1
+ /**
2
+ * A value or an error.
3
+ */
4
+ class Result<T = undefined, E extends Error = Error> {
5
+ /**
6
+ * The optional value if successful.
7
+ */
8
+ #value: T | undefined;
9
+ /**
10
+ * The error if unsuccessful.
11
+ */
12
+ #error: E | undefined;
13
+
14
+ /**
15
+ * Constructs a successful or failed result from a value and an error.
16
+ */
17
+ private constructor(value: T | undefined, error: E | undefined) {
18
+ this.#value = value;
19
+ this.#error = error;
20
+ }
21
+ /**
22
+ * Constructs a successful result from an optional value.
23
+ */
24
+ static fromValue<T>(value: T | undefined = undefined): Result<T> {
25
+ return new Result<T>(value, undefined);
26
+ }
27
+ /**
28
+ * Constructs a failed result from an error.
29
+ */
30
+ static fromError<T = undefined, E extends Error = Error>(error: E): Result<T, E> {
31
+ return new Result<T, E>(undefined, error);
32
+ }
33
+
34
+ /**
35
+ * Returns whether this result is successful (has an optional value).
36
+ */
37
+ get isValue(): boolean {
38
+ return this.#error === undefined;
39
+ }
40
+ /**
41
+ * Returns whether this result is failed (has an error).
42
+ */
43
+ get isError(): boolean {
44
+ return this.#error !== undefined;
45
+ }
46
+
47
+ /**
48
+ * Returns the value or throws an error.
49
+ */
50
+ get value(): T {
51
+ if (this.#error !== undefined) {
52
+ throw new Error(`Result was error: \"${this.#error.message}\"`);
53
+ }
54
+ if (this.#value === undefined) {
55
+ throw new Error("Result was not error but had no value");
56
+ }
57
+ return this.#value;
58
+ }
59
+ /**
60
+ * Changes the optional value or throws an error.
61
+ */
62
+ set value(value: T | undefined) {
63
+ if (this.#error !== undefined) {
64
+ throw new Error(`Result was error: \"${this.#error.message}\"`);
65
+ }
66
+ this.#value = value;
67
+ }
68
+ /**
69
+ * Returns the optional value or undefined.
70
+ */
71
+ get valueOrUndefined(): T | undefined {
72
+ if (this.#error !== undefined) {
73
+ return undefined;
74
+ }
75
+ return this.#value;
76
+ }
77
+ /**
78
+ * Returns the error or throws an error.
79
+ */
80
+ get error(): E {
81
+ if (this.#error === undefined) {
82
+ throw new Error("Result was value");
83
+ }
84
+ return this.#error;
85
+ }
86
+ /**
87
+ * Returns the optional error or undefined.
88
+ */
89
+ get errorOrUndefined(): E | undefined {
90
+ if (this.#error === undefined) {
91
+ return undefined;
92
+ }
93
+ return this.#error;
94
+ }
95
+ }
96
+
97
+ export = Result;
@@ -0,0 +1,35 @@
1
+ import TextReader = require("./text-reader.js");
2
+
3
+ class StringTextReader extends TextReader {
4
+ #string: string;
5
+ #index: number;
6
+
7
+ constructor(string: string) {
8
+ super();
9
+ this.#string = string;
10
+ this.#index = 0;
11
+ }
12
+
13
+ override read(): string | null {
14
+ if (this.#index >= this.#string.length) {
15
+ return null;
16
+ }
17
+ return this.#string.at(this.#index++)!;
18
+ }
19
+ override peek(): string | null {
20
+ if (this.#index >= this.#string.length) {
21
+ return null;
22
+ }
23
+ return this.#string.at(this.#index)!;
24
+ }
25
+ override done(): boolean {
26
+ return this.#index >= this.#string.length;
27
+ }
28
+ override readToEnd(): string {
29
+ let currentIndex: number = this.#index;
30
+ this.#index = this.#string.length;
31
+ return this.#string.slice(currentIndex);
32
+ }
33
+ }
34
+
35
+ export = StringTextReader;
package/text-reader.ts ADDED
@@ -0,0 +1,30 @@
1
+ abstract class TextReader {
2
+ /**
3
+ * Returns the next character and increments the position, or returns null.
4
+ */
5
+ abstract read(): string | null;
6
+ /**
7
+ * Returns the next character, or returns null.
8
+ */
9
+ abstract peek(): string | null;
10
+ /**
11
+ * Returns whether the text reader is finished reading characters.
12
+ */
13
+ abstract done(): boolean;
14
+ /**
15
+ * Returns all of the remaining characters.
16
+ */
17
+ readToEnd(): string {
18
+ let result: string = "";
19
+ while (true) {
20
+ let next: string | null = this.peek();
21
+ if (next === null) {
22
+ break;
23
+ }
24
+ result += next;
25
+ }
26
+ return result;
27
+ }
28
+ }
29
+
30
+ export = TextReader;
package/tsconfig.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "compilerOptions": {
3
+ // File Layout
4
+ "outDir": "./build",
5
+
6
+ // Environment Settings
7
+ "module": "commonjs",
8
+ "target": "es2022",
9
+ "lib": ["ES2022"],
10
+ "types": [],
11
+
12
+ // Other Outputs
13
+ "sourceMap": true,
14
+ "declaration": true,
15
+ "declarationMap": true,
16
+
17
+ // Stricter Typechecking Options
18
+ "noUncheckedIndexedAccess": true,
19
+ "exactOptionalPropertyTypes": true,
20
+
21
+ // Style Options
22
+ "noImplicitReturns": true,
23
+ "noImplicitOverride": true,
24
+ //"noUnusedLocals": true,
25
+ "noUnusedParameters": true,
26
+ "noFallthroughCasesInSwitch": true,
27
+ "noPropertyAccessFromIndexSignature": true,
28
+
29
+ // Recommended Options
30
+ "strict": true,
31
+ "jsx": "react-jsx",
32
+ "verbatimModuleSyntax": true,
33
+ "isolatedModules": true,
34
+ "noUncheckedSideEffectImports": true,
35
+ "moduleDetection": "force",
36
+ "skipLibCheck": true,
37
+ },
38
+ }