isaacscript-common 1.2.51 → 1.2.52
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/dist/types/DefaultMap.d.ts +26 -0
- package/dist/types/DefaultMap.lua +67 -0
- package/package.json +1 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
declare type FirstArg<K, V> = Iterable<[K, V]> | V | ((k: K) => V);
|
|
2
|
+
declare type SecondArg<K, V> = V | ((k: K) => V);
|
|
3
|
+
/**
|
|
4
|
+
* An extended Map with a new method of `getAndSetDefault`.
|
|
5
|
+
*
|
|
6
|
+
* When instantiating the object, you must specify either a default value or a function that returns
|
|
7
|
+
* a default value.
|
|
8
|
+
*
|
|
9
|
+
* Example:
|
|
10
|
+
* ```ts
|
|
11
|
+
* const defaultMap1 = new DefaultMap<string, string>("foo");
|
|
12
|
+
* const defaultMap2 = new DefaultMap<string, string>(["a", "b", "c"], "bar");
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare class DefaultMap<K, V> extends Map<K, V> {
|
|
16
|
+
private defaultValue;
|
|
17
|
+
private defaultValueFactory;
|
|
18
|
+
constructor(iterableOrDefaultValueOrDefaultValueFactory: FirstArg<K, V>, defaultValueOrDefaultValueFactory?: SecondArg<K, V>);
|
|
19
|
+
/**
|
|
20
|
+
* Almost the same as `Map.prototype.get`. However, if the key does not exist in the map, then
|
|
21
|
+
* this method will set the default value for the key and return the default value.
|
|
22
|
+
*/
|
|
23
|
+
getAndSetDefault(key: K): V | undefined;
|
|
24
|
+
getDefaultValue(key: K): V;
|
|
25
|
+
}
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
|
|
2
|
+
local ____lualib = require("lualib_bundle")
|
|
3
|
+
local __TS__Class = ____lualib.__TS__Class
|
|
4
|
+
local Map = ____lualib.Map
|
|
5
|
+
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
|
|
6
|
+
local ____exports = {}
|
|
7
|
+
local parseArguments, parseArgumentsOne, parseArgumentsTwo, parseDefaultValueOrDefaultValueFactory
|
|
8
|
+
function parseArguments(self, firstArg, secondArg)
|
|
9
|
+
return secondArg == nil and parseArgumentsOne(nil, firstArg) or parseArgumentsTwo(nil, firstArg, secondArg)
|
|
10
|
+
end
|
|
11
|
+
function parseArgumentsOne(self, firstArg)
|
|
12
|
+
local defaultValue, defaultValueFactory = table.unpack(parseDefaultValueOrDefaultValueFactory(nil, firstArg))
|
|
13
|
+
return {nil, defaultValue, defaultValueFactory}
|
|
14
|
+
end
|
|
15
|
+
function parseArgumentsTwo(self, firstArg, secondArg)
|
|
16
|
+
local firstArgType = type(firstArg)
|
|
17
|
+
if firstArgType ~= "table" then
|
|
18
|
+
error("A DefaultMap constructor with two arguments must have the first argument be an array.")
|
|
19
|
+
end
|
|
20
|
+
local defaultValue, defaultValueFactory = table.unpack(parseDefaultValueOrDefaultValueFactory(nil, secondArg))
|
|
21
|
+
return {firstArg, defaultValue, defaultValueFactory}
|
|
22
|
+
end
|
|
23
|
+
function parseDefaultValueOrDefaultValueFactory(self, arg)
|
|
24
|
+
local argType = type(arg)
|
|
25
|
+
if argType == "function" then
|
|
26
|
+
return {nil, arg}
|
|
27
|
+
end
|
|
28
|
+
if argType == "boolean" or argType == "number" or argType == "string" then
|
|
29
|
+
return {arg, nil}
|
|
30
|
+
end
|
|
31
|
+
return error("A DefaultMap must be initialized with a default value that is either a primitive or a function that returns a default value.")
|
|
32
|
+
end
|
|
33
|
+
____exports.DefaultMap = __TS__Class()
|
|
34
|
+
local DefaultMap = ____exports.DefaultMap
|
|
35
|
+
DefaultMap.name = "DefaultMap"
|
|
36
|
+
__TS__ClassExtends(DefaultMap, Map)
|
|
37
|
+
function DefaultMap.prototype.____constructor(self, iterableOrDefaultValueOrDefaultValueFactory, defaultValueOrDefaultValueFactory)
|
|
38
|
+
local iterable, defaultValue, defaultValueFactory = table.unpack(parseArguments(nil, iterableOrDefaultValueOrDefaultValueFactory, defaultValueOrDefaultValueFactory))
|
|
39
|
+
if defaultValue == nil and defaultValueFactory == nil then
|
|
40
|
+
error("A DefaultMap must be instantiated with either a default value or a function that returns a default value.")
|
|
41
|
+
end
|
|
42
|
+
if iterable == nil then
|
|
43
|
+
Map.prototype.____constructor(self)
|
|
44
|
+
else
|
|
45
|
+
Map.prototype.____constructor(self, iterable)
|
|
46
|
+
end
|
|
47
|
+
self.defaultValue = defaultValue
|
|
48
|
+
self.defaultValueFactory = defaultValueFactory
|
|
49
|
+
end
|
|
50
|
+
function DefaultMap.prototype.getAndSetDefault(self, key)
|
|
51
|
+
if self:has(key) then
|
|
52
|
+
return self:get(key)
|
|
53
|
+
end
|
|
54
|
+
local defaultValue = self:getDefaultValue(key)
|
|
55
|
+
self:set(key, defaultValue)
|
|
56
|
+
return defaultValue
|
|
57
|
+
end
|
|
58
|
+
function DefaultMap.prototype.getDefaultValue(self, key)
|
|
59
|
+
if self.defaultValue ~= nil then
|
|
60
|
+
return self.defaultValue
|
|
61
|
+
end
|
|
62
|
+
if self.defaultValueFactory ~= nil then
|
|
63
|
+
return self:defaultValueFactory(key)
|
|
64
|
+
end
|
|
65
|
+
return error("A DefaultMap was incorrectly instantiated.")
|
|
66
|
+
end
|
|
67
|
+
return ____exports
|