@augment-vir/common 14.3.1 → 15.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.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.executeAndReturnError = exports.wrapInTry = exports.ensureError = exports.extractErrorMessage = exports.combineErrorMessages = exports.combineErrors = void 0;
3
+ exports.executeAndReturnError = exports.wrapInTry = exports.ensureErrorAndPrependMessage = exports.ensureError = exports.extractErrorMessage = exports.combineErrorMessages = exports.combineErrors = void 0;
4
4
  const __1 = require("..");
5
5
  function combineErrors(errors) {
6
6
  if (!errors || errors.length === 0) {
@@ -20,27 +20,36 @@ function combineErrorMessages(errors) {
20
20
  return errors.map(extractErrorMessage).filter(__1.isTruthy).join('\n');
21
21
  }
22
22
  exports.combineErrorMessages = combineErrorMessages;
23
- function extractErrorMessage(error) {
24
- if (!error) {
23
+ function extractErrorMessage(maybeError) {
24
+ if (!maybeError) {
25
25
  return '';
26
26
  }
27
- if (error instanceof Error) {
28
- return error.message;
27
+ if (maybeError instanceof Error) {
28
+ return maybeError.message;
29
+ }
30
+ else if ((0, __1.typedHasProperty)(maybeError, 'message')) {
31
+ return String(maybeError.message);
29
32
  }
30
33
  else {
31
- return String(error);
34
+ return String(maybeError);
32
35
  }
33
36
  }
34
37
  exports.extractErrorMessage = extractErrorMessage;
35
- function ensureError(input) {
36
- if (input instanceof Error) {
37
- return input;
38
+ function ensureError(maybeError) {
39
+ if (maybeError instanceof Error) {
40
+ return maybeError;
38
41
  }
39
42
  else {
40
- return new Error(extractErrorMessage(input));
43
+ return new Error(extractErrorMessage(maybeError));
41
44
  }
42
45
  }
43
46
  exports.ensureError = ensureError;
47
+ function ensureErrorAndPrependMessage(maybeError, prependMessage) {
48
+ const error = ensureError(maybeError);
49
+ error.message = `${prependMessage}: ${error.message}`;
50
+ return error;
51
+ }
52
+ exports.ensureErrorAndPrependMessage = ensureErrorAndPrependMessage;
44
53
  function wrapInTry(inputs) {
45
54
  try {
46
55
  return inputs.callback();
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.removePx = exports.addPx = void 0;
4
+ function addPx(input) {
5
+ if (String(input).endsWith('px')) {
6
+ return String(input);
7
+ }
8
+ else {
9
+ return `${input}px`;
10
+ }
11
+ }
12
+ exports.addPx = addPx;
13
+ function removePx(input) {
14
+ return Number(input.replace(/px$/, ''));
15
+ }
16
+ exports.removePx = removePx;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.measureCallbackDuration = exports.timeCallback = void 0;
4
+ /**
5
+ * Measures how long (in milliseconds) the given callback takes to run to completion. Automatically
6
+ * switches to async mode and awaits callbacks if they return a promise (otherwise this function is
7
+ * purely synchronous).
8
+ */
9
+ function timeCallback(callback) {
10
+ const startTime = Date.now();
11
+ const result = callback();
12
+ if (result instanceof Promise) {
13
+ return new Promise(async (resolve, reject) => {
14
+ try {
15
+ await result;
16
+ const endTime = Date.now();
17
+ resolve(endTime - startTime);
18
+ }
19
+ catch (caught) {
20
+ reject(caught);
21
+ }
22
+ });
23
+ }
24
+ const endTime = Date.now();
25
+ return (endTime - startTime);
26
+ }
27
+ exports.timeCallback = timeCallback;
28
+ /**
29
+ * Measures how long (in milliseconds) the given callback takes to run to completion. Automatically
30
+ * switches to async mode and awaits callbacks if they return a promise (otherwise this function is
31
+ * purely synchronous).
32
+ *
33
+ * Alias of timeCallback.
34
+ */
35
+ exports.measureCallbackDuration = timeCallback;
package/dist/cjs/index.js CHANGED
@@ -19,8 +19,6 @@ __exportStar(require("./augments/array"), exports);
19
19
  __exportStar(require("./augments/async"), exports);
20
20
  __exportStar(require("./augments/common-number"), exports);
21
21
  __exportStar(require("./augments/common-string"), exports);
22
- __exportStar(require("./augments/date/date"), exports);
23
- __exportStar(require("./augments/date/relative-date"), exports);
24
22
  __exportStar(require("./augments/environment"), exports);
25
23
  __exportStar(require("./augments/error"), exports);
26
24
  __exportStar(require("./augments/function"), exports);
@@ -38,11 +36,13 @@ __exportStar(require("./augments/object/object"), exports);
38
36
  __exportStar(require("./augments/object/object-entries"), exports);
39
37
  __exportStar(require("./augments/object/pick-deep"), exports);
40
38
  __exportStar(require("./augments/object/typed-has-property"), exports);
39
+ __exportStar(require("./augments/pixel"), exports);
41
40
  __exportStar(require("./augments/promise"), exports);
42
41
  __exportStar(require("./augments/regexp"), exports);
43
42
  __exportStar(require("./augments/runtime-type-of"), exports);
44
43
  __exportStar(require("./augments/string/url"), exports);
45
44
  __exportStar(require("./augments/string/uuid"), exports);
45
+ __exportStar(require("./augments/time"), exports);
46
46
  __exportStar(require("./augments/truncate-number"), exports);
47
47
  __exportStar(require("./augments/tuple"), exports);
48
48
  __exportStar(require("./augments/type"), exports);
@@ -1,4 +1,4 @@
1
- import { isPromiseLike, isTruthy } from '..';
1
+ import { isPromiseLike, isTruthy, typedHasProperty, } from '..';
2
2
  export function combineErrors(errors) {
3
3
  if (!errors || errors.length === 0) {
4
4
  return undefined;
@@ -15,25 +15,33 @@ export function combineErrorMessages(errors) {
15
15
  }
16
16
  return errors.map(extractErrorMessage).filter(isTruthy).join('\n');
17
17
  }
18
- export function extractErrorMessage(error) {
19
- if (!error) {
18
+ export function extractErrorMessage(maybeError) {
19
+ if (!maybeError) {
20
20
  return '';
21
21
  }
22
- if (error instanceof Error) {
23
- return error.message;
22
+ if (maybeError instanceof Error) {
23
+ return maybeError.message;
24
+ }
25
+ else if (typedHasProperty(maybeError, 'message')) {
26
+ return String(maybeError.message);
24
27
  }
25
28
  else {
26
- return String(error);
29
+ return String(maybeError);
27
30
  }
28
31
  }
29
- export function ensureError(input) {
30
- if (input instanceof Error) {
31
- return input;
32
+ export function ensureError(maybeError) {
33
+ if (maybeError instanceof Error) {
34
+ return maybeError;
32
35
  }
33
36
  else {
34
- return new Error(extractErrorMessage(input));
37
+ return new Error(extractErrorMessage(maybeError));
35
38
  }
36
39
  }
40
+ export function ensureErrorAndPrependMessage(maybeError, prependMessage) {
41
+ const error = ensureError(maybeError);
42
+ error.message = `${prependMessage}: ${error.message}`;
43
+ return error;
44
+ }
37
45
  export function wrapInTry(inputs) {
38
46
  try {
39
47
  return inputs.callback();
@@ -0,0 +1,11 @@
1
+ export function addPx(input) {
2
+ if (String(input).endsWith('px')) {
3
+ return String(input);
4
+ }
5
+ else {
6
+ return `${input}px`;
7
+ }
8
+ }
9
+ export function removePx(input) {
10
+ return Number(input.replace(/px$/, ''));
11
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Measures how long (in milliseconds) the given callback takes to run to completion. Automatically
3
+ * switches to async mode and awaits callbacks if they return a promise (otherwise this function is
4
+ * purely synchronous).
5
+ */
6
+ export function timeCallback(callback) {
7
+ const startTime = Date.now();
8
+ const result = callback();
9
+ if (result instanceof Promise) {
10
+ return new Promise(async (resolve, reject) => {
11
+ try {
12
+ await result;
13
+ const endTime = Date.now();
14
+ resolve(endTime - startTime);
15
+ }
16
+ catch (caught) {
17
+ reject(caught);
18
+ }
19
+ });
20
+ }
21
+ const endTime = Date.now();
22
+ return (endTime - startTime);
23
+ }
24
+ /**
25
+ * Measures how long (in milliseconds) the given callback takes to run to completion. Automatically
26
+ * switches to async mode and awaits callbacks if they return a promise (otherwise this function is
27
+ * purely synchronous).
28
+ *
29
+ * Alias of timeCallback.
30
+ */
31
+ export const measureCallbackDuration = timeCallback;
package/dist/esm/index.js CHANGED
@@ -3,8 +3,6 @@ export * from './augments/array';
3
3
  export * from './augments/async';
4
4
  export * from './augments/common-number';
5
5
  export * from './augments/common-string';
6
- export * from './augments/date/date';
7
- export * from './augments/date/relative-date';
8
6
  export * from './augments/environment';
9
7
  export * from './augments/error';
10
8
  export * from './augments/function';
@@ -22,11 +20,13 @@ export * from './augments/object/object';
22
20
  export * from './augments/object/object-entries';
23
21
  export * from './augments/object/pick-deep';
24
22
  export * from './augments/object/typed-has-property';
23
+ export * from './augments/pixel';
25
24
  export * from './augments/promise';
26
25
  export * from './augments/regexp';
27
26
  export * from './augments/runtime-type-of';
28
27
  export * from './augments/string/url';
29
28
  export * from './augments/string/uuid';
29
+ export * from './augments/time';
30
30
  export * from './augments/truncate-number';
31
31
  export * from './augments/tuple';
32
32
  export * from './augments/type';
@@ -5,8 +5,9 @@ export declare function combineErrors(errors: ReadonlyArray<never>): undefined;
5
5
  export declare function combineErrors(errors: ReadonlyArray<Error>): Error | undefined;
6
6
  export declare function combineErrors(errors?: undefined): undefined;
7
7
  export declare function combineErrorMessages(errors?: ReadonlyArray<Error | string | undefined> | undefined): string;
8
- export declare function extractErrorMessage(error: unknown): string;
9
- export declare function ensureError(input: unknown): Error;
8
+ export declare function extractErrorMessage(maybeError: unknown): string;
9
+ export declare function ensureError(maybeError: unknown): Error;
10
+ export declare function ensureErrorAndPrependMessage(maybeError: unknown, prependMessage: string): Error;
10
11
  export type TryWrapInputs<CallbackReturn, FallbackReturn> = {
11
12
  callback: () => CallbackReturn;
12
13
  } & RequireExactlyOne<{
@@ -0,0 +1,3 @@
1
+ export type WithPx = `${string | number}px`;
2
+ export declare function addPx(input: number | string): WithPx;
3
+ export declare function removePx(input: WithPx | string): number;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Measures how long (in milliseconds) the given callback takes to run to completion. Automatically
3
+ * switches to async mode and awaits callbacks if they return a promise (otherwise this function is
4
+ * purely synchronous).
5
+ */
6
+ export declare function timeCallback<T>(callback: () => T): T extends Promise<any> ? Promise<number> : number;
7
+ /**
8
+ * Measures how long (in milliseconds) the given callback takes to run to completion. Automatically
9
+ * switches to async mode and awaits callbacks if they return a promise (otherwise this function is
10
+ * purely synchronous).
11
+ *
12
+ * Alias of timeCallback.
13
+ */
14
+ export declare const measureCallbackDuration: typeof timeCallback;
@@ -3,8 +3,6 @@ export * from './augments/array';
3
3
  export * from './augments/async';
4
4
  export * from './augments/common-number';
5
5
  export * from './augments/common-string';
6
- export * from './augments/date/date';
7
- export * from './augments/date/relative-date';
8
6
  export * from './augments/environment';
9
7
  export * from './augments/error';
10
8
  export * from './augments/function';
@@ -22,11 +20,13 @@ export * from './augments/object/object';
22
20
  export * from './augments/object/object-entries';
23
21
  export * from './augments/object/pick-deep';
24
22
  export * from './augments/object/typed-has-property';
23
+ export * from './augments/pixel';
25
24
  export * from './augments/promise';
26
25
  export * from './augments/regexp';
27
26
  export * from './augments/runtime-type-of';
28
27
  export * from './augments/string/url';
29
28
  export * from './augments/string/uuid';
29
+ export * from './augments/time';
30
30
  export * from './augments/truncate-number';
31
31
  export * from './augments/tuple';
32
32
  export * from './augments/type';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "14.3.1",
3
+ "version": "15.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"