functionalscript 0.6.6 → 0.6.7
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/djs/ast/test.f.js +1 -1
- package/djs/module.f.js +5 -0
- package/djs/parser/module.f.js +1 -1
- package/djs/parser/test.f.d.ts +1 -0
- package/djs/parser/test.f.js +41 -5
- package/djs/serializer/module.f.d.ts +9 -10
- package/djs/serializer/module.f.js +103 -12
- package/djs/serializer/test.f.d.ts +5 -9
- package/djs/serializer/test.f.js +73 -79
- package/djs/serializer-old/module.f.d.ts +13 -0
- package/djs/serializer-old/module.f.js +61 -0
- package/djs/serializer-old/test.f.d.ts +12 -0
- package/djs/serializer-old/test.f.js +84 -0
- package/djs/test/input.f.js +1 -1
- package/djs/tokenizer/test.f.js +1 -1
- package/djs/transpiler/test.f.js +1 -1
- package/js/tokenizer/test.f.js +1 -1
- package/json/tokenizer/test.f.js +1 -1
- package/package.json +2 -2
package/djs/ast/test.f.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as list from "../../types/object/module.f.js";
|
|
2
2
|
const { sort } = list;
|
|
3
3
|
import * as shared from "./module.f.js";
|
|
4
|
-
import { stringify } from "../serializer/module.f.js";
|
|
4
|
+
import { stringify } from "../serializer-old/module.f.js";
|
|
5
5
|
export default {
|
|
6
6
|
test: () => {
|
|
7
7
|
const djs = shared.run([1])([]);
|
package/djs/module.f.js
CHANGED
|
@@ -13,6 +13,11 @@ export const compile = ({ console: { error }, fs, process: { argv } }) => {
|
|
|
13
13
|
const result = transpile(fs)(inputFileName);
|
|
14
14
|
switch (result[0]) {
|
|
15
15
|
case 'ok': {
|
|
16
|
+
if (outputFileName.endsWith('.json')) {
|
|
17
|
+
const output = JSON.stringify(result[1]);
|
|
18
|
+
fs.writeFileSync(outputFileName, output);
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
16
21
|
const output = stringifyUnknown(result[1]);
|
|
17
22
|
fs.writeFileSync(outputFileName, output);
|
|
18
23
|
break;
|
package/djs/parser/module.f.js
CHANGED
|
@@ -16,7 +16,7 @@ const parseInitialOp = token => state => {
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
-
return { state: '
|
|
19
|
+
return foldOp(token)({ ...state, state: 'exportValue', valueState: '', top: null, stack: null });
|
|
20
20
|
};
|
|
21
21
|
const parseNewLineRequiredOp = token => state => {
|
|
22
22
|
switch (token.kind) {
|
package/djs/parser/test.f.d.ts
CHANGED
package/djs/parser/test.f.js
CHANGED
|
@@ -3,7 +3,7 @@ import * as tokenizer from "../tokenizer/module.f.js";
|
|
|
3
3
|
import { toArray } from "../../types/list/module.f.js";
|
|
4
4
|
import { sort } from "../../types/object/module.f.js";
|
|
5
5
|
import * as encoding from "../../text/utf16/module.f.js";
|
|
6
|
-
import { stringify } from "../serializer/module.f.js";
|
|
6
|
+
import { stringify } from "../serializer-old/module.f.js";
|
|
7
7
|
const tokenizeString = s => toArray(tokenizer.tokenize(encoding.stringToList(s)));
|
|
8
8
|
const stringifyDjsModule = stringify(sort);
|
|
9
9
|
export default {
|
|
@@ -484,24 +484,60 @@ export default {
|
|
|
484
484
|
}
|
|
485
485
|
},
|
|
486
486
|
],
|
|
487
|
-
|
|
487
|
+
validJson: [
|
|
488
488
|
() => {
|
|
489
489
|
const tokenList = tokenizeString('null');
|
|
490
490
|
const obj = parser.parseFromTokens(tokenList);
|
|
491
|
-
if (obj[0] !== '
|
|
491
|
+
if (obj[0] !== 'ok') {
|
|
492
492
|
throw obj;
|
|
493
493
|
}
|
|
494
|
-
|
|
494
|
+
const result = stringifyDjsModule(obj[1]);
|
|
495
|
+
if (result !== '[[],[null]]') {
|
|
496
|
+
throw result;
|
|
497
|
+
}
|
|
498
|
+
},
|
|
499
|
+
() => {
|
|
500
|
+
const tokenList = tokenizeString('1');
|
|
501
|
+
const obj = parser.parseFromTokens(tokenList);
|
|
502
|
+
if (obj[0] !== 'ok') {
|
|
495
503
|
throw obj;
|
|
496
504
|
}
|
|
505
|
+
const result = stringifyDjsModule(obj[1]);
|
|
506
|
+
if (result !== '[[],[1]]') {
|
|
507
|
+
throw result;
|
|
508
|
+
}
|
|
497
509
|
},
|
|
510
|
+
() => {
|
|
511
|
+
const tokenList = tokenizeString('[]');
|
|
512
|
+
const obj = parser.parseFromTokens(tokenList);
|
|
513
|
+
if (obj[0] !== 'ok') {
|
|
514
|
+
throw obj;
|
|
515
|
+
}
|
|
516
|
+
const result = stringifyDjsModule(obj[1]);
|
|
517
|
+
if (result !== '[[],[["array",[]]]]') {
|
|
518
|
+
throw result;
|
|
519
|
+
}
|
|
520
|
+
},
|
|
521
|
+
() => {
|
|
522
|
+
const tokenList = tokenizeString('{"valid":"json"}');
|
|
523
|
+
const obj = parser.parseFromTokens(tokenList);
|
|
524
|
+
if (obj[0] !== 'ok') {
|
|
525
|
+
throw obj;
|
|
526
|
+
}
|
|
527
|
+
const result = stringifyDjsModule(obj[1]);
|
|
528
|
+
if (result !== '[[],[{"valid":"json"}]]') {
|
|
529
|
+
throw result;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
],
|
|
533
|
+
invalidModule: [
|
|
498
534
|
() => {
|
|
499
535
|
const tokenList = tokenizeString('module=null');
|
|
500
536
|
const obj = parser.parseFromTokens(tokenList);
|
|
501
537
|
if (obj[0] !== 'error') {
|
|
502
538
|
throw obj;
|
|
503
539
|
}
|
|
504
|
-
if (obj[1] !== '
|
|
540
|
+
if (obj[1] !== 'const not found') {
|
|
505
541
|
throw obj;
|
|
506
542
|
}
|
|
507
543
|
},
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import type * as djs from '../module.f.ts';
|
|
2
2
|
import type * as O from '../../types/object/module.f.ts';
|
|
3
|
-
import type
|
|
4
|
-
|
|
5
|
-
type
|
|
3
|
+
import { type List } from '../../types/list/module.f.ts';
|
|
4
|
+
export declare const undefinedSerialize: string[];
|
|
5
|
+
type RefCounter = [number, number, boolean];
|
|
6
|
+
type Entry = O.Entry<djs.Unknown>;
|
|
7
|
+
type Entries = List<Entry>;
|
|
6
8
|
type MapEntries = (entries: Entries) => Entries;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* https://262.ecma-international.org/6.0/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys
|
|
11
|
-
*/
|
|
12
|
-
export declare const stringify: (mapEntries: MapEntries) => (value: Unknown) => string;
|
|
9
|
+
type Refs = Map<djs.Unknown, RefCounter>;
|
|
10
|
+
export declare const stringify: (sort: MapEntries) => (djs: djs.Unknown) => string;
|
|
11
|
+
export declare const countRefs: (djs: djs.Unknown) => Refs;
|
|
13
12
|
export {};
|
|
@@ -1,17 +1,55 @@
|
|
|
1
|
-
import
|
|
2
|
-
const { flat, map } = list;
|
|
1
|
+
import { fold } from "../../types/list/module.f.js";
|
|
3
2
|
import * as string from "../../types/string/module.f.js";
|
|
4
3
|
const { concat } = string;
|
|
4
|
+
import { flat, flatMap, map, concat as listConcat } from "../../types/list/module.f.js";
|
|
5
|
+
const { entries } = Object;
|
|
5
6
|
import * as f from "../../types/function/module.f.js";
|
|
6
7
|
const { compose, fn } = f;
|
|
7
|
-
const { entries } = Object;
|
|
8
8
|
import * as bi from "../../types/bigint/module.f.js";
|
|
9
9
|
const { serialize: bigintSerialize } = bi;
|
|
10
|
-
import * as
|
|
11
|
-
const { objectWrap, arrayWrap, stringSerialize, numberSerialize, nullSerialize, boolSerialize } =
|
|
10
|
+
import * as serializer from "../../json/serializer/module.f.js";
|
|
11
|
+
const { objectWrap, arrayWrap, stringSerialize, numberSerialize, nullSerialize, boolSerialize } = serializer;
|
|
12
12
|
const colon = [':'];
|
|
13
|
-
const undefinedSerialize = ['undefined'];
|
|
14
|
-
|
|
13
|
+
export const undefinedSerialize = ['undefined'];
|
|
14
|
+
const getConstantsOp = djs => state => {
|
|
15
|
+
switch (typeof djs) {
|
|
16
|
+
case 'boolean': {
|
|
17
|
+
return state;
|
|
18
|
+
}
|
|
19
|
+
case 'number':
|
|
20
|
+
case 'string':
|
|
21
|
+
case 'bigint': {
|
|
22
|
+
return getConstantSelf(djs)(state);
|
|
23
|
+
}
|
|
24
|
+
default: {
|
|
25
|
+
if (djs === null) {
|
|
26
|
+
return state;
|
|
27
|
+
}
|
|
28
|
+
if (djs === undefined) {
|
|
29
|
+
return state;
|
|
30
|
+
}
|
|
31
|
+
if (djs instanceof Array) {
|
|
32
|
+
return getConstantSelf(djs)(fold(getConstantsOp)(state)(djs));
|
|
33
|
+
}
|
|
34
|
+
return getConstantSelf(djs)(fold(getConstantsOp)(state)(map(entryValue)(entries(djs))));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const getConstantSelf = djs => state => {
|
|
39
|
+
const refs = state.refs;
|
|
40
|
+
const refCounter = refs.get(djs);
|
|
41
|
+
if (refCounter !== undefined && refCounter[1] > 1 && !refCounter[2]) {
|
|
42
|
+
refCounter[2] = true;
|
|
43
|
+
refs.set(djs, refCounter);
|
|
44
|
+
return { refs, consts: { head: state.consts, tail: [djs] } };
|
|
45
|
+
}
|
|
46
|
+
return state;
|
|
47
|
+
};
|
|
48
|
+
const getConstants = djs => refs => {
|
|
49
|
+
return getConstantsOp(djs)(refs);
|
|
50
|
+
};
|
|
51
|
+
const entryValue = kv => kv[1];
|
|
52
|
+
const serialize = sort => refs => root => {
|
|
15
53
|
const propertySerialize = ([k, v]) => flat([
|
|
16
54
|
stringSerialize(k),
|
|
17
55
|
colon,
|
|
@@ -24,6 +62,12 @@ export const serialize = sort => {
|
|
|
24
62
|
.then(objectWrap)
|
|
25
63
|
.result;
|
|
26
64
|
const f = value => {
|
|
65
|
+
if (value !== root) {
|
|
66
|
+
const refCounter = refs.get(value);
|
|
67
|
+
if (refCounter !== undefined && refCounter[1] > 1) {
|
|
68
|
+
return [`c${refCounter[0]}`];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
27
71
|
switch (typeof value) {
|
|
28
72
|
case 'boolean': {
|
|
29
73
|
return boolSerialize(value);
|
|
@@ -54,8 +98,55 @@ export const serialize = sort => {
|
|
|
54
98
|
const arraySerialize = compose(map(f))(arrayWrap);
|
|
55
99
|
return f;
|
|
56
100
|
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
101
|
+
const countRefsOp = djs => refs => {
|
|
102
|
+
switch (typeof djs) {
|
|
103
|
+
case 'boolean':
|
|
104
|
+
case 'number': {
|
|
105
|
+
return refs;
|
|
106
|
+
}
|
|
107
|
+
case 'string':
|
|
108
|
+
case 'bigint': {
|
|
109
|
+
return addRef(djs)(refs);
|
|
110
|
+
}
|
|
111
|
+
default: {
|
|
112
|
+
switch (djs) {
|
|
113
|
+
case null:
|
|
114
|
+
case undefined: {
|
|
115
|
+
return refs;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (djs instanceof Array) {
|
|
119
|
+
if (refs.has(djs))
|
|
120
|
+
return addRef(djs)(refs);
|
|
121
|
+
return addRef(djs)(fold(countRefsOp)(refs)(djs));
|
|
122
|
+
}
|
|
123
|
+
if (refs.has(djs))
|
|
124
|
+
return addRef(djs)(refs);
|
|
125
|
+
return addRef(djs)(fold(countRefsOp)(refs)(map(entryValue)(entries(djs))));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const addRef = djs => refs => {
|
|
130
|
+
const refCounter = refs.get(djs);
|
|
131
|
+
if (refCounter === undefined) {
|
|
132
|
+
return refs.set(djs, [refs.size, 1, false]);
|
|
133
|
+
}
|
|
134
|
+
return refs.set(djs, [refCounter[0], refCounter[1] + 1, false]);
|
|
135
|
+
};
|
|
136
|
+
export const stringify = sort => djs => {
|
|
137
|
+
const refs = countRefs(djs);
|
|
138
|
+
const consts = getConstants(djs)({ refs, consts: [] }).consts;
|
|
139
|
+
const constSerialize = entry => {
|
|
140
|
+
const refCounter = refs.get(entry);
|
|
141
|
+
if (refCounter === undefined) {
|
|
142
|
+
throw 'unexpected behaviour';
|
|
143
|
+
}
|
|
144
|
+
return flat([['const c'], numberSerialize(refCounter[0]), [' = '], serialize(sort)(refs)(entry)(entry), ['\n']]);
|
|
145
|
+
};
|
|
146
|
+
const constStrings = flatMap(constSerialize)(consts);
|
|
147
|
+
const rootStrings = listConcat(['export default '])(serialize(sort)(refs)(djs)(djs));
|
|
148
|
+
return concat(listConcat(constStrings)(rootStrings));
|
|
149
|
+
};
|
|
150
|
+
export const countRefs = djs => {
|
|
151
|
+
return countRefsOp(djs)(new Map());
|
|
152
|
+
};
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
declare const _default: {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
stringify: () => void;
|
|
8
|
-
sort?: never;
|
|
9
|
-
identity?: never;
|
|
10
|
-
})[];
|
|
2
|
+
testPrimitives: () => void;
|
|
3
|
+
testArray: () => void;
|
|
4
|
+
testObj: () => void;
|
|
5
|
+
testSort: () => void;
|
|
6
|
+
testIdentity: () => void;
|
|
11
7
|
};
|
|
12
8
|
export default _default;
|
package/djs/serializer/test.f.js
CHANGED
|
@@ -1,84 +1,78 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { countRefs, stringify } from "./module.f.js";
|
|
2
2
|
import * as list from "../../types/object/module.f.js";
|
|
3
3
|
const { sort } = list;
|
|
4
|
-
import * as
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
import * as serializer from "../serializer-old/module.f.js";
|
|
5
|
+
import { identity } from "../../types/function/module.f.js";
|
|
6
|
+
const stringifyOld = serializer.stringify(sort);
|
|
7
7
|
export default {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (x !== '{"a":"Hello"}') {
|
|
14
|
-
throw x;
|
|
15
|
-
}
|
|
16
|
-
},
|
|
17
|
-
identity: () => {
|
|
18
|
-
const x = serializer.stringify(identity)(json.setProperty("Hello")(['a'])({}));
|
|
19
|
-
if (x !== '{"a":"Hello"}') {
|
|
20
|
-
throw x;
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
sort: () => {
|
|
26
|
-
const x = serializer.stringify(sort)(json.setProperty("Hello")(['a'])({ c: [], b: 12 }));
|
|
27
|
-
if (x !== '{"a":"Hello","b":12,"c":[]}') {
|
|
28
|
-
throw x;
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
identity: () => {
|
|
32
|
-
const x = serializer.stringify(identity)(json.setProperty("Hello")(['a'])({ c: [], b: 12 }));
|
|
33
|
-
if (x !== '{"c":[],"b":12,"a":"Hello"}') {
|
|
34
|
-
throw x;
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
sort: () => {
|
|
40
|
-
const _0 = { a: { y: [24] }, c: [], b: 12 };
|
|
41
|
-
const _1 = json.setProperty("Hello")(['a', 'x'])(_0);
|
|
42
|
-
const _2 = serializer.stringify(sort)(_1);
|
|
43
|
-
if (_2 !== '{"a":{"x":"Hello","y":[24]},"b":12,"c":[]}') {
|
|
44
|
-
throw _2;
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
identity: () => {
|
|
48
|
-
const _0 = { a: { y: [24] }, c: [], b: 12 };
|
|
49
|
-
const _1 = json.setProperty("Hello")(['a', 'x'])(_0);
|
|
50
|
-
const _2 = serializer.stringify(identity)(_1);
|
|
51
|
-
if (_2 !== '{"a":{"y":[24],"x":"Hello"},"c":[],"b":12}') {
|
|
52
|
-
throw _2;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
stringify: () => {
|
|
58
|
-
const bi = 1234567890n;
|
|
59
|
-
const result = serializer.stringify(sort)(bi);
|
|
60
|
-
if (result !== '1234567890n') {
|
|
61
|
-
throw result;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
{
|
|
66
|
-
stringify: () => {
|
|
67
|
-
const arr = [0n, 1, 2n];
|
|
68
|
-
const result = serializer.stringify(sort)(arr);
|
|
69
|
-
if (result !== '[0n,1,2n]') {
|
|
70
|
-
throw result;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
{
|
|
75
|
-
stringify: () => {
|
|
76
|
-
const obj = { "a": 0n, "b": 1, "c": 2n };
|
|
77
|
-
const result = serializer.stringify(sort)(obj);
|
|
78
|
-
if (result !== '{"a":0n,"b":1,"c":2n}') {
|
|
79
|
-
throw result;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
8
|
+
testPrimitives: () => {
|
|
9
|
+
const djs = [1, 2, 2, 2, true, false, undefined, null, 3n, "str"];
|
|
10
|
+
const refs = countRefs(djs);
|
|
11
|
+
if (refs.size !== 3) {
|
|
12
|
+
throw refs.size;
|
|
82
13
|
}
|
|
83
|
-
|
|
14
|
+
const refsBigInt = stringifyOld(refs.get(3n));
|
|
15
|
+
if (refsBigInt !== '[0,1,false]') {
|
|
16
|
+
throw refsBigInt;
|
|
17
|
+
}
|
|
18
|
+
const refsString = stringifyOld(refs.get("str"));
|
|
19
|
+
if (refsString !== '[1,1,false]') {
|
|
20
|
+
throw refsString;
|
|
21
|
+
}
|
|
22
|
+
const refsRoot = stringifyOld(refs.get(djs));
|
|
23
|
+
if (refsRoot !== '[2,1,false]') {
|
|
24
|
+
throw refsRoot;
|
|
25
|
+
}
|
|
26
|
+
if (refs.get(null) !== undefined) {
|
|
27
|
+
throw refs.get(null);
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
testArray: () => {
|
|
31
|
+
const array = [null];
|
|
32
|
+
const djs = [array, array, array];
|
|
33
|
+
const refs = countRefs(djs);
|
|
34
|
+
if (refs.size !== 2) {
|
|
35
|
+
throw refs.size;
|
|
36
|
+
}
|
|
37
|
+
const refsArray = stringifyOld(refs.get(array));
|
|
38
|
+
if (refsArray !== '[0,3,false]') {
|
|
39
|
+
throw refsArray;
|
|
40
|
+
}
|
|
41
|
+
const refsRoot = stringifyOld(refs.get(djs));
|
|
42
|
+
if (refsRoot !== '[1,1,false]') {
|
|
43
|
+
throw refsRoot;
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
testObj: () => {
|
|
47
|
+
const obj = { "a": 1, "b": 2 };
|
|
48
|
+
const djs = [obj, obj, 1];
|
|
49
|
+
const refs = countRefs(djs);
|
|
50
|
+
if (refs.size !== 2) {
|
|
51
|
+
throw refs.size;
|
|
52
|
+
}
|
|
53
|
+
const refsObj = stringifyOld(refs.get(obj));
|
|
54
|
+
if (refsObj !== '[0,2,false]') {
|
|
55
|
+
throw refsObj;
|
|
56
|
+
}
|
|
57
|
+
const refsRoot = stringifyOld(refs.get(djs));
|
|
58
|
+
if (refsRoot !== '[1,1,false]') {
|
|
59
|
+
throw refsRoot;
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
testSort: () => {
|
|
63
|
+
const obj = { "a": 1, "c": 2n, "b": [undefined, null, true, false] };
|
|
64
|
+
const djs = [obj, obj, 1];
|
|
65
|
+
const res = stringify(sort)(djs);
|
|
66
|
+
if (res !== 'const c2 = {"a":1,"b":[undefined,null,true,false],"c":2n}\nexport default [c2,c2,1]') {
|
|
67
|
+
throw res;
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
testIdentity: () => {
|
|
71
|
+
const obj = { "a": 1, "c": 2n, "b": [undefined, null, true, false] };
|
|
72
|
+
const djs = [obj, obj, 1];
|
|
73
|
+
const res = stringify(identity)(djs);
|
|
74
|
+
if (res !== 'const c2 = {"a":1,"c":2n,"b":[undefined,null,true,false]}\nexport default [c2,c2,1]') {
|
|
75
|
+
throw res;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
84
78
|
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as list from '../../types/list/module.f.ts';
|
|
2
|
+
import type * as O from '../../types/object/module.f.ts';
|
|
3
|
+
import type { Unknown } from '../module.f.ts';
|
|
4
|
+
type Entry = O.Entry<Unknown>;
|
|
5
|
+
type Entries = list.List<Entry>;
|
|
6
|
+
type MapEntries = (entries: Entries) => Entries;
|
|
7
|
+
export declare const serialize: (mapEntries: MapEntries) => (value: Unknown) => list.List<string>;
|
|
8
|
+
/**
|
|
9
|
+
* The standard `JSON.stringify` rules determined by
|
|
10
|
+
* https://262.ecma-international.org/6.0/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys
|
|
11
|
+
*/
|
|
12
|
+
export declare const stringify: (mapEntries: MapEntries) => (value: Unknown) => string;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import * as list from "../../types/list/module.f.js";
|
|
2
|
+
const { flat, map } = list;
|
|
3
|
+
import * as string from "../../types/string/module.f.js";
|
|
4
|
+
const { concat } = string;
|
|
5
|
+
import * as f from "../../types/function/module.f.js";
|
|
6
|
+
const { compose, fn } = f;
|
|
7
|
+
const { entries } = Object;
|
|
8
|
+
import * as bi from "../../types/bigint/module.f.js";
|
|
9
|
+
const { serialize: bigintSerialize } = bi;
|
|
10
|
+
import * as j from "../../json/serializer/module.f.js";
|
|
11
|
+
const { objectWrap, arrayWrap, stringSerialize, numberSerialize, nullSerialize, boolSerialize } = j;
|
|
12
|
+
const colon = [':'];
|
|
13
|
+
const undefinedSerialize = ['undefined'];
|
|
14
|
+
export const serialize = sort => {
|
|
15
|
+
const propertySerialize = ([k, v]) => flat([
|
|
16
|
+
stringSerialize(k),
|
|
17
|
+
colon,
|
|
18
|
+
f(v)
|
|
19
|
+
]);
|
|
20
|
+
const mapPropertySerialize = map(propertySerialize);
|
|
21
|
+
const objectSerialize = fn(entries)
|
|
22
|
+
.then(sort)
|
|
23
|
+
.then(mapPropertySerialize)
|
|
24
|
+
.then(objectWrap)
|
|
25
|
+
.result;
|
|
26
|
+
const f = value => {
|
|
27
|
+
switch (typeof value) {
|
|
28
|
+
case 'boolean': {
|
|
29
|
+
return boolSerialize(value);
|
|
30
|
+
}
|
|
31
|
+
case 'number': {
|
|
32
|
+
return numberSerialize(value);
|
|
33
|
+
}
|
|
34
|
+
case 'string': {
|
|
35
|
+
return stringSerialize(value);
|
|
36
|
+
}
|
|
37
|
+
case 'bigint': {
|
|
38
|
+
return [bigintSerialize(value)];
|
|
39
|
+
}
|
|
40
|
+
default: {
|
|
41
|
+
if (value === null) {
|
|
42
|
+
return nullSerialize;
|
|
43
|
+
}
|
|
44
|
+
if (value === undefined) {
|
|
45
|
+
return undefinedSerialize;
|
|
46
|
+
}
|
|
47
|
+
if (value instanceof Array) {
|
|
48
|
+
return arraySerialize(value);
|
|
49
|
+
}
|
|
50
|
+
return objectSerialize(value);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const arraySerialize = compose(map(f))(arrayWrap);
|
|
55
|
+
return f;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* The standard `JSON.stringify` rules determined by
|
|
59
|
+
* https://262.ecma-international.org/6.0/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys
|
|
60
|
+
*/
|
|
61
|
+
export const stringify = sort => compose(serialize(sort))(concat);
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import * as json from "../../json/module.f.js";
|
|
2
|
+
import * as list from "../../types/object/module.f.js";
|
|
3
|
+
const { sort } = list;
|
|
4
|
+
import * as f from "../../types/function/module.f.js";
|
|
5
|
+
const { identity } = f;
|
|
6
|
+
import * as serializer from "./module.f.js";
|
|
7
|
+
export default {
|
|
8
|
+
stringify: [
|
|
9
|
+
{
|
|
10
|
+
sort: () => {
|
|
11
|
+
const r = json.setProperty("Hello")(['a'])({});
|
|
12
|
+
const x = serializer.stringify(sort)(r);
|
|
13
|
+
if (x !== '{"a":"Hello"}') {
|
|
14
|
+
throw x;
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
identity: () => {
|
|
18
|
+
const x = serializer.stringify(identity)(json.setProperty("Hello")(['a'])({}));
|
|
19
|
+
if (x !== '{"a":"Hello"}') {
|
|
20
|
+
throw x;
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
sort: () => {
|
|
26
|
+
const x = serializer.stringify(sort)(json.setProperty("Hello")(['a'])({ c: [], b: 12 }));
|
|
27
|
+
if (x !== '{"a":"Hello","b":12,"c":[]}') {
|
|
28
|
+
throw x;
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
identity: () => {
|
|
32
|
+
const x = serializer.stringify(identity)(json.setProperty("Hello")(['a'])({ c: [], b: 12 }));
|
|
33
|
+
if (x !== '{"c":[],"b":12,"a":"Hello"}') {
|
|
34
|
+
throw x;
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
sort: () => {
|
|
40
|
+
const _0 = { a: { y: [24] }, c: [], b: 12 };
|
|
41
|
+
const _1 = json.setProperty("Hello")(['a', 'x'])(_0);
|
|
42
|
+
const _2 = serializer.stringify(sort)(_1);
|
|
43
|
+
if (_2 !== '{"a":{"x":"Hello","y":[24]},"b":12,"c":[]}') {
|
|
44
|
+
throw _2;
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
identity: () => {
|
|
48
|
+
const _0 = { a: { y: [24] }, c: [], b: 12 };
|
|
49
|
+
const _1 = json.setProperty("Hello")(['a', 'x'])(_0);
|
|
50
|
+
const _2 = serializer.stringify(identity)(_1);
|
|
51
|
+
if (_2 !== '{"a":{"y":[24],"x":"Hello"},"c":[],"b":12}') {
|
|
52
|
+
throw _2;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
stringify: () => {
|
|
58
|
+
const bi = 1234567890n;
|
|
59
|
+
const result = serializer.stringify(sort)(bi);
|
|
60
|
+
if (result !== '1234567890n') {
|
|
61
|
+
throw result;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
stringify: () => {
|
|
67
|
+
const arr = [0n, 1, 2n];
|
|
68
|
+
const result = serializer.stringify(sort)(arr);
|
|
69
|
+
if (result !== '[0n,1,2n]') {
|
|
70
|
+
throw result;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
stringify: () => {
|
|
76
|
+
const obj = { "a": 0n, "b": 1, "c": 2n };
|
|
77
|
+
const result = serializer.stringify(sort)(obj);
|
|
78
|
+
if (result !== '{"a":0n,"b":1,"c":2n}') {
|
|
79
|
+
throw result;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
]
|
|
84
|
+
};
|
package/djs/test/input.f.js
CHANGED
package/djs/tokenizer/test.f.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as tokenizer from "./module.f.js";
|
|
2
2
|
import * as list from "../../types/list/module.f.js";
|
|
3
3
|
const { toArray } = list;
|
|
4
|
-
import * as serializer from "../serializer/module.f.js";
|
|
4
|
+
import * as serializer from "../serializer-old/module.f.js";
|
|
5
5
|
import * as o from "../../types/object/module.f.js";
|
|
6
6
|
const { sort } = o;
|
|
7
7
|
import * as encoding from "../../text/utf16/module.f.js";
|
package/djs/transpiler/test.f.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { sort } from "../../types/object/module.f.js";
|
|
2
2
|
import { setReplace } from "../../types/ordered_map/module.f.js";
|
|
3
3
|
import { transpile } from "./module.f.js";
|
|
4
|
-
import { stringify } from "../serializer/module.f.js";
|
|
4
|
+
import { stringify } from "../serializer-old/module.f.js";
|
|
5
5
|
import { createVirtualIo } from "../../io/virtual/module.f.js";
|
|
6
6
|
const virtualFs = map => {
|
|
7
7
|
return createVirtualIo(map).fs;
|
package/js/tokenizer/test.f.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as tokenizer from "./module.f.js";
|
|
2
2
|
import * as list from "../../types/list/module.f.js";
|
|
3
3
|
const { toArray } = list;
|
|
4
|
-
import * as serializer from "../../djs/serializer/module.f.js";
|
|
4
|
+
import * as serializer from "../../djs/serializer-old/module.f.js";
|
|
5
5
|
import * as o from "../../types/object/module.f.js";
|
|
6
6
|
const { sort } = o;
|
|
7
7
|
import * as encoding from "../../text/utf16/module.f.js";
|
package/json/tokenizer/test.f.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as tokenizer from "./module.f.js";
|
|
2
2
|
import * as list from "../../types/list/module.f.js";
|
|
3
3
|
const { toArray } = list;
|
|
4
|
-
import * as serializer from "../../djs/serializer/module.f.js";
|
|
4
|
+
import * as serializer from "../../djs/serializer-old/module.f.js";
|
|
5
5
|
import * as o from "../../types/object/module.f.js";
|
|
6
6
|
const { sort } = o;
|
|
7
7
|
import * as encoding from "../../text/utf16/module.f.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "functionalscript",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"**/*.js",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
},
|
|
47
47
|
"homepage": "https://github.com/functionalscript/functionalscript#readme",
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@types/node": "^22.
|
|
49
|
+
"@types/node": "^22.15.17",
|
|
50
50
|
"typescript": "^5.8.3"
|
|
51
51
|
}
|
|
52
52
|
}
|