tjawn 0.0.0-stage → 2.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 +230 -2
- package/dist/errors.d.ts +7 -0
- package/dist/errors.js +10 -0
- package/dist/files.d.ts +10 -0
- package/dist/files.js +52 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +5 -0
- package/dist/jawnset.d.ts +55 -0
- package/dist/jawnset.js +250 -0
- package/dist/lexer.d.ts +62 -0
- package/dist/lexer.js +190 -0
- package/dist/parser.d.ts +7 -0
- package/dist/parser.js +443 -0
- package/dist/serializer.d.ts +14 -0
- package/dist/serializer.js +110 -0
- package/dist/types.d.ts +11 -0
- package/dist/types.js +1 -0
- package/dist/values.d.ts +16 -0
- package/dist/values.js +105 -0
- package/package.json +38 -4
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface StringifyOptions {
|
|
2
|
+
/** Indentation per level of nesting, by default two spaces. */
|
|
3
|
+
indent?: string;
|
|
4
|
+
/** Linebreak, by default "\n"; if empty, all is written on one line. */
|
|
5
|
+
eoln?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Serialize a jawn, which is a map, from an object or Map. Class
|
|
9
|
+
* instances are written as maps of their own enumerable properties not
|
|
10
|
+
* starting with `_`, and properties whose value is undefined are skipped.
|
|
11
|
+
*/
|
|
12
|
+
export declare function stringify(o: unknown, options?: StringifyOptions): string;
|
|
13
|
+
/** Serialize a list of jawns from an array of objects or Maps. */
|
|
14
|
+
export declare function stringifyList(o: unknown, options?: StringifyOptions): string;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { JawnTypeError } from './errors.js';
|
|
2
|
+
import { classify, mapEntries, nested, toNumber, typeName } from './values.js';
|
|
3
|
+
// Keys matching the grammar's KEY_BARE terminal can be written unquoted,
|
|
4
|
+
// except those starting with `//`, which would be read as a comment.
|
|
5
|
+
// Reserved words are matched ASCII case-insensitively, as in the grammar.
|
|
6
|
+
const BARE_KEY = /^(?!\/\/)(?!(?:[_+-]|[Nn][Uu][Ll][Ll]|[Nn][Ii][Ll]|[Nn][Oo][Nn][Ee]|[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee])$)[^\x00-\x20\x7f-\xa0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000"'{}\[\],:=]+$/u;
|
|
7
|
+
// Control characters are escaped so that they're visible and linebreaks
|
|
8
|
+
// survive CRLF normalization, with short escapes where there's one.
|
|
9
|
+
const NEEDS_ESCAPE = /["\\\x00-\x1f\x7f-\x9f]/g;
|
|
10
|
+
const ESCAPES = new Map([['"', '\\"'], ['\\', '\\\\'], ['\n', '\\n'], ['\t', '\\t'], ['\r', '\\r'],
|
|
11
|
+
['\f', '\\f']]);
|
|
12
|
+
function quote(s) {
|
|
13
|
+
return '"' + s.replace(NEEDS_ESCAPE, (c) => ESCAPES.get(c) ?? '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0')) + '"';
|
|
14
|
+
}
|
|
15
|
+
function comma(eoln) {
|
|
16
|
+
// Without linebreaks, elements are separated by a space instead.
|
|
17
|
+
return ',' + (eoln || ' ');
|
|
18
|
+
}
|
|
19
|
+
class Serializer {
|
|
20
|
+
indent;
|
|
21
|
+
eoln;
|
|
22
|
+
// The collections being written, to reject one containing itself.
|
|
23
|
+
stack = new Set();
|
|
24
|
+
constructor(indent, eoln) {
|
|
25
|
+
this.indent = indent;
|
|
26
|
+
this.eoln = eoln;
|
|
27
|
+
}
|
|
28
|
+
value(o, prefix) {
|
|
29
|
+
switch (classify(o)) {
|
|
30
|
+
case 'null':
|
|
31
|
+
return 'null';
|
|
32
|
+
case 'boolean':
|
|
33
|
+
return o ? 'true' : 'false';
|
|
34
|
+
case 'number': {
|
|
35
|
+
// Numbers are float64s, so bigints are rounded to one.
|
|
36
|
+
const n = toNumber(o);
|
|
37
|
+
return Object.is(n, -0) ? '-0' : String(n);
|
|
38
|
+
}
|
|
39
|
+
case 'string':
|
|
40
|
+
return quote(o);
|
|
41
|
+
case 'list':
|
|
42
|
+
return this.elements(o, '[]', '[', ']', prefix);
|
|
43
|
+
case 'set':
|
|
44
|
+
return this.elements(o, '{_}', '{', '}', prefix);
|
|
45
|
+
case 'map':
|
|
46
|
+
return this.map(o, prefix);
|
|
47
|
+
}
|
|
48
|
+
throw new JawnTypeError(`Cannot serialize ${typeName(o)} as a jawn value`);
|
|
49
|
+
}
|
|
50
|
+
elements(o, empty, open, close, prefix) {
|
|
51
|
+
return nested(o, this.stack, () => {
|
|
52
|
+
const pf = prefix + this.indent;
|
|
53
|
+
const items = Array.from(o, (v) => pf + this.value(v, pf));
|
|
54
|
+
if (items.length === 0) {
|
|
55
|
+
return empty;
|
|
56
|
+
}
|
|
57
|
+
return open + this.eoln + items.join(comma(this.eoln)) + this.eoln + prefix + close;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
map(o, prefix) {
|
|
61
|
+
return nested(o, this.stack, () => {
|
|
62
|
+
const entries = mapEntries(o);
|
|
63
|
+
if (entries.length === 0) {
|
|
64
|
+
return '{}';
|
|
65
|
+
}
|
|
66
|
+
return '{' + this.eoln + this.pairs(entries, prefix + this.indent) + this.eoln + prefix + '}';
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
pairs(entries, prefix) {
|
|
70
|
+
return entries.map(([k, v]) => prefix + (BARE_KEY.test(k) ? k : quote(k)) + ': ' + this.value(v, prefix))
|
|
71
|
+
.join(comma(this.eoln));
|
|
72
|
+
}
|
|
73
|
+
root(o) {
|
|
74
|
+
return nested(o, this.stack, () => this.pairs(mapEntries(o), ''));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function serializer(options) {
|
|
78
|
+
const eoln = options.eoln ?? '\n';
|
|
79
|
+
// Indentation only makes sense following linebreaks.
|
|
80
|
+
return new Serializer(eoln ? options.indent ?? ' ' : '', eoln);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Serialize a jawn, which is a map, from an object or Map. Class
|
|
84
|
+
* instances are written as maps of their own enumerable properties not
|
|
85
|
+
* starting with `_`, and properties whose value is undefined are skipped.
|
|
86
|
+
*/
|
|
87
|
+
export function stringify(o, options = {}) {
|
|
88
|
+
const kind = classify(o);
|
|
89
|
+
if (kind === 'map') {
|
|
90
|
+
return serializer(options).root(o);
|
|
91
|
+
}
|
|
92
|
+
if (kind === 'list') {
|
|
93
|
+
throw new JawnTypeError('Jawn root must be an object or Map;'
|
|
94
|
+
+ ' use stringifyList to write a list of jawns');
|
|
95
|
+
}
|
|
96
|
+
throw new JawnTypeError('Jawn root must be an object or Map');
|
|
97
|
+
}
|
|
98
|
+
/** Serialize a list of jawns from an array of objects or Maps. */
|
|
99
|
+
export function stringifyList(o, options = {}) {
|
|
100
|
+
if (!Array.isArray(o)) {
|
|
101
|
+
throw new JawnTypeError(`A list of jawns must be an array, not ${typeName(o)}`);
|
|
102
|
+
}
|
|
103
|
+
for (const x of Array.from(o)) {
|
|
104
|
+
if (classify(x) !== 'map') {
|
|
105
|
+
throw new JawnTypeError('A list of jawns may contain only objects or Maps,'
|
|
106
|
+
+ ` not ${typeName(x)}`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return serializer(options).value(o, '');
|
|
110
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { JawnSet } from './jawnset.js';
|
|
2
|
+
/** A string, (float64) number, boolean, or null. */
|
|
3
|
+
export type JawnPrimitive = null | boolean | number | string;
|
|
4
|
+
/** A collection or primitive. */
|
|
5
|
+
export type JawnValue = JawnPrimitive | JawnValue[] | JawnSet | JawnMap;
|
|
6
|
+
/** A mapping from string keys to jawn values. */
|
|
7
|
+
export interface JawnMap {
|
|
8
|
+
[key: string]: JawnValue;
|
|
9
|
+
}
|
|
10
|
+
/** A jawn is a map. */
|
|
11
|
+
export type Jawn = JawnMap;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/values.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type Kind = 'null' | 'boolean' | 'number' | 'string' | 'list' | 'set' | 'map';
|
|
2
|
+
/** The name of a value's type, for error messages. */
|
|
3
|
+
export declare function typeName(v: unknown): string;
|
|
4
|
+
/** The kind of jawn value v is, or undefined if it can't be one. */
|
|
5
|
+
export declare function classify(v: unknown): Kind | undefined;
|
|
6
|
+
/** A number or bigint as a float64, which must be finite. */
|
|
7
|
+
export declare function toNumber(v: number | bigint): number;
|
|
8
|
+
/**
|
|
9
|
+
* The entries of a map-like value: a Map, a plain object's own enumerable
|
|
10
|
+
* properties, or a class instance's own enumerable properties not
|
|
11
|
+
* starting with `_`. Properties whose value is undefined are skipped, as
|
|
12
|
+
* JSON.stringify does.
|
|
13
|
+
*/
|
|
14
|
+
export declare function mapEntries(o: object): [string, unknown][];
|
|
15
|
+
/** Run f on collection o, guarding against o containing itself. */
|
|
16
|
+
export declare function nested<R>(o: object, stack: Set<object>, f: () => R): R;
|
package/dist/values.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Classification of JavaScript values as jawn values, shared by the
|
|
2
|
+
// serializer and JawnSet.
|
|
3
|
+
import { JawnError, JawnTypeError } from './errors.js';
|
|
4
|
+
import { JawnSet } from './jawnset.js';
|
|
5
|
+
/** The name of a value's type, for error messages. */
|
|
6
|
+
export function typeName(v) {
|
|
7
|
+
if (v === null) {
|
|
8
|
+
return 'null';
|
|
9
|
+
}
|
|
10
|
+
if (typeof v === 'object') {
|
|
11
|
+
return Object.getPrototypeOf(v)?.constructor?.name || 'Object';
|
|
12
|
+
}
|
|
13
|
+
return typeof v;
|
|
14
|
+
}
|
|
15
|
+
// Objects that hold their data in internal slots rather than properties,
|
|
16
|
+
// and so would otherwise be written as empty maps.
|
|
17
|
+
function isBuiltin(o) {
|
|
18
|
+
return o instanceof Date || o instanceof RegExp || o instanceof Promise || o instanceof Error
|
|
19
|
+
|| o instanceof WeakMap || o instanceof WeakSet || o instanceof WeakRef
|
|
20
|
+
|| o instanceof ArrayBuffer || ArrayBuffer.isView(o)
|
|
21
|
+
|| (typeof SharedArrayBuffer !== 'undefined' && o instanceof SharedArrayBuffer)
|
|
22
|
+
|| o instanceof Number || o instanceof String || o instanceof Boolean
|
|
23
|
+
|| o instanceof BigInt || o instanceof Symbol;
|
|
24
|
+
}
|
|
25
|
+
/** The kind of jawn value v is, or undefined if it can't be one. */
|
|
26
|
+
export function classify(v) {
|
|
27
|
+
if (v === null) {
|
|
28
|
+
return 'null';
|
|
29
|
+
}
|
|
30
|
+
switch (typeof v) {
|
|
31
|
+
case 'boolean':
|
|
32
|
+
return 'boolean';
|
|
33
|
+
case 'number':
|
|
34
|
+
case 'bigint':
|
|
35
|
+
return 'number';
|
|
36
|
+
case 'string':
|
|
37
|
+
return 'string';
|
|
38
|
+
case 'object':
|
|
39
|
+
if (Array.isArray(v)) {
|
|
40
|
+
return 'list';
|
|
41
|
+
}
|
|
42
|
+
if (v instanceof JawnSet || v instanceof Set) {
|
|
43
|
+
return 'set';
|
|
44
|
+
}
|
|
45
|
+
if (v instanceof Map || !isBuiltin(v)) {
|
|
46
|
+
return 'map';
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
/** A number or bigint as a float64, which must be finite. */
|
|
52
|
+
export function toNumber(v) {
|
|
53
|
+
const n = Number(v);
|
|
54
|
+
if (!Number.isFinite(n)) {
|
|
55
|
+
throw new JawnError('Jawn numbers must be finite and within float64 range');
|
|
56
|
+
}
|
|
57
|
+
return n;
|
|
58
|
+
}
|
|
59
|
+
// Plain objects, including those from other realms or without a
|
|
60
|
+
// prototype, as opposed to class instances.
|
|
61
|
+
function isPlain(o) {
|
|
62
|
+
const proto = Object.getPrototypeOf(o);
|
|
63
|
+
return proto === null || Object.getPrototypeOf(proto) === null;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The entries of a map-like value: a Map, a plain object's own enumerable
|
|
67
|
+
* properties, or a class instance's own enumerable properties not
|
|
68
|
+
* starting with `_`. Properties whose value is undefined are skipped, as
|
|
69
|
+
* JSON.stringify does.
|
|
70
|
+
*/
|
|
71
|
+
export function mapEntries(o) {
|
|
72
|
+
const entries = [];
|
|
73
|
+
if (o instanceof Map) {
|
|
74
|
+
for (const [k, v] of o) {
|
|
75
|
+
if (typeof k !== 'string') {
|
|
76
|
+
throw new JawnTypeError(`Jawn map keys must be strings, not ${typeName(k)}`);
|
|
77
|
+
}
|
|
78
|
+
if (v !== undefined) {
|
|
79
|
+
entries.push([k, v]);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return entries;
|
|
83
|
+
}
|
|
84
|
+
const plain = isPlain(o);
|
|
85
|
+
for (const k of Object.keys(o)) {
|
|
86
|
+
const v = o[k];
|
|
87
|
+
if (v !== undefined && (plain || !k.startsWith('_'))) {
|
|
88
|
+
entries.push([k, v]);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return entries;
|
|
92
|
+
}
|
|
93
|
+
/** Run f on collection o, guarding against o containing itself. */
|
|
94
|
+
export function nested(o, stack, f) {
|
|
95
|
+
if (stack.has(o)) {
|
|
96
|
+
throw new JawnTypeError('Jawn values cannot contain themselves');
|
|
97
|
+
}
|
|
98
|
+
stack.add(o);
|
|
99
|
+
try {
|
|
100
|
+
return f();
|
|
101
|
+
}
|
|
102
|
+
finally {
|
|
103
|
+
stack.delete(o);
|
|
104
|
+
}
|
|
105
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tjawn",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Human friendly lightweight object notation.",
|
|
5
|
+
"author": "tjkopena <tjkopena@gmail.com>",
|
|
6
|
+
"license": "MIT-0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/tjkopena/tjawn.git",
|
|
10
|
+
"directory": "src/ts"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=20.16"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsc -p tsconfig.build.json",
|
|
30
|
+
"typecheck": "tsc --noEmit",
|
|
31
|
+
"test": "vitest run",
|
|
32
|
+
"prepack": "npm run build && cp ../../README.md README.md",
|
|
33
|
+
"postpack": "rm README.md"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^20.19.0",
|
|
37
|
+
"typescript": "^5.9.0",
|
|
38
|
+
"vitest": "^4.1.0"
|
|
39
|
+
}
|
|
40
|
+
}
|