Skip to content

Commit

Permalink
ArraySchema: add missing definitions from ES2023. colyseus/colyseus#648
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Nov 1, 2023
1 parent 11c0988 commit bea23c0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@colyseus/schema",
"version": "2.0.20",
"version": "2.0.21",
"description": "Binary state serializer with delta encoding for games",
"bin": {
"schema-codegen": "./bin/schema-codegen"
Expand Down Expand Up @@ -79,7 +79,7 @@
"ts-node": "^7.0.1",
"tslib": "^2.1.0",
"tsx": "^3.13.0",
"typescript": "^5.0.4"
"typescript": "^5.2.2"
},
"nyc": {
"extension": [
Expand Down
29 changes: 26 additions & 3 deletions src/types/ArraySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,10 @@ export class ArraySchema<V = any> implements Array<V>, SchemaDecoderCallbacks {
return Array.from(this.$items.values()).includes(searchElement, fromIndex);
}

//
// ES2022
//

/**
* Calls a defined callback function on each element of an array. Then, flattens the result into
* a new array.
Expand Down Expand Up @@ -594,9 +598,28 @@ export class ArraySchema<V = any> implements Array<V>, SchemaDecoderCallbacks {
return arr.findLastIndex.apply(arr, arguments);
}

// get size () {
// return this.$items.size;
// }
//
// ES2023
//
with(index: number, value: V): V[] {
const copy = Array.from(this.$items.values());
copy[index] = value;
return new ArraySchema(...copy);
}
toReversed(): V[] {
return Array.from(this.$items.values()).reverse();
}
toSorted(compareFn?: (a: V, b: V) => number): V[] {
return Array.from(this.$items.values()).sort(compareFn);
}
toSpliced(start: number, deleteCount: number, ...items: V[]): V[];
toSpliced(start: number, deleteCount?: number): V[];
// @ts-ignore
toSpliced(start: unknown, deleteCount?: unknown, ...items?: unknown[]): V[] {
const copy = Array.from(this.$items.values());
// @ts-ignore
return copy.toSpliced.apply(copy, arguments);
}

protected setIndex(index: number, key: number) {
this.$indexes.set(index, key);
Expand Down

0 comments on commit bea23c0

Please sign in to comment.