functionalscript 0.6.7 → 0.6.8

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 CHANGED
@@ -21,6 +21,31 @@ Learn more about
21
21
 
22
22
  This repository is a [monorepo](https://en.wikipedia.org/wiki/Monorepo) and distributted under [AGPL-3.0](https://www.gnu.org/licenses/agpl-3.0.en.html#license-text). Let us know if you need another license by sending an [email](mailto:sergey.oss@proton.me).
23
23
 
24
+ ## Getting Started
25
+
26
+ Install FunctionalScript via npm:
27
+
28
+ ```bash
29
+ npm install functionalscript
30
+ ```
31
+
32
+ The FunctionalScript compiler (`fsc`) currently supports:
33
+
34
+ * `import` statements
35
+ * `const` declarations
36
+
37
+ It does **not** yet support functions or complex expressions.
38
+
39
+ Example usage with `fsc`:
40
+
41
+ ```bash
42
+ npx fsc example.f.js output.json
43
+ # or
44
+ npx fsc example.f.js output.f.js
45
+ ```
46
+
47
+ FunctionalScript code can be compiled directly into either JSON or JavaScript without imports.
48
+
24
49
  ## Vision
25
50
 
26
51
  We aim to create a safe, cross-platform programming language that can work in any JS platform without any build step. There are thousands of programming languages, and we don't want to create another one that others must learn. Instead, we take the opposite approach: we remove everything that makes the most popular and cross-platform language unsafe, insecure, or less portable.
package/djs/ast/test.f.js CHANGED
@@ -1,39 +1,39 @@
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-old/module.f.js";
4
+ import { stringifyAsTree } from "../serializer/module.f.js";
5
5
  export default {
6
6
  test: () => {
7
7
  const djs = shared.run([1])([]);
8
- const result = stringify(sort)(djs);
8
+ const result = stringifyAsTree(sort)(djs);
9
9
  if (result !== '1') {
10
10
  throw result;
11
11
  }
12
12
  },
13
13
  testCref: () => {
14
14
  const djs = shared.run([1, 2, 3, 4, 5, ['cref', 3]])([11, 12, 13, 14, 15]);
15
- const result = stringify(sort)(djs);
15
+ const result = stringifyAsTree(sort)(djs);
16
16
  if (result !== '4') {
17
17
  throw result;
18
18
  }
19
19
  },
20
20
  testAref: () => {
21
21
  const djs = shared.run([1, 2, 3, 4, 5, ['aref', 3]])([11, 12, 13, 14, 15]);
22
- const result = stringify(sort)(djs);
22
+ const result = stringifyAsTree(sort)(djs);
23
23
  if (result !== '14') {
24
24
  throw result;
25
25
  }
26
26
  },
27
27
  testArray: () => {
28
28
  const djs = shared.run([1, 2, 3, 4, 5, ['array', [['aref', 3], ['cref', 3]]]])([11, 12, 13, 14, 15]);
29
- const result = stringify(sort)(djs);
29
+ const result = stringifyAsTree(sort)(djs);
30
30
  if (result !== '[14,4]') {
31
31
  throw result;
32
32
  }
33
33
  },
34
34
  testObj: () => {
35
35
  const djs = shared.run([1, 2, 3, 4, 5, { "key": { "key2": ['array', [['aref', 3], ['cref', 3]]] } }])([11, 12, 13, 14, 15]);
36
- const result = stringify(sort)(djs);
36
+ const result = stringifyAsTree(sort)(djs);
37
37
  if (result !== '{"key":{"key2":[14,4]}}') {
38
38
  throw result;
39
39
  }
package/djs/module.f.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import { transpile } from "./transpiler/module.f.js";
2
- import { stringify } from "./serializer/module.f.js";
2
+ import { stringify, stringifyAsTree } from "./serializer/module.f.js";
3
3
  import { sort } from "../types/object/module.f.js";
4
- const stringifyUnknown = stringify(sort);
5
4
  export const compile = ({ console: { error }, fs, process: { argv } }) => {
6
5
  const args = argv.slice(2);
7
6
  if (args.length < 2) {
@@ -14,11 +13,11 @@ export const compile = ({ console: { error }, fs, process: { argv } }) => {
14
13
  switch (result[0]) {
15
14
  case 'ok': {
16
15
  if (outputFileName.endsWith('.json')) {
17
- const output = JSON.stringify(result[1]);
16
+ const output = stringifyAsTree(sort)(result[1]);
18
17
  fs.writeFileSync(outputFileName, output);
19
18
  break;
20
19
  }
21
- const output = stringifyUnknown(result[1]);
20
+ const output = stringify(sort)(result[1]);
22
21
  fs.writeFileSync(outputFileName, output);
23
22
  break;
24
23
  }
@@ -3,9 +3,9 @@ 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-old/module.f.js";
6
+ import { stringifyAsTree } from "../serializer/module.f.js";
7
7
  const tokenizeString = s => toArray(tokenizer.tokenize(encoding.stringToList(s)));
8
- const stringifyDjsModule = stringify(sort);
8
+ const stringifyDjsModule = stringifyAsTree(sort);
9
9
  export default {
10
10
  valid: [
11
11
  () => {
@@ -7,6 +7,8 @@ type Entry = O.Entry<djs.Unknown>;
7
7
  type Entries = List<Entry>;
8
8
  type MapEntries = (entries: Entries) => Entries;
9
9
  type Refs = Map<djs.Unknown, RefCounter>;
10
+ export declare const serializeWithoutConst: (mapEntries: MapEntries) => (value: djs.Unknown) => List<string>;
10
11
  export declare const stringify: (sort: MapEntries) => (djs: djs.Unknown) => string;
12
+ export declare const stringifyAsTree: (mapEntries: MapEntries) => (value: djs.Unknown) => string;
11
13
  export declare const countRefs: (djs: djs.Unknown) => Refs;
12
14
  export {};
@@ -5,8 +5,7 @@ import { flat, flatMap, map, concat as listConcat } from "../../types/list/modul
5
5
  const { entries } = Object;
6
6
  import * as f from "../../types/function/module.f.js";
7
7
  const { compose, fn } = f;
8
- import * as bi from "../../types/bigint/module.f.js";
9
- const { serialize: bigintSerialize } = bi;
8
+ import { serialize as bigintSerialize } from "../../types/bigint/module.f.js";
10
9
  import * as serializer from "../../json/serializer/module.f.js";
11
10
  const { objectWrap, arrayWrap, stringSerialize, numberSerialize, nullSerialize, boolSerialize } = serializer;
12
11
  const colon = [':'];
@@ -49,7 +48,50 @@ const getConstants = djs => refs => {
49
48
  return getConstantsOp(djs)(refs);
50
49
  };
51
50
  const entryValue = kv => kv[1];
52
- const serialize = sort => refs => root => {
51
+ export const serializeWithoutConst = sort => {
52
+ const propertySerialize = ([k, v]) => flat([
53
+ stringSerialize(k),
54
+ colon,
55
+ f(v)
56
+ ]);
57
+ const mapPropertySerialize = map(propertySerialize);
58
+ const objectSerialize = fn(entries)
59
+ .then(sort)
60
+ .then(mapPropertySerialize)
61
+ .then(objectWrap)
62
+ .result;
63
+ const f = value => {
64
+ switch (typeof value) {
65
+ case 'boolean': {
66
+ return boolSerialize(value);
67
+ }
68
+ case 'number': {
69
+ return numberSerialize(value);
70
+ }
71
+ case 'string': {
72
+ return stringSerialize(value);
73
+ }
74
+ case 'bigint': {
75
+ return [bigintSerialize(value)];
76
+ }
77
+ default: {
78
+ if (value === null) {
79
+ return nullSerialize;
80
+ }
81
+ if (value === undefined) {
82
+ return undefinedSerialize;
83
+ }
84
+ if (value instanceof Array) {
85
+ return arraySerialize(value);
86
+ }
87
+ return objectSerialize(value);
88
+ }
89
+ }
90
+ };
91
+ const arraySerialize = compose(map(f))(arrayWrap);
92
+ return f;
93
+ };
94
+ const serializeWithConst = sort => refs => root => {
53
95
  const propertySerialize = ([k, v]) => flat([
54
96
  stringSerialize(k),
55
97
  colon,
@@ -141,12 +183,13 @@ export const stringify = sort => djs => {
141
183
  if (refCounter === undefined) {
142
184
  throw 'unexpected behaviour';
143
185
  }
144
- return flat([['const c'], numberSerialize(refCounter[0]), [' = '], serialize(sort)(refs)(entry)(entry), ['\n']]);
186
+ return flat([['const c'], numberSerialize(refCounter[0]), [' = '], serializeWithConst(sort)(refs)(entry)(entry), ['\n']]);
145
187
  };
146
188
  const constStrings = flatMap(constSerialize)(consts);
147
- const rootStrings = listConcat(['export default '])(serialize(sort)(refs)(djs)(djs));
189
+ const rootStrings = listConcat(['export default '])(serializeWithConst(sort)(refs)(djs)(djs));
148
190
  return concat(listConcat(constStrings)(rootStrings));
149
191
  };
192
+ export const stringifyAsTree = sort => compose(serializeWithoutConst(sort))(concat);
150
193
  export const countRefs = djs => {
151
194
  return countRefsOp(djs)(new Map());
152
195
  };
@@ -1,8 +1,25 @@
1
1
  declare const _default: {
2
- testPrimitives: () => void;
3
- testArray: () => void;
4
- testObj: () => void;
5
- testSort: () => void;
6
- testIdentity: () => void;
2
+ stringify: ({
3
+ testPrimitives: () => void;
4
+ testArray: () => void;
5
+ testObj: () => void;
6
+ testSort?: never;
7
+ testIdentity?: never;
8
+ } | {
9
+ testSort: () => void;
10
+ testIdentity: () => void;
11
+ testPrimitives?: never;
12
+ testArray?: never;
13
+ testObj?: never;
14
+ })[];
15
+ stringifyAsTree: ({
16
+ sort: () => void;
17
+ identity: () => void;
18
+ stringify?: never;
19
+ } | {
20
+ stringify: () => void;
21
+ sort?: never;
22
+ identity?: never;
23
+ })[];
7
24
  };
8
25
  export default _default;
@@ -1,78 +1,159 @@
1
- import { countRefs, stringify } from "./module.f.js";
1
+ import { countRefs, stringify, stringifyAsTree } from "./module.f.js";
2
2
  import * as list from "../../types/object/module.f.js";
3
3
  const { sort } = list;
4
- import * as serializer from "../serializer-old/module.f.js";
5
4
  import { identity } from "../../types/function/module.f.js";
6
- const stringifyOld = serializer.stringify(sort);
5
+ import * as json from "../../json/module.f.js";
7
6
  export default {
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;
7
+ stringify: [
8
+ {
9
+ testPrimitives: () => {
10
+ const djs = [1, 2, 2, 2, true, false, undefined, null, 3n, "str"];
11
+ const refs = countRefs(djs);
12
+ if (refs.size !== 3) {
13
+ throw refs.size;
14
+ }
15
+ const refsBigInt = stringifyAsTree(sort)(refs.get(3n));
16
+ if (refsBigInt !== '[0,1,false]') {
17
+ throw refsBigInt;
18
+ }
19
+ const refsString = stringifyAsTree(sort)(refs.get("str"));
20
+ if (refsString !== '[1,1,false]') {
21
+ throw refsString;
22
+ }
23
+ const refsRoot = stringifyAsTree(sort)(refs.get(djs));
24
+ if (refsRoot !== '[2,1,false]') {
25
+ throw refsRoot;
26
+ }
27
+ if (refs.get(null) !== undefined) {
28
+ throw refs.get(null);
29
+ }
30
+ },
31
+ testArray: () => {
32
+ const array = [null];
33
+ const djs = [array, array, array];
34
+ const refs = countRefs(djs);
35
+ if (refs.size !== 2) {
36
+ throw refs.size;
37
+ }
38
+ const refsArray = stringifyAsTree(sort)(refs.get(array));
39
+ if (refsArray !== '[0,3,false]') {
40
+ throw refsArray;
41
+ }
42
+ const refsRoot = stringifyAsTree(sort)(refs.get(djs));
43
+ if (refsRoot !== '[1,1,false]') {
44
+ throw refsRoot;
45
+ }
46
+ },
47
+ testObj: () => {
48
+ const obj = { "a": 1, "b": 2 };
49
+ const djs = [obj, obj, 1];
50
+ const refs = countRefs(djs);
51
+ if (refs.size !== 2) {
52
+ throw refs.size;
53
+ }
54
+ const refsObj = stringifyAsTree(sort)(refs.get(obj));
55
+ if (refsObj !== '[0,2,false]') {
56
+ throw refsObj;
57
+ }
58
+ const refsRoot = stringifyAsTree(sort)(refs.get(djs));
59
+ if (refsRoot !== '[1,1,false]') {
60
+ throw refsRoot;
61
+ }
62
+ },
63
+ },
64
+ {
65
+ testSort: () => {
66
+ const obj = { "a": 1, "c": 2n, "b": [undefined, null, true, false] };
67
+ const djs = [obj, obj, 1];
68
+ const res = stringify(sort)(djs);
69
+ if (res !== 'const c2 = {"a":1,"b":[undefined,null,true,false],"c":2n}\nexport default [c2,c2,1]') {
70
+ throw res;
71
+ }
72
+ },
73
+ testIdentity: () => {
74
+ const obj = { "a": 1, "c": 2n, "b": [undefined, null, true, false] };
75
+ const djs = [obj, obj, 1];
76
+ const res = stringify(identity)(djs);
77
+ if (res !== 'const c2 = {"a":1,"c":2n,"b":[undefined,null,true,false]}\nexport default [c2,c2,1]') {
78
+ throw res;
79
+ }
80
+ },
13
81
  }
14
- const refsBigInt = stringifyOld(refs.get(3n));
15
- if (refsBigInt !== '[0,1,false]') {
16
- throw refsBigInt;
82
+ ],
83
+ stringifyAsTree: [
84
+ {
85
+ sort: () => {
86
+ const r = json.setProperty("Hello")(['a'])({});
87
+ const x = stringifyAsTree(sort)(r);
88
+ if (x !== '{"a":"Hello"}') {
89
+ throw x;
90
+ }
91
+ },
92
+ identity: () => {
93
+ const x = stringifyAsTree(identity)(json.setProperty("Hello")(['a'])({}));
94
+ if (x !== '{"a":"Hello"}') {
95
+ throw x;
96
+ }
97
+ },
98
+ },
99
+ {
100
+ sort: () => {
101
+ const x = stringifyAsTree(sort)(json.setProperty("Hello")(['a'])({ c: [], b: 12 }));
102
+ if (x !== '{"a":"Hello","b":12,"c":[]}') {
103
+ throw x;
104
+ }
105
+ },
106
+ identity: () => {
107
+ const x = stringifyAsTree(identity)(json.setProperty("Hello")(['a'])({ c: [], b: 12 }));
108
+ if (x !== '{"c":[],"b":12,"a":"Hello"}') {
109
+ throw x;
110
+ }
111
+ },
112
+ },
113
+ {
114
+ sort: () => {
115
+ const _0 = { a: { y: [24] }, c: [], b: 12 };
116
+ const _1 = json.setProperty("Hello")(['a', 'x'])(_0);
117
+ const _2 = stringifyAsTree(sort)(_1);
118
+ if (_2 !== '{"a":{"x":"Hello","y":[24]},"b":12,"c":[]}') {
119
+ throw _2;
120
+ }
121
+ },
122
+ identity: () => {
123
+ const _0 = { a: { y: [24] }, c: [], b: 12 };
124
+ const _1 = json.setProperty("Hello")(['a', 'x'])(_0);
125
+ const _2 = stringifyAsTree(identity)(_1);
126
+ if (_2 !== '{"a":{"y":[24],"x":"Hello"},"c":[],"b":12}') {
127
+ throw _2;
128
+ }
129
+ }
130
+ },
131
+ {
132
+ stringify: () => {
133
+ const bi = 1234567890n;
134
+ const result = stringifyAsTree(sort)(bi);
135
+ if (result !== '1234567890n') {
136
+ throw result;
137
+ }
138
+ }
139
+ },
140
+ {
141
+ stringify: () => {
142
+ const arr = [0n, 1, 2n];
143
+ const result = stringifyAsTree(sort)(arr);
144
+ if (result !== '[0n,1,2n]') {
145
+ throw result;
146
+ }
147
+ }
148
+ },
149
+ {
150
+ stringify: () => {
151
+ const obj = { "a": 0n, "b": 1, "c": 2n };
152
+ const result = stringifyAsTree(sort)(obj);
153
+ if (result !== '{"a":0n,"b":1,"c":2n}') {
154
+ throw result;
155
+ }
156
+ }
17
157
  }
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
- }
158
+ ]
78
159
  };
@@ -1,12 +1,12 @@
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-old/module.f.js";
4
+ import * as serializer from "../serializer/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";
8
8
  const tokenizeString = s => toArray(tokenizer.tokenize(encoding.stringToList(s)));
9
- const stringify = serializer.stringify(sort);
9
+ const stringify = serializer.stringifyAsTree(sort);
10
10
  export default {
11
11
  djs: [
12
12
  () => {
@@ -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-old/module.f.js";
4
+ import { stringifyAsTree } from "../serializer/module.f.js";
5
5
  import { createVirtualIo } from "../../io/virtual/module.f.js";
6
6
  const virtualFs = map => {
7
7
  return createVirtualIo(map).fs;
@@ -14,7 +14,7 @@ export default {
14
14
  if (result[0] === 'error') {
15
15
  throw result[1];
16
16
  }
17
- const s = stringify(sort)(result[1]);
17
+ const s = stringifyAsTree(sort)(result[1]);
18
18
  if (s !== '1') {
19
19
  throw s;
20
20
  }
@@ -27,7 +27,7 @@ export default {
27
27
  if (result[0] === 'error') {
28
28
  throw result[1];
29
29
  }
30
- const s = stringify(sort)(result[1]);
30
+ const s = stringifyAsTree(sort)(result[1]);
31
31
  if (s !== '2') {
32
32
  throw s;
33
33
  }
@@ -42,7 +42,7 @@ export default {
42
42
  if (result[0] === 'error') {
43
43
  throw result[1];
44
44
  }
45
- const s = stringify(sort)(result[1]);
45
+ const s = stringifyAsTree(sort)(result[1]);
46
46
  if (s !== '[[0,2],[1,2],[0,2]]') {
47
47
  throw s;
48
48
  }
@@ -1,12 +1,12 @@
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-old/module.f.js";
4
+ import * as serializer from "../../djs/serializer/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";
8
8
  const tokenizeString = s => toArray(tokenizer.tokenize(encoding.stringToList(s)));
9
- const stringify = serializer.stringify(sort);
9
+ const stringify = serializer.stringifyAsTree(sort);
10
10
  export default {
11
11
  djs: [
12
12
  () => {
@@ -1,12 +1,12 @@
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-old/module.f.js";
4
+ import * as serializer from "../../djs/serializer/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";
8
8
  const tokenizeString = s => toArray(tokenizer.tokenize(encoding.stringToList(s)));
9
- const stringify = serializer.stringify(sort);
9
+ const stringify = serializer.stringifyAsTree(sort);
10
10
  export default {
11
11
  json: [
12
12
  () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.6.7",
3
+ "version": "0.6.8",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "**/*.js",
@@ -1,13 +0,0 @@
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 {};
@@ -1,61 +0,0 @@
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);
@@ -1,12 +0,0 @@
1
- declare const _default: {
2
- stringify: ({
3
- sort: () => void;
4
- identity: () => void;
5
- stringify?: never;
6
- } | {
7
- stringify: () => void;
8
- sort?: never;
9
- identity?: never;
10
- })[];
11
- };
12
- export default _default;
@@ -1,84 +0,0 @@
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
- };