@oscarpalmer/atoms 0.165.0 → 0.165.2
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/dist/index.mjs +125 -125
- package/dist/promise/delay.mjs +1 -1
- package/dist/promise/helpers.mjs +0 -1
- package/dist/promise/index.mjs +1 -1
- package/dist/promise/timed.mjs +1 -1
- package/dist/result/index.mjs +3 -3
- package/package.json +5 -1
- package/src/promise/helpers.ts +1 -1
package/dist/index.mjs
CHANGED
|
@@ -3659,31 +3659,6 @@ function sum(array, key) {
|
|
|
3659
3659
|
return getAggregated("sum", array, key);
|
|
3660
3660
|
}
|
|
3661
3661
|
//#endregion
|
|
3662
|
-
//#region src/result/match.ts
|
|
3663
|
-
async function asyncMatchResult(result, first, error) {
|
|
3664
|
-
let value;
|
|
3665
|
-
if (typeof result === "function") value = await result();
|
|
3666
|
-
else if (result instanceof Promise) value = await result;
|
|
3667
|
-
else value = result;
|
|
3668
|
-
if (!isResult(value)) throw new Error(MESSAGE_RESULT);
|
|
3669
|
-
const hasObj = typeof first === "object" && first !== null;
|
|
3670
|
-
const okHandler = hasObj ? first.ok : first;
|
|
3671
|
-
const errorHandler = hasObj ? first.error : error;
|
|
3672
|
-
if (isOk(value)) return okHandler(value.value);
|
|
3673
|
-
return errorHandler(value.error, value.original);
|
|
3674
|
-
}
|
|
3675
|
-
function matchResult(result, first, error) {
|
|
3676
|
-
const value = typeof result === "function" ? result() : result;
|
|
3677
|
-
if (!isResult(value)) throw new Error(MESSAGE_RESULT);
|
|
3678
|
-
const hasObj = typeof first === "object" && first !== null;
|
|
3679
|
-
const okHandler = hasObj ? first.ok : first;
|
|
3680
|
-
const errorHandler = hasObj ? first.error : error;
|
|
3681
|
-
if (isOk(value)) return okHandler(value.value);
|
|
3682
|
-
return errorHandler(value.error, value.original);
|
|
3683
|
-
}
|
|
3684
|
-
matchResult.async = asyncMatchResult;
|
|
3685
|
-
const MESSAGE_RESULT = "`result.match` expected a Result or a function that returns a Result";
|
|
3686
|
-
//#endregion
|
|
3687
3662
|
//#region src/result/misc.ts
|
|
3688
3663
|
function error(value, original) {
|
|
3689
3664
|
return getError(value, original);
|
|
@@ -3724,48 +3699,6 @@ function unwrap(value, defaultValue) {
|
|
|
3724
3699
|
}
|
|
3725
3700
|
const MESSAGE_PROMISE_RESULT = "toPromise expected to receive a Result";
|
|
3726
3701
|
//#endregion
|
|
3727
|
-
//#region src/result/work/flow.ts
|
|
3728
|
-
function attemptAsyncFlow(...fns) {
|
|
3729
|
-
let Flow;
|
|
3730
|
-
return (...args) => attempt.async(() => {
|
|
3731
|
-
Flow ??= flow.async(...fns);
|
|
3732
|
-
return Flow(...args.map((value) => {
|
|
3733
|
-
if (isError(value)) throw value.error;
|
|
3734
|
-
return isOk(value) ? value.value : value;
|
|
3735
|
-
}));
|
|
3736
|
-
});
|
|
3737
|
-
}
|
|
3738
|
-
function attemptFlow(...fns) {
|
|
3739
|
-
let Flow;
|
|
3740
|
-
return (...args) => attempt(() => {
|
|
3741
|
-
Flow ??= flow(...fns);
|
|
3742
|
-
return Flow(...args.map((value) => {
|
|
3743
|
-
if (isError(value)) throw value.error;
|
|
3744
|
-
return isOk(value) ? value.value : value;
|
|
3745
|
-
}));
|
|
3746
|
-
});
|
|
3747
|
-
}
|
|
3748
|
-
attemptFlow.async = attemptAsyncFlow;
|
|
3749
|
-
//#endregion
|
|
3750
|
-
//#region src/result/work/pipe.ts
|
|
3751
|
-
async function attemptAsyncPipe(initial, first, ...seconds) {
|
|
3752
|
-
return attempt.async(() => {
|
|
3753
|
-
if (isError(initial)) throw initial.error;
|
|
3754
|
-
const value = typeof initial === "function" ? initial() : isOk(initial) ? initial.value : initial;
|
|
3755
|
-
if (first == null) return value;
|
|
3756
|
-
return pipe.async(value, ...[first, ...seconds]);
|
|
3757
|
-
});
|
|
3758
|
-
}
|
|
3759
|
-
function attemptPipe(initial, first, ...seconds) {
|
|
3760
|
-
return attempt(() => {
|
|
3761
|
-
if (isError(initial)) throw initial.error;
|
|
3762
|
-
const value = typeof initial === "function" ? initial() : isOk(initial) ? initial.value : initial;
|
|
3763
|
-
if (first == null) return value;
|
|
3764
|
-
return pipe(value, ...[first, ...seconds]);
|
|
3765
|
-
});
|
|
3766
|
-
}
|
|
3767
|
-
attemptPipe.async = attemptAsyncPipe;
|
|
3768
|
-
//#endregion
|
|
3769
3702
|
//#region src/promise/models.ts
|
|
3770
3703
|
var CancelablePromise = class extends Promise {
|
|
3771
3704
|
#rejector;
|
|
@@ -3803,64 +3736,6 @@ const PROMISE_STRATEGY_DEFAULT = "complete";
|
|
|
3803
3736
|
const PROMISE_TYPE_FULFILLED = "fulfilled";
|
|
3804
3737
|
const PROMISE_TYPE_REJECTED = "rejected";
|
|
3805
3738
|
//#endregion
|
|
3806
|
-
//#region src/promise/misc.ts
|
|
3807
|
-
/**
|
|
3808
|
-
* Create a cancelable promise
|
|
3809
|
-
* @param executor Executor function for the promise
|
|
3810
|
-
* @returns Cancelable promise
|
|
3811
|
-
*/
|
|
3812
|
-
function cancelable(executor) {
|
|
3813
|
-
return new CancelablePromise(executor);
|
|
3814
|
-
}
|
|
3815
|
-
function handleResult$1(status, parameters) {
|
|
3816
|
-
const { abort, complete, data, handlers, index, signal, value } = parameters;
|
|
3817
|
-
if (signal?.aborted ?? false) return;
|
|
3818
|
-
if (!complete && status === "rejected") {
|
|
3819
|
-
settlePromise(abort, handlers.reject, value, signal);
|
|
3820
|
-
return;
|
|
3821
|
-
}
|
|
3822
|
-
data.result[index] = !complete ? value : status === "fulfilled" ? {
|
|
3823
|
-
status,
|
|
3824
|
-
value
|
|
3825
|
-
} : {
|
|
3826
|
-
status,
|
|
3827
|
-
reason: value
|
|
3828
|
-
};
|
|
3829
|
-
if (index === data.last) settlePromise(abort, handlers.resolve, data.result, signal);
|
|
3830
|
-
}
|
|
3831
|
-
function settlePromise(aborter, settler, value, signal) {
|
|
3832
|
-
signal?.removeEventListener(PROMISE_ABORT_EVENT, aborter);
|
|
3833
|
-
settler(value);
|
|
3834
|
-
}
|
|
3835
|
-
async function toResult(value) {
|
|
3836
|
-
const actual = typeof value === "function" ? value() : value;
|
|
3837
|
-
if (!(actual instanceof Promise)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_RESULT));
|
|
3838
|
-
return actual.then((result) => ok(result)).catch((reason) => error(reason));
|
|
3839
|
-
}
|
|
3840
|
-
//#endregion
|
|
3841
|
-
//#region src/result/index.ts
|
|
3842
|
-
async function asyncAttempt(value, err) {
|
|
3843
|
-
try {
|
|
3844
|
-
let result = typeof value === "function" ? value() : await value;
|
|
3845
|
-
if (result instanceof Promise) result = await result;
|
|
3846
|
-
return ok(result);
|
|
3847
|
-
} catch (thrown) {
|
|
3848
|
-
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
3849
|
-
}
|
|
3850
|
-
}
|
|
3851
|
-
function attempt(callback, err) {
|
|
3852
|
-
try {
|
|
3853
|
-
return ok(callback());
|
|
3854
|
-
} catch (thrown) {
|
|
3855
|
-
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
3856
|
-
}
|
|
3857
|
-
}
|
|
3858
|
-
attempt.async = asyncAttempt;
|
|
3859
|
-
attempt.flow = attemptFlow;
|
|
3860
|
-
attempt.match = matchResult;
|
|
3861
|
-
attempt.pipe = attemptPipe;
|
|
3862
|
-
attempt.promise = attemptPromise;
|
|
3863
|
-
//#endregion
|
|
3864
3739
|
//#region src/promise/helpers.ts
|
|
3865
3740
|
function getNumberOrDefault$1(value) {
|
|
3866
3741
|
return typeof value === "number" && value > 0 ? value : 0;
|
|
@@ -3915,6 +3790,41 @@ function isType(value, type) {
|
|
|
3915
3790
|
return typeof value === "object" && value !== null && value.status === type;
|
|
3916
3791
|
}
|
|
3917
3792
|
//#endregion
|
|
3793
|
+
//#region src/promise/misc.ts
|
|
3794
|
+
/**
|
|
3795
|
+
* Create a cancelable promise
|
|
3796
|
+
* @param executor Executor function for the promise
|
|
3797
|
+
* @returns Cancelable promise
|
|
3798
|
+
*/
|
|
3799
|
+
function cancelable(executor) {
|
|
3800
|
+
return new CancelablePromise(executor);
|
|
3801
|
+
}
|
|
3802
|
+
function handleResult$1(status, parameters) {
|
|
3803
|
+
const { abort, complete, data, handlers, index, signal, value } = parameters;
|
|
3804
|
+
if (signal?.aborted ?? false) return;
|
|
3805
|
+
if (!complete && status === "rejected") {
|
|
3806
|
+
settlePromise(abort, handlers.reject, value, signal);
|
|
3807
|
+
return;
|
|
3808
|
+
}
|
|
3809
|
+
data.result[index] = !complete ? value : status === "fulfilled" ? {
|
|
3810
|
+
status,
|
|
3811
|
+
value
|
|
3812
|
+
} : {
|
|
3813
|
+
status,
|
|
3814
|
+
reason: value
|
|
3815
|
+
};
|
|
3816
|
+
if (index === data.last) settlePromise(abort, handlers.resolve, data.result, signal);
|
|
3817
|
+
}
|
|
3818
|
+
function settlePromise(aborter, settler, value, signal) {
|
|
3819
|
+
signal?.removeEventListener(PROMISE_ABORT_EVENT, aborter);
|
|
3820
|
+
settler(value);
|
|
3821
|
+
}
|
|
3822
|
+
async function toResult(value) {
|
|
3823
|
+
const actual = typeof value === "function" ? value() : value;
|
|
3824
|
+
if (!(actual instanceof Promise)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_RESULT));
|
|
3825
|
+
return actual.then((result) => ok(result)).catch((reason) => error(reason));
|
|
3826
|
+
}
|
|
3827
|
+
//#endregion
|
|
3918
3828
|
//#region src/promise/timed.ts
|
|
3919
3829
|
async function getTimedPromise(promise, time, signal) {
|
|
3920
3830
|
function abort() {
|
|
@@ -4372,6 +4282,96 @@ const BOOLEAN_MODIFIER = .5;
|
|
|
4372
4282
|
const HEX_CHARACTERS = "0123456789ABCDEF";
|
|
4373
4283
|
const HEX_MAXIMUM = 15;
|
|
4374
4284
|
//#endregion
|
|
4285
|
+
//#region src/result/match.ts
|
|
4286
|
+
async function asyncMatchResult(result, first, error) {
|
|
4287
|
+
let value;
|
|
4288
|
+
if (typeof result === "function") value = await result();
|
|
4289
|
+
else if (result instanceof Promise) value = await result;
|
|
4290
|
+
else value = result;
|
|
4291
|
+
if (!isResult(value)) throw new Error(MESSAGE_RESULT);
|
|
4292
|
+
const hasObj = typeof first === "object" && first !== null;
|
|
4293
|
+
const okHandler = hasObj ? first.ok : first;
|
|
4294
|
+
const errorHandler = hasObj ? first.error : error;
|
|
4295
|
+
if (isOk(value)) return okHandler(value.value);
|
|
4296
|
+
return errorHandler(value.error, value.original);
|
|
4297
|
+
}
|
|
4298
|
+
function matchResult(result, first, error) {
|
|
4299
|
+
const value = typeof result === "function" ? result() : result;
|
|
4300
|
+
if (!isResult(value)) throw new Error(MESSAGE_RESULT);
|
|
4301
|
+
const hasObj = typeof first === "object" && first !== null;
|
|
4302
|
+
const okHandler = hasObj ? first.ok : first;
|
|
4303
|
+
const errorHandler = hasObj ? first.error : error;
|
|
4304
|
+
if (isOk(value)) return okHandler(value.value);
|
|
4305
|
+
return errorHandler(value.error, value.original);
|
|
4306
|
+
}
|
|
4307
|
+
matchResult.async = asyncMatchResult;
|
|
4308
|
+
const MESSAGE_RESULT = "`result.match` expected a Result or a function that returns a Result";
|
|
4309
|
+
//#endregion
|
|
4310
|
+
//#region src/result/work/flow.ts
|
|
4311
|
+
function attemptAsyncFlow(...fns) {
|
|
4312
|
+
let Flow;
|
|
4313
|
+
return (...args) => attempt.async(() => {
|
|
4314
|
+
Flow ??= flow.async(...fns);
|
|
4315
|
+
return Flow(...args.map((value) => {
|
|
4316
|
+
if (isError(value)) throw value.error;
|
|
4317
|
+
return isOk(value) ? value.value : value;
|
|
4318
|
+
}));
|
|
4319
|
+
});
|
|
4320
|
+
}
|
|
4321
|
+
function attemptFlow(...fns) {
|
|
4322
|
+
let Flow;
|
|
4323
|
+
return (...args) => attempt(() => {
|
|
4324
|
+
Flow ??= flow(...fns);
|
|
4325
|
+
return Flow(...args.map((value) => {
|
|
4326
|
+
if (isError(value)) throw value.error;
|
|
4327
|
+
return isOk(value) ? value.value : value;
|
|
4328
|
+
}));
|
|
4329
|
+
});
|
|
4330
|
+
}
|
|
4331
|
+
attemptFlow.async = attemptAsyncFlow;
|
|
4332
|
+
//#endregion
|
|
4333
|
+
//#region src/result/work/pipe.ts
|
|
4334
|
+
async function attemptAsyncPipe(initial, first, ...seconds) {
|
|
4335
|
+
return attempt.async(() => {
|
|
4336
|
+
if (isError(initial)) throw initial.error;
|
|
4337
|
+
const value = typeof initial === "function" ? initial() : isOk(initial) ? initial.value : initial;
|
|
4338
|
+
if (first == null) return value;
|
|
4339
|
+
return pipe.async(value, ...[first, ...seconds]);
|
|
4340
|
+
});
|
|
4341
|
+
}
|
|
4342
|
+
function attemptPipe(initial, first, ...seconds) {
|
|
4343
|
+
return attempt(() => {
|
|
4344
|
+
if (isError(initial)) throw initial.error;
|
|
4345
|
+
const value = typeof initial === "function" ? initial() : isOk(initial) ? initial.value : initial;
|
|
4346
|
+
if (first == null) return value;
|
|
4347
|
+
return pipe(value, ...[first, ...seconds]);
|
|
4348
|
+
});
|
|
4349
|
+
}
|
|
4350
|
+
attemptPipe.async = attemptAsyncPipe;
|
|
4351
|
+
//#endregion
|
|
4352
|
+
//#region src/result/index.ts
|
|
4353
|
+
async function asyncAttempt(value, err) {
|
|
4354
|
+
try {
|
|
4355
|
+
let result = typeof value === "function" ? value() : await value;
|
|
4356
|
+
if (result instanceof Promise) result = await result;
|
|
4357
|
+
return ok(result);
|
|
4358
|
+
} catch (thrown) {
|
|
4359
|
+
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
4360
|
+
}
|
|
4361
|
+
}
|
|
4362
|
+
function attempt(callback, err) {
|
|
4363
|
+
try {
|
|
4364
|
+
return ok(callback());
|
|
4365
|
+
} catch (thrown) {
|
|
4366
|
+
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
4367
|
+
}
|
|
4368
|
+
}
|
|
4369
|
+
attempt.async = asyncAttempt;
|
|
4370
|
+
attempt.flow = attemptFlow;
|
|
4371
|
+
attempt.match = matchResult;
|
|
4372
|
+
attempt.pipe = attemptPipe;
|
|
4373
|
+
attempt.promise = attemptPromise;
|
|
4374
|
+
//#endregion
|
|
4375
4375
|
//#region src/sized/set.ts
|
|
4376
4376
|
/**
|
|
4377
4377
|
* - A Set with a maximum size
|
package/dist/promise/delay.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TIMER_WAIT, getTimer } from "../internal/function/timer.mjs";
|
|
2
2
|
import { PROMISE_ABORT_EVENT, PROMISE_ABORT_OPTIONS } from "./models.mjs";
|
|
3
|
-
import { settlePromise } from "./misc.mjs";
|
|
4
3
|
import { getPromiseOptions } from "./helpers.mjs";
|
|
4
|
+
import { settlePromise } from "./misc.mjs";
|
|
5
5
|
//#region src/promise/delay.ts
|
|
6
6
|
function delay(options) {
|
|
7
7
|
const { signal, time } = getPromiseOptions(options);
|
package/dist/promise/helpers.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { error, ok } from "../result/misc.mjs";
|
|
2
2
|
import { PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED } from "./models.mjs";
|
|
3
|
-
import "../result/index.mjs";
|
|
4
3
|
//#region src/promise/helpers.ts
|
|
5
4
|
function getNumberOrDefault(value) {
|
|
6
5
|
return typeof value === "number" && value > 0 ? value : 0;
|
package/dist/promise/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { toPromise } from "../result/misc.mjs";
|
|
2
2
|
import { CancelablePromise, PROMISE_ABORT_EVENT, PROMISE_ABORT_OPTIONS, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED, PromiseTimeoutError } from "./models.mjs";
|
|
3
|
-
import { cancelable, handleResult, settlePromise, toResult } from "./misc.mjs";
|
|
4
3
|
import { getPromiseOptions, getPromisesOptions, getResultsFromPromises, isFulfilled, isRejected } from "./helpers.mjs";
|
|
4
|
+
import { cancelable, handleResult, settlePromise, toResult } from "./misc.mjs";
|
|
5
5
|
import { getTimedPromise, timed } from "./timed.mjs";
|
|
6
6
|
import { delay } from "./delay.mjs";
|
|
7
7
|
//#region src/promise/index.ts
|
package/dist/promise/timed.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TIMER_WAIT, getTimer } from "../internal/function/timer.mjs";
|
|
2
2
|
import { PROMISE_ABORT_EVENT, PROMISE_ABORT_OPTIONS, PROMISE_MESSAGE_EXPECTATION_TIMED, PromiseTimeoutError } from "./models.mjs";
|
|
3
|
-
import { settlePromise } from "./misc.mjs";
|
|
4
3
|
import { getPromiseOptions } from "./helpers.mjs";
|
|
4
|
+
import { settlePromise } from "./misc.mjs";
|
|
5
5
|
//#region src/promise/timed.ts
|
|
6
6
|
async function getTimedPromise(promise, time, signal) {
|
|
7
7
|
function abort() {
|
package/dist/result/index.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { isError, isOk, isResult } from "../internal/result.mjs";
|
|
2
|
-
import { matchResult } from "./match.mjs";
|
|
3
2
|
import { error, getError, ok, toPromise, unwrap } from "./misc.mjs";
|
|
4
|
-
import { attemptFlow } from "./work/flow.mjs";
|
|
5
|
-
import { attemptPipe } from "./work/pipe.mjs";
|
|
6
3
|
import { toResult } from "../promise/misc.mjs";
|
|
7
4
|
import { attemptPromise } from "../promise/index.mjs";
|
|
5
|
+
import { matchResult } from "./match.mjs";
|
|
6
|
+
import { attemptFlow } from "./work/flow.mjs";
|
|
7
|
+
import { attemptPipe } from "./work/pipe.mjs";
|
|
8
8
|
//#region src/result/index.ts
|
|
9
9
|
async function asyncAttempt(value, err) {
|
|
10
10
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oscarpalmer/atoms",
|
|
3
|
-
"version": "0.165.
|
|
3
|
+
"version": "0.165.2",
|
|
4
4
|
"description": "Atomic utilities for making your JavaScript better.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"helper",
|
|
@@ -111,6 +111,10 @@
|
|
|
111
111
|
"types": "./dist/promise/delay.d.mts",
|
|
112
112
|
"default": "./dist/promise/delay.mjs"
|
|
113
113
|
},
|
|
114
|
+
"./promise/models": {
|
|
115
|
+
"types": "./dist/promise/models.d.mts",
|
|
116
|
+
"default": "./dist/promise/models.mjs"
|
|
117
|
+
},
|
|
114
118
|
"./query": {
|
|
115
119
|
"types": "./dist/query.d.mts",
|
|
116
120
|
"default": "./dist/query.mjs"
|
package/src/promise/helpers.ts
CHANGED