@duplojs/utils 1.4.49 → 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 };
@@ -78,9 +78,7 @@ exports.Path = void 0;
78
78
  clearedPath = segment;
79
79
  continue;
80
80
  }
81
- const formattedSegment = segment
82
- .replace(Path.segmentTrailingRegex, "")
83
- .replace(Path.segmentRelativeRegex, "");
81
+ const formattedSegment = fix(segment);
84
82
  if (formattedSegment.startsWith("/")) {
85
83
  clearedPath = formattedSegment;
86
84
  continue;
@@ -111,4 +109,13 @@ exports.Path = void 0;
111
109
  return `${dotResult.join("/")}/${result.join("/")}`;
112
110
  }
113
111
  Path.resolveRelative = resolveRelative;
112
+ /**
113
+ * {@include common/path/fix/index.md}
114
+ */
115
+ function fix(path) {
116
+ return path
117
+ .replace(Path.segmentTrailingRegex, "")
118
+ .replace(Path.segmentRelativeRegex, "");
119
+ }
120
+ Path.fix = fix;
114
121
  })(exports.Path || (exports.Path = {}));
@@ -142,4 +142,25 @@ export declare namespace Path {
142
142
  *
143
143
  */
144
144
  function resolveRelative<GenericSegment extends string>(segments: AnyTuple<GenericSegment>): string;
145
+ /**
146
+ * Cleans a POSIX path by removing a trailing slash and a leading `./` prefix.
147
+ *
148
+ * **Supported call styles:**
149
+ * - Classic: `fix(path)` -> returns a cleaned path
150
+ *
151
+ * The function does not resolve `..` segments and only removes a single trailing slash and a single leading `./`.
152
+ *
153
+ * ```ts
154
+ * const trimmedTrailing = Path.fix("alpha/beta/");
155
+ * // trimmedTrailing: "alpha/beta"
156
+ * const trimmedRelative = Path.fix("./alpha/beta");
157
+ * // trimmedRelative: "alpha/beta"
158
+ * const cleanedBoth = Path.fix("./alpha/");
159
+ * // cleanedBoth: "alpha"
160
+ * ```
161
+ *
162
+ * @see https://utils.duplojs.dev/en/v1/api/common/path/fix
163
+ *
164
+ */
165
+ function fix(path: string): string;
145
166
  }
@@ -76,9 +76,7 @@ var Path;
76
76
  clearedPath = segment;
77
77
  continue;
78
78
  }
79
- const formattedSegment = segment
80
- .replace(Path.segmentTrailingRegex, "")
81
- .replace(Path.segmentRelativeRegex, "");
79
+ const formattedSegment = fix(segment);
82
80
  if (formattedSegment.startsWith("/")) {
83
81
  clearedPath = formattedSegment;
84
82
  continue;
@@ -109,6 +107,15 @@ var Path;
109
107
  return `${dotResult.join("/")}/${result.join("/")}`;
110
108
  }
111
109
  Path.resolveRelative = resolveRelative;
110
+ /**
111
+ * {@include common/path/fix/index.md}
112
+ */
113
+ function fix(path) {
114
+ return path
115
+ .replace(Path.segmentTrailingRegex, "")
116
+ .replace(Path.segmentRelativeRegex, "");
117
+ }
118
+ Path.fix = fix;
112
119
  })(Path || (Path = {}));
113
120
 
114
121
  export { Path };
@@ -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 };
@@ -5,7 +5,7 @@ export declare const failKind: import("../../common/kind").KindHandler<import(".
5
5
  * @deprecated use failKind
6
6
  */
7
7
  export declare const eitherFailKind: import("../../common/kind").KindHandler<import("../../common/kind").KindDefinition<"@DuplojsUtilsEither/fail", unknown>>;
8
- type _Fail = (Left<"fail", never> & Kind<typeof failKind.definition>);
8
+ type _Fail = (Left<"fail", void> & Kind<typeof failKind.definition>);
9
9
  export interface Fail extends _Fail {
10
10
  }
11
11
  /**
@@ -13,7 +13,7 @@ export interface Fail extends _Fail {
13
13
  */
14
14
  export type EitherFail = Fail;
15
15
  /**
16
- * Returns an Left<"fail", never>: perfect to signal a failure without carrying extra data.
16
+ * Returns an Left<"fail", void>: perfect to signal a failure without carrying extra data.
17
17
  *
18
18
  * Signature: `fail()` → returns a value
19
19
  *
@@ -5,7 +5,7 @@ export declare const okKind: import("../../common/kind").KindHandler<import("../
5
5
  * @deprecated use okKind
6
6
  */
7
7
  export declare const eitherOkKind: import("../../common/kind").KindHandler<import("../../common/kind").KindDefinition<"@DuplojsUtilsEither/ok", unknown>>;
8
- type _Ok = (Right<"ok", never> & Kind<typeof okKind.definition>);
8
+ type _Ok = (Right<"ok", void> & Kind<typeof okKind.definition>);
9
9
  export interface Ok extends _Ok {
10
10
  }
11
11
  /**
@@ -13,7 +13,7 @@ export interface Ok extends _Ok {
13
13
  */
14
14
  export type EitherOk = Ok;
15
15
  /**
16
- * Returns an Right<"ok", never>: an empty success that confirms an operation went fine without extra data.
16
+ * Returns an Right<"ok", void>: an empty success that confirms an operation went fine without extra data.
17
17
  *
18
18
  * Signature: `ok()` → returns a value
19
19
  *
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.49",
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,