@augment-vir/common 22.4.0 → 23.0.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.
@@ -79,8 +79,7 @@ function compareInnerValue(testValue, matchValue, throwKeyError, allowExtraProps
79
79
  if ((0, typed_has_property_1.typedHasProperty)(matchValue, 'constructor')) {
80
80
  if (!(0, typed_has_property_1.typedHasProperty)(testValue, 'constructor') ||
81
81
  testValue.constructor !== matchValue.constructor) {
82
- throwKeyError(`constructor "${testValue?.constructor
83
- ?.name}" did not match expected constructor "${matchValue.constructor}"`);
82
+ throwKeyError(`constructor "${testValue?.constructor?.name}" did not match expected constructor "${matchValue.constructor}"`);
84
83
  }
85
84
  }
86
85
  }
@@ -14,9 +14,24 @@ exports.isPromiseLike = isPromiseLike;
14
14
  class PromiseTimeoutError extends Error {
15
15
  constructor(durationMs, message = `Promised timed out after ${durationMs} ms.`) {
16
16
  super(message);
17
- this.durationMs = durationMs;
18
- this.message = message;
19
- this.name = 'PromiseTimeoutError';
17
+ Object.defineProperty(this, "durationMs", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: durationMs
22
+ });
23
+ Object.defineProperty(this, "message", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: message
28
+ });
29
+ Object.defineProperty(this, "name", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: 'PromiseTimeoutError'
34
+ });
20
35
  }
21
36
  }
22
37
  exports.PromiseTimeoutError = PromiseTimeoutError;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.waitForCondition = exports.waitValue = exports.wait = void 0;
3
+ exports.waitUntilTruthy = exports.defaultWaitUntilDefinedOptions = exports.waitValue = exports.wait = void 0;
4
4
  const error_1 = require("../error");
5
5
  const deferred_promise_1 = require("./deferred-promise");
