-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
405 lines (391 loc) · 16.5 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright (c) 2022 System233
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
/**
* TSONSerializer interface
*/
export interface TSONSerializer<T = any, D = any> {
/**A function to deserialize the data.
* @param data A value to be deserialized.
* @returns Deserialized data for `data`
*/
load(data: D): T;
/**A function to serialize the value.
* @param value A value to be serialized.
* @returns Serialized data for `value`
*/
dump(value: T): D;
/**A function to check the value matches this serializer.
* @param value A value to be checked.
* @returns Match if `true`, otherwise pass.
*/
match(value: any): boolean;
/**The name of the serializer*/
readonly type: string;
/**Whether to process `load()` input and `dump()` output recursively. */
readonly recursive: boolean;
}
/**
* TSONData interface
*/
export interface TSONData<T = any, D = any> {
/**The name of the serializer that handles this data.*/
type: T;
/**Serialized data from serializer.*/
data: D;
}
/**ArrayBuffer serialized data interface */
export interface ArrayBfferData {
buffer: ArrayBuffer;
offset: number;
length: number;
};
/**
TSON - A Type-safe Serializer like JSON
*/
export class TSON {
private readonly types: TSONSerializer[] = [];
private readonly map: Record<string, TSONSerializer> = {};
/** @hidden */
private readonly refname = '$ref';
constructor() {
this.register(Object, x => x, x => x, null, true);
this.register(String, x => new String(x), x => x.valueOf());
this.register(Number, x => new Number(x), x => x.valueOf());
this.register(Boolean, x => new Boolean(x), x => x.valueOf());
this.register(RegExp, (x: { source: string, flags: string }) => new RegExp(x.source, x.flags), x => ({ source: x.source, flags: x.flags }));
this.register(Set, (x: any[]) => new Set(x), x => Array.from(x.values()), null, true);
this.register(Map, (x: [any, any][]) => new Map(x), x => Array.from(x.entries()), null, true);
this.register(ArrayBuffer, (x: ArrayLike<number>) => new Uint8Array(x).buffer, x => Array.from(new Uint8Array(x)));
[
Int8Array, Uint8Array, Uint8ClampedArray,
Int16Array, Uint16Array,
Int32Array, Uint32Array,
Float32Array, Float64Array,
BigInt64Array, BigUint64Array
].forEach(constructor => this.register(constructor,
(x: ArrayBfferData) => new constructor(x.buffer, x.offset, x.length),
(x): ArrayBfferData => ({ buffer: x.buffer, offset: x.byteOffset, length: x.length }),
null, true));
this.register<DataView, ArrayBfferData>(DataView,
data => new DataView(data.buffer, data.offset, data.length),
value => ({ buffer: value.buffer, offset: value.byteOffset, length: value.byteLength }),
null, true)
this.register<Array<any>, Array<any>>('Array', x => x, x => x, x => Array.isArray(x), true);
this.register<Date, string>('Date', x => new Date(x), x => x.toJSON(), x => x instanceof Date);
this.register<Number, void>('NaN', () => NaN, () => void 0, x => Number.isNaN(x));
this.register<Number, boolean>('Infinity', (x) => x ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY, (x) => x > 0, x => typeof x == 'number' && !Number.isNaN(x) && !Number.isFinite(x));
const builtins=Object.values(Object.getOwnPropertyDescriptors(Symbol)).filter(value => typeof value.value == 'symbol').map(x=>x.value as symbol);
const n2smap= Object.fromEntries(builtins.map(symbol=>[symbol.description,symbol]));
const s2nmap= Object.fromEntries(builtins.map(symbol=>[symbol,symbol.description]));
this.register<symbol, { description: string | undefined, builtin:boolean , shared: boolean }>('symbol',
data => {
if (!data.description) {
return Symbol();
}
if (data.builtin) {
return n2smap[data.description];
}
if(data.shared){
return Symbol.for(data.description);
}
return Symbol(data.description);
},
value => ({
description: value.description,
builtin:value in s2nmap,
shared: value.description!=null&&!!Symbol.for(value.description)
}),
value => typeof value == 'symbol');
this.register<bigint, string>('bigint', x => BigInt(x), x => x.toString(), x => typeof x == 'bigint');
this.register<undefined, undefined>('undefined', () => void 0, () => void 0, x => typeof x == 'undefined');
this.register<boolean, boolean>('boolean', x => x, x => x, x => typeof x == 'boolean');
this.register<string, string>('string', x => x, x => x, x => typeof x == 'string');
this.register<number, number>('number', x => x, x => x, x => typeof x == 'number'&&!Number.isNaN(x)&&Number.isFinite(x));
}
/** @hidden */
private referenceable(value: any) {
return value != null && (typeof value == 'object' || typeof value == 'symbol')
}
/** @hidden */
private reference(path: any[]): TSONData {
return {
type: this.refname,
data: path
}
}
/** @hidden */
private dereference(data: TSONData) {
if (data.type == this.refname) {
return data.data;
}
return null;
}
/**
* Transform the array of values to an array of TSONData objects.
* @param value Array of values.
* @returns The array of `TSONData` objects of the `value`.
*/
forward(value: any[]): TSONData[];
/**
* Transform the value to a TSONData object.
* @param value A value whose type can be object,number,boolean,or other type.
* @returns The `TSONData` object of the `value`.
*/
forward(value: any): TSONData;
/**
* Transform the value to a TSONData object.
* The implementation of all `forward` overloads.
* @param value A value to be transformed.
* @returns The `TSONData` object of the `value`.
*/
forward(value: any): TSONData | TSONData[] | null {
const map: Map<any, any[]> = new Map;
const reference = (value: any, path: any[]) => {
const ref = map.get(value);
if (ref == null) {
map.set(value, path);
return null;
}
return this.reference(ref);
}
const iforward = (value: any, path: any[]): TSONData | TSONData[] | null => {
if (value === null) {
return null;
}
if (this.referenceable(value)) {
const ref = reference(value, path);
if (ref) {
return ref;
}
}
const typeDef = this.types.find(d => d.match(value));
if (!typeDef) {
return value;
}
const data = typeDef.dump(value);
const type = typeDef.type;
if (typeDef.recursive && data != null && typeof data == 'object') {
if (Array.isArray(data)) {
return {
type,
data: data.map((item, index) => iforward(item, [...path, index]))
}
}
return {
type,
data: Object.fromEntries(
Object.entries(data).map(([key, value]) => {
return [key, iforward(value, [...path, key])];
}))
}
}
return {
type,
data
}
}
return iforward(value, []);
}
/**
* Transform the `TSONData` back to the original object.
* @param value A TSONData object or array of TSONData objects to be transformed.
* @returns The original object of the `TSONData` object.
*/
backward<T = any>(value: TSONData | TSONData[]): T {
let refs: [TSONData, string[]][] = [];
const map: Record<string, any> = {};
const get = (path: any[]) => {
const key = JSON.stringify(path);
if (key in map) {
return map[key];
}
return null;
}
const set = (path: any[], value: any) => {
if (this.referenceable(value)) {
const key = JSON.stringify(path);
map[key] = value;
}
}
const ibackward = (value: TSONData | TSONData[], path: any[]): any => {
const record = (value: any) => {
set(path, value);
return value;
}
if (value === null) {
return null;
}
if (Array.isArray(value)) {
return record(value.map((item, index) => ibackward(item, [...path, index])));
}
const ref = this.dereference(value);
if (ref != null) {
const obj = get(ref);
if (!obj) {
refs.push([value, path]);
}
return obj;
}
const { type, data } = value;
const typeDef = this.map[type];
if (typeDef != null) {
if (typeDef.recursive && data != null && typeof data == 'object') {
if (Array.isArray(data)) {
return record(typeDef.load(data.map((item, index) => ibackward(item, [...path, index]))));
}
return record(typeDef.load(Object.fromEntries(Object.entries(data).map(([key, value]) => {
return [key, ibackward(value as any, [...path, key])]
}))));
}
return record(typeDef.load(data));
}
return record(data);
}
const data = ibackward(value, []);
while (refs.length) {
const cached = refs;
refs = [];
cached.forEach(([value, path]) => {
const distKey = path[path.length - 1];
const distObj = get(path.slice(0, -1));
distObj[distKey] = ibackward(value, path);
});
}
return data;
}
/**
* Wrapper for `JSON.stringify(TSON.forward(value),replacer,space)`\
* Converts a JavaScript value to a TSON string.
* @param value A JavaScript value, usually an object or array, to be converted.
* @param replacer A function that transforms the results.
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
*/
stringify(value: any, replacer?: ((this: any, key: string, value: any) => any) | null, space?: number): string {
return JSON.stringify(this.forward(value), replacer as any, space);
}
/**
* Wrapper for `TSON.backward(JSON.parse(text, reviver))`\
* Converts a TSON string into an object.
* @param text A valid TSON string.
* @param reviver A function that transforms the results. This function is called for each member of the object. If a member contains nested objects, the nested objects are transformed before the parent object is.
*/
parse<T = any>(text: string, reviver?: (this: any, key: string, value: any) => any) {
return this.backward<T>(JSON.parse(text, reviver));
}
/**
* Register a TSON Serializer for Type.
* @template T The original type.
* @template D The serialized type.
* @param serializer A TSONSerializer Object.
* @returns `true` if register was successful, `false` otherwise.
*/
register<T, D>(serializer: TSONSerializer<T, D>): boolean;
/**
* Register a TSON Serializer for Type.
* @template T The original type.
* @template D The serialized type.
* @param name The name of the serializer.
* @param load A function to deserialize the data.
* @param dump A function to serialize the value.
* @param match A function that checks the value matches this serializer.
* @param recursive Whether to process `load()` input and `dump()` output recursively.
* @returns `true` if register was successful, `false` otherwise.
*/
register<T, D>(name: string, load: (data: D) => T, dump: (value: T) => D, match: (value: any) => boolean, recursive?: boolean): boolean;
/**
* Register a TSON Serializer for Type.
* @template T The original type.
* @template D The serialized type.
* @param constructor Type constructor.
* @param load A function to deserialize the data.
* @param dump A function to serialize the value.
* @param match A function that checks the value matches this serializer. If null, `value instanceof constructor` is used by default.
* @param recursive Whether to process `load()` input and `dump()` output recursively.
* @returns `true` if register was successful, `false` otherwise.
*/
register<T, D>(constructor: { new(...data: any): T }, load: (data: D) => T, dump: (value: T) => D, match?: ((value: any) => boolean) | null, recursive?: boolean): boolean;
/**
* Register a TSON Serializer for Type.
* The implementation of all `register` overloads.
* @param serializer TSONSerializer object or string name or type constructor.
* @param load A function to deserialize the data.
* @param dump A function to serialize the value.
* @param match A function that checks the value matches this serializer. If null, `value instanceof constructor` is used by default.
* @param recursive Whether to process `load()` input and `dump()` output recursively.
* @returns `true` if register was successful, `false` otherwise.
*/
register(serializer: TSONSerializer | string | Function, load?: (data: any) => any, dump?: (value: any) => any, match?: (value: any) => boolean, recursive?: boolean): boolean {
if (typeof serializer == 'string') {
if (!load || !dump || !match) {
return false;
}
return this.register({
type: serializer,
load,
dump,
match,
recursive: !!recursive
});
}
if (typeof serializer == 'function') {
if (!load || !dump) {
return false;
}
return this.register({
type: serializer.name,
load,
dump,
match: match || (x => x instanceof serializer),
recursive: !!recursive
});
}
if (serializer.type in this.map) {
return false;
}
this.types.splice(0, 0, serializer);
this.map[serializer.type] = serializer;
return true;
}
/**
* Deregister a TSON Serializer.
* @param name The name of the serializer.
* @returns `true` if deregister was successful, `false` otherwise.
*/
deregister(name: string) {
if (name in this.map) {
const index = this.types.findIndex(x => x.type == name);
this.types.splice(index, 1);
delete this.map[name];
return true;
}
return false;
}
static readonly instance = new TSON;
/**Link to {@link TSON.forward}. */
static readonly forward = this.instance.forward.bind(this.instance);
/**Link to {@link TSON.backward}. */
static readonly backward = this.instance.backward.bind(this.instance);
/**Link to {@link TSON.stringify}. */
static readonly stringify = this.instance.stringify.bind(this.instance);
/**Link to {@link TSON.parse}. */
static readonly parse = this.instance.parse.bind(this.instance);
/**Link to {@link TSON.register}. */
static readonly register = this.instance.register.bind(this.instance);
/**Link to {@link TSON.deregister}. */
static readonly deregister = this.instance.deregister.bind(this.instance);
}
/**Link to {@link TSON.forward}. */
export const forward = TSON.forward;
/**Link to {@link TSON.backward}. */
export const backward = TSON.backward;
/**Link to {@link TSON.stringify}. */
export const stringify = TSON.stringify;
/**Link to {@link TSON.parse}. */
export const parse = TSON.parse;
/**Link to {@link TSON.register}. */
export const register = TSON.register;
/**Link to {@link TSON.deregister}. */
export const deregister = TSON.deregister;
export default TSON;