@tempots/std 0.24.0 → 0.25.1

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 CHANGED
@@ -4,6 +4,8 @@ A comprehensive standard library for TypeScript that provides utility functions
4
4
 
5
5
  [![npm version](https://img.shields.io/npm/v/@tempots/std.svg)](https://www.npmjs.com/package/@tempots/std)
6
6
  [![license](https://img.shields.io/npm/l/@tempots/std.svg)](https://github.com/fponticelli/tempots/blob/main/LICENSE)
7
+ [![codecov](https://codecov.io/gh/fponticelli/tempots/branch/main/graph/badge.svg)](https://codecov.io/gh/fponticelli/tempots)
8
+ [![CI](https://github.com/fponticelli/tempots/workflows/CI/badge.svg)](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=r=>r.length>0?r[0]:void 0,k=r=>r.slice(1),E=(r,n,t)=>{if(r.length!==n.length)return!1;for(let e=0;e<r.length;e++)if(!t(r[e],n[e]))return!1;return!0},x=r=>r.length===0,F=r=>r.length>0,b=(r,n)=>{const t=[];for(let e=0;e<r.length;e++){const o=n(r[e],e);o!=null&&t.push(o)}return t},j=r=>r.filter(n=>n!=null),q=(r,n,t,e=!0)=>{if(r.length<n.length)return-1*(e?1:-1);if(r.length>n.length)return 1*(e?1:-1);for(let o=0;o<r.length;o++){const a=t(r[o],n[o]);if(a!==0)return a}return 0},g=(r,n)=>Array.from({length:r},(t,e)=>n(e)),P=(r,n=0)=>Array.from({length:r},(t,e)=>n+e),D=(r,n)=>g(r,()=>n),M=(r,n)=>{const t={};return r.forEach(e=>{t[n(e)]=e}),w.objectKeys(t).map(e=>t[e])},d=(r,n)=>{const t=r.indexOf(n);return t<0?!1:(r.splice(t,1),!0)},_=(r,n)=>{let t=!1;for(;d(r,n);)t=!0;return t},v=(r,n)=>{const t=r.findIndex(n);return t<0?!1:(r.splice(t,1),!0)},T=(r,n)=>{let t=!1;for(;v(r,n);)t=!0;return t},H=(r,n,t)=>{const e={removals:[],swaps:[],inserts:[]},{removals:o,inserts:a,swaps:f}=e,i=new Map;n.forEach((s,l)=>i.set(t(s),l));const c=r.map((s,l)=>[s,l]).filter(([s])=>!i.has(t(s))).map(([s,l])=>l);for(let s=c.length-1;s>=0;s--){const l=c[s],u=o.length>0?o[o.length-1]:void 0;u!=null&&u.at===l+1?(u.at--,u.qt++):o.push({at:l,qt:1})}const h=new Map;r.forEach((s,l)=>h.set(t(s),l));const m=n.map((s,l)=>[s,l]).filter(([s])=>!h.has(t(s))).map(([s,l])=>l);for(const s of m){const l=a.length>0?a[a.length-1]:void 0;l!=null&&l.at+l.values.length===s?l.values.push(n[s]):a.push({at:s,values:[n[s]]})}const p=r.filter((s,l)=>!c.includes(l)),y=new Map;for(let s=0;s<p.length;s++)y.set(t(p[s]),s);const A=n.filter((s,l)=>!m.includes(l));for(let s=0;s<A.length;s++){const l=t(A[s]),u=y.get(l);if(u==null||s===u)continue;const O=t(p[s]);y.delete(O),f.push({from:s,to:u})}return e},N=(r,n)=>{const t=[...n];for(const{at:e,qt:o}of r.removals)t.splice(e,o);for(const{from:e,to:o}of r.swaps){const a=t[o];t[o]=t[e],t[e]=a}for(const e of r.inserts)t.splice(e.at,0,...e.values);return t},S=(r,n=" and ",t=", ")=>r.length===0?"":r.length===1?String(r[0]):`${r.slice(0,-1).join(t)}${n}${String(r[r.length-1])}`,C=(r,n,t=!0)=>{const e=r.map((i,c)=>[i,c]);e.sort((i,c)=>n(i[0],c[0]));const o=new Array(e.length);let a=0,f=e[0][0];for(let i=0;i<e.length;i++){const[c,h]=e[i];n(c,f)!==0&&(a=i,f=c),o[h]=a,t&&a++}return o},I=(r,n)=>{if(n<=0)throw new Error("Chunk size must be positive");const t=[];for(let e=0;e<r.length;e+=n)t.push(r.slice(e,e+n));return t},$=(r,n)=>{const t=[],e=[];for(const o of r)n(o)?t.push(o):e.push(o);return[t,e]},V=(r,n)=>{const t={};for(const e of r){const o=n(e);t[o]?t[o].push(e):t[o]=[e]}return t};exports.applyArrayDiffOperations=N;exports.areArraysEqual=E;exports.arrayDiffOperations=H;exports.arrayHasValues=F;exports.arrayHead=B;exports.arrayTail=k;exports.buildArray=g;exports.chunk=I;exports.compareArrays=q;exports.fillArray=D;exports.filterMapArray=b;exports.filterNullsFromArray=j;exports.groupBy=V;exports.isArrayEmpty=x;exports.joinArrayWithConjunction=S;exports.partition=$;exports.range=P;exports.rankArray=C;exports.removeAllFromArray=_;exports.removeAllFromArrayByPredicate=T;exports.removeOneFromArray=d;exports.removeOneFromArrayByPredicate=v;exports.uniqueByPrimitive=M;
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 d } from "./object.js";
2
- const E = (t) => t.length > 0 ? t[0] : void 0, B = (t) => t.slice(1), O = (t, r, e) => {
3
- if (t.length !== r.length) return !1;
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], r[n])) return !1;
5
+ if (!e(t[n], s[n])) return !1;
6
6
  return !0;
7
- }, _ = (t) => t.length === 0, q = (t) => t.length > 0, b = (t, r) => {
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 o = r(t[n], n);
11
- o != null && e.push(o);
10
+ const r = s(t[n], n);
11
+ r != null && e.push(r);
12
12
  }
13
13
  return e;
14
- }, j = (t) => t.filter((r) => r != null), D = (t, r, e, n = !0) => {
15
- if (t.length < r.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 > r.length)
17
+ if (t.length > s.length)
18
18
  return 1 * (n ? 1 : -1);
19
- for (let o = 0; o < t.length; o++) {
20
- const i = e(t[o], r[o]);
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
- }, v = (t, r) => Array.from({ length: t }, (e, n) => r(n)), F = (t, r = 0) => Array.from({ length: t }, (e, n) => r + n), M = (t, r) => v(t, () => r), I = (t, r) => {
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[r(n)] = n;
28
- }), d(e).map((n) => e[n]);
29
- }, w = (t, r) => {
30
- const e = t.indexOf(r);
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, r) => {
33
- let e = !1;
34
- for (; w(t, r); )
35
- e = !0;
36
- return e;
37
- }, x = (t, r) => {
38
- const e = t.findIndex(r);
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
- }, P = (t, r) => {
41
- let e = !1;
42
- for (; x(t, r); )
43
- e = !0;
44
- return e;
45
- }, T = (t, r, e) => {
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: o, inserts: i, swaps: u } = n, c = /* @__PURE__ */ new Map();
51
- r.forEach((s, l) => c.set(e(s), l));
52
- const a = t.map((s, l) => [s, l]).filter(([s]) => !c.has(e(s))).map(([s, l]) => l);
53
- for (let s = a.length - 1; s >= 0; s--) {
54
- const l = a[s], f = o.length > 0 ? o[o.length - 1] : void 0;
55
- f != null && f.at === l + 1 ? (f.at--, f.qt++) : o.push({ at: l, qt: 1 });
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((s, l) => h.set(e(s), l));
59
- const g = r.map((s, l) => [s, l]).filter(([s]) => !h.has(e(s))).map(([s, l]) => l);
60
- for (const s of g) {
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 === s ? l.values.push(r[s]) : i.push({ at: s, values: [r[s]] });
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((s, l) => !a.includes(l)), m = /* @__PURE__ */ new Map();
65
- for (let s = 0; s < p.length; s++)
66
- m.set(e(p[s]), s);
67
- const y = r.filter((s, l) => !g.includes(l));
68
- for (let s = 0; s < y.length; s++) {
69
- const l = e(y[s]), f = m.get(l);
70
- if (f == null || s === f) continue;
71
- const A = e(p[s]);
72
- m.delete(A), u.push({ from: s, to: f });
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
- }, $ = (t, r) => {
76
- const e = [...r];
77
- for (const { at: n, qt: o } of t.removals)
78
- e.splice(n, o);
79
- for (const { from: n, to: o } of t.swaps) {
80
- const i = e[o];
81
- e[o] = e[n], e[n] = i;
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
- }, C = (t, r = " and ", e = ", ") => t.length === 0 ? "" : t.length === 1 ? String(t[0]) : `${t.slice(0, -1).join(e)}${r}${String(t[t.length - 1])}`, H = (t, r, e = !0) => {
87
- const n = t.map((c, a) => [c, a]);
88
- n.sort((c, a) => r(c[0], a[0]));
89
- const o = new Array(n.length);
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 [a, h] = n[c];
93
- r(a, u) !== 0 && (i = c, u = a), o[h] = i, e && i++;
92
+ const [f, h] = n[c];
93
+ s(f, u) !== 0 && (i = c, u = f), r[h] = i, e && i++;
94
94
  }
95
- return o;
96
- }, S = (t, r) => {
97
- if (r <= 0)
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 += r)
101
- e.push(t.slice(n, n + r));
100
+ for (let n = 0; n < t.length; n += s)
101
+ e.push(t.slice(n, n + s));
102
102
  return e;
103
- }, R = (t, r) => {
103
+ }, W = (t, s) => {
104
104
  const e = [], n = [];
105
- for (const o of t)
106
- r(o) ? e.push(o) : n.push(o);
105
+ for (const r of t)
106
+ s(r) ? e.push(r) : n.push(r);
107
107
  return [e, n];
108
- }, V = (t, r) => {
108
+ }, G = (t, s) => {
109
109
  const e = {};
110
110
  for (const n of t) {
111
- const o = r(n);
112
- e[o] ? e[o].push(n) : e[o] = [n];
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
- $ as applyArrayDiffOperations,
118
- O as areArraysEqual,
119
- T as arrayDiffOperations,
120
- q as arrayHasValues,
121
- E as arrayHead,
122
- B as arrayTail,
123
- v as buildArray,
124
- S as chunk,
125
- D as compareArrays,
126
- M as fillArray,
127
- b as filterMapArray,
128
- j as filterNullsFromArray,
129
- V as groupBy,
130
- _ as isArrayEmpty,
131
- C as joinArrayWithConjunction,
132
- R as partition,
133
- F as range,
134
- H as rankArray,
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
- P as removeAllFromArrayByPredicate,
137
- w as removeOneFromArray,
138
- x as removeOneFromArrayByPredicate,
139
- I as uniqueByPrimitive
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 u={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,s){return u.isSuccess(e)?e.value:s},getOrElseLazy(e,s){return u.isSuccess(e)?e.value:s()},getOrNull(e){return u.isSuccess(e)?e.value:null},getOrUndefined(e){return u.isSuccess(e)?e.value:void 0},getUnsafe:e=>{if(u.isSuccess(e))return e.value;throw u.isFailure(e)?e.error:new Error("Cannot get value from a not-asked or loading result")},match:(e,{success:s,failure:r,loading:t,notAsked:n=t})=>u.isSuccess(e)?s(e.value):u.isFailure(e)?r(e.error):u.isNotAsked(e)?n():t(e.previousValue),whenSuccess:(e,s)=>(u.isSuccess(e)&&s(e.value),e),whenFailure:(e,s)=>(u.isFailure(e)&&s(e.error),e),equals:(e,s,r={valueEquals:(t,n)=>t===n,errorEquals:(t,n)=>t===n})=>e.type==="AsyncSuccess"&&s.type==="AsyncSuccess"?r.valueEquals(e.value,s.value):e.type==="AsyncFailure"&&s.type==="AsyncFailure"?r.errorEquals(e.error,s.error):e.type==="Loading"&&s.type==="Loading"?r.valueEquals(e.previousValue,s.previousValue):e.type==="NotAsked"&&s.type==="NotAsked",all:e=>{const s=[];for(const r of e)if(u.isSuccess(r))s.push(r.value);else return r;return u.success(s)},ofPromise:async e=>{try{const s=await e;return u.success(s)}catch(s){return u.failure(s instanceof Error?s:new Error(String(s)))}}};exports.AsyncResult=u;
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:c=a})=>s.isSuccess(e)?r(e.value):s.isFailure(e)?u(e.error):s.isNotAsked(e)?c():a(e.previousValue),effect:(e,r)=>{switch(e.type){case"AsyncSuccess":r.success?.(e.value);break;case"AsyncFailure":r.failure?.(e.error);break;case"Loading":r.loading?.(e.previousValue);break;case"NotAsked":r.notAsked?.();break}return e},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,c)=>a===c,errorEquals:(a,c)=>a===c})=>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
@@ -169,6 +169,20 @@ export declare const AsyncResult: {
169
169
  loading: (previousValue?: V1) => V2;
170
170
  notAsked: () => V2;
171
171
  }) => V2;
