@colyseus/schema 2.0.4 → 2.0.6

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 (107) hide show
  1. package/README.md +0 -4
  2. package/build/cjs/index.js +48 -48
  3. package/build/cjs/index.js.map +1 -1
  4. package/build/esm/index.mjs +130 -104
  5. package/build/esm/index.mjs.map +1 -1
  6. package/build/umd/index.js +50 -50
  7. package/lib/Reflection.js +87 -119
  8. package/lib/Reflection.js.map +1 -1
  9. package/lib/Schema.js +195 -257
  10. package/lib/Schema.js.map +1 -1
  11. package/lib/annotations.d.ts +6 -6
  12. package/lib/annotations.js +64 -92
  13. package/lib/annotations.js.map +1 -1
  14. package/lib/changes/ChangeTree.d.ts +1 -1
  15. package/lib/changes/ChangeTree.js +63 -70
  16. package/lib/changes/ChangeTree.js.map +1 -1
  17. package/lib/changes/ReferenceTracker.js +24 -27
  18. package/lib/changes/ReferenceTracker.js.map +1 -1
  19. package/lib/codegen/api.js +9 -9
  20. package/lib/codegen/api.js.map +1 -1
  21. package/lib/codegen/argv.d.ts +1 -1
  22. package/lib/codegen/argv.js +11 -11
  23. package/lib/codegen/argv.js.map +1 -1
  24. package/lib/codegen/cli.js +21 -10
  25. package/lib/codegen/cli.js.map +1 -1
  26. package/lib/codegen/languages/cpp.js +126 -77
  27. package/lib/codegen/languages/cpp.js.map +1 -1
  28. package/lib/codegen/languages/csharp.js +121 -62
  29. package/lib/codegen/languages/csharp.js.map +1 -1
  30. package/lib/codegen/languages/haxe.js +34 -26
  31. package/lib/codegen/languages/haxe.js.map +1 -1
  32. package/lib/codegen/languages/java.js +39 -27
  33. package/lib/codegen/languages/java.js.map +1 -1
  34. package/lib/codegen/languages/js.js +48 -32
  35. package/lib/codegen/languages/js.js.map +1 -1
  36. package/lib/codegen/languages/lua.js +35 -24
  37. package/lib/codegen/languages/lua.js.map +1 -1
  38. package/lib/codegen/languages/ts.js +63 -68
  39. package/lib/codegen/languages/ts.js.map +1 -1
  40. package/lib/codegen/parser.d.ts +9 -1
  41. package/lib/codegen/parser.js +88 -46
  42. package/lib/codegen/parser.js.map +1 -1
  43. package/lib/codegen/types.d.ts +8 -0
  44. package/lib/codegen/types.js +64 -54
  45. package/lib/codegen/types.js.map +1 -1
  46. package/lib/encoding/decode.js +15 -15
  47. package/lib/encoding/decode.js.map +1 -1
  48. package/lib/encoding/encode.js +14 -14
  49. package/lib/encoding/encode.js.map +1 -1
  50. package/lib/events/EventEmitter.d.ts +1 -1
  51. package/lib/events/EventEmitter.js +16 -47
  52. package/lib/events/EventEmitter.js.map +1 -1
  53. package/lib/filters/index.js +7 -8
  54. package/lib/filters/index.js.map +1 -1
  55. package/lib/index.js +11 -11
  56. package/lib/index.js.map +1 -1
  57. package/lib/types/ArraySchema.d.ts +1 -1
  58. package/lib/types/ArraySchema.js +161 -219
  59. package/lib/types/ArraySchema.js.map +1 -1
  60. package/lib/types/CollectionSchema.d.ts +1 -1
  61. package/lib/types/CollectionSchema.js +63 -71
  62. package/lib/types/CollectionSchema.js.map +1 -1
  63. package/lib/types/HelperTypes.d.ts +9 -9
  64. package/lib/types/MapSchema.d.ts +16 -16
  65. package/lib/types/MapSchema.js +68 -78
  66. package/lib/types/MapSchema.js.map +1 -1
  67. package/lib/types/SetSchema.js +62 -71
  68. package/lib/types/SetSchema.js.map +1 -1
  69. package/lib/types/index.js +1 -1
  70. package/lib/types/index.js.map +1 -1
  71. package/lib/types/typeRegistry.js +1 -1
  72. package/lib/types/typeRegistry.js.map +1 -1
  73. package/lib/types/utils.js +9 -10
  74. package/lib/types/utils.js.map +1 -1
  75. package/lib/utils.js +10 -13
  76. package/lib/utils.js.map +1 -1
  77. package/package.json +18 -15
  78. package/src/Reflection.ts +159 -0
  79. package/src/Schema.ts +1024 -0
  80. package/src/annotations.ts +400 -0
  81. package/src/changes/ChangeTree.ts +295 -0
  82. package/src/changes/ReferenceTracker.ts +81 -0
  83. package/src/codegen/api.ts +46 -0
  84. package/src/codegen/argv.ts +40 -0
  85. package/src/codegen/cli.ts +65 -0
  86. package/src/codegen/languages/cpp.ts +297 -0
  87. package/src/codegen/languages/csharp.ts +208 -0
  88. package/src/codegen/languages/haxe.ts +110 -0
  89. package/src/codegen/languages/java.ts +115 -0
  90. package/src/codegen/languages/js.ts +115 -0
  91. package/src/codegen/languages/lua.ts +125 -0
  92. package/src/codegen/languages/ts.ts +129 -0
  93. package/src/codegen/parser.ts +299 -0
  94. package/src/codegen/types.ts +177 -0
  95. package/src/encoding/decode.ts +278 -0
  96. package/src/encoding/encode.ts +283 -0
  97. package/src/filters/index.ts +23 -0
  98. package/src/index.ts +59 -0
  99. package/src/spec.ts +49 -0
  100. package/src/types/ArraySchema.ts +612 -0
  101. package/src/types/CollectionSchema.ts +199 -0
  102. package/src/types/HelperTypes.ts +34 -0
  103. package/src/types/MapSchema.ts +268 -0
  104. package/src/types/SetSchema.ts +208 -0
  105. package/src/types/typeRegistry.ts +19 -0
  106. package/src/types/utils.ts +62 -0
  107. package/src/utils.ts +28 -0
