@duplojs/utils 1.4.50 → 1.4.51

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.
@@ -42,6 +42,7 @@ export * from "./toJSON";
42
42
  export * from "./toTransform";
43
43
  export * from "./toWrappedValue";
44
44
  export * from "./unwrap";
45
+ export * from "./unwrapGroup";
45
46
  export * from "./asyncLoop";
46
47
  export * from "./asyncRetry";
47
48
  export * from "./wrapValue";
@@ -59,6 +60,7 @@ export * from "./or";
59
60
  export * from "./whenElse";
60
61
  export * from "./justReturn";
61
62
  export * from "./memo";
63
+ export * from "./memoPromise";
62
64
  export * from "./instanceOf";
63
65
  export * from "./globalStore";
64
66
  export * from "./builder";
@@ -1,13 +1,12 @@
1
- import { type AnyValue } from "./types";
2
1
  export interface Memoized<GenericValue extends unknown> {
3
2
  readonly value: GenericValue;
4
3
  }
5
4
  /**
6
- * The memo() function evaluates a function only once then memorizes the result. Subsequent calls return the same value without recalculation.
5
+ * The memo() function evaluates a function only once then memorizes the result. The first access to `value` triggers the evaluation.
7
6
  *
8
- * Signature: `memo(theFunction)` → returns a value
7
+ * Signature: `memo(theFunction)` → returns a memoized object
9
8
  *
10
- * The input value is not mutated.
9
+ * The input function is called only once.
11
10
  *
12
11
  * ```ts
13
12
  * let calls = 0;
@@ -25,4 +24,4 @@ export interface Memoized<GenericValue extends unknown> {
25
24
  * @see https://utils.duplojs.dev/en/v1/api/common/memo
26
25
  *
27
26
  */
