@colyseus/schema 4.0.29 → 4.0.30
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/build/index.cjs +31 -0
- package/build/index.cjs.map +1 -1
- package/build/index.js +31 -0
- package/build/index.mjs +31 -0
- package/build/index.mjs.map +1 -1
- package/build/types/custom/MapSchema.d.ts +17 -0
- package/package.json +1 -1
- package/src/types/custom/MapSchema.ts +33 -0
|
@@ -33,6 +33,23 @@ export declare class MapSchema<V = any, K extends string = string> implements Ma
|
|
|
33
33
|
static get [Symbol.species](): typeof MapSchema;
|
|
34
34
|
set(key: K, value: V): this;
|
|
35
35
|
get(key: K): V | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Returns the value for `key` if present. Otherwise inserts `defaultValue`
|
|
38
|
+
* (tracked as an ADD change, like `set()`) and returns it.
|
|
39
|
+
*
|
|
40
|
+
* Mirrors `Map.prototype.getOrInsert` (TC39 "upsert" proposal, typed in
|
|
41
|
+
* TypeScript 6's standard library).
|
|
42
|
+
*/
|
|
43
|
+
getOrInsert(key: K, defaultValue: V): V;
|
|
44
|
+
/**
|
|
45
|
+
* Returns the value for `key` if present. Otherwise computes a value via
|
|
46
|
+
* `callbackfn(key)`, inserts it (tracked as an ADD change, like `set()`)
|
|
47
|
+
* and returns it. The callback is only invoked when the key is missing.
|
|
48
|
+
*
|
|
49
|
+
* Mirrors `Map.prototype.getOrInsertComputed` (TC39 "upsert" proposal,
|
|
50
|
+
* typed in TypeScript 6's standard library).
|
|
51
|
+
*/
|
|
52
|
+
getOrInsertComputed(key: K, callbackfn: (key: K) => V): V;
|
|
36
53
|
delete(key: K): boolean;
|
|
37
54
|
clear(): void;
|
|
38
55
|
has(key: K): boolean;
|
package/package.json
CHANGED
|
@@ -152,6 +152,39 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, C
|
|
|
152
152
|
return this.$items.get(key);
|
|
153
153
|
}
|
|
154
154
|
|
|
155
|
+
/**
|
|
156
|
+
* Returns the value for `key` if present. Otherwise inserts `defaultValue`
|
|
157
|
+
* (tracked as an ADD change, like `set()`) and returns it.
|
|
158
|
+
*
|
|
159
|
+
* Mirrors `Map.prototype.getOrInsert` (TC39 "upsert" proposal, typed in
|
|
160
|
+
* TypeScript 6's standard library).
|
|
161
|
+
*/
|
|
162
|
+
getOrInsert(key: K, defaultValue: V): V {
|
|
163
|
+
if (this.$items.has(key)) {
|
|
164
|
+
return this.$items.get(key);
|
|
165
|
+
}
|
|
166
|
+
this.set(key, defaultValue);
|
|
167
|
+
return defaultValue;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Returns the value for `key` if present. Otherwise computes a value via
|
|
172
|
+
* `callbackfn(key)`, inserts it (tracked as an ADD change, like `set()`)
|
|
173
|
+
* and returns it. The callback is only invoked when the key is missing.
|
|
174
|
+
*
|
|
175
|
+
* Mirrors `Map.prototype.getOrInsertComputed` (TC39 "upsert" proposal,
|
|
176
|
+
* typed in TypeScript 6's standard library).
|
|
177
|
+
*/
|
|
178
|
+
getOrInsertComputed(key: K, callbackfn: (key: K) => V): V {
|
|
179
|
+
if (this.$items.has(key)) {
|
|
180
|
+
return this.$items.get(key);
|
|
181
|
+
}
|
|
182
|
+
const value = callbackfn(key);
|
|
183
|
+
// per spec: overwrites even if callbackfn itself inserted `key`
|
|
184
|
+
this.set(key, value);
|
|
185
|
+
return value;
|
|
186
|
+
}
|
|
187
|
+
|
|
155
188
|
delete(key: K) {
|
|
156
189
|
if (!this.$items.has(key)) {
|
|
157
190
|
return false;
|