@tempots/std 0.24.0 → 0.25.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.
- package/README.md +2 -0
- package/array.cjs +1 -1
- package/array.js +95 -95
- package/async-result.cjs +1 -1
- package/async-result.d.ts +88 -0
- package/async-result.js +189 -32
- package/index.cjs +1 -1
- package/index.js +1 -1
- package/json.cjs +1 -1
- package/json.js +1 -1
- package/object.cjs +1 -1
- package/object.js +21 -18
- package/package.json +1 -1
- package/regexp.cjs +1 -1
- package/regexp.js +12 -10
- package/result-BrVFeaNO.js +462 -0
- package/result-cY1uEC-G.cjs +1 -0
- package/result.cjs +1 -1
- package/result.d.ts +93 -0
- package/result.js +1 -1
- package/string.cjs +4 -4
- package/string.js +44 -43
- package/timer.cjs +1 -1
- package/timer.d.ts +3 -3
- package/timer.js +37 -23
- package/validation.cjs +1 -1
- package/validation.d.ts +77 -0
- package/validation.js +1 -1
- package/result-CGd0jCdl.js +0 -265
- package/result-CdwVhaAc.cjs +0 -1
package/README.md
CHANGED
|
@@ -4,6 +4,8 @@ A comprehensive standard library for TypeScript that provides utility functions
|
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@tempots/std)
|
|
6
6
|
[](https://github.com/fponticelli/tempots/blob/main/LICENSE)
|
|
7
|
+
[](https://codecov.io/gh/fponticelli/tempots)
|
|
8
|
+
[](https://github.com/fponticelli/tempots/actions)
|
|
7
9
|
|
|
8
10
|
## Installation
|
|
9
11
|
|
package/array.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("./object.cjs"),B=
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("./object.cjs"),B=e=>e.length>0?e[0]:void 0,O=e=>e.slice(1),k=(e,n,t)=>{if(e.length!==n.length)return!1;for(let r=0;r<e.length;r++)if(!t(e[r],n[r]))return!1;return!0},E=e=>e.length===0,F=e=>e.length>0,b=(e,n)=>{const t=[];for(let r=0;r<e.length;r++){const s=n(e[r],r);s!=null&&t.push(s)}return t},j=e=>e.filter(n=>n!=null),q=(e,n,t,r=!0)=>{if(e.length<n.length)return-1*(r?1:-1);if(e.length>n.length)return 1*(r?1:-1);for(let s=0;s<e.length;s++){const a=t(e[s],n[s]);if(a!==0)return a}return 0},d=(e,n)=>Array.from({length:e},(t,r)=>n(r)),S=(e,n=0)=>Array.from({length:e},(t,r)=>n+r),I=(e,n)=>d(e,()=>n),P=(e,n)=>{const t={};return e.forEach(r=>{t[n(r)]=r}),w.objectKeys(t).map(r=>t[r])},D=(e,n)=>{const t=e.indexOf(n);return t<0?!1:(e.splice(t,1),!0)},M=(e,n)=>{let t=0,r=!1;for(let s=0;s<e.length;s++)e[s]!==n?(e[t]=e[s],t++):r=!0;return e.length=t,r},_=(e,n)=>{const t=e.findIndex(n);return t<0?!1:(e.splice(t,1),!0)},T=(e,n)=>{let t=0,r=!1;for(let s=0;s<e.length;s++)n(e[s])?r=!0:(e[t]=e[s],t++);return e.length=t,r},H=(e,n,t)=>{const r={removals:[],swaps:[],inserts:[]},{removals:s,inserts:a,swaps:u}=r,i=new Map;n.forEach((o,l)=>i.set(t(o),l));const c=e.map((o,l)=>[o,l]).filter(([o])=>!i.has(t(o))).map(([o,l])=>l);for(let o=c.length-1;o>=0;o--){const l=c[o],f=s.length>0?s[s.length-1]:void 0;f!=null&&f.at===l+1?(f.at--,f.qt++):s.push({at:l,qt:1})}const h=new Map;e.forEach((o,l)=>h.set(t(o),l));const m=n.map((o,l)=>[o,l]).filter(([o])=>!h.has(t(o))).map(([o,l])=>l);for(const o of m){const l=a.length>0?a[a.length-1]:void 0;l!=null&&l.at+l.values.length===o?l.values.push(n[o]):a.push({at:o,values:[n[o]]})}const g=new Set(c),v=new Set(m),p=e.filter((o,l)=>!g.has(l)),y=new Map;for(let o=0;o<p.length;o++)y.set(t(p[o]),o);const A=n.filter((o,l)=>!v.has(l));for(let o=0;o<A.length;o++){const l=t(A[o]),f=y.get(l);if(f==null||o===f)continue;const x=t(p[o]);y.delete(x),u.push({from:o,to:f})}return r},N=(e,n)=>{const t=[...n];for(const{at:r,qt:s}of e.removals)t.splice(r,s);for(const{from:r,to:s}of e.swaps){const a=t[s];t[s]=t[r],t[r]=a}for(const r of e.inserts)t.splice(r.at,0,...r.values);return t},C=(e,n=" and ",t=", ")=>e.length===0?"":e.length===1?String(e[0]):`${e.slice(0,-1).join(t)}${n}${String(e[e.length-1])}`,$=(e,n,t=!0)=>{const r=e.map((i,c)=>[i,c]);r.sort((i,c)=>n(i[0],c[0]));const s=new Array(r.length);let a=0,u=r[0][0];for(let i=0;i<r.length;i++){const[c,h]=r[i];n(c,u)!==0&&(a=i,u=c),s[h]=a,t&&a++}return s},V=(e,n)=>{if(n<=0)throw new Error("Chunk size must be positive");const t=[];for(let r=0;r<e.length;r+=n)t.push(e.slice(r,r+n));return t},W=(e,n)=>{const t=[],r=[];for(const s of e)n(s)?t.push(s):r.push(s);return[t,r]},R=(e,n)=>{const t={};for(const r of e){const s=n(r);t[s]?t[s].push(r):t[s]=[r]}return t};exports.applyArrayDiffOperations=N;exports.areArraysEqual=k;exports.arrayDiffOperations=H;exports.arrayHasValues=F;exports.arrayHead=B;exports.arrayTail=O;exports.buildArray=d;exports.chunk=V;exports.compareArrays=q;exports.fillArray=I;exports.filterMapArray=b;exports.filterNullsFromArray=j;exports.groupBy=R;exports.isArrayEmpty=E;exports.joinArrayWithConjunction=C;exports.partition=W;exports.range=S;exports.rankArray=$;exports.removeAllFromArray=M;exports.removeAllFromArrayByPredicate=T;exports.removeOneFromArray=D;exports.removeOneFromArrayByPredicate=_;exports.uniqueByPrimitive=P;
|
package/array.js
CHANGED
|
@@ -1,140 +1,140 @@
|
|
|
1
|
-
import { objectKeys as
|
|
2
|
-
const
|
|
3
|
-
if (t.length !==
|
|
1
|
+
import { objectKeys as x } from "./object.js";
|
|
2
|
+
const B = (t) => t.length > 0 ? t[0] : void 0, E = (t) => t.slice(1), I = (t, s, e) => {
|
|
3
|
+
if (t.length !== s.length) return !1;
|
|
4
4
|
for (let n = 0; n < t.length; n++)
|
|
5
|
-
if (!e(t[n],
|
|
5
|
+
if (!e(t[n], s[n])) return !1;
|
|
6
6
|
return !0;
|
|
7
|
-
},
|
|
7
|
+
}, O = (t) => t.length === 0, S = (t) => t.length > 0, _ = (t, s) => {
|
|
8
8
|
const e = [];
|
|
9
9
|
for (let n = 0; n < t.length; n++) {
|
|
10
|
-
const
|
|
11
|
-
|
|
10
|
+
const r = s(t[n], n);
|
|
11
|
+
r != null && e.push(r);
|
|
12
12
|
}
|
|
13
13
|
return e;
|
|
14
|
-
},
|
|
15
|
-
if (t.length <
|
|
14
|
+
}, q = (t) => t.filter((s) => s != null), b = (t, s, e, n = !0) => {
|
|
15
|
+
if (t.length < s.length)
|
|
16
16
|
return -1 * (n ? 1 : -1);
|
|
17
|
-
if (t.length >
|
|
17
|
+
if (t.length > s.length)
|
|
18
18
|
return 1 * (n ? 1 : -1);
|
|
19
|
-
for (let
|
|
20
|
-
const i = e(t[
|
|
19
|
+
for (let r = 0; r < t.length; r++) {
|
|
20
|
+
const i = e(t[r], s[r]);
|
|
21
21
|
if (i !== 0) return i;
|
|
22
22
|
}
|
|
23
23
|
return 0;
|
|
24
|
-
},
|
|
24
|
+
}, w = (t, s) => Array.from({ length: t }, (e, n) => s(n)), j = (t, s = 0) => Array.from({ length: t }, (e, n) => s + n), D = (t, s) => w(t, () => s), F = (t, s) => {
|
|
25
25
|
const e = {};
|
|
26
26
|
return t.forEach((n) => {
|
|
27
|
-
e[
|
|
28
|
-
}),
|
|
29
|
-
},
|
|
30
|
-
const e = t.indexOf(
|
|
27
|
+
e[s(n)] = n;
|
|
28
|
+
}), x(e).map((n) => e[n]);
|
|
29
|
+
}, M = (t, s) => {
|
|
30
|
+
const e = t.indexOf(s);
|
|
31
31
|
return e < 0 ? !1 : (t.splice(e, 1), !0);
|
|
32
|
-
}, N = (t,
|
|
33
|
-
let e = !1;
|
|
34
|
-
for (;
|
|
35
|
-
e = !0;
|
|
36
|
-
return e;
|
|
37
|
-
},
|
|
38
|
-
const e = t.findIndex(
|
|
32
|
+
}, N = (t, s) => {
|
|
33
|
+
let e = 0, n = !1;
|
|
34
|
+
for (let r = 0; r < t.length; r++)
|
|
35
|
+
t[r] !== s ? (t[e] = t[r], e++) : n = !0;
|
|
36
|
+
return t.length = e, n;
|
|
37
|
+
}, P = (t, s) => {
|
|
38
|
+
const e = t.findIndex(s);
|
|
39
39
|
return e < 0 ? !1 : (t.splice(e, 1), !0);
|
|
40
|
-
},
|
|
41
|
-
let e = !1;
|
|
42
|
-
for (;
|
|
43
|
-
|
|
44
|
-
return e;
|
|
45
|
-
},
|
|
40
|
+
}, T = (t, s) => {
|
|
41
|
+
let e = 0, n = !1;
|
|
42
|
+
for (let r = 0; r < t.length; r++)
|
|
43
|
+
s(t[r]) ? n = !0 : (t[e] = t[r], e++);
|
|
44
|
+
return t.length = e, n;
|
|
45
|
+
}, $ = (t, s, e) => {
|
|
46
46
|
const n = {
|
|
47
47
|
removals: [],
|
|
48
48
|
swaps: [],
|
|
49
49
|
inserts: []
|
|
50
|
-
}, { removals:
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
for (let
|
|
54
|
-
const l =
|
|
55
|
-
|
|
50
|
+
}, { removals: r, inserts: i, swaps: u } = n, c = /* @__PURE__ */ new Map();
|
|
51
|
+
s.forEach((o, l) => c.set(e(o), l));
|
|
52
|
+
const f = t.map((o, l) => [o, l]).filter(([o]) => !c.has(e(o))).map(([o, l]) => l);
|
|
53
|
+
for (let o = f.length - 1; o >= 0; o--) {
|
|
54
|
+
const l = f[o], a = r.length > 0 ? r[r.length - 1] : void 0;
|
|
55
|
+
a != null && a.at === l + 1 ? (a.at--, a.qt++) : r.push({ at: l, qt: 1 });
|
|
56
56
|
}
|
|
57
57
|
const h = /* @__PURE__ */ new Map();
|
|
58
|
-
t.forEach((
|
|
59
|
-
const g =
|
|
60
|
-
for (const
|
|
58
|
+
t.forEach((o, l) => h.set(e(o), l));
|
|
59
|
+
const g = s.map((o, l) => [o, l]).filter(([o]) => !h.has(e(o))).map(([o, l]) => l);
|
|
60
|
+
for (const o of g) {
|
|
61
61
|
const l = i.length > 0 ? i[i.length - 1] : void 0;
|
|
62
|
-
l != null && l.at + l.values.length ===
|
|
62
|
+
l != null && l.at + l.values.length === o ? l.values.push(s[o]) : i.push({ at: o, values: [s[o]] });
|
|
63
63
|
}
|
|
64
|
-
const p = t.filter((
|
|
65
|
-
for (let
|
|
66
|
-
m.set(e(p[
|
|
67
|
-
const
|
|
68
|
-
for (let
|
|
69
|
-
const l = e(
|
|
70
|
-
if (
|
|
71
|
-
const
|
|
72
|
-
m.delete(
|
|
64
|
+
const y = new Set(f), A = new Set(g), p = t.filter((o, l) => !y.has(l)), m = /* @__PURE__ */ new Map();
|
|
65
|
+
for (let o = 0; o < p.length; o++)
|
|
66
|
+
m.set(e(p[o]), o);
|
|
67
|
+
const d = s.filter((o, l) => !A.has(l));
|
|
68
|
+
for (let o = 0; o < d.length; o++) {
|
|
69
|
+
const l = e(d[o]), a = m.get(l);
|
|
70
|
+
if (a == null || o === a) continue;
|
|
71
|
+
const v = e(p[o]);
|
|
72
|
+
m.delete(v), u.push({ from: o, to: a });
|
|
73
73
|
}
|
|
74
74
|
return n;
|
|
75
|
-
},
|
|
76
|
-
const e = [...
|
|
77
|
-
for (const { at: n, qt:
|
|
78
|
-
e.splice(n,
|
|
79
|
-
for (const { from: n, to:
|
|
80
|
-
const i = e[
|
|
81
|
-
e[
|
|
75
|
+
}, C = (t, s) => {
|
|
76
|
+
const e = [...s];
|
|
77
|
+
for (const { at: n, qt: r } of t.removals)
|
|
78
|
+
e.splice(n, r);
|
|
79
|
+
for (const { from: n, to: r } of t.swaps) {
|
|
80
|
+
const i = e[r];
|
|
81
|
+
e[r] = e[n], e[n] = i;
|
|
82
82
|
}
|
|
83
83
|
for (const n of t.inserts)
|
|
84
84
|
e.splice(n.at, 0, ...n.values);
|
|
85
85
|
return e;
|
|
86
|
-
},
|
|
87
|
-
const n = t.map((c,
|
|
88
|
-
n.sort((c,
|
|
89
|
-
const
|
|
86
|
+
}, H = (t, s = " and ", e = ", ") => t.length === 0 ? "" : t.length === 1 ? String(t[0]) : `${t.slice(0, -1).join(e)}${s}${String(t[t.length - 1])}`, R = (t, s, e = !0) => {
|
|
87
|
+
const n = t.map((c, f) => [c, f]);
|
|
88
|
+
n.sort((c, f) => s(c[0], f[0]));
|
|
89
|
+
const r = new Array(n.length);
|
|
90
90
|
let i = 0, u = n[0][0];
|
|
91
91
|
for (let c = 0; c < n.length; c++) {
|
|
92
|
-
const [
|
|
93
|
-
|
|
92
|
+
const [f, h] = n[c];
|
|
93
|
+
s(f, u) !== 0 && (i = c, u = f), r[h] = i, e && i++;
|
|
94
94
|
}
|
|
95
|
-
return
|
|
96
|
-
},
|
|
97
|
-
if (
|
|
95
|
+
return r;
|
|
96
|
+
}, V = (t, s) => {
|
|
97
|
+
if (s <= 0)
|
|
98
98
|
throw new Error("Chunk size must be positive");
|
|
99
99
|
const e = [];
|
|
100
|
-
for (let n = 0; n < t.length; n +=
|
|
101
|
-
e.push(t.slice(n, n +
|
|
100
|
+
for (let n = 0; n < t.length; n += s)
|
|
101
|
+
e.push(t.slice(n, n + s));
|
|
102
102
|
return e;
|
|
103
|
-
},
|
|
103
|
+
}, W = (t, s) => {
|
|
104
104
|
const e = [], n = [];
|
|
105
|
-
for (const
|
|
106
|
-
r
|
|
105
|
+
for (const r of t)
|
|
106
|
+
s(r) ? e.push(r) : n.push(r);
|
|
107
107
|
return [e, n];
|
|
108
|
-
},
|
|
108
|
+
}, G = (t, s) => {
|
|
109
109
|
const e = {};
|
|
110
110
|
for (const n of t) {
|
|
111
|
-
const
|
|
112
|
-
e[
|
|
111
|
+
const r = s(n);
|
|
112
|
+
e[r] ? e[r].push(n) : e[r] = [n];
|
|
113
113
|
}
|
|
114
114
|
return e;
|
|
115
115
|
};
|
|
116
116
|
export {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
117
|
+
C as applyArrayDiffOperations,
|
|
118
|
+
I as areArraysEqual,
|
|
119
|
+
$ as arrayDiffOperations,
|
|
120
|
+
S as arrayHasValues,
|
|
121
|
+
B as arrayHead,
|
|
122
|
+
E as arrayTail,
|
|
123
|
+
w as buildArray,
|
|
124
|
+
V as chunk,
|
|
125
|
+
b as compareArrays,
|
|
126
|
+
D as fillArray,
|
|
127
|
+
_ as filterMapArray,
|
|
128
|
+
q as filterNullsFromArray,
|
|
129
|
+
G as groupBy,
|
|
130
|
+
O as isArrayEmpty,
|
|
131
|
+
H as joinArrayWithConjunction,
|
|
132
|
+
W as partition,
|
|
133
|
+
j as range,
|
|
134
|
+
R as rankArray,
|
|
135
135
|
N as removeAllFromArray,
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
136
|
+
T as removeAllFromArrayByPredicate,
|
|
137
|
+
M as removeOneFromArray,
|
|
138
|
+
P as removeOneFromArrayByPredicate,
|
|
139
|
+
F as uniqueByPrimitive
|
|
140
140
|
};
|
package/async-result.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s={notAsked:{type:"NotAsked"},loading(e=void 0){return{type:"Loading",previousValue:e}},success(e){return{type:"AsyncSuccess",value:e}},failure(e){return{type:"AsyncFailure",error:e}},isSuccess(e){return e.type==="AsyncSuccess"},isFailure(e){return e.type==="AsyncFailure"},isNotAsked(e){return e.type==="NotAsked"},isLoading(e){return e.type==="Loading"},getOrElse(e,r){return s.isSuccess(e)?e.value:r},getOrElseLazy(e,r){return s.isSuccess(e)?e.value:r()},getOrNull(e){return s.isSuccess(e)?e.value:null},getOrUndefined(e){return s.isSuccess(e)?e.value:void 0},getUnsafe:e=>{if(s.isSuccess(e))return e.value;throw s.isFailure(e)?e.error:new Error("Cannot get value from a not-asked or loading result")},match:(e,{success:r,failure:u,loading:a,notAsked:i=a})=>s.isSuccess(e)?r(e.value):s.isFailure(e)?u(e.error):s.isNotAsked(e)?i():a(e.previousValue),whenSuccess:(e,r)=>(s.isSuccess(e)&&r(e.value),e),whenFailure:(e,r)=>(s.isFailure(e)&&r(e.error),e),equals:(e,r,u={valueEquals:(a,i)=>a===i,errorEquals:(a,i)=>a===i})=>e.type==="AsyncSuccess"&&r.type==="AsyncSuccess"?u.valueEquals(e.value,r.value):e.type==="AsyncFailure"&&r.type==="AsyncFailure"?u.errorEquals(e.error,r.error):e.type==="Loading"&&r.type==="Loading"?u.valueEquals(e.previousValue,r.previousValue):e.type==="NotAsked"&&r.type==="NotAsked",all:e=>{const r=[];for(const u of e)if(s.isSuccess(u))r.push(u.value);else return u;return s.success(r)},ofPromise:async e=>{try{const r=await e;return s.success(r)}catch(r){return s.failure(r instanceof Error?r:new Error(String(r)))}},map:(e,r)=>{switch(e.type){case"AsyncSuccess":return s.success(r(e.value));case"NotAsked":return s.notAsked;case"AsyncFailure":return s.failure(e.error);case"Loading":return s.loading(e.previousValue!=null?r(e.previousValue):void 0)}},flatMap:(e,r)=>{switch(e.type){case"AsyncSuccess":return r(e.value);case"NotAsked":return s.notAsked;case"AsyncFailure":return s.failure(e.error);case"Loading":return s.loading()}},mapError:(e,r)=>{switch(e.type){case"AsyncSuccess":return s.success(e.value);case"NotAsked":return s.notAsked;case"AsyncFailure":return s.failure(r(e.error));case"Loading":return s.loading(e.previousValue)}},flatMapError:(e,r)=>{switch(e.type){case"AsyncSuccess":return s.success(e.value);case"NotAsked":return s.notAsked;case"AsyncFailure":return r(e.error);case"Loading":return s.loading(e.previousValue)}},toResult:e=>{switch(e.type){case"AsyncSuccess":return{type:"Success",value:e.value};case"AsyncFailure":return{type:"Failure",error:e.error};case"NotAsked":case"Loading":return}},isSettled(e){return e.type==="AsyncSuccess"||e.type==="AsyncFailure"},recover:(e,r)=>{switch(e.type){case"AsyncSuccess":return s.success(e.value);case"NotAsked":return s.notAsked;case"AsyncFailure":return s.success(r(e.error));case"Loading":return s.loading(e.previousValue)}},ap:(e,r)=>s.isSuccess(e)&&s.isSuccess(r)?s.success(e.value(r.value)):s.isFailure(e)?s.failure(e.error):s.isFailure(r)?s.failure(r.error):s.isLoading(e)||s.isLoading(r)?s.loading():s.notAsked,map2:(e,r,u)=>s.isSuccess(e)&&s.isSuccess(r)?s.success(u(e.value,r.value)):s.isFailure(e)?s.failure(e.error):s.isFailure(r)?s.failure(r.error):s.isLoading(e)||s.isLoading(r)?s.loading():s.notAsked,map3:(e,r,u,a)=>s.isSuccess(e)&&s.isSuccess(r)&&s.isSuccess(u)?s.success(a(e.value,r.value,u.value)):s.isFailure(e)?s.failure(e.error):s.isFailure(r)?s.failure(r.error):s.isFailure(u)?s.failure(u.error):s.isLoading(e)||s.isLoading(r)||s.isLoading(u)?s.loading():s.notAsked};exports.AsyncResult=s;
|
package/async-result.d.ts
CHANGED
|
@@ -210,4 +210,92 @@ export declare const AsyncResult: {
|
|
|
210
210
|
* @returns A Promise that resolves to an AsyncResult.
|
|
211
211
|
*/
|
|
212
212
|
ofPromise: <V>(p: Promise<V>) => Promise<AsyncResult<V, Error>>;
|
|
213
|
+
/**
|
|
214
|
+
* Maps the value of a successful `AsyncResult` to a new value using the provided function.
|
|
215
|
+
* For other states (NotAsked, Loading, Failure), the state is preserved.
|
|
216
|
+
* When mapping a Loading state with a previous value, the previous value is also mapped.
|
|
217
|
+
* @param result - The `AsyncResult` to map.
|
|
218
|
+
* @param fn - The mapping function to apply to the success value.
|
|
219
|
+
* @returns A new `AsyncResult` with the mapped value if successful, otherwise the original state.
|
|
220
|
+
* @public
|
|
221
|
+
*/
|
|
222
|
+
map: <V, U, E>(result: AsyncResult<V, E>, fn: (value: V) => U) => AsyncResult<U, E>;
|
|
223
|
+
/**
|
|
224
|
+
* Maps the value of a successful `AsyncResult` to a new `AsyncResult` using the provided function.
|
|
225
|
+
* This is useful for chaining operations that may also fail.
|
|
226
|
+
* @param result - The `AsyncResult` to flat map.
|
|
227
|
+
* @param fn - The mapping function that returns a new `AsyncResult`.
|
|
228
|
+
* @returns The result of the mapping function if successful, otherwise the original state.
|
|
229
|
+
* @public
|
|
230
|
+
*/
|
|
231
|
+
flatMap: <V, U, E>(result: AsyncResult<V, E>, fn: (value: V) => AsyncResult<U, E>) => AsyncResult<U, E>;
|
|
232
|
+
/**
|
|
233
|
+
* Maps the error of a failed `AsyncResult` to a new error using the provided function.
|
|
234
|
+
* For other states, the state is preserved.
|
|
235
|
+
* @param result - The `AsyncResult` to map the error of.
|
|
236
|
+
* @param fn - The mapping function to apply to the error.
|
|
237
|
+
* @returns A new `AsyncResult` with the mapped error if failed, otherwise the original state.
|
|
238
|
+
* @public
|
|
239
|
+
*/
|
|
240
|
+
mapError: <V, E, F>(result: AsyncResult<V, E>, fn: (error: E) => F) => AsyncResult<V, F>;
|
|
241
|
+
/**
|
|
242
|
+
* Maps the error of a failed `AsyncResult` to a new `AsyncResult` using the provided function.
|
|
243
|
+
* This allows recovery from errors by returning a new successful result.
|
|
244
|
+
* @param result - The `AsyncResult` to recover from.
|
|
245
|
+
* @param fn - The recovery function that returns a new `AsyncResult`.
|
|
246
|
+
* @returns The result of the recovery function if failed, otherwise the original state.
|
|
247
|
+
* @public
|
|
248
|
+
*/
|
|
249
|
+
flatMapError: <V, E, F>(result: AsyncResult<V, E>, fn: (error: E) => AsyncResult<V, F>) => AsyncResult<V, F>;
|
|
250
|
+
/**
|
|
251
|
+
* Converts an `AsyncResult` to a `Result`, discarding the loading and not-asked states.
|
|
252
|
+
* Returns `undefined` if the result is not settled (i.e., NotAsked or Loading).
|
|
253
|
+
* @param result - The `AsyncResult` to convert.
|
|
254
|
+
* @returns A `Result` if the `AsyncResult` is settled, otherwise `undefined`.
|
|
255
|
+
* @public
|
|
256
|
+
*/
|
|
257
|
+
toResult: <V, E>(result: AsyncResult<V, E>) => import('./result').Result<V, E> | undefined;
|
|
258
|
+
/**
|
|
259
|
+
* Checks if the result is settled (either success or failure).
|
|
260
|
+
* @param r - The result.
|
|
261
|
+
* @returns `true` if the result is settled; otherwise, `false`.
|
|
262
|
+
* @public
|
|
263
|
+
*/
|
|
264
|
+
isSettled<V, E>(r: AsyncResult<V, E>): r is Settled<V, E>;
|
|
265
|
+
/**
|
|
266
|
+
* Recovers from a failure by providing an alternative value.
|
|
267
|
+
* @param result - The `AsyncResult` to recover from.
|
|
268
|
+
* @param fn - The function that provides an alternative value given the error.
|
|
269
|
+
* @returns A successful `AsyncResult` with the alternative value if failed, otherwise the original state.
|
|
270
|
+
* @public
|
|
271
|
+
*/
|
|
272
|
+
recover: <V, E>(result: AsyncResult<V, E>, fn: (error: E) => V) => AsyncResult<V, never>;
|
|
273
|
+
/**
|
|
274
|
+
* Applies a function wrapped in an `AsyncResult` to a value wrapped in an `AsyncResult`.
|
|
275
|
+
* Useful for applying multiple arguments to a function in a safe way.
|
|
276
|
+
* @param resultFn - The `AsyncResult` containing the function.
|
|
277
|
+
* @param resultVal - The `AsyncResult` containing the value.
|
|
278
|
+
* @returns A new `AsyncResult` with the result of applying the function to the value.
|
|
279
|
+
* @public
|
|
280
|
+
*/
|
|
281
|
+
ap: <V, U, E>(resultFn: AsyncResult<(v: V) => U, E>, resultVal: AsyncResult<V, E>) => AsyncResult<U, E>;
|
|
282
|
+
/**
|
|
283
|
+
* Maps two `AsyncResult` values using a function.
|
|
284
|
+
* @param r1 - The first `AsyncResult`.
|
|
285
|
+
* @param r2 - The second `AsyncResult`.
|
|
286
|
+
* @param fn - The function to apply to both values.
|
|
287
|
+
* @returns A new `AsyncResult` with the result of applying the function to both values.
|
|
288
|
+
* @public
|
|
289
|
+
*/
|
|
290
|
+
map2: <V1, V2, U, E>(r1: AsyncResult<V1, E>, r2: AsyncResult<V2, E>, fn: (v1: V1, v2: V2) => U) => AsyncResult<U, E>;
|
|
291
|
+
/**
|
|
292
|
+
* Maps three `AsyncResult` values using a function.
|
|
293
|
+
* @param r1 - The first `AsyncResult`.
|
|
294
|
+
* @param r2 - The second `AsyncResult`.
|
|
295
|
+
* @param r3 - The third `AsyncResult`.
|
|
296
|
+
* @param fn - The function to apply to all three values.
|
|
297
|
+
* @returns A new `AsyncResult` with the result of applying the function to all three values.
|
|
298
|
+
* @public
|
|
299
|
+
*/
|
|
300
|
+
map3: <V1, V2, V3, U, E>(r1: AsyncResult<V1, E>, r2: AsyncResult<V2, E>, r3: AsyncResult<V3, E>, fn: (v1: V1, v2: V2, v3: V3) => U) => AsyncResult<U, E>;
|
|
213
301
|
};
|