28
- export declare function memo<GenericOutput extends AnyValue>(theFunction: () => GenericOutput): Memoized<GenericOutput>;
27
+ export declare function memo<GenericOutput extends unknown>(theFunction: () => GenericOutput): Memoized<GenericOutput>;
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ var externalPromise = require('./externalPromise.cjs');
4
+ var forward = require('./forward.cjs');
5
+
6
+ /**
7
+ * {@include common/memoPromise/index.md}
8
+ */
9
+ function memoPromise(theFunction) {
10
+ let externalPromise$1 = forward.forward(undefined);
11
+ const payload = {
12
+ get value() {
13
+ if (externalPromise$1) {
14
+ return externalPromise$1.promise;
15
+ }
16
+ externalPromise$1 = externalPromise.createExternalPromise();
17
+ const { resolve, reject } = externalPromise$1;
18
+ const promise = theFunction();
19
+ void (promise instanceof Promise
20
+ ? promise
21
+ : Promise.resolve(promise))
22
+ .then((result) => {
23
+ Object.defineProperty(payload, "value", {
24
+ value: result,
25
+ });
26
+ resolve(result);
27
+ })
28
+ .catch(reject);
29
+ return externalPromise$1.promise;
30
+ },
31
+ };
32
+ return payload;
33
+ }
34
+
35
+ exports.memoPromise = memoPromise;
@@ -0,0 +1,25 @@
1
+ import { type MaybePromise } from "./types";
2
+ export interface MemoizedPromise<GenericValue extends unknown> {
3
+ readonly value: MaybePromise<GenericValue>;
4
+ }
5
+ /**
6
+ * The memoPromise() function lazily evaluates a function that returns a value or a promise, then memoizes the resolved result.
7
+ *
8
+ * Signature: `memoPromise(theFunction)` → returns a memoized object
9
+ *
10
+ * The input function is called only once, and concurrent reads share the same promise.
11
+ *
12
+ * ```ts
13
+ * const memoizedValue = memoPromise(() => "ready");
14
+ * const first = await memoizedValue.value;
15
+ * // "ready"
16
+ *
17
+ * const memoizedPromise = memoPromise(() => Promise.resolve("ok"));
18
+ * const okValue = await memoizedPromise.value;
19
+ * // "ok"
20
+ * ```
21
+ *
22
+ * @see https://utils.duplojs.dev/en/v1/api/common/memoPromise
23
+ *
24
+ */
25
+ export declare function memoPromise<GenericOutput extends unknown>(theFunction: () => MaybePromise<GenericOutput>): MemoizedPromise<GenericOutput>;
@@ -0,0 +1,33 @@
1
+ import { createExternalPromise } from './externalPromise.mjs';
2
+ import { forward } from './forward.mjs';
3
+
4
+ /**
5
+ * {@include common/memoPromise/index.md}
6
+ */
7
+ function memoPromise(theFunction) {
8
+ let externalPromise = forward(undefined);
9
+ const payload = {
10
+ get value() {
11
+ if (externalPromise) {
12
+ return externalPromise.promise;
13
+ }
14
+ externalPromise = createExternalPromise();
15
+ const { resolve, reject } = externalPromise;
16
+ const promise = theFunction();
17
+ void (promise instanceof Promise
18
+ ? promise
19
+ : Promise.resolve(promise))
20
+ .then((result) => {
21
+ Object.defineProperty(payload, "value", {
22
+ value: result,
23
+ });
24
+ resolve(result);
25
+ })
26
+ .catch(reject);
27
+ return externalPromise.promise;
28
+ },
29
+ };
30
+ return payload;
31
+ }
32
+
33
+ export { memoPromise };
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ var unwrap = require('./unwrap.cjs');
4
+
5
+ /**
6
+ * {@include common/unwrapGroup/index.md}
7
+ */
8
+ function unwrapGroup(group) {
9
+ const result = {};
10
+ for (const key in group) {
11
+ result[key] = unwrap.unwrap(group[key]);
12
+ }
13
+ return result;
14
+ }
15
+
16
+ exports.unwrapGroup = unwrapGroup;
@@ -0,0 +1,36 @@
1
+ import { type SimplifyTopLevel } from "./types";
2
+ import { type Unwrap } from "./unwrap";
3
+ type ComputeResult<GenericGroup extends Record<string, unknown>> = SimplifyTopLevel<{
4
+ [Prop in keyof GenericGroup]: Unwrap<GenericGroup[Prop]>;
5
+ }>;
6
+ /**
7
+ * The unwrapGroup() function unwraps every value of an object using unwrap(), returning a new object with the same keys.
8
+ *
9
+ * Signature: `unwrapGroup(group)` → returns an object
10
+ *
11
+ * The input object is not mutated.
12
+ *
13
+ * ```ts
14
+ * const first = unwrapGroup({
15
+ * value: wrapValue(1),
16
+ * });
17
+ * // { value: 1 }
18
+ *
19
+ * const mixed = unwrapGroup({
20
+ * count: wrapValue(2),
21
+ * label: "ok",
22
+ * });
23
+ * // { count: 2, label: "ok" }
24
+ *
25
+ * const user = unwrapGroup({
26
+ * firstName: wrapValue("Ada"),
27
+ * lastName: "Lovelace",
28
+ * });
29
+ * // { firstName: "Ada", lastName: "Lovelace" }
30
+ * ```
31
+ *
32
+ * @see https://utils.duplojs.dev/en/v1/api/common/unwrapGroup
33
+ *
34
+ */
35
+ export declare function unwrapGroup<const GenericGroup extends Record<string, unknown>>(group: GenericGroup): ComputeResult<GenericGroup>;
36
+ export {};
@@ -0,0 +1,14 @@
1
+ import { unwrap } from './unwrap.mjs';
2
+
3
+ /**
4
+ * {@include common/unwrapGroup/index.md}
5
+ */
6
+ function unwrapGroup(group) {
7
+ const result = {};
8
+ for (const key in group) {
9
+ result[key] = unwrap(group[key]);
10
+ }
11
+ return result;
12
+ }
13
+
14
+ export { unwrapGroup };
package/dist/index.cjs CHANGED
@@ -31,6 +31,7 @@ var toJSON = require('./common/toJSON.cjs');
31
31
  var toTransform = require('./common/toTransform.cjs');
32
32
  var toWrappedValue = require('./common/toWrappedValue.cjs');
33
33
  var unwrap = require('./common/unwrap.cjs');
34
+ var unwrapGroup = require('./common/unwrapGroup.cjs');
34
35
  var asyncLoop = require('./common/asyncLoop.cjs');
35
36
  var asyncRetry = require('./common/asyncRetry.cjs');
36
37
  var wrapValue = require('./common/wrapValue.cjs');
@@ -48,6 +49,7 @@ var or = require('./common/or.cjs');
48
49
  var whenElse = require('./common/whenElse.cjs');
49
50
  var justReturn = require('./common/justReturn.cjs');
50
51
  var memo = require('./common/memo.cjs');
52
+ var memoPromise = require('./common/memoPromise.cjs');
51
53
  var instanceOf = require('./common/instanceOf.cjs');
