@augment-vir/common 13.2.3 → 13.3.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.
@@ -2,27 +2,38 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.copyThroughJson = exports.areJsonEqual = exports.isObject = void 0;
4
4
  const error_1 = require("../error");
5
- const object_entries_1 = require("./object-entries");
6
5
  function isObject(input) {
7
6
  return !!input && typeof input === 'object';
8
7
  }
9
8
  exports.isObject = isObject;
9
+ const areJsonEqualFailureMessage = 'Failed to compare objects using JSON.stringify';
10
+ function baseAreJsonEqual(a, b) {
11
+ return JSON.stringify(a) === JSON.stringify(b);
12
+ }
10
13
  function areJsonEqual(a, b) {
11
14
  try {
12
15
  if (a === b) {
13
16
  return true;
14
17
  }
15
18
  if (isObject(a) && isObject(b)) {
16
- const sortedAEntries = (0, object_entries_1.getEntriesSortedByKey)(a);
17
- const sortedBEntries = (0, object_entries_1.getEntriesSortedByKey)(b);
18
- return JSON.stringify(sortedAEntries) === JSON.stringify(sortedBEntries);
19
+ const areKeysEqual = baseAreJsonEqual(Object.keys(a).sort(), Object.keys(b).sort());
20
+ if (!areKeysEqual) {
21
+ return false;
22
+ }
23
+ return Object.keys(a).every((keyName) => {
24
+ return areJsonEqual(a[keyName], b[keyName]);
25
+ });
19
26
  }
20
27
  else {
21
- return JSON.stringify(a) === JSON.stringify(b);
28
+ return baseAreJsonEqual(a, b);
22
29
  }
23
30
  }
24
- catch (error) {
25
- console.error(`Failed to compare objects using JSON.stringify: ${(0, error_1.extractErrorMessage)(error)}`);
31
+ catch (caught) {
32
+ const error = (0, error_1.ensureError)(caught);
33
+ if (error.message.startsWith(areJsonEqualFailureMessage)) {
34
+ throw error;
35
+ }
36
+ error.message = `${areJsonEqualFailureMessage}: ${error.message}`;
26
37
  throw error;
27
38
  }
28
39
  }
@@ -1,7 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isLengthAtLeast = void 0;
3
+ exports.assertLengthAtLeast = exports.isLengthAtLeast = void 0;
4
4
  function isLengthAtLeast(array, length) {
5
- return array.length >= length;
5
+ try {
6
+ assertLengthAtLeast(array, length);
7
+ return true;
8
+ }
9
+ catch (error) {
10
+ return false;
11
+ }
6
12
  }
7
13
  exports.isLengthAtLeast = isLengthAtLeast;
14
+ function assertLengthAtLeast(array, length, arrayName) {
15
+ if (array.length < length) {
16
+ throw new Error(arrayName
17
+ ? `'${arrayName}' is not at least '${length}' in length.`
18
+ : `Array is not at least '${length}' in length.`);
19
+ }
20
+ }
21
+ exports.assertLengthAtLeast = assertLengthAtLeast;
@@ -1,24 +1,35 @@
1
- import { extractErrorMessage } from '../error';
2
- import { getEntriesSortedByKey } from './object-entries';
1
+ import { ensureError } from '../error';
3
2
  export function isObject(input) {
4
3
  return !!input && typeof input === 'object';
5
4
  }
5
+ const areJsonEqualFailureMessage = 'Failed to compare objects using JSON.stringify';
6
+ function baseAreJsonEqual(a, b) {
7
+ return JSON.stringify(a) === JSON.stringify(b);
8
+ }
6
9
  export function areJsonEqual(a, b) {
7
10
  try {
8
11
  if (a === b) {
9
12
  return true;
10
13
  }
11
14
  if (isObject(a) && isObject(b)) {
12
- const sortedAEntries = getEntriesSortedByKey(a);
13
- const sortedBEntries = getEntriesSortedByKey(b);
14
- return JSON.stringify(sortedAEntries) === JSON.stringify(sortedBEntries);
15
+ const areKeysEqual = baseAreJsonEqual(Object.keys(a).sort(), Object.keys(b).sort());
16
+ if (!areKeysEqual) {
17
+ return false;
18
+ }
19
+ return Object.keys(a).every((keyName) => {
20
+ return areJsonEqual(a[keyName], b[keyName]);
21
+ });
15
22
  }
16
23
  else {
17
- return JSON.stringify(a) === JSON.stringify(b);
24
+ return baseAreJsonEqual(a, b);
18
25
  }
19
26
  }
20
- catch (error) {
21
- console.error(`Failed to compare objects using JSON.stringify: ${extractErrorMessage(error)}`);
27
+ catch (caught) {
28
+ const error = ensureError(caught);
29
+ if (error.message.startsWith(areJsonEqualFailureMessage)) {
30
+ throw error;
31
+ }
32
+ error.message = `${areJsonEqualFailureMessage}: ${error.message}`;
22
33
  throw error;
23
34
  }
24
35
  }
@@ -1,3 +1,16 @@
1
1
  export function isLengthAtLeast(array, length) {
2
- return array.length >= length;
2
+ try {
3
+ assertLengthAtLeast(array, length);
4
+ return true;
5
+ }
6
+ catch (error) {
7
+ return false;
8
+ }
9
+ }
10
+ export function assertLengthAtLeast(array, length, arrayName) {
11
+ if (array.length < length) {
12
+ throw new Error(arrayName
13
+ ? `'${arrayName}' is not at least '${length}' in length.`
14
+ : `Array is not at least '${length}' in length.`);
15
+ }
3
16
  }
@@ -23,4 +23,4 @@ export type NoInputsFunction<ReturnGeneric = any> = () => ReturnGeneric;
23
23
  * TypedFunction<[string | undefined], number>; // (input1: string|undefined) => number
24
24
  */
25
25
  export type TypedFunction<Arguments, Return> = Arguments extends readonly any[] ? number extends Arguments['length'] ? (...args: ArrayElement<Arguments>[]) => Return : (...args: Arguments) => Return : void extends Arguments ? () => Return : (arg: Arguments) => Return;
26
- export declare function isTruthy<T>(input: T): input is NonNullable<T>;
26
+ export declare function isTruthy<T>(input: T): input is Exclude<T, undefined | null | false | 0 | '' | -0 | 0n>;
@@ -5,4 +5,5 @@ export type AtLeastTuple<ArrayElementGeneric, LengthGeneric extends number> = re
5
5
  ...(ArrayElementGeneric | undefined)[]
6
6
  ];
7
7
  export declare function isLengthAtLeast<ArrayElementGeneric, LengthGeneric extends number>(array: ReadonlyArray<ArrayElementGeneric | undefined>, length: LengthGeneric): array is AtLeastTuple<ArrayElementGeneric, LengthGeneric>;
8
+ export declare function assertLengthAtLeast<ArrayElementGeneric, LengthGeneric extends number>(array: ReadonlyArray<ArrayElementGeneric | undefined>, length: LengthGeneric, arrayName?: string): asserts array is AtLeastTuple<ArrayElementGeneric, LengthGeneric>;
8
9
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "13.2.3",
3
+ "version": "13.3.0",
4
4
  "homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
5
5
  "bugs": {
6
6
  "url": "https://github.com/electrovir/augment-vir/issues"
@@ -24,10 +24,10 @@
24
24
  "test:types": "tsc --noEmit"
25
25
  },
26
26
  "dependencies": {
27
- "type-fest": "^3.7.2"
27
+ "type-fest": "^3.8.0"
28
28
  },
29
29
  "devDependencies": {
30
- "typescript": "^5.0.3"
30
+ "typescript": "^5.0.4"
31
31
  },
32
32
  "publishConfig": {
33
33
  "access": "public"