172
+ /**
173
+ * Executes side effects based on the state of the result.
174
+ * Unlike `match`, all handlers are optional, allowing you to react only to specific states.
175
+ * @param r - The result.
176
+ * @param handlers - An object with optional handlers for each state.
177
+ * @returns The result that was passed in, allowing for chaining.
178
+ * @public
179
+ */
180
+ effect: <V, E>(r: AsyncResult<V, E>, handlers: {
181
+ success?: (value: V) => void;
182
+ failure?: (error: E) => void;
183
+ loading?: (previousValue?: V) => void;
184
+ notAsked?: () => void;
185
+ }) => AsyncResult<V, E>;
172
186
  /**
173
187
  * When the result is a success, it applies the function to the value.
174
188
  *
@@ -210,4 +224,92 @@ export declare const AsyncResult: {
210
224
  * @returns A Promise that resolves to an AsyncResult.
211
225
  */
212
226
  ofPromise: <V>(p: Promise<V>) => Promise<AsyncResult<V, Error>>;
227
+ /**
228
+ * Maps the value of a successful `AsyncResult` to a new value using the provided function.
229
+ * For other states (NotAsked, Loading, Failure), the state is preserved.
230
+ * When mapping a Loading state with a previous value, the previous value is also mapped.
231
+ * @param result - The `AsyncResult` to map.
232
+ * @param fn - The mapping function to apply to the success value.
233
+ * @returns A new `AsyncResult` with the mapped value if successful, otherwise the original state.
234
+ * @public
235
+ */
236
+ map: <V, U, E>(result: AsyncResult<V, E>, fn: (value: V) => U) => AsyncResult<U, E>;
237
+ /**
238
+ * Maps the value of a successful `AsyncResult` to a new `AsyncResult` using the provided function.
239
+ * This is useful for chaining operations that may also fail.
240
+ * @param result - The `AsyncResult` to flat map.
241
+ * @param fn - The mapping function that returns a new `AsyncResult`.
242
+ * @returns The result of the mapping function if successful, otherwise the original state.
243
+ * @public
244
+ */
245
+ flatMap: <V, U, E>(result: AsyncResult<V, E>, fn: (value: V) => AsyncResult<U, E>) => AsyncResult<U, E>;
246
+ /**
247
+ * Maps the error of a failed `AsyncResult` to a new error using the provided function.
248
+ * For other states, the state is preserved.
249
+ * @param result - The `AsyncResult` to map the error of.
250
+ * @param fn - The mapping function to apply to the error.
251
+ * @returns A new `AsyncResult` with the mapped error if failed, otherwise the original state.
252
+ * @public
253
+ */
254
+ mapError: <V, E, F>(result: AsyncResult<V, E>, fn: (error: E) => F) => AsyncResult<V, F>;
255
+ /**
256
+ * Maps the error of a failed `AsyncResult` to a new `AsyncResult` using the provided function.
257
+ * This allows recovery from errors by returning a new successful result.
258
+ * @param result - The `AsyncResult` to recover from.
259
+ * @param fn - The recovery function that returns a new `AsyncResult`.
260
+ * @returns The result of the recovery function if failed, otherwise the original state.
261
+ * @public
262
+ */
263
+ flatMapError: <V, E, F>(result: AsyncResult<V, E>, fn: (error: E) => AsyncResult<V, F>) => AsyncResult<V, F>;
264
+ /**
265
+ * Converts an `AsyncResult` to a `Result`, discarding the loading and not-asked states.
266
+ * Returns `undefined` if the result is not settled (i.e., NotAsked or Loading).
267
+ * @param result - The `AsyncResult` to convert.
268
+ * @returns A `Result` if the `AsyncResult` is settled, otherwise `undefined`.
269
+ * @public
270
+ */
271
+ toResult: <V, E>(result: AsyncResult<V, E>) => import('./result').Result<V, E> | undefined;
272
+ /**
273
+ * Checks if the result is settled (either success or failure).
274
+ * @param r - The result.
275
+ * @returns `true` if the result is settled; otherwise, `false`.
276
+ * @public
277
+ */
278
+ isSettled<V, E>(r: AsyncResult<V, E>): r is Settled<V, E>;
279
+ /**
280
+ * Recovers from a failure by providing an alternative value.
281
+ * @param result - The `AsyncResult` to recover from.
282
+ * @param fn - The function that provides an alternative value given the error.
283
+ * @returns A successful `AsyncResult` with the alternative value if failed, otherwise the original state.
284
+ * @public
285
+ */
286
+ recover: <V, E>(result: AsyncResult<V, E>, fn: (error: E) => V) => AsyncResult<V, never>;
287
+ /**
288
+ * Applies a function wrapped in an `AsyncResult` to a value wrapped in an `AsyncResult`.
289
+ * Useful for applying multiple arguments to a function in a safe way.
290
+ * @param resultFn - The `AsyncResult` containing the function.
291
+ * @param resultVal - The `AsyncResult` containing the value.
292
+ * @returns A new `AsyncResult` with the result of applying the function to the value.
293
+ * @public
294
+ */
295
+ ap: <V, U, E>(resultFn: AsyncResult<(v: V) => U, E>, resultVal: AsyncResult<V, E>) => AsyncResult<U, E>;
296
+ /**
297
+ * Maps two `AsyncResult` values using a function.
298
+ * @param r1 - The first `AsyncResult`.
299
+ * @param r2 - The second `AsyncResult`.
300
+ * @param fn - The function to apply to both values.
301
+ * @returns A new `AsyncResult` with the result of applying the function to both values.
302
+ * @public
303
+ */
304
+ map2: <V1, V2, U, E>(r1: AsyncResult<V1, E>, r2: AsyncResult<V2, E>, fn: (v1: V1, v2: V2) => U) => AsyncResult<U, E>;
305
+ /**
306
+ * Maps three `AsyncResult` values using a function.
307
+ * @param r1 - The first `AsyncResult`.
308
+ * @param r2 - The second `AsyncResult`.
309
+ * @param r3 - The third `AsyncResult`.
310
+ * @param fn - The function to apply to all three values.
311
+ * @returns A new `AsyncResult` with the result of applying the function to all three values.
312
+ * @public
313
+ */
314
+ 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
315
  };