52
54
  var globalStore = require('./common/globalStore.cjs');
53
55
  var builder = require('./common/builder.cjs');
@@ -115,6 +117,7 @@ exports.toJSON = toJSON.toJSON;
115
117
  exports.toTransform = toTransform.toTransform;
116
118
  exports.toWrappedValue = toWrappedValue.toWrappedValue;
117
119
  exports.unwrap = unwrap.unwrap;
120
+ exports.unwrapGroup = unwrapGroup.unwrapGroup;
118
121
  exports.asyncLoop = asyncLoop.asyncLoop;
119
122
  exports.createAsyncRetry = asyncRetry.createAsyncRetry;
120
123
  exports.useAsyncRetry = asyncRetry.useAsyncRetry;
@@ -136,6 +139,7 @@ exports.or = or.or;
136
139
  exports.whenElse = whenElse.whenElse;
137
140
  exports.justReturn = justReturn.justReturn;
138
141
  exports.memo = memo.memo;
142
+ exports.memoPromise = memoPromise.memoPromise;
139
143
  exports.instanceOf = instanceOf.instanceOf;
140
144
  exports.createGlobalStore = globalStore.createGlobalStore;
141
145
  exports.MissingBuilderMethodsError = builder.MissingBuilderMethodsError;
package/dist/index.mjs CHANGED
@@ -53,6 +53,7 @@ export { toJSON } from './common/toJSON.mjs';
53
53
  export { toTransform } from './common/toTransform.mjs';
54
54
  export { toWrappedValue } from './common/toWrappedValue.mjs';
55
55
  export { unwrap } from './common/unwrap.mjs';
56
+ export { unwrapGroup } from './common/unwrapGroup.mjs';
56
57
  export { asyncLoop } from './common/asyncLoop.mjs';
57
58
  export { createAsyncRetry, useAsyncRetry } from './common/asyncRetry.mjs';
58
59
  export { isRuntimeWrappedValueKey, isWrappedValue, keyWrappedValue, wrapValue } from './common/wrapValue.mjs';
@@ -70,6 +71,7 @@ export { or } from './common/or.mjs';
70
71
  export { whenElse } from './common/whenElse.mjs';
71
72
  export { justReturn } from './common/justReturn.mjs';
72
73
  export { memo } from './common/memo.mjs';
74
+ export { memoPromise } from './common/memoPromise.mjs';
73
75
  export { instanceOf } from './common/instanceOf.mjs';
74
76
  export { createGlobalStore } from './common/globalStore.mjs';
75
77
  export { MissingBuilderMethodsError, builderKind, createBuilder } from './common/builder.mjs';
@@ -1415,6 +1415,15 @@
1415
1415
  {
1416
1416
  "name": "memo.mjs"
1417
1417
  },
1418
+ {
1419
+ "name": "memoPromise.cjs"
1420
+ },
1421
+ {
1422
+ "name": "memoPromise.d.ts"
1423
+ },
1424
+ {
1425
+ "name": "memoPromise.mjs"
1426
+ },
1418
1427
  {
1419
1428
  "name": "mimeType.cjs"
1420
1429
  },
@@ -1577,6 +1586,15 @@
1577
1586
  {
1578
1587
  "name": "unwrap.mjs"
1579
1588
  },
1589
+ {
1590
+ "name": "unwrapGroup.cjs"
1591
+ },
1592
+ {
1593
+ "name": "unwrapGroup.d.ts"
1594
+ },
1595
+ {
1596
+ "name": "unwrapGroup.mjs"
1597
+ },
1580
1598
  {
1581
1599
  "name": "when.cjs"
1582
1600
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@duplojs/utils",
3
- "version": "1.4.50",
3
+ "version": "1.4.51",
4
4
  "author": {
5
5
  "name": "mathcovax",
6
6
  "url": "https://github.com/mathcovax"
@@ -27,8 +27,8 @@
27
27
  "test:tu:watch": "vitest --coverage --watch",
28
28
  "test:tu:update": "vitest --coverage --update",
29
29
  "test:types": "tsc -p tsconfig.test.json && npm -w docs run test:types",
30
- "test:lint": "eslint",
31
- "test:lint:fix": "eslint --fix",
30
+ "test:lint": "eslint --quiet",
31
+ "test:lint:fix": "eslint --fix --quiet",
32
32
  "prepare": "husky"
33
33
  },
34
34
  "sideEffects": false,