@alextheman/utility 5.14.0 → 5.15.1

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/index.cjs CHANGED
@@ -133,6 +133,21 @@ var CodeError = class CodeError extends Error {
133
133
  throw error;
134
134
  }
135
135
  /**
136
+ * Check a `CodeError` against its error code
137
+ *
138
+ * This will also automatically narrow down the type of the input to be `CodeError`, with its error code properly typed if this function returns true.
139
+ *
140
+ * @template ErrorCode The type of the error code
141
+ *
142
+ * @param input - The input to check.
143
+ * @param code - The expected code of the resulting error.
144
+ *
145
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to CodeError, and its code will be narrowed to the expected code's type if the function returns `true`.
146
+ */
147
+ static checkWithCode(input, code) {
148
+ return this.check(input) && input.code === code;
149
+ }
150
+ /**
136
151
  * Gets the thrown `CodeError` from a given function if one was thrown, and re-throws any other errors, or throws a default `CodeError` if no error thrown.
137
152
  *
138
153
  * @param errorFunction - The function expected to throw the error.
@@ -188,11 +203,10 @@ var DataError$1 = class DataError$1 extends CodeError {
188
203
  * @param message - A human-readable error message (e.g. The data provided is invalid).
189
204
  * @param options - Extra options to pass to super Error constructor.
190
205
  */
191
- constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
206
+ constructor(data, code, message = "The data provided is invalid", options) {
192
207
  super(code, message, options);
193
208
  if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
194
209
  this.name = new.target.name;
195
- this.code = code;
196
210
  this.data = data;
197
211
  Object.defineProperty(this, "message", { enumerable: true });
198
212
  Object.setPrototypeOf(this, new.target.prototype);
@@ -209,6 +223,21 @@ var DataError$1 = class DataError$1 extends CodeError {
209
223
  return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
210
224
  }
211
225
  /**
226
+ * Check a `DataError` against its error code
227
+ *
228
+ * This will also automatically narrow down the type of the input to be `DataError`, with its error code properly typed if this function returns true.
229
+ *
230
+ * @template ErrorCode The type of the error code
231
+ *
232
+ * @param input - The input to check.
233
+ * @param code - The expected code of the resulting error.
234
+ *
235
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to `DataError`, and its code will be narrowed to the expected code's type if the function returns `true`.
236
+ */
237
+ static checkWithCode(input, code) {
238
+ return this.check(input) && input.code === code;
239
+ }
240
+ /**
212
241
  * Gets the thrown `DataError` from a given function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
213
242
  *
214
243
  * @param errorFunction - The function expected to throw the error.
@@ -1801,6 +1830,22 @@ var DataError = class DataError extends Error {
1801
1830
  }
1802
1831
  };
1803
1832
  //#endregion
1833
+ //#region src/root/errors/assertNotNull.ts
1834
+ /**
1835
+ * Asserts that a given input is not `null`, and throws a DataError if it does.
1836
+ *
1837
+ * If no error is thrown from this, the input type gets narrowed down to not include `null`.
1838
+ *
1839
+ * @template InputType The type of the input.
1840
+ *
1841
+ * @param input - The input to assert against
1842
+ *
1843
+ * @throws {DataError} If the input is `null`.
1844
+ */
1845
+ function assertNotNull(input) {
1846
+ if (input === null) throw new DataError$1({ input }, "NULL_INPUT", "Expected the input not to be null");
1847
+ }
1848
+ //#endregion
1804
1849
  exports.APIError = APIError;
1805
1850
  exports.DataError = DataError;
1806
1851
  exports.Env = Env;
@@ -1815,6 +1860,7 @@ exports.VersionNumber = VersionNumber;
1815
1860
  exports.VersionType = VersionType;
1816
1861
  exports.addDaysToDate = addDaysToDate;
1817
1862
  exports.appendSemicolon = appendSemicolon;
1863
+ exports.assertNotNull = assertNotNull;
1818
1864
  exports.az = az;
1819
1865
  exports.calculateMonthlyDifference = calculateMonthlyDifference;
1820
1866
  exports.camelToKebab = camelToKebab;