@elyukai/utils 0.1.3 → 0.1.4
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.
- package/CHANGELOG.md +9 -0
- package/README.md +2 -0
- package/dist/async.d.ts +8 -0
- package/dist/async.js +35 -0
- package/dist/maybe.d.ts +4 -0
- package/dist/maybe.js +4 -0
- package/dist/result.d.ts +12 -2
- package/dist/result.js +6 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [0.1.4](https://github.com/elyukai/ts-utils/compare/v0.1.3...v0.1.4) (2026-01-24)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* add `then` function to Maybe and Result ([d6fea69](https://github.com/elyukai/ts-utils/commit/d6fea6979ce0bf687b0e377207483ca151e789d2))
|
|
11
|
+
* add mapAsync function ([55077cd](https://github.com/elyukai/ts-utils/commit/55077cd097faf43d56827399d45011ef079a5206))
|
|
12
|
+
* **result:** constructor with no argument ([f65e830](https://github.com/elyukai/ts-utils/commit/f65e8308158b37d7afca3a5f1059bb93f0db072c))
|
|
13
|
+
|
|
5
14
|
## [0.1.3](https://github.com/elyukai/ts-utils/compare/v0.1.2...v0.1.3) (2026-01-24)
|
|
6
15
|
|
|
7
16
|
## [0.1.2](https://github.com/elyukai/ts-utils/compare/v0.1.1...v0.1.2) (2026-01-24)
|
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# General TypeScript Helpers
|
|
2
2
|
|
|
3
|
+
  [](https://jsr.io/@elyukai/utils) [](https://jsr.io/@elyukai/utils)
|
|
4
|
+
|
|
3
5
|
This package provides a range of utility functions that I personally often need, which is why I bundled them in a package.
|
|
4
6
|
|
|
5
7
|
```ts
|
package/dist/async.d.ts
CHANGED
|
@@ -13,3 +13,11 @@
|
|
|
13
13
|
* ```
|
|
14
14
|
*/
|
|
15
15
|
export declare const wait: (delay: number) => Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* A simple async map to process tasks with a concurrency limit.
|
|
18
|
+
* @param tasks The list of tasks to process.
|
|
19
|
+
* @param worker The async function to process each task.
|
|
20
|
+
* @param concurrency The maximum number of concurrent tasks.
|
|
21
|
+
* @returns A promise that resolves to the list of results.
|
|
22
|
+
*/
|
|
23
|
+
export declare const mapAsync: <T, R>(tasks: readonly T[], worker: (task: T) => Promise<R>, concurrency: number) => Promise<R[]>;
|
package/dist/async.js
CHANGED
|
@@ -13,3 +13,38 @@
|
|
|
13
13
|
* ```
|
|
14
14
|
*/
|
|
15
15
|
export const wait = (delay) => new Promise((resolve) => setTimeout(resolve, delay));
|
|
16
|
+
/**
|
|
17
|
+
* A simple async map to process tasks with a concurrency limit.
|
|
18
|
+
* @param tasks The list of tasks to process.
|
|
19
|
+
* @param worker The async function to process each task.
|
|
20
|
+
* @param concurrency The maximum number of concurrent tasks.
|
|
21
|
+
* @returns A promise that resolves to the list of results.
|
|
22
|
+
*/
|
|
23
|
+
export const mapAsync = (tasks, worker, concurrency) => new Promise((resolve, reject) => {
|
|
24
|
+
const results = [];
|
|
25
|
+
let activeIndex = 0;
|
|
26
|
+
let activeCount = 0;
|
|
27
|
+
let resolvedCount = 0;
|
|
28
|
+
const totalCount = tasks.length;
|
|
29
|
+
const runNext = () => {
|
|
30
|
+
if (resolvedCount === totalCount) {
|
|
31
|
+
resolve(results);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
while (activeCount < concurrency && activeIndex < totalCount) {
|
|
35
|
+
const currentIndex = activeIndex++;
|
|
36
|
+
const task = tasks[currentIndex];
|
|
37
|
+
activeCount++;
|
|
38
|
+
worker(task)
|
|
39
|
+
.then((result) => {
|
|
40
|
+
results[currentIndex] = result;
|
|
41
|
+
resolvedCount++;
|
|
42
|
+
activeCount--;
|
|
43
|
+
runNext();
|
|
44
|
+
})
|
|
45
|
+
.catch(reject);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
runNext();
|
|
50
|
+
});
|
package/dist/maybe.d.ts
CHANGED
|
@@ -47,6 +47,10 @@ export declare const reduce: <T, R>(maybe: Maybe<T>, def: R, f: (value: T) => R)
|
|
|
47
47
|
* Maps the value of a maybe to a new value.
|
|
48
48
|
*/
|
|
49
49
|
export declare const map: <T, U>(maybe: Maybe<T>, f: (value: T) => U) => Maybe<U>;
|
|
50
|
+
/**
|
|
51
|
+
* Chains a result to a new result.
|
|
52
|
+
*/
|
|
53
|
+
export declare const then: <T, U>(maybe: Maybe<T>, f: (value: T) => Maybe<U>) => Maybe<U>;
|
|
50
54
|
/**
|
|
51
55
|
* Combines two maybes into one maybe. If both maybes contain a value, the
|
|
52
56
|
* values are combined using the provided function. If at least one maybe
|
package/dist/maybe.js
CHANGED
|
@@ -30,6 +30,10 @@ export const reduce = (maybe, def, f) => isJust(maybe) ? f(maybe.value) : def;
|
|
|
30
30
|
* Maps the value of a maybe to a new value.
|
|
31
31
|
*/
|
|
32
32
|
export const map = (maybe, f) => isJust(maybe) ? Just(f(maybe.value)) : Nothing;
|
|
33
|
+
/**
|
|
34
|
+
* Chains a result to a new result.
|
|
35
|
+
*/
|
|
36
|
+
export const then = (maybe, f) => (isJust(maybe) ? f(maybe.value) : maybe);
|
|
33
37
|
/**
|
|
34
38
|
* Combines two maybes into one maybe. If both maybes contain a value, the
|
|
35
39
|
* values are combined using the provided function. If at least one maybe
|
package/dist/result.d.ts
CHANGED
|
@@ -23,7 +23,10 @@ export interface Error<E> {
|
|
|
23
23
|
/**
|
|
24
24
|
* Creates a result that contains a value.
|
|
25
25
|
*/
|
|
26
|
-
export declare const ok:
|
|
26
|
+
export declare const ok: {
|
|
27
|
+
(): Result<void, never>;
|
|
28
|
+
<T>(value: T): Result<T, never>;
|
|
29
|
+
};
|
|
27
30
|
/**
|
|
28
31
|
* Checks if a result contains a value.
|
|
29
32
|
*/
|
|
@@ -31,7 +34,10 @@ export declare const isOk: <T, E>(result: Result<T, E>) => result is Ok<T>;
|
|
|
31
34
|
/**
|
|
32
35
|
* Creates a result that contains an error.
|
|
33
36
|
*/
|
|
34
|
-
export declare const error:
|
|
37
|
+
export declare const error: {
|
|
38
|
+
(): Result<never, void>;
|
|
39
|
+
<E>(error: E): Result<never, E>;
|
|
40
|
+
};
|
|
35
41
|
/**
|
|
36
42
|
* Checks if a result contains an error.
|
|
37
43
|
*/
|
|
@@ -48,6 +54,10 @@ export declare const map: <T, U, E>(result: Result<T, E>, f: (value: T) => U) =>
|
|
|
48
54
|
* Maps an error to a new error.
|
|
49
55
|
*/
|
|
50
56
|
export declare const mapError: <T, E, F>(result: Result<T, E>, f: (value: E) => F) => Result<T, F>;
|
|
57
|
+
/**
|
|
58
|
+
* Chains a result to a new result.
|
|
59
|
+
*/
|
|
60
|
+
export declare const then: <T, U, E>(result: Result<T, E>, f: (value: T) => Result<U, E>) => Result<U, E>;
|
|
51
61
|
/**
|
|
52
62
|
* Combines two results into one.
|
|
53
63
|
*
|
package/dist/result.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
/**
|
|
6
6
|
* Creates a result that contains a value.
|
|
7
7
|
*/
|
|
8
|
-
export const ok = (value) => ({ tag: "Ok", value });
|
|
8
|
+
export const ok = (value) => ({ tag: "Ok", value: value });
|
|
9
9
|
/**
|
|
10
10
|
* Checks if a result contains a value.
|
|
11
11
|
*/
|
|
@@ -15,7 +15,7 @@ export const isOk = (result) => result.tag === "Ok";
|
|
|
15
15
|
*/
|
|
16
16
|
export const error = (error) => ({
|
|
17
17
|
tag: "Error",
|
|
18
|
-
error,
|
|
18
|
+
error: error,
|
|
19
19
|
});
|
|
20
20
|
/**
|
|
21
21
|
* Checks if a result contains an error.
|
|
@@ -33,6 +33,10 @@ export const map = (result, f) => (isOk(result) ? ok(f(result.value)) : result);
|
|
|
33
33
|
* Maps an error to a new error.
|
|
34
34
|
*/
|
|
35
35
|
export const mapError = (result, f) => (isError(result) ? error(f(result.error)) : result);
|
|
36
|
+
/**
|
|
37
|
+
* Chains a result to a new result.
|
|
38
|
+
*/
|
|
39
|
+
export const then = (result, f) => (isOk(result) ? f(result.value) : result);
|
|
36
40
|
/**
|
|
37
41
|
* Combines two results into one.
|
|
38
42
|
*
|