isaacscript-common 1.2.62 → 1.2.63

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.
@@ -1,6 +1,6 @@
1
- declare type FactoryFunction<K, V> = (k: K) => V;
2
- declare type FirstArg<K, V> = Iterable<[K, V]> | V | FactoryFunction<K, V>;
3
- declare type SecondArg<K, V> = V | FactoryFunction<K, V>;
1
+ declare type FactoryFunction<K, V, A extends unknown[]> = (k: K, ...extraArgs: A) => V;
2
+ declare type FirstArg<K, V, A extends unknown[]> = Iterable<[K, V]> | V | FactoryFunction<K, V, A>;
3
+ declare type SecondArg<K, V, A extends unknown[]> = V | FactoryFunction<K, V, A>;
4
4
  /**
5
5
  * An extended Map with some new methods:
6
6
  *
@@ -31,36 +31,45 @@ declare type SecondArg<K, V> = V | FactoryFunction<K, V>;
31
31
  * ], "bar");
32
32
  * ```
33
33
  *
34
- * If specified, the first argument of a factory function must be equal to the key:
34
+ * If specified, the first argument of a factory function must always be equal to the key:
35
35
  *
36
36
  * ```ts
37
37
  * const defaultMapWithConditionalDefaultValue = new DefaultMap<number, number>((key: number) => {
38
38
  * return isOdd(key) ? 0 : 1;
39
39
  * });
40
40
  * ```
41
+ *
42
+ * You can also specify a factory function that takes a generic amount of arguments beyond the
43
+ * first:
44
+ *
45
+ * ```ts
46
+ * const defaultMapWithExtraArgs = new DefaultMap<string, string>((_key: string, arg2: boolean) => {
47
+ * return arg2 ? 0 : 1;
48
+ * })
49
+ * ```
41
50
  */
42
- export declare class DefaultMap<K, V> extends Map<K, V> {
51
+ export declare class DefaultMap<K, V, A extends unknown[] = []> extends Map<K, V> {
43
52
  private defaultValue;
44
53
  private defaultValueFactory;
45
54
  /**
46
55
  * See the DefaultMap documentation:
47
56
  * https://isaacscript.github.io/isaacscript-common/classes/types_DefaultMap.DefaultMap.html
48
57
  */
49
- constructor(iterableOrDefaultValueOrDefaultValueFactory: FirstArg<K, V>, defaultValueOrDefaultValueFactory?: SecondArg<K, V>);
58
+ constructor(iterableOrDefaultValueOrDefaultValueFactory: FirstArg<K, V, A>, defaultValueOrDefaultValueFactory?: SecondArg<K, V, A>);
50
59
  /**
51
60
  * If the key exists, this will return the same thing as the `get` method. Otherwise, it will set
52
61
  * a default value to the key, and then return the default value.
53
62
  */
54
- getAndSetDefault(key: K): V;
63
+ getAndSetDefault(key: K, ...extraArgs: A): V;
55
64
  /**
56
65
  * Returns the default value to be used for a new key. (If a factory function was provided during
57
66
  * instantiation, this will execute the factory function.)
58
67
  */
59
- getDefaultValue(key: K): V;
68
+ getDefaultValue(key: K, ...extraArgs: A): V;
60
69
  /**
61
70
  * Helper method for cloning the map. Returns either the default value or a reference to the
62
71
  * factory function.
63
72
  */
64
- getConstructorArg(): V | FactoryFunction<K, V>;
73
+ getConstructorArg(): V | FactoryFunction<K, V, A>;
65
74
  }
66
75
  export {};
@@ -55,21 +55,21 @@ function DefaultMap.prototype.____constructor(self, iterableOrDefaultValueOrDefa
55
55
  self.defaultValue = defaultValue
56
56
  self.defaultValueFactory = defaultValueFactory
57
57
  end
58
- function DefaultMap.prototype.getAndSetDefault(self, key)
58
+ function DefaultMap.prototype.getAndSetDefault(self, key, ...)
59
59
  local value = self:get(key)
60
60
  if value ~= nil then
61
61
  return value
62
62
  end
63
- local defaultValue = self:getDefaultValue(key)
63
+ local defaultValue = self:getDefaultValue(key, ...)
64
64
  self:set(key, defaultValue)
65
65
  return defaultValue
66
66
  end
67
- function DefaultMap.prototype.getDefaultValue(self, key)
67
+ function DefaultMap.prototype.getDefaultValue(self, key, ...)
68
68
  if self.defaultValue ~= nil then
69
69
  return self.defaultValue
70
70
  end
71
71
  if self.defaultValueFactory ~= nil then
72
- return self:defaultValueFactory(key)
72
+ return self:defaultValueFactory(key, ...)
73
73
  end
74
74
  return error("A DefaultMap was incorrectly instantiated.")
75
75
  end
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "isaacscript-common",
3
- "version": "1.2.62",
3
+ "version": "1.2.63",
4
4
  "description": "Helper functions for IsaacScript mods",
5
5
  "keywords": [
6
6
  "isaac",