@@ -0,0 +1,612 @@
1
+ import { ChangeTree } from "../changes/ChangeTree";
2
+ import { OPERATION } from "../spec";
3
+ import { SchemaDecoderCallbacks, Schema } from "../Schema";
4
+ import { addCallback, removeChildRefs } from "./utils";
5
+ import { DataChange } from "..";
6
+
7
+ const DEFAULT_SORT = (a: any, b: any) => {
8
+ const A = a.toString();
9
+ const B = b.toString();
10
+ if (A < B) return -1;
11
+ else if (A > B) return 1;
12
+ else return 0
13
+ }
14
+
15
+ export function getArrayProxy(value: ArraySchema) {
16
+ value['$proxy'] = true;
17
+
18
+ //
19
+ // compatibility with @colyseus/schema 0.5.x
20
+ // - allow `map["key"]`
21
+ // - allow `map["key"] = "xxx"`
22
+ // - allow `delete map["key"]`
23
+ //
24
+ value = new Proxy(value, {
25
+ get: (obj, prop) => {
26
+ if (
27
+ typeof (prop) !== "symbol" &&
28
+ !isNaN(prop as any) // https://stackoverflow.com/a/175787/892698
29
+ ) {
30
+ return obj.at(prop as unknown as number);
31
+
32
+ } else {
33
+ return obj[prop];
34
+ }
35
+ },
36
+
37
+ set: (obj, prop, setValue) => {
38
+ if (
39
+ typeof (prop) !== "symbol" &&
40
+ !isNaN(prop as any)
41
+ ) {
42
+ const indexes = Array.from(obj['$items'].keys());
43
+ const key = parseInt(indexes[prop] || prop);
44
+ if (setValue === undefined || setValue === null) {
45
+ obj.deleteAt(key);
46
+
47
+ } else {
48
+ obj.setAt(key, setValue);
49
+ }
50
+
51
+ } else {
52
+ obj[prop] = setValue;
53
+ }
54
+
55
+ return true;
56
+ },
57
+
58
+ deleteProperty: (obj, prop) => {
59
+ if (typeof (prop) === "number") {
60
+ obj.deleteAt(prop);
61
+
62
+ } else {
63
+ delete obj[prop];
64
+ }
65
+
66
+ return true;
67
+ },
68
+ });
69
+
70
+ return value;
71
+ }
72
+
73
+ export class ArraySchema<V = any> implements Array<V>, SchemaDecoderCallbacks {
74
+ protected $changes: ChangeTree = new ChangeTree(this);
75
+
76
+ protected $items: Map<number, V> = new Map<number, V>();
77
+ protected $indexes: Map<number, number> = new Map<number, number>();
78
+
79
+ protected $refId: number = 0;
80
+
81
+ [n: number]: V;
82
+
83
+ //
84
+ // Decoding callbacks
85
+ //
86
+ public $callbacks: { [operation: number]: Array<(item: V, key: number) => void> };
87
+ public onAdd(callback: (item: V, key: number) => void, triggerAll: boolean = true) {
88
+ return addCallback(
89
+ (this.$callbacks || (this.$callbacks = [])),
90
+ OPERATION.ADD,
91
+ callback,
92
+ (triggerAll)
93
+ ? this.$items
94
+ : undefined
95
+ );
96
+ }
97
+ public onRemove(callback: (item: V, key: number) => void) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.DELETE, callback); }
98
+ public onChange(callback: (item: V, key: number) => void) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.REPLACE, callback); }
99
+
100
+ static is(type: any) {
101
+ return (
102
+ // type format: ["string"]
103
+ Array.isArray(type) ||
104
+
105
+ // type format: { array: "string" }
106
+ (type['array'] !== undefined)
107
+ );
108
+ }
109
+
110
+ constructor (...items: V[]) {
111
+ this.push.apply(this, items);
112
+ }
113
+
114
+ set length (value: number) {
115
+ if (value === 0) {
116
+ this.clear();
117
+
118
+ } else {
119
+ this.splice(value, this.length - value);
120
+ }
121
+ }
122
+
123
+ get length() {
124
+ return this.$items.size;
125
+ }
126
+
127
+ push(...values: V[]) {
128
+ let lastIndex: number;
129
+
130
+ values.forEach(value => {
131
+ // set "index" for reference.
132
+ lastIndex = this.$refId++;
133
+
134
+ this.setAt(lastIndex, value);
135
+ });
136
+
137
+ return lastIndex;
138
+ }
139
+
140
+ /**
141
+ * Removes the last element from an array and returns it.
142
+ */
143
+ pop(): V | undefined {
144
+ const key = Array.from(this.$indexes.values()).pop();
145
+ if (key === undefined) { return undefined; }
146
+
147
+ this.$changes.delete(key);
148
+ this.$indexes.delete(key);
149
+
150
+ const value = this.$items.get(key);
151
+ this.$items.delete(key);
152
+
153
+ return value;
154
+ }
155
+
156
+ at(index: number) {
157
+ //
158
+ // FIXME: this should be O(1)
159
+ //
160
+ const key = Array.from(this.$items.keys())[index];
161
+ return this.$items.get(key);
162
+ }
163
+
164
+ setAt(index: number, value: V) {
165
+ if (value['$changes'] !== undefined) {
166
+ (value['$changes'] as ChangeTree).setParent(this, this.$changes.root, index);
167
+ }
168
+
169
+ const operation = this.$changes.indexes[index]?.op ?? OPERATION.ADD;
170
+
171
+ this.$changes.indexes[index] = index;
172
+
173
+ this.$indexes.set(index, index);
174
+ this.$items.set(index, value);
175
+
176
+ this.$changes.change(index, operation);
177
+ }
178
+
179
+ deleteAt(index: number) {
180
+ const key = Array.from(this.$items.keys())[index];
181
+ if (key === undefined) { return false; }
182
+ return this.$deleteAt(key);
183
+ }
184
+
185
+ protected $deleteAt(index) {
186
+ // delete at internal index
187
+ this.$changes.delete(index);
188
+ this.$indexes.delete(index);
189
+
190
+ return this.$items.delete(index);
191
+ }
192
+
193
+ clear(changes?: DataChange[]) {
194
+ // discard previous operations.
195
+ this.$changes.discard(true, true);
196
+ this.$changes.indexes = {};
197
+
198
+ // clear previous indexes
199
+ this.$indexes.clear();
200
+
201
+ //
202
+ // When decoding:
203
+ // - enqueue items for DELETE callback.
204
+ // - flag child items for garbage collection.
205
+ //
206
+ if (changes) {
207
+ removeChildRefs.call(this, changes);
208
+ }
209
+
210
+ // clear items
211
+ this.$items.clear();
212
+
213
+ this.$changes.operation({ index: 0, op: OPERATION.CLEAR });
214
+
215
+ // touch all structures until reach root
216
+ this.$changes.touchParents();
217
+ }
218
+
219
+ /**
220
+ * Combines two or more arrays.
221
+ * @param items Additional items to add to the end of array1.
222
+ */
223
+ concat(...items: (V | ConcatArray<V>)[]): ArraySchema<V> {
224
+ return new ArraySchema(...Array.from(this.$items.values()).concat(...items));
225
+ }
226
+
227
+ /**
228
+ * Adds all the elements of an array separated by the specified separator string.
229
+ * @param separator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.
230
+ */
231
+ join(separator?: string): string {
232
+ return Array.from(this.$items.values()).join(separator);
233
+ }
234
+
235
+ /**
236
+ * Reverses the elements in an Array.
237
+ */
238
+ reverse(): ArraySchema<V> {
239
+ const indexes = Array.from(this.$items.keys());
240
+ const reversedItems = Array.from(this.$items.values()).reverse();
241
+
242
+ reversedItems.forEach((item, i) => {
243
+ this.setAt(indexes[i], item);
244
+ });
245
+
246
+ return this;
247
+ }
248
+
249
+ /**
250
+ * Removes the first element from an array and returns it.
251
+ */
252
+ shift(): V | undefined {
253
+ const indexes = Array.from(this.$items.keys());
254
+
255
+ const shiftAt = indexes.shift();
256
+ if (shiftAt === undefined) { return undefined; }
257
+
258
+ const value = this.$items.get(shiftAt);
259
+ this.$deleteAt(shiftAt);
260
+
261
+ return value;
262
+ }
263
+
264
+ /**
265
+ * Returns a section of an array.
266
+ * @param start The beginning of the specified portion of the array.
267
+ * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
268
+ */
269
+ slice(start?: number, end?: number): V[] {
270
+ return new ArraySchema(...Array.from(this.$items.values()).slice(start, end));
271
+ }
272
+
273
+ /**
274
+ * Sorts an array.
275
+ * @param compareFn Function used to determine the order of the elements. It is expected to return
276
+ * a negative value if first argument is less than second argument, zero if they're equal and a positive
277
+ * value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
278
+ * ```ts
279
+ * [11,2,22,1].sort((a, b) => a - b)
280
+ * ```
281
+ */
282
+ sort(compareFn: (a: V, b: V) => number = DEFAULT_SORT): this {
283
+ const indexes = Array.from(this.$items.keys());
284
+ const sortedItems = Array.from(this.$items.values()).sort(compareFn);
285
+
286
+ sortedItems.forEach((item, i) => {
287
+ this.setAt(indexes[i], item);
288
+ });
289
+
290
+ return this;
291
+ }
292
+
293
+ /**
294
+ * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
295
+ * @param start The zero-based location in the array from which to start removing elements.
296
+ * @param deleteCount The number of elements to remove.
297
+ * @param items Elements to insert into the array in place of the deleted elements.
298
+ */
299
+ splice(
300
+ start: number,
301
+ deleteCount: number = this.length - start,
302
+ ...items: V[]
303
+ ): V[] {
304
+ const indexes = Array.from(this.$items.keys());
305
+ const removedItems: V[] = [];
306
+
307
+ for (let i = start; i < start + deleteCount; i++) {
308
+ removedItems.push(this.$items.get(indexes[i]));
309
+ this.$deleteAt(indexes[i]);
310
+ }
311
+
312
+ return removedItems;
313
+ }
314
+
315
+ /**
316
+ * Inserts new elements at the start of an array.
317
+ * @param items Elements to insert at the start of the Array.
318
+ */
319
+ unshift(...items: V[]): number {
320
+ const length = this.length;
321
+ const addedLength = items.length;
322
+
323
+ // const indexes = Array.from(this.$items.keys());
324
+ const previousValues = Array.from(this.$items.values());
325
+
326
+ items.forEach((item, i) => {
327
+ this.setAt(i, item);
328
+ });
329
+
330
+ previousValues.forEach((previousValue, i) => {
331
+ this.setAt(addedLength + i, previousValue);
332
+ });
333
+
334
+ return length + addedLength;
335
+ }
336
+
337
+ /**
338
+ * Returns the index of the first occurrence of a value in an array.
339
+ * @param searchElement The value to locate in the array.
340
+ * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
341
+ */
342
+ indexOf(searchElement: V, fromIndex?: number): number {
343
+ return Array.from(this.$items.values()).indexOf(searchElement, fromIndex);
344
+ }
345
+
346
+ /**
347
+ * Returns the index of the last occurrence of a specified value in an array.
348
+ * @param searchElement The value to locate in the array.
349
+ * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array.
350
+ */
351
+ lastIndexOf(searchElement: V, fromIndex: number = this.length - 1): number {
352
+ return Array.from(this.$items.values()).lastIndexOf(searchElement, fromIndex);
353
+ }
354
+
355
+ /**
356
+ * Determines whether all the members of an array satisfy the specified test.
357
+ * @param callbackfn A function that accepts up to three arguments. The every method calls
358
+ * the callbackfn function for each element in the array until the callbackfn returns a value
359
+ * which is coercible to the Boolean value false, or until the end of the array.
360
+ * @param thisArg An object to which the this keyword can refer in the callbackfn function.
361
+ * If thisArg is omitted, undefined is used as the this value.
362
+ */
363
+ every(callbackfn: (value: V, index: number, array: V[]) => unknown, thisArg?: any): boolean {
364
+ return Array.from(this.$items.values()).every(callbackfn, thisArg);
365
+ }
366
+
367
+ /**
368
+ * Determines whether the specified callback function returns true for any element of an array.
369
+ * @param callbackfn A function that accepts up to three arguments. The some method calls
370
+ * the callbackfn function for each element in the array until the callbackfn returns a value
371
+ * which is coercible to the Boolean value true, or until the end of the array.
372
+ * @param thisArg An object to which the this keyword can refer in the callbackfn function.
373
+ * If thisArg is omitted, undefined is used as the this value.
374
+ */
375
+ some(callbackfn: (value: V, index: number, array: V[]) => unknown, thisArg?: any): boolean {
376
+ return Array.from(this.$items.values()).some(callbackfn, thisArg);
377
+ }
378
+
379
+ /**
380
+ * Performs the specified action for each element in an array.
381
+ * @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
382
+ * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
383
+ */
384
+ forEach(callbackfn: (value: V, index: number, array: V[]) => void, thisArg?: any): void {
385
+ Array.from(this.$items.values()).forEach(callbackfn, thisArg);
386
+ }
387
+
388
+ /**
389
+ * Calls a defined callback function on each element of an array, and returns an array that contains the results.
390
+ * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
391
+ * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
392
+ */
393
+ map<U>(callbackfn: (value: V, index: number, array: V[]) => U, thisArg?: any): U[] {
394
+ return Array.from(this.$items.values()).map(callbackfn, thisArg);
395
+ }
396
+
397
+ /**
398
+ * Returns the elements of an array that meet the condition specified in a callback function.
399
+ * @param callbackfn A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.
400
+ * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
401
+ */
402
+ filter(callbackfn: (value: V, index: number, array: V[]) => unknown, thisArg?: any)
403
+ filter<S extends V>(callbackfn: (value: V, index: number, array: V[]) => value is S, thisArg?: any): V[] {
404
+ return Array.from(this.$items.values()).filter(callbackfn, thisArg);
405
+ }
406
+
407
+ /**
408
+ * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
409
+ * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
410
+ * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
411
+ */
412
+ reduce<U=V>(callbackfn: (previousValue: U, currentValue: V, currentIndex: number, array: V[]) => U, initialValue?: U): U {
413
+ return Array.prototype.reduce.apply(Array.from(this.$items.values()), arguments);
414
+ }
415
+
416
+ /**
417
+ * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
418
+ * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
419
+ * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
420
+ */
421
+ reduceRight<U=V>(callbackfn: (previousValue: U, currentValue: V, currentIndex: number, array: V[]) => U, initialValue?: U): U {
422
+ return Array.prototype.reduceRight.apply(Array.from(this.$items.values()), arguments);
423
+ }
424
+
425
+ /**
426
+ * Returns the value of the first element in the array where predicate is true, and undefined
427
+ * otherwise.
428
+ * @param predicate find calls predicate once for each element of the array, in ascending
429
+ * order, until it finds one where predicate returns true. If such an element is found, find
430
+ * immediately returns that element value. Otherwise, find returns undefined.
431
+ * @param thisArg If provided, it will be used as the this value for each invocation of
432
+ * predicate. If it is not provided, undefined is used instead.
433
+ */
434
+ find(predicate: (value: V, index: number, obj: V[]) => boolean, thisArg?: any): V | undefined {
435
+ return Array.from(this.$items.values()).find(predicate, thisArg);
436
+ }
437
+
438
+ /**
439
+ * Returns the index of the first element in the array where predicate is true, and -1
440
+ * otherwise.
441
+ * @param predicate find calls predicate once for each element of the array, in ascending
442
+ * order, until it finds one where predicate returns true. If such an element is found,
443
+ * findIndex immediately returns that element index. Otherwise, findIndex returns -1.
444
+ * @param thisArg If provided, it will be used as the this value for each invocation of
445
+ * predicate. If it is not provided, undefined is used instead.
446
+ */
447
+ findIndex(predicate: (value: V, index: number, obj: V[]) => unknown, thisArg?: any): number {
448
+ return Array.from(this.$items.values()).findIndex(predicate, thisArg);
449
+ }
450
+
451
+ /**
452
+ * Returns the this object after filling the section identified by start and end with value
453
+ * @param value value to fill array section with
454
+ * @param start index to start filling the array at. If start is negative, it is treated as
455
+ * length+start where length is the length of the array.
456
+ * @param end index to stop filling the array at. If end is negative, it is treated as
457
+ * length+end.
458
+ */
459
+ fill(value: V, start?: number, end?: number): this {
460
+ //
461
+ // TODO
462
+ //
463
+ throw new Error("ArraySchema#fill() not implemented");
464
+ // this.$items.fill(value, start, end);
465
+
466
+ return this;
467
+ }
468
+
469
+ /**
470
+ * Returns the this object after copying a section of the array identified by start and end
471
+ * to the same array starting at position target
472
+ * @param target If target is negative, it is treated as length+target where length is the
473
+ * length of the array.
474
+ * @param start If start is negative, it is treated as length+start. If end is negative, it
475
+ * is treated as length+end.
476
+ * @param end If not specified, length of the this object is used as its default value.
477
+ */
478
+ copyWithin(target: number, start: number, end?: number): this {
479
+ //
480
+ // TODO
481
+ //
482
+ throw new Error("ArraySchema#copyWithin() not implemented");
483
+ return this;
484
+ }
485
+
486
+ /**
487
+ * Returns a string representation of an array.
488
+ */
489
+ toString(): string { return this.$items.toString(); }
490
+
491
+ /**
492
+ * Returns a string representation of an array. The elements are converted to string using their toLocalString methods.
493
+ */
494
+ toLocaleString(): string { return this.$items.toLocaleString() };
495
+
496
+ /** Iterator */
497
+ [Symbol.iterator](): IterableIterator<V> {
498
+ return Array.from(this.$items.values())[Symbol.iterator]();
499
+ }
500
+
501
+ // WORKAROUND for compatibility
502
+ // - TypeScript 4 defines @@unscopables as a function
503
+ // - TypeScript 5 defines @@unscopables as an object
504
+ [Symbol.unscopables]: any;
505
+
506
+ /**
507
+ * Returns an iterable of key, value pairs for every entry in the array
508
+ */
509
+ entries(): IterableIterator<[number, V]> { return this.$items.entries(); }
510
+
511
+ /**
512
+ * Returns an iterable of keys in the array
513
+ */
514
+ keys(): IterableIterator<number> { return this.$items.keys(); }
515
+
516
+ /**
517
+ * Returns an iterable of values in the array
518
+ */
519
+ values(): IterableIterator<V> { return this.$items.values(); }
520
+
521
+ /**
522
+ * Determines whether an array includes a certain element, returning true or false as appropriate.
523
+ * @param searchElement The element to search for.
524
+ * @param fromIndex The position in this array at which to begin searching for searchElement.
525
+ */
526
+ includes(searchElement: V, fromIndex?: number): boolean {
527
+ return Array.from(this.$items.values()).includes(searchElement, fromIndex);
528
+ }
529
+
530
+ /**
531
+ * Calls a defined callback function on each element of an array. Then, flattens the result into
532
+ * a new array.
533
+ * This is identical to a map followed by flat with depth 1.
534
+ *
535
+ * @param callback A function that accepts up to three arguments. The flatMap method calls the
536
+ * callback function one time for each element in the array.
537
+ * @param thisArg An object to which the this keyword can refer in the callback function. If
538
+ * thisArg is omitted, undefined is used as the this value.
539
+ */
540
+ // @ts-ignore
541
+ flatMap<U, This = undefined>(callback: (this: This, value: V, index: number, array: V[]) => U | ReadonlyArray<U>, thisArg?: This): U[] {
542
+ // @ts-ignore
543
+ throw new Error("ArraySchema#flatMap() is not supported.");
544
+ }
545
+
546
+ /**
547
+ * Returns a new array with all sub-array elements concatenated into it recursively up to the
548
+ * specified depth.
549
+ *
550
+ * @param depth The maximum recursion depth
551
+ */
552
+ // @ts-ignore
553
+ flat<A, D extends number = 1>(this: A, depth?: D): any {
554
+ // @ts-ignore
555
+ throw new Error("ArraySchema#flat() is not supported.");
556
+ }
557
+
558
+ // get size () {
559
+ // return this.$items.size;
560
+ // }
561
+
562
+ protected setIndex(index: number, key: number) {
563
+ this.$indexes.set(index, key);
564
+ }
565
+
566
+ protected getIndex(index: number) {
567
+ return this.$indexes.get(index);
568
+ }
569
+
570
+ protected getByIndex(index: number) {
571
+ return this.$items.get(this.$indexes.get(index));
572
+ }
573
+
574
+ protected deleteByIndex(index: number) {
575
+ const key = this.$indexes.get(index);
576
+ this.$items.delete(key);
577
+ this.$indexes.delete(index);
578
+ }
579
+
580
+ toArray() {
581
+ return Array.from(this.$items.values());
582
+ }
583
+
584
+ toJSON() {
585
+ return this.toArray().map((value) => {
586
+ return (typeof (value['toJSON']) === "function")
587
+ ? value['toJSON']()
588
+ : value;
589
+ });
590
+ }
591
+
592
+ //
593
+ // Decoding utilities
594
+ //
595
+ clone(isDecoding?: boolean): ArraySchema<V> {
596
+ let cloned: ArraySchema;
597
+
598
+ if (isDecoding) {
599
+ cloned = new ArraySchema(...Array.from(this.$items.values()));
600
+
601
+ } else {
602
+ cloned = new ArraySchema(...this.map(item => (
603
+ (item['$changes'])
604
+ ? (item as any as Schema).clone()
605
+ : item
606
+ )));
607
+ }
608
+
609
+ return cloned;
610
+ };
611
+
612
+ }