isaacscript-common 4.0.5 → 4.2.0

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.
@@ -55,6 +55,12 @@ export declare function getEnumLength<T>(transpiledEnum: T): int;
55
55
  * https://isaacscript.github.io/gotchas#iterating-over-enums
56
56
  */
57
57
  export declare function getEnumValues<T>(transpiledEnum: T): Array<T[keyof T]>;
58
+ /**
59
+ * Helper function to get the enum value with the highest value.
60
+ *
61
+ * Note that this is not necessarily the enum value that is declared last, since there is no way to
62
+ * infer that at run-time.
63
+ */
58
64
  export declare function getLastEnumValue<T>(transpiledEnum: T): T[keyof T];
59
65
  /**
60
66
  * Helper function to get a random value from the provided enum.
@@ -66,8 +72,9 @@ export declare function getLastEnumValue<T>(transpiledEnum: T): T[keyof T];
66
72
  */
67
73
  export declare function getRandomEnumValue<T>(transpiledEnum: T, seedOrRNG?: Seed | RNG, exceptions?: Array<T[keyof T]> | ReadonlyArray<T[keyof T]>): T[keyof T];
68
74
  /**
69
- * Helper function to check every value of a custom enum for -1. This is helpful as a run-time check
70
- * because many methods of the Isaac class return -1 if they fail.
75
+ * Helper function to check every value of a custom enum for -1. Will throw an run-time error if any
76
+ * -1 values are found. This is helpful because many methods of the Isaac class return -1 if they
77
+ * fail.
71
78
  *
72
79
  * For example:
73
80
  *
@@ -80,3 +87,9 @@ export declare function getRandomEnumValue<T>(transpiledEnum: T, seedOrRNG?: See
80
87
  * ```
81
88
  */
82
89
  export declare function validateCustomEnum(transpiledEnumName: string, transpiledEnum: unknown): void;
90
+ /**
91
+ * Helper function to validate if every value in an enum is contiguous, starting at 0.
92
+ *
93
+ * This is useful to automate checking large enums for typos.
94
+ */
95
+ export declare function validateEnumContiguous<T>(transpiledEnumName: string, transpiledEnum: T): void;
@@ -1,6 +1,8 @@
1
1
  local ____lualib = require("lualib_bundle")
2
2
  local __TS__ArraySort = ____lualib.__TS__ArraySort
3
3
  local __TS__ArrayMap = ____lualib.__TS__ArrayMap
4
+ local Set = ____lualib.Set
5
+ local __TS__New = ____lualib.__TS__New
4
6
  local ____exports = {}
5
7
  local ____array = require("functions.array")
6
8
  local getRandomArrayElement = ____array.getRandomArrayElement
@@ -8,6 +10,8 @@ local ____rng = require("functions.rng")
8
10
  local getRandomSeed = ____rng.getRandomSeed
9
11
  local ____types = require("functions.types")
10
12
  local isString = ____types.isString
13
+ local ____utils = require("functions.utils")
14
+ local irange = ____utils.irange
11
15
  --- TypeScriptToLua will transpile TypeScript enums to Lua tables that have a double mapping. Thus,
12
16
  -- when you iterate over them, you will get both the names of the enums and the values of the enums,
13
17
  -- in a random order. Use this helper function to get the entries of the enum with the reverse
@@ -101,6 +105,10 @@ function ____exports.getEnumValues(self, transpiledEnum)
101
105
  end
102
106
  )
103
107
  end
108
+ --- Helper function to get the enum value with the highest value.
109
+ --
110
+ -- Note that this is not necessarily the enum value that is declared last, since there is no way to
111
+ -- infer that at run-time.
104
112
  function ____exports.getLastEnumValue(self, transpiledEnum)
105
113
  local enumValues = ____exports.getEnumValues(nil, transpiledEnum)
106
114
  local lastElement = enumValues[#enumValues]
@@ -125,8 +133,9 @@ function ____exports.getRandomEnumValue(self, transpiledEnum, seedOrRNG, excepti
125
133
  local enumValues = ____exports.getEnumValues(nil, transpiledEnum)
126
134
  return getRandomArrayElement(nil, enumValues, seedOrRNG, exceptions)
127
135
  end
128
- --- Helper function to check every value of a custom enum for -1. This is helpful as a run-time check
129
- -- because many methods of the Isaac class return -1 if they fail.
136
+ --- Helper function to check every value of a custom enum for -1. Will throw an run-time error if any
137
+ -- -1 values are found. This is helpful because many methods of the Isaac class return -1 if they
138
+ -- fail.
130
139
  --
131
140
  -- For example:
132
141
  --
@@ -142,7 +151,26 @@ function ____exports.validateCustomEnum(self, transpiledEnumName, transpiledEnum
142
151
  local key = ____value[1]
143
152
  local value = ____value[2]
144
153
  if value == -1 then
145
- error((("Failed to find custom enum value: " .. transpiledEnumName) .. ".") .. key)
154
+ error((("Failed to find the custom enum value: " .. transpiledEnumName) .. ".") .. key)
155
+ end
156
+ end
157
+ end
158
+ --- Helper function to validate if every value in an enum is contiguous, starting at 0.
159
+ --
160
+ -- This is useful to automate checking large enums for typos.
161
+ function ____exports.validateEnumContiguous(self, transpiledEnumName, transpiledEnum)
162
+ local values = ____exports.getEnumValues(nil, transpiledEnum)
163
+ local valuesSet = __TS__New(Set, values)
164
+ local lastValue = values[#values]
165
+ if lastValue == nil then
166
+ error("Failed to validate that an enum was contiguous, since the last value was undefined.")
167
+ end
168
+ if type(lastValue) ~= "number" then
169
+ error("Failed to validate that an enum was contiguous, since the last value was not a number.")
170
+ end
171
+ for ____, value in ipairs(irange(nil, lastValue)) do
172
+ if not valuesSet:has(value) then
173
+ error((("Failed to find a custom enum value of " .. tostring(value)) .. " for: ") .. transpiledEnumName)
146
174
  end
147
175
  end
148
176
  end
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "isaacscript-common",
3
- "version": "4.0.5",
3
+ "version": "4.2.0",
4
4
  "description": "Helper functions and features for IsaacScript mods.",
5
5
  "keywords": [
6
6
  "isaac",
@@ -22,6 +22,6 @@
22
22
  "main": "index",
23
23
  "types": "index.d.ts",
24
24
  "dependencies": {
25
- "isaac-typescript-definitions": "^3.0.6"
25
+ "isaac-typescript-definitions": "^3.0.7"
26
26
  }
27
27
  }