6
6
  function wait(delayMs) {
@@ -17,27 +17,50 @@ async function waitValue(delayMs, returnValue) {
17
17
  return wait(delayMs).then(() => returnValue);
18
18
  }
19
19
  exports.waitValue = waitValue;
20
- async function waitForCondition({ conditionCallback, timeoutMs = 10000, intervalMs = 100, timeoutMessage = '', }) {
21
- let condition = false;
20
+ exports.defaultWaitUntilDefinedOptions = {
21
+ interval: {
22
+ milliseconds: 100,
23
+ },
24
+ timeout: {
25
+ milliseconds: 10000,
26
+ },
27
+ };
28
+ /**
29
+ * Runs the predicate until it returns a truthy value, then returns that value. Use the options
30
+ * input to modify the timeout and interval durations. Automatically catches errors and handles
31
+ * async predicates.
32
+ */
33
+ async function waitUntilTruthy(predicate, failureMessage, optionsInput = {}) {
34
+ const options = {
35
+ ...exports.defaultWaitUntilDefinedOptions,
36
+ ...optionsInput,
37
+ };
38
+ let lastValue = undefined;
22
39
  let lastError;
23
40
  async function checkCondition() {
24
41
  try {
25
- condition = !!(await conditionCallback());
42
+ lastValue = await predicate();
26
43
  }
27
44
  catch (error) {
28
- condition = false;
45
+ lastValue = undefined;
29
46
  lastError = error;
30
47
  }
31
48
  }
32
49
  const startTime = Date.now();
33
- await checkCondition();
34
- while (!condition) {
35
- await wait(intervalMs);
36
- if (Date.now() - startTime >= timeoutMs) {
37
- const message = timeoutMessage ? `${timeoutMessage}: ` : '';
38
- throw new Error(`${message}Timeout of "${timeoutMs}" exceeded waiting for condition to be true${(0, error_1.extractErrorMessage)(lastError)}`);
39
- }
50
+ while (!lastValue) {
40
51
  await checkCondition();
52
+ await wait(options.interval.milliseconds);
53
+ if (Date.now() - startTime >= options.timeout.milliseconds) {
54
+ const message = failureMessage ? `${failureMessage}: ` : '';
55
+ const preMessage = `${message}Timeout of "${options.timeout.milliseconds}" exceeded waiting for value to be defined`;
56
+ if (lastError) {
57
+ throw (0, error_1.ensureErrorAndPrependMessage)(lastError, preMessage);
58
+ }
59
+ else {
60
+ throw new Error(preMessage);
61
+ }
62
+ }
41
63
  }
64
+ return lastValue;
42
65
  }
43
- exports.waitForCondition = waitForCondition;
66
+ exports.waitUntilTruthy = waitUntilTruthy;
@@ -2,7 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.randomString = exports.createUuid = exports.randomBoolean = exports.randomInteger = void 0;
4
4
  const common_number_1 = require("./common-number");
5
- const crypto = globalThis.crypto;
5
+ function accessCrypto() {
6
+ if (globalThis.crypto) {
7
+ return globalThis.crypto;
8
+ }
9
+ else {
10
+ return require('crypto').webcrypto;
11
+ }
12
+ }
13
+ const crypto = accessCrypto();
6
14
  // can't get this coverage to work
7
15
  /* c8 ignore start */
8
16
  /**
@@ -5,13 +5,13 @@ const common_number_1 = require("./common-number");
5
5
  const common_string_1 = require("./common-string");
6
6
  const regexp_1 = require("./regexp");
7
7
  const defaultTruncationSuffixes = [
8
- 'k',
9
- 'M',
10
- 'B',
11
- 'T',
12
- 'P',
13
- 'E',
14
- 'Z',
8
+ 'k', // thousand
9
+ 'M', // million
10
+ 'B', // billion
11
+ 'T', // trillion
12
+ 'P', // peta-, quadrillion
13
+ 'E', // exa- quintillion
14
+ 'Z', // zetta- sextillion
15
15
  'Y', // yotta- septillion
16
16
  ];
17
17
  function combineBeforeAndAfterDot({ beforeDot, afterDot = '', maxLength, }) {
@@ -74,8 +74,7 @@ function compareInnerValue(testValue, matchValue, throwKeyError, allowExtraProps
74
74
  if (typedHasProperty(matchValue, 'constructor')) {
75
75
  if (!typedHasProperty(testValue, 'constructor') ||
76
76
  testValue.constructor !== matchValue.constructor) {
77
- throwKeyError(`constructor "${testValue?.constructor
78
- ?.name}" did not match expected constructor "${matchValue.constructor}"`);
77
+ throwKeyError(`constructor "${testValue?.constructor?.name}" did not match expected constructor "${matchValue.constructor}"`);
79
78
  }
80
79
  }
81
80
  }
@@ -10,9 +10,24 @@ export function isPromiseLike(input) {
10
10
  export class PromiseTimeoutError extends Error {
11
11
  constructor(durationMs, message = `Promised timed out after ${durationMs} ms.`) {
12
12
  super(message);
13
- this.durationMs = durationMs;
14
- this.message = message;
15
- this.name = 'PromiseTimeoutError';
13
+ Object.defineProperty(this, "durationMs", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: durationMs
18
+ });
19
+ Object.defineProperty(this, "message", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: message
24
+ });
25
+ Object.defineProperty(this, "name", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: 'PromiseTimeoutError'
30
+ });
16
31
  }
17
32
  }
18
33
  export function wrapPromiseInTimeout(durationMs, originalPromise) {
@@ -1,4 +1,4 @@
1
- import { extractErrorMessage } from '../error';
1
+ import { ensureErrorAndPrependMessage } from '../error';
2
2
  import { createDeferredPromiseWrapper } from './deferred-promise';
3
3
  export function wait(delayMs) {
4
4
  const deferredPromiseWrapper = createDeferredPromiseWrapper();
@@ -12,26 +12,49 @@ export function wait(delayMs) {
12
12
  export async function waitValue(delayMs, returnValue) {
13
13
  return wait(delayMs).then(() => returnValue);
14
14
  }
15
- export async function waitForCondition({ conditionCallback, timeoutMs = 10000, intervalMs = 100, timeoutMessage = '', }) {
16
- let condition = false;
15
+ export const defaultWaitUntilDefinedOptions = {
16
+ interval: {
17
+ milliseconds: 100,
18
+ },
19
+ timeout: {
20
+ milliseconds: 10000,
21
+ },
22
+ };
23
+ /**
24
+ * Runs the predicate until it returns a truthy value, then returns that value. Use the options
25
+ * input to modify the timeout and interval durations. Automatically catches errors and handles
26
+ * async predicates.
27
+ */
28
+ export async function waitUntilTruthy(predicate, failureMessage, optionsInput = {}) {
29
+ const options = {
30
+ ...defaultWaitUntilDefinedOptions,
31
+ ...optionsInput,
32
+ };
33
+ let lastValue = undefined;
17
34
  let lastError;
18
35
  async function checkCondition() {
19
36
  try {
20
- condition = !!(await conditionCallback());
37
+ lastValue = await predicate();
21
38
  }
22
39
  catch (error) {
23
- condition = false;
40
+ lastValue = undefined;
24
41
  lastError = error;
25
42
  }
26
43
  }
27
44
  const startTime = Date.now();
28
- await checkCondition();
29
- while (!condition) {
30
- await wait(intervalMs);
31
- if (Date.now() - startTime >= timeoutMs) {
32
- const message = timeoutMessage ? `${timeoutMessage}: ` : '';
33
- throw new Error(`${message}Timeout of "${timeoutMs}" exceeded waiting for condition to be true${extractErrorMessage(lastError)}`);
34
- }
45
+ while (!lastValue) {
35
46
  await checkCondition();
47
+ await wait(options.interval.milliseconds);
48
+ if (Date.now() - startTime >= options.timeout.milliseconds) {
49
+ const message = failureMessage ? `${failureMessage}: ` : '';
50
+ const preMessage = `${message}Timeout of "${options.timeout.milliseconds}" exceeded waiting for value to be defined`;
51
+ if (lastError) {
52
+ throw ensureErrorAndPrependMessage(lastError, preMessage);
53
+ }
54
+ else {
55
+ throw new Error(preMessage);
56
+ }
57
+ }
36
58
  }
59
+ return lastValue;
37
60
  }
@@ -1,5 +1,13 @@
1
1
  import { clamp, ensureMinAndMax } from './common-number';
2
- const crypto = globalThis.crypto;
2
+ function accessCrypto() {
3
+ if (globalThis.crypto) {
4
+ return globalThis.crypto;
5
+ }
6
+ else {
7
+ return require('crypto').webcrypto;
8
+ }
9
+ }
10
+ const crypto = accessCrypto();
3
11
  // can't get this coverage to work
4
12
  /* c8 ignore start */
5
13
  /**
@@ -2,13 +2,13 @@ import { addCommasToNumber, convertIntoNumber, doesRequireScientificNotation } f
2
2
  import { typedSplit } from './common-string';
3
3
  import { safeMatch } from './regexp';
4
4
  const defaultTruncationSuffixes = [
5
- 'k',
6
- 'M',
7
- 'B',
8
- 'T',
9
- 'P',
10
- 'E',
11
- 'Z',
5
+ 'k', // thousand
6
+ 'M', // million
7
+ 'B', // billion
8
+ 'T', // trillion
9
+ 'P', // peta-, quadrillion
10
+ 'E', // exa- quintillion
11
+ 'Z', // zetta- sextillion
12
12
  'Y', // yotta- septillion
13
13
  ];
14
14
  function combineBeforeAndAfterDot({ beforeDot, afterDot = '', maxLength, }) {
@@ -1,9 +1,18 @@
1
+ import { FalsyTypes } from '../boolean';
1
2
  export declare function wait(delayMs: number): Promise<void>;
2
3
  export declare function waitValue<ResolutionValue>(delayMs: number, returnValue: ResolutionValue): Promise<ResolutionValue>;
3
- export type WaitForConditionInputs = {
4
- conditionCallback: () => boolean | Promise<boolean>;
5
- timeoutMs?: number;
6
- intervalMs?: number;
7
- timeoutMessage?: string;
4
+ export type WaitUntilTruthyOptions = {
5
+ interval: {
6
+ milliseconds: number;
7
+ };
8
+ timeout: {
9
+ milliseconds: number;
10
+ };
8
11
  };
9
- export declare function waitForCondition({ conditionCallback, timeoutMs, intervalMs, timeoutMessage, }: WaitForConditionInputs): Promise<void>;
12
+ export declare const defaultWaitUntilDefinedOptions: WaitUntilTruthyOptions;
13
+ /**
14
+ * Runs the predicate until it returns a truthy value, then returns that value. Use the options
15
+ * input to modify the timeout and interval durations. Automatically catches errors and handles
16
+ * async predicates.
17
+ */
18
+ export declare function waitUntilTruthy<Value>(predicate: () => Value | Promise<Value> | FalsyTypes, failureMessage?: string | undefined, optionsInput?: Partial<WaitUntilTruthyOptions>): Promise<Awaited<Value>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "22.4.0",
3
+ "version": "23.0.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"
@@ -29,7 +29,7 @@
29
29
  "type-fest": "^4.9.0"
30
30
  },
31
31
  "devDependencies": {
32
- "typescript": "5.2.2"
32
+ "typescript": "5.3.3"
33
33
  },
34
34
  "publishConfig": {
35
35
  "access": "public"