@alextheman/utility 5.19.2 → 5.20.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.
package/dist/index.cjs CHANGED
@@ -115,7 +115,7 @@ var CodeError = class CodeError extends Error {
115
115
  */
116
116
  static check(input) {
117
117
  if (input instanceof CodeError) return true;
118
- return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string";
118
+ return containsKeys(input, ["message", "code"]) && typeof input.message === "string" && typeof input.code === "string";
119
119
  }
120
120
  static checkCaughtError(error, options) {
121
121
  if (this.check(error)) {
@@ -228,7 +228,11 @@ var DataError$1 = class DataError$1 extends CodeError {
228
228
  */
229
229
  static check(input) {
230
230
  if (input instanceof DataError$1) return true;
231
- return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input && typeof input.data === "object" && input.data !== null;
231
+ return containsKeys(input, [
232
+ "data",
233
+ "code",
234
+ "message"
235
+ ]) && typeof input.message === "string" && typeof input.code === "string" && isNonNullableObject(input.data);
232
236
  }
233
237
  /**
234
238
  * Check a `DataError` against its error code
@@ -325,6 +329,107 @@ function range(start, stop, step = 1) {
325
329
  return numbers;
326
330
  }
327
331
  //#endregion
332
+ //#region src/root/functions/typeAssertions/assertNotNull.ts
333
+ /**
334
+ * Asserts that a given input is not `null`, and throws a DataError if it does.
335
+ *
336
+ * If no error is thrown from this, the input type gets narrowed down to not include `null`.
337
+ *
338
+ * @category Type Assertions
339
+ *
340
+ * @template InputType The type of the input.
341
+ *
342
+ * @param input - The input to assert against
343
+ *
344
+ * @throws {DataError} If the input is `null`.
345
+ */
346
+ function assertNotNull(input) {
347
+ if (input === null) throw new DataError$1({ input }, "NULL_INPUT", "Expected the input not to be null");
348
+ }
349
+ //#endregion
350
+ //#region src/root/functions/typeAssertions/assertNotNullable.ts
351
+ /**
352
+ * Asserts that a given input is not `undefined` or `null`, and throws a DataError if it does.
353
+ *
354
+ * If no error is thrown from this, the input type gets narrowed down to not include `undefined` or `null`.
355
+ *
356
+ * @category Type Assertions
357
+ *
358
+ * @template InputType The type of the input.
359
+ *
360
+ * @param input - The input to assert against.
361
+ *
362
+ * @throws {DataError} If the input is `undefined` or `null`.
363
+ */
364
+ function assertNotNullable(input) {
365
+ if (input === null || input === void 0) throw new DataError$1({ input }, "NULLABLE_INPUT", "Expected the input not to be undefined or null");
366
+ }
367
+ //#endregion
368
+ //#region src/root/functions/typeAssertions/assertNotUndefined.ts
369
+ /**
370
+ * Asserts that a given input is not `undefined`, and throws a DataError if it does.
371
+ *
372
+ * If no error is thrown from this, the input type gets narrowed down to not include `undefined`.
373
+ *
374
+ * @category Type Assertions
375
+ *
376
+ * @template InputType The type of the input.
377
+ *
378
+ * @param input - The input to assert against.
379
+ *
380
+ * @throws {DataError} If the input is `undefined`.
381
+ */
382
+ function assertNotUndefined(input) {
383
+ if (input === void 0) throw new DataError$1({ input }, "UNDEFINED_INPUT", "Expected the input not to be undefined");
384
+ }
385
+ //#endregion
386
+ //#region src/root/functions/typeAssertions/isNonNullableObject.ts
387
+ /**
388
+ * Determines if the given input is a non-nullable object, narrowing the type down as such if it is
389
+ *
390
+ * @category Type Assertions
391
+ *
392
+ * @param input - The input to check
393
+ *
394
+ * @returns `true` if the input is a non-nullable object, and `false` otherwise. The input type will also be narrowed down to be a non-nullable object.
395
+ */
396
+ function isNonNullableObject(input) {
397
+ return typeof input === "object" && input !== null;
398
+ }
399
+ //#endregion
400
+ //#region src/root/functions/typeAssertions/objectContainsKeys.ts
401
+ /**
402
+ * Determines if the given object contains all of the keys provided.
403
+ *
404
+ * @category Type Assertions
405
+ *
406
+ * @param input - The input object to check.
407
+ * @param keys - The keys expected to be in the input object.
408
+ *
409
+ * @returns `true` if the input object contains all provided keys, and `false` otherwise. The input type will also be narrowed down to be a record with the provided keys with an unknown value type.
410
+ */
411
+ function objectContainsKeys(input, keys) {
412
+ const expectedKeys = Array.isArray(keys) ? keys : [keys];
413
+ if (expectedKeys.length === 0) return false;
414
+ for (const key of expectedKeys) if (!(key in input)) return false;
415
+ return true;
416
+ }
417
+ //#endregion
418
+ //#region src/root/functions/typeAssertions/containsKeys.ts
419
+ /**
420
+ * Determines if the given input is a non-nullable object, and if that object contains all of the keys provided.
421
+ *
422
+ * @category Type Assertions
423
+ *
424
+ * @param input - The input to check.
425
+ * @param keys - The keys expected to be in the input if it's an object.
426
+ *
427
+ * @returns `true` if the input is an object containing all provided keys, and `false` otherwise. The input type will also be narrowed down to be a record with the provided keys, each with an unknown value type.
428
+ */
429
+ function containsKeys(input, keys) {
430
+ return isNonNullableObject(input) && objectContainsKeys(input, keys);
431
+ }
432
+ //#endregion
328
433
  //#region src/root/functions/arrayHelpers/fillArray.ts
329
434
  /**
330
435
  * Creates a new array where each element is the result of the provided callback.
@@ -352,7 +457,7 @@ function fillArray(callback, length = 1, options) {
352
457
  return callback(index);
353
458
  });
354
459
  if (outputArray.some((item) => {
355
- return item instanceof Promise || typeof item === "object" && item !== null && "then" in item && typeof item.then === "function";
460
+ return item instanceof Promise || containsKeys(item, "then") && typeof item.then === "function";
356
461
  })) return Promise.all(outputArray);
357
462
  return outputArray;
358
463
  }
@@ -653,7 +758,7 @@ function convertFileToBase64(file) {
653
758
  //#endregion
654
759
  //#region src/root/functions/miscellaneous/createFormData.ts
655
760
  function getNullableResolutionStrategy(key, strategy) {
656
- return (typeof strategy === "object" ? strategy[key] : strategy) ?? "empty";
761
+ return (isNonNullableObject(strategy) ? strategy[key] : strategy) ?? "empty";
657
762
  }
658
763
  function isPrimitive(item) {
659
764
  return typeof item === "string" || typeof item === "number" || typeof item === "boolean";
@@ -1354,7 +1459,7 @@ function parseVersionType(input) {
1354
1459
  //#endregion
1355
1460
  //#region src/root/functions/recursive/deepCopy.ts
1356
1461
  function callDeepCopy(input) {
1357
- return typeof input === "object" && input !== null ? deepCopy(input) : input;
1462
+ return isNonNullableObject(input) ? deepCopy(input) : input;
1358
1463
  }
1359
1464
  /**
1360
1465
  * Deeply copies an object or array such that all child objects/arrays are also copied.
@@ -1673,7 +1778,7 @@ function interpolateObjects(strings, ...interpolations) {
1673
1778
  * @returns `true` if the input is a valid `TemplateStringsArray`, and false otherwise. The type of the input will also be narrowed down to `TemplateStringsArray` if `true`.
1674
1779
  */
1675
1780
  function isTemplateStringsArray(input) {
1676
- return typeof input === "object" && input !== null && "raw" in input;
1781
+ return containsKeys(input, "raw");
1677
1782
  }
1678
1783
  //#endregion
1679
1784
  //#region src/root/functions/taggedTemplate/normaliseIndents.ts
@@ -1728,7 +1833,7 @@ function reduceLines(lines, { preserveTabs = true }) {
1728
1833
  * @returns An additional function to invoke, or a new string with the strings and interpolations from the template applied, and extraneous indents removed.
1729
1834
  */
1730
1835
  function normaliseIndents(first, ...args) {
1731
- if (typeof first === "object" && first !== null && !Array.isArray(first)) {
1836
+ if (isNonNullableObject(first) && !Array.isArray(first)) {
1732
1837
  const options = first;
1733
1838
  return (strings, ...interpolations) => {
1734
1839
  return normaliseIndents(strings, ...interpolations, options);
@@ -1855,54 +1960,6 @@ var DataError = class DataError extends Error {
1855
1960
  }
1856
1961
  };
1857
1962
  //#endregion
1858
- //#region src/root/errors/assertNotNull.ts
1859
- /**
1860
- * Asserts that a given input is not `null`, and throws a DataError if it does.
1861
- *
1862
- * If no error is thrown from this, the input type gets narrowed down to not include `null`.
1863
- *
1864
- * @template InputType The type of the input.
1865
- *
1866
- * @param input - The input to assert against
1867
- *
1868
- * @throws {DataError} If the input is `null`.
1869
- */
1870
- function assertNotNull(input) {
1871
- if (input === null) throw new DataError$1({ input }, "NULL_INPUT", "Expected the input not to be null");
1872
- }
1873
- //#endregion
1874
- //#region src/root/errors/assertNotNullable.ts
1875
- /**
1876
- * Asserts that a given input is not `undefined` or `null`, and throws a DataError if it does.
1877
- *
1878
- * If no error is thrown from this, the input type gets narrowed down to not include `undefined` or `null`.
1879
- *
1880
- * @template InputType The type of the input.
1881
- *
1882
- * @param input - The input to assert against.
1883
- *
1884
- * @throws {DataError} If the input is `undefined` or `null`.
1885
- */
1886
- function assertNotNullable(input) {
1887
- if (input === null || input === void 0) throw new DataError$1({ input }, "NULLABLE_INPUT", "Expected the input not to be undefined or null");
1888
- }
1889
- //#endregion
1890
- //#region src/root/errors/assertNotUndefined.ts
1891
- /**
1892
- * Asserts that a given input is not `undefined`, and throws a DataError if it does.
1893
- *
1894
- * If no error is thrown from this, the input type gets narrowed down to not include `undefined`.
1895
- *
1896
- * @template InputType The type of the input.
1897
- *
1898
- * @param input - The input to assert against.
1899
- *
1900
- * @throws {DataError} If the input is `undefined`.
1901
- */
1902
- function assertNotUndefined(input) {
1903
- if (input === void 0) throw new DataError$1({ input }, "UNDEFINED_INPUT", "Expected the input not to be undefined");
1904
- }
1905
- //#endregion
1906
1963
  exports.APIError = APIError;
1907
1964
  exports.DataError = DataError;
1908
1965
  exports.Env = Env;
@@ -1923,6 +1980,7 @@ exports.assertNotUndefined = assertNotUndefined;
1923
1980
  exports.az = az;
1924
1981
  exports.calculateMonthlyDifference = calculateMonthlyDifference;
1925
1982
  exports.camelToKebab = camelToKebab;
1983
+ exports.containsKeys = containsKeys;
1926
1984
  exports.convertFileToBase64 = convertFileToBase64;
1927
1985
  exports.createFormData = createFormData;
1928
1986
  exports.createTemplateStringsArray = createTemplateStringsArray;
@@ -1940,12 +1998,14 @@ exports.interpolateObjects = interpolateObjects;
1940
1998
  exports.isAnniversary = isAnniversary;
1941
1999
  exports.isLeapYear = isLeapYear;
1942
2000
  exports.isMonthlyMultiple = isMonthlyMultiple;
2001
+ exports.isNonNullableObject = isNonNullableObject;
1943
2002
  exports.isOrdered = isOrdered;
1944
2003
  exports.isSameDate = isSameDate;
1945
2004
  exports.isTemplateStringsArray = isTemplateStringsArray;
1946
2005
  exports.kebabToCamel = kebabToCamel;
1947
2006
  exports.normaliseIndents = normaliseIndents;
1948
2007
  exports.normalizeIndents = normalizeIndents;
2008
+ exports.objectContainsKeys = objectContainsKeys;
1949
2009
  exports.omitProperties = omitProperties;
1950
2010
  exports.paralleliseArrays = paralleliseArrays;
1951
2011
  exports.parseBoolean = parseBoolean;