massaman 0.0.1-rc.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/LICENSE +21 -0
- package/README.md +139 -0
- package/dist/array/index.d.mts +2 -0
- package/dist/array/index.mjs +2 -0
- package/dist/array-C8MqjyiP.mjs +191 -0
- package/dist/array-C8MqjyiP.mjs.map +1 -0
- package/dist/control/index.d.mts +2 -0
- package/dist/control/index.mjs +2 -0
- package/dist/control-B4mDPBn6.mjs +164 -0
- package/dist/control-B4mDPBn6.mjs.map +1 -0
- package/dist/conversion/index.d.mts +2 -0
- package/dist/conversion/index.mjs +2 -0
- package/dist/conversion-ByBXBR5i.mjs +189 -0
- package/dist/conversion-ByBXBR5i.mjs.map +1 -0
- package/dist/error/index.d.mts +2 -0
- package/dist/error/index.mjs +2 -0
- package/dist/function/index.d.mts +2 -0
- package/dist/function/index.mjs +2 -0
- package/dist/function-2-6QB4m7.mjs +122 -0
- package/dist/function-2-6QB4m7.mjs.map +1 -0
- package/dist/index-BkyNt8th.d.mts +20 -0
- package/dist/index-BkyNt8th.d.mts.map +1 -0
- package/dist/index-Br9PO6mU.d.mts +155 -0
- package/dist/index-Br9PO6mU.d.mts.map +1 -0
- package/dist/index-BuBpCB73.d.mts +197 -0
- package/dist/index-BuBpCB73.d.mts.map +1 -0
- package/dist/index-COFf0Pih.d.mts +128 -0
- package/dist/index-COFf0Pih.d.mts.map +1 -0
- package/dist/index-DqsBQ7Pp.d.mts +120 -0
- package/dist/index-DqsBQ7Pp.d.mts.map +1 -0
- package/dist/index-Dw_tuZdb.d.mts +134 -0
- package/dist/index-Dw_tuZdb.d.mts.map +1 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.mjs +12 -0
- package/dist/math/index.d.mts +2 -0
- package/dist/math/index.mjs +2 -0
- package/dist/object/index.d.mts +2 -0
- package/dist/object/index.mjs +2 -0
- package/dist/object-BHnp9Sr9.mjs +27 -0
- package/dist/object-BHnp9Sr9.mjs.map +1 -0
- package/dist/pattern/index.d.mts +2 -0
- package/dist/pattern/index.mjs +2 -0
- package/dist/predicate/index.d.mts +2 -0
- package/dist/predicate/index.mjs +2 -0
- package/dist/predicate-CYzSOMLW.mjs +108 -0
- package/dist/predicate-CYzSOMLW.mjs.map +1 -0
- package/dist/promise/index.d.mts +2 -0
- package/dist/promise/index.mjs +2 -0
- package/dist/string/index.d.mts +2 -0
- package/dist/string/index.mjs +2 -0
- package/package.json +108 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { isError, isMap, isNil, isPrimitive, isSet, isString } from "es-toolkit/predicate";
|
|
2
|
+
//#region src/conversion/convert.ts
|
|
3
|
+
/**
|
|
4
|
+
* Coerces an unknown thrown value into a proper `Error` instance.
|
|
5
|
+
*
|
|
6
|
+
* Handles the common cases where libraries throw non-`Error` values
|
|
7
|
+
* (e.g. plain API response bodies, arrays, Maps) that would otherwise
|
|
8
|
+
* serialize as `[object Object]` in error messages.
|
|
9
|
+
*
|
|
10
|
+
* @param thrown - The caught value from a `catch` block.
|
|
11
|
+
* @returns An `Error` with a meaningful `.message`. If `thrown` is
|
|
12
|
+
* already an `Error`, it is returned as-is. The original value is
|
|
13
|
+
* preserved as `.cause` for debugging.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* try {
|
|
18
|
+
* await riskyCall()
|
|
19
|
+
* } catch (thrown) {
|
|
20
|
+
* const error = toError(thrown)
|
|
21
|
+
* console.error(error.message)
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
function toError(thrown) {
|
|
26
|
+
if (isError(thrown)) return thrown;
|
|
27
|
+
if (isString(thrown)) return new Error(thrown);
|
|
28
|
+
return new Error(stringify(thrown), { cause: thrown });
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Produces a human-readable string from any unknown value.
|
|
32
|
+
*
|
|
33
|
+
* Uses `JSON.stringify` for structured types (plain objects, arrays)
|
|
34
|
+
* so the message contains actual content instead of `[object Object]`.
|
|
35
|
+
* Maps and Sets are converted to their array representation first.
|
|
36
|
+
* Falls back to `String()` for primitives or when serialization fails
|
|
37
|
+
* (e.g. circular references).
|
|
38
|
+
*
|
|
39
|
+
* @param value - The value to stringify.
|
|
40
|
+
* @returns A meaningful string representation.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* stringify({ status: 400 }) // '{"status":400}'
|
|
45
|
+
* stringify(new Map([['k', 'v']])) // '[["k","v"]]'
|
|
46
|
+
* stringify(null) // 'null'
|
|
47
|
+
* stringify(42) // '42'
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
function stringify(value) {
|
|
51
|
+
if (isNil(value) || isPrimitive(value)) return String(value);
|
|
52
|
+
try {
|
|
53
|
+
return JSON.stringify(toSerializable(value));
|
|
54
|
+
} catch {
|
|
55
|
+
return String(value);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Convert types that `JSON.stringify` handles poorly into
|
|
60
|
+
* serializable equivalents, recursively walking objects and arrays.
|
|
61
|
+
*
|
|
62
|
+
* - `Map` -> array of `[key, value]` entries (recursed)
|
|
63
|
+
* - `Set` -> array of values (recursed)
|
|
64
|
+
* - `Error` -> plain object with `name`, `message`, `stack`, and enumerable props
|
|
65
|
+
* - Arrays and plain objects are recursed
|
|
66
|
+
* - Uses a `WeakSet` to detect and break circular references
|
|
67
|
+
*/
|
|
68
|
+
function toSerializable(value, seen = /* @__PURE__ */ new WeakSet()) {
|
|
69
|
+
if (isNil(value) || isPrimitive(value)) return value;
|
|
70
|
+
const obj = value;
|
|
71
|
+
if (seen.has(obj)) return "[Circular]";
|
|
72
|
+
seen.add(obj);
|
|
73
|
+
if (isError(value)) {
|
|
74
|
+
const errorObj = {
|
|
75
|
+
name: value.name,
|
|
76
|
+
message: value.message,
|
|
77
|
+
stack: value.stack
|
|
78
|
+
};
|
|
79
|
+
for (const key of Object.keys(value)) errorObj[key] = toSerializable(value[key], seen);
|
|
80
|
+
return errorObj;
|
|
81
|
+
}
|
|
82
|
+
if (isMap(value)) return Array.from(value.entries()).map(([k, v]) => [toSerializable(k, seen), toSerializable(v, seen)]);
|
|
83
|
+
if (isSet(value)) return Array.from(value).map((v) => toSerializable(v, seen));
|
|
84
|
+
if (Array.isArray(value)) return value.map((v) => toSerializable(v, seen));
|
|
85
|
+
const result = {};
|
|
86
|
+
for (const key of Object.keys(value)) result[key] = toSerializable(value[key], seen);
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Converts a value to a number.
|
|
91
|
+
*
|
|
92
|
+
* Thin wrapper over `Number(value)` — kept as a stable surface so callers
|
|
93
|
+
* don't depend directly on the global constructor's semantics (which have
|
|
94
|
+
* shifted in the past, e.g. `Number(BigInt)`, and may again).
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* toNumber('42') // 42
|
|
99
|
+
* toNumber(null) // 0
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
function toNumber(value) {
|
|
103
|
+
return Number(value);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Converts a value to a string.
|
|
107
|
+
*
|
|
108
|
+
* Thin wrapper over `String(value)` — kept as a stable surface against
|
|
109
|
+
* future global-constructor changes.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* toString(42) // '42'
|
|
114
|
+
* toString(null) // 'null'
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
function toString(value) {
|
|
118
|
+
return String(value);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Converts a value to an integer by calling {@link toNumber} and truncating.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* toInteger('4.9') // 4
|
|
126
|
+
* toInteger(null) // 0
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
function toInteger(value) {
|
|
130
|
+
return Math.trunc(toNumber(value));
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Converts a value to a finite number.
|
|
134
|
+
*
|
|
135
|
+
* Returns `0` for non-finite results (`NaN`, `Infinity`, `-Infinity`).
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* toFinite('3.14') // 3.14
|
|
140
|
+
* toFinite(Infinity) // 0
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
function toFinite(value) {
|
|
144
|
+
const n = Number(value);
|
|
145
|
+
if (Number.isFinite(n)) return n;
|
|
146
|
+
return 0;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Converts a value to an array.
|
|
150
|
+
*
|
|
151
|
+
* - Arrays are returned as-is.
|
|
152
|
+
* - Iterables (strings, Sets, Maps) are spread into an array.
|
|
153
|
+
* - `null` / `undefined` return `[]`.
|
|
154
|
+
* - All other values are wrapped in a single-element array.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```ts
|
|
158
|
+
* toArray(new Set([1, 2])) // [1, 2]
|
|
159
|
+
* toArray('abc') // ['a', 'b', 'c']
|
|
160
|
+
* toArray(null) // []
|
|
161
|
+
* toArray(42) // [42]
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
function toArray(value) {
|
|
165
|
+
if (value == null) return [];
|
|
166
|
+
if (Array.isArray(value)) return value;
|
|
167
|
+
if (typeof value[Symbol.iterator] === "function") return Array.from(value);
|
|
168
|
+
return [value];
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Converts a value to a boolean.
|
|
172
|
+
*
|
|
173
|
+
* Thin wrapper over `Boolean(value)` — kept as a stable surface against
|
|
174
|
+
* future global-constructor changes.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```ts
|
|
178
|
+
* toBoolean(1) // true
|
|
179
|
+
* toBoolean(0) // false
|
|
180
|
+
* toBoolean('') // false
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
function toBoolean(value) {
|
|
184
|
+
return Boolean(value);
|
|
185
|
+
}
|
|
186
|
+
//#endregion
|
|
187
|
+
export { toFinite as a, toString as c, toError as i, toArray as n, toInteger as o, toBoolean as r, toNumber as s, stringify as t };
|
|
188
|
+
|
|
189
|
+
//# sourceMappingURL=conversion-ByBXBR5i.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conversion-ByBXBR5i.mjs","names":[],"sources":["../src/conversion/convert.ts"],"sourcesContent":["import { isError, isMap, isNil, isPrimitive, isSet, isString } from 'es-toolkit/predicate'\n\n/**\n * Coerces an unknown thrown value into a proper `Error` instance.\n *\n * Handles the common cases where libraries throw non-`Error` values\n * (e.g. plain API response bodies, arrays, Maps) that would otherwise\n * serialize as `[object Object]` in error messages.\n *\n * @param thrown - The caught value from a `catch` block.\n * @returns An `Error` with a meaningful `.message`. If `thrown` is\n * already an `Error`, it is returned as-is. The original value is\n * preserved as `.cause` for debugging.\n *\n * @example\n * ```ts\n * try {\n * await riskyCall()\n * } catch (thrown) {\n * const error = toError(thrown)\n * console.error(error.message)\n * }\n * ```\n */\nexport function toError(thrown: unknown): Error {\n if (isError(thrown)) {\n return thrown\n }\n if (isString(thrown)) {\n return new Error(thrown)\n }\n return new Error(stringify(thrown), { cause: thrown })\n}\n\n/**\n * Produces a human-readable string from any unknown value.\n *\n * Uses `JSON.stringify` for structured types (plain objects, arrays)\n * so the message contains actual content instead of `[object Object]`.\n * Maps and Sets are converted to their array representation first.\n * Falls back to `String()` for primitives or when serialization fails\n * (e.g. circular references).\n *\n * @param value - The value to stringify.\n * @returns A meaningful string representation.\n *\n * @example\n * ```ts\n * stringify({ status: 400 }) // '{\"status\":400}'\n * stringify(new Map([['k', 'v']])) // '[[\"k\",\"v\"]]'\n * stringify(null) // 'null'\n * stringify(42) // '42'\n * ```\n */\nexport function stringify(value: unknown): string {\n if (isNil(value) || isPrimitive(value)) {\n return String(value)\n }\n try {\n return JSON.stringify(toSerializable(value))\n } catch {\n return String(value)\n }\n}\n\n// ---------------------------------------------------------------------------\n// private helpers\n// ---------------------------------------------------------------------------\n\n/**\n * Convert types that `JSON.stringify` handles poorly into\n * serializable equivalents, recursively walking objects and arrays.\n *\n * - `Map` -> array of `[key, value]` entries (recursed)\n * - `Set` -> array of values (recursed)\n * - `Error` -> plain object with `name`, `message`, `stack`, and enumerable props\n * - Arrays and plain objects are recursed\n * - Uses a `WeakSet` to detect and break circular references\n */\nfunction toSerializable(value: unknown, seen: WeakSet<object> = new WeakSet()): unknown {\n if (isNil(value) || isPrimitive(value)) {\n return value\n }\n\n const obj = value as object\n if (seen.has(obj)) {\n return '[Circular]'\n }\n seen.add(obj)\n\n if (isError(value)) {\n const errorObj: Record<string, unknown> = {\n name: value.name,\n message: value.message,\n stack: value.stack,\n }\n for (const key of Object.keys(value)) {\n // oxlint-disable-next-line security/detect-object-injection -- safe: key from Object.keys\n errorObj[key] = toSerializable((value as unknown as Record<string, unknown>)[key], seen)\n }\n return errorObj\n }\n\n if (isMap(value)) {\n return Array.from(value.entries()).map(([k, v]) => [\n toSerializable(k, seen),\n toSerializable(v, seen),\n ])\n }\n\n if (isSet(value)) {\n return Array.from(value).map((v) => toSerializable(v, seen))\n }\n\n if (Array.isArray(value)) {\n return value.map((v) => toSerializable(v, seen))\n }\n\n const result: Record<string, unknown> = {}\n for (const key of Object.keys(value as Record<string, unknown>)) {\n // oxlint-disable-next-line security/detect-object-injection -- safe: key from Object.keys\n result[key] = toSerializable((value as Record<string, unknown>)[key], seen)\n }\n return result\n}\n\n/**\n * Converts a value to a number.\n *\n * Thin wrapper over `Number(value)` — kept as a stable surface so callers\n * don't depend directly on the global constructor's semantics (which have\n * shifted in the past, e.g. `Number(BigInt)`, and may again).\n *\n * @example\n * ```ts\n * toNumber('42') // 42\n * toNumber(null) // 0\n * ```\n */\nexport function toNumber(value: unknown): number {\n return Number(value)\n}\n\n/**\n * Converts a value to a string.\n *\n * Thin wrapper over `String(value)` — kept as a stable surface against\n * future global-constructor changes.\n *\n * @example\n * ```ts\n * toString(42) // '42'\n * toString(null) // 'null'\n * ```\n */\nexport function toString(value: unknown): string {\n return String(value)\n}\n\n/**\n * Converts a value to an integer by calling {@link toNumber} and truncating.\n *\n * @example\n * ```ts\n * toInteger('4.9') // 4\n * toInteger(null) // 0\n * ```\n */\nexport function toInteger(value: unknown): number {\n return Math.trunc(toNumber(value))\n}\n\n/**\n * Converts a value to a finite number.\n *\n * Returns `0` for non-finite results (`NaN`, `Infinity`, `-Infinity`).\n *\n * @example\n * ```ts\n * toFinite('3.14') // 3.14\n * toFinite(Infinity) // 0\n * ```\n */\nexport function toFinite(value: unknown): number {\n const n = Number(value)\n if (Number.isFinite(n)) {\n return n\n }\n return 0\n}\n\n/**\n * Converts a value to an array.\n *\n * - Arrays are returned as-is.\n * - Iterables (strings, Sets, Maps) are spread into an array.\n * - `null` / `undefined` return `[]`.\n * - All other values are wrapped in a single-element array.\n *\n * @example\n * ```ts\n * toArray(new Set([1, 2])) // [1, 2]\n * toArray('abc') // ['a', 'b', 'c']\n * toArray(null) // []\n * toArray(42) // [42]\n * ```\n */\nexport function toArray<T>(value: Iterable<T> | T | null | undefined): T[] {\n if (value == null) {\n return []\n }\n if (Array.isArray(value)) {\n return value as T[]\n }\n if (typeof (value as unknown as Record<symbol, unknown>)[Symbol.iterator] === 'function') {\n return Array.from(value as Iterable<T>)\n }\n return [value as T]\n}\n\n/**\n * Converts a value to a boolean.\n *\n * Thin wrapper over `Boolean(value)` — kept as a stable surface against\n * future global-constructor changes.\n *\n * @example\n * ```ts\n * toBoolean(1) // true\n * toBoolean(0) // false\n * toBoolean('') // false\n * ```\n */\nexport function toBoolean(value: unknown): boolean {\n return Boolean(value)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAwBA,SAAgB,QAAQ,QAAwB;CAC9C,IAAI,QAAQ,OAAO,EACjB,OAAO;CAET,IAAI,SAAS,OAAO,EAClB,OAAO,IAAI,MAAM,OAAO;CAE1B,OAAO,IAAI,MAAM,UAAU,OAAO,EAAE,EAAE,OAAO,QAAQ,CAAC;;;;;;;;;;;;;;;;;;;;;;AAuBxD,SAAgB,UAAU,OAAwB;CAChD,IAAI,MAAM,MAAM,IAAI,YAAY,MAAM,EACpC,OAAO,OAAO,MAAM;CAEtB,IAAI;EACF,OAAO,KAAK,UAAU,eAAe,MAAM,CAAC;SACtC;EACN,OAAO,OAAO,MAAM;;;;;;;;;;;;;AAkBxB,SAAS,eAAe,OAAgB,uBAAwB,IAAI,SAAS,EAAW;CACtF,IAAI,MAAM,MAAM,IAAI,YAAY,MAAM,EACpC,OAAO;CAGT,MAAM,MAAM;CACZ,IAAI,KAAK,IAAI,IAAI,EACf,OAAO;CAET,KAAK,IAAI,IAAI;CAEb,IAAI,QAAQ,MAAM,EAAE;EAClB,MAAM,WAAoC;GACxC,MAAM,MAAM;GACZ,SAAS,MAAM;GACf,OAAO,MAAM;GACd;EACD,KAAK,MAAM,OAAO,OAAO,KAAK,MAAM,EAElC,SAAS,OAAO,eAAgB,MAA6C,MAAM,KAAK;EAE1F,OAAO;;CAGT,IAAI,MAAM,MAAM,EACd,OAAO,MAAM,KAAK,MAAM,SAAS,CAAC,CAAC,KAAK,CAAC,GAAG,OAAO,CACjD,eAAe,GAAG,KAAK,EACvB,eAAe,GAAG,KAAK,CACxB,CAAC;CAGJ,IAAI,MAAM,MAAM,EACd,OAAO,MAAM,KAAK,MAAM,CAAC,KAAK,MAAM,eAAe,GAAG,KAAK,CAAC;CAG9D,IAAI,MAAM,QAAQ,MAAM,EACtB,OAAO,MAAM,KAAK,MAAM,eAAe,GAAG,KAAK,CAAC;CAGlD,MAAM,SAAkC,EAAE;CAC1C,KAAK,MAAM,OAAO,OAAO,KAAK,MAAiC,EAE7D,OAAO,OAAO,eAAgB,MAAkC,MAAM,KAAK;CAE7E,OAAO;;;;;;;;;;;;;;;AAgBT,SAAgB,SAAS,OAAwB;CAC/C,OAAO,OAAO,MAAM;;;;;;;;;;;;;;AAetB,SAAgB,SAAS,OAAwB;CAC/C,OAAO,OAAO,MAAM;;;;;;;;;;;AAYtB,SAAgB,UAAU,OAAwB;CAChD,OAAO,KAAK,MAAM,SAAS,MAAM,CAAC;;;;;;;;;;;;;AAcpC,SAAgB,SAAS,OAAwB;CAC/C,MAAM,IAAI,OAAO,MAAM;CACvB,IAAI,OAAO,SAAS,EAAE,EACpB,OAAO;CAET,OAAO;;;;;;;;;;;;;;;;;;AAmBT,SAAgB,QAAW,OAAgD;CACzE,IAAI,SAAS,MACX,OAAO,EAAE;CAEX,IAAI,MAAM,QAAQ,MAAM,EACtB,OAAO;CAET,IAAI,OAAQ,MAA6C,OAAO,cAAc,YAC5E,OAAO,MAAM,KAAK,MAAqB;CAEzC,OAAO,CAAC,MAAW;;;;;;;;;;;;;;;AAgBrB,SAAgB,UAAU,OAAyB;CACjD,OAAO,QAAQ,MAAM"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { A as unless, C as retry, D as flowAsync, E as unary, M as call, N as callAsync, O as tap, S as rest, T as throttle, _ as negate, a as ThrottledFunction, b as partial, c as asyncNoop, d as curryRight, f as debounce, g as memoize, h as identity, i as ThrottleOptions, j as when, k as ifElse, l as before, m as flowRight, n as DebouncedFunction, o as after, p as flow, r as MemoizeCache, s as ary, t as DebounceOptions, u as curry, v as noop, w as spread, x as partialRight, y as once } from "../index-DqsBQ7Pp.mjs";
|
|
2
|
+
export { DebounceOptions, DebouncedFunction, MemoizeCache, ThrottleOptions, ThrottledFunction, after, ary, asyncNoop, before, call, callAsync, curry, curryRight, debounce, flow, flowAsync, flowRight, identity, ifElse, memoize, negate, noop, once, partial, partialRight, rest, retry, spread, tap, throttle, unary, unless, when };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { C as tap, D as call, E as when, O as callAsync, S as flowAsync, T as unless, _ as rest, a as curry, b as throttle, c as flow, d as memoize, f as negate, g as partialRight, h as partial, i as before, l as flowRight, m as once, n as ary, o as curryRight, p as noop, r as asyncNoop, s as debounce, t as after, u as identity, v as retry, w as ifElse, x as unary, y as spread } from "../function-2-6QB4m7.mjs";
|
|
2
|
+
export { after, ary, asyncNoop, before, call, callAsync, curry, curryRight, debounce, flow, flowAsync, flowRight, identity, ifElse, memoize, negate, noop, once, partial, partialRight, rest, retry, spread, tap, throttle, unary, unless, when };
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { after, ary, asyncNoop, before, curry, curryRight, debounce, flow, flowRight, identity, memoize, negate, noop, once, partial, partialRight, rest, retry, spread, throttle, unary } from "es-toolkit/function";
|
|
2
|
+
//#region src/function/call.ts
|
|
3
|
+
/**
|
|
4
|
+
* Invokes a function with the provided arguments and returns the result.
|
|
5
|
+
*
|
|
6
|
+
* Equivalent to `fn(...args)` — exists as a stable, named surface for the
|
|
7
|
+
* "apply function to args" operation. Use inside `.map(call)` over arrays
|
|
8
|
+
* of thunks, or anywhere a value-position function-call is clearer than an
|
|
9
|
+
* inline arrow.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* call(Math.max, 1, 2, 3) // 3
|
|
14
|
+
* [getUser, getAccount, getPrefs].map((fn) => call(fn))
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
function call(fn, ...args) {
|
|
18
|
+
return fn(...args);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Invokes a function returning a Promise with the provided arguments.
|
|
22
|
+
*
|
|
23
|
+
* Async-typed sibling of {@link call} — equivalent to `fn(...args)` for
|
|
24
|
+
* promise-returning functions, but the signature constrains the return to
|
|
25
|
+
* `Promise<R>` so it composes cleanly inside async pipelines.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* const user = await callAsync(fetchUser, 'user-123')
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
async function callAsync(fn, ...args) {
|
|
33
|
+
return fn(...args);
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/function/branching.ts
|
|
37
|
+
/**
|
|
38
|
+
* Returns a function that applies a transform only when a predicate passes,
|
|
39
|
+
* otherwise returns the value unchanged.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const trimIfString = when(isString, (s: string) => s.trim())
|
|
44
|
+
* trimIfString(' hello ') // 'hello'
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
function when(predicate, fn) {
|
|
48
|
+
return (value) => {
|
|
49
|
+
if (predicate(value)) return fn(value);
|
|
50
|
+
return value;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns a function that applies a transform only when a predicate fails,
|
|
55
|
+
* otherwise returns the value unchanged.
|
|
56
|
+
*
|
|
57
|
+
* Complement of {@link when}.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* const ensureArray = unless(Array.isArray, (v) => [v])
|
|
62
|
+
* ensureArray(42) // [42]
|
|
63
|
+
* ensureArray([1]) // [1]
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
function unless(predicate, fn) {
|
|
67
|
+
return (value) => {
|
|
68
|
+
if (predicate(value)) return value;
|
|
69
|
+
return fn(value);
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Returns a function that branches between two functions based on a predicate.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* const format = ifElse(
|
|
78
|
+
* (n: number) => n > 0,
|
|
79
|
+
* (n) => `+${n}`,
|
|
80
|
+
* (n) => `${n}`,
|
|
81
|
+
* )
|
|
82
|
+
* format(5) // '+5'
|
|
83
|
+
* format(-3) // '-3'
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
function ifElse(predicate, onTrue, onFalse) {
|
|
87
|
+
return (value) => {
|
|
88
|
+
if (predicate(value)) return onTrue(value);
|
|
89
|
+
return onFalse(value);
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/function/tap.ts
|
|
94
|
+
/**
|
|
95
|
+
* Returns a function that runs a side-effect and returns the value unchanged.
|
|
96
|
+
*
|
|
97
|
+
* Useful for debugging inside `flow` pipelines without breaking the chain.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* const process = flow(
|
|
102
|
+
* fetchUsers,
|
|
103
|
+
* tap(users => console.log('count:', users.length)),
|
|
104
|
+
* filterActive,
|
|
105
|
+
* )
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
function tap(fn) {
|
|
109
|
+
return (value) => {
|
|
110
|
+
fn(value);
|
|
111
|
+
return value;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region src/function/flowAsync.ts
|
|
116
|
+
function flowAsync(first, ...rest) {
|
|
117
|
+
return async (...args) => rest.reduce(async (acc, fn) => fn(await acc), Promise.resolve(first(...args)));
|
|
118
|
+
}
|
|
119
|
+
//#endregion
|
|
120
|
+
export { tap as C, call as D, when as E, callAsync as O, flowAsync as S, unless as T, rest as _, curry as a, throttle as b, flow as c, memoize as d, negate as f, partialRight as g, partial as h, before as i, flowRight as l, once as m, ary as n, curryRight as o, noop as p, asyncNoop as r, debounce as s, after as t, identity as u, retry as v, ifElse as w, unary as x, spread as y };
|
|
121
|
+
|
|
122
|
+
//# sourceMappingURL=function-2-6QB4m7.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function-2-6QB4m7.mjs","names":[],"sources":["../src/function/call.ts","../src/function/branching.ts","../src/function/tap.ts","../src/function/flowAsync.ts"],"sourcesContent":["/**\n * Invokes a function with the provided arguments and returns the result.\n *\n * Equivalent to `fn(...args)` — exists as a stable, named surface for the\n * \"apply function to args\" operation. Use inside `.map(call)` over arrays\n * of thunks, or anywhere a value-position function-call is clearer than an\n * inline arrow.\n *\n * @example\n * ```ts\n * call(Math.max, 1, 2, 3) // 3\n * [getUser, getAccount, getPrefs].map((fn) => call(fn))\n * ```\n */\nexport function call<A extends readonly unknown[], R>(fn: (...args: A) => R, ...args: A): R {\n return fn(...args)\n}\n\n/**\n * Invokes a function returning a Promise with the provided arguments.\n *\n * Async-typed sibling of {@link call} — equivalent to `fn(...args)` for\n * promise-returning functions, but the signature constrains the return to\n * `Promise<R>` so it composes cleanly inside async pipelines.\n *\n * @example\n * ```ts\n * const user = await callAsync(fetchUser, 'user-123')\n * ```\n */\nexport async function callAsync<A extends readonly unknown[], R>(\n fn: (...args: A) => Promise<R>,\n ...args: A\n): Promise<R> {\n return fn(...args)\n}\n","/**\n * Returns a function that applies a transform only when a predicate passes,\n * otherwise returns the value unchanged.\n *\n * @example\n * ```ts\n * const trimIfString = when(isString, (s: string) => s.trim())\n * trimIfString(' hello ') // 'hello'\n * ```\n */\nexport function when<T>(predicate: (value: T) => boolean, fn: (value: T) => T): (value: T) => T {\n return (value: T) => {\n if (predicate(value)) {\n return fn(value)\n }\n return value\n }\n}\n\n/**\n * Returns a function that applies a transform only when a predicate fails,\n * otherwise returns the value unchanged.\n *\n * Complement of {@link when}.\n *\n * @example\n * ```ts\n * const ensureArray = unless(Array.isArray, (v) => [v])\n * ensureArray(42) // [42]\n * ensureArray([1]) // [1]\n * ```\n */\nexport function unless<T>(predicate: (value: T) => boolean, fn: (value: T) => T): (value: T) => T {\n return (value: T) => {\n if (predicate(value)) {\n return value\n }\n return fn(value)\n }\n}\n\n/**\n * Returns a function that branches between two functions based on a predicate.\n *\n * @example\n * ```ts\n * const format = ifElse(\n * (n: number) => n > 0,\n * (n) => `+${n}`,\n * (n) => `${n}`,\n * )\n * format(5) // '+5'\n * format(-3) // '-3'\n * ```\n */\nexport function ifElse<T, R>(\n predicate: (value: T) => boolean,\n onTrue: (value: T) => R,\n onFalse: (value: T) => R\n): (value: T) => R {\n return (value: T) => {\n if (predicate(value)) {\n return onTrue(value)\n }\n return onFalse(value)\n }\n}\n","/**\n * Returns a function that runs a side-effect and returns the value unchanged.\n *\n * Useful for debugging inside `flow` pipelines without breaking the chain.\n *\n * @example\n * ```ts\n * const process = flow(\n * fetchUsers,\n * tap(users => console.log('count:', users.length)),\n * filterActive,\n * )\n * ```\n */\nexport function tap<T>(fn: (value: T) => unknown): (value: T) => T {\n return (value: T) => {\n fn(value)\n return value\n }\n}\n","import type { DangerouslyAllowAny } from '../types/internal.js'\n\ntype MaybePromise<T> = T | Promise<T>\n\n/**\n * Creates an async pipeline that awaits each step before passing the result\n * to the next function. Like {@link flow} from es-toolkit but async-aware.\n *\n * Each function can return a value or a Promise — `flowAsync` always returns\n * a Promise that resolves to the final step's awaited result.\n *\n * @example\n * ```ts\n * const getUsername = flowAsync(\n * (id: string) => fetchUser(id), // Promise<User>\n * (user) => user.name, // string\n * (name) => name.toUpperCase(),\n * )\n * await getUsername('123') // 'ALICE'\n * ```\n */\nexport function flowAsync<A extends readonly unknown[], R1>(\n f1: (...args: A) => MaybePromise<R1>\n): (...args: A) => Promise<Awaited<R1>>\nexport function flowAsync<A extends readonly unknown[], R1, R2>(\n f1: (...args: A) => MaybePromise<R1>,\n f2: (a: Awaited<R1>) => MaybePromise<R2>\n): (...args: A) => Promise<Awaited<R2>>\nexport function flowAsync<A extends readonly unknown[], R1, R2, R3>(\n f1: (...args: A) => MaybePromise<R1>,\n f2: (a: Awaited<R1>) => MaybePromise<R2>,\n f3: (a: Awaited<R2>) => MaybePromise<R3>\n): (...args: A) => Promise<Awaited<R3>>\nexport function flowAsync<A extends readonly unknown[], R1, R2, R3, R4>(\n f1: (...args: A) => MaybePromise<R1>,\n f2: (a: Awaited<R1>) => MaybePromise<R2>,\n f3: (a: Awaited<R2>) => MaybePromise<R3>,\n f4: (a: Awaited<R3>) => MaybePromise<R4>\n): (...args: A) => Promise<Awaited<R4>>\nexport function flowAsync<A extends readonly unknown[], R1, R2, R3, R4, R5>(\n f1: (...args: A) => MaybePromise<R1>,\n f2: (a: Awaited<R1>) => MaybePromise<R2>,\n f3: (a: Awaited<R2>) => MaybePromise<R3>,\n f4: (a: Awaited<R3>) => MaybePromise<R4>,\n f5: (a: Awaited<R4>) => MaybePromise<R5>\n): (...args: A) => Promise<Awaited<R5>>\nexport function flowAsync<A extends readonly unknown[], R1, R2, R3, R4, R5, R6>(\n f1: (...args: A) => MaybePromise<R1>,\n f2: (a: Awaited<R1>) => MaybePromise<R2>,\n f3: (a: Awaited<R2>) => MaybePromise<R3>,\n f4: (a: Awaited<R3>) => MaybePromise<R4>,\n f5: (a: Awaited<R4>) => MaybePromise<R5>,\n f6: (a: Awaited<R5>) => MaybePromise<R6>\n): (...args: A) => Promise<Awaited<R6>>\nexport function flowAsync<A extends readonly unknown[], R1, R2, R3, R4, R5, R6, R7>(\n f1: (...args: A) => MaybePromise<R1>,\n f2: (a: Awaited<R1>) => MaybePromise<R2>,\n f3: (a: Awaited<R2>) => MaybePromise<R3>,\n f4: (a: Awaited<R3>) => MaybePromise<R4>,\n f5: (a: Awaited<R4>) => MaybePromise<R5>,\n f6: (a: Awaited<R5>) => MaybePromise<R6>,\n f7: (a: Awaited<R6>) => MaybePromise<R7>\n): (...args: A) => Promise<Awaited<R7>>\nexport function flowAsync(\n first: (...args: readonly DangerouslyAllowAny[]) => unknown,\n ...rest: ReadonlyArray<(arg: DangerouslyAllowAny) => unknown>\n): (...args: readonly unknown[]) => Promise<unknown> {\n return async (...args: readonly unknown[]) =>\n rest.reduce<Promise<unknown>>(async (acc, fn) => fn(await acc), Promise.resolve(first(...args)))\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAcA,SAAgB,KAAsC,IAAuB,GAAG,MAAY;CAC1F,OAAO,GAAG,GAAG,KAAK;;;;;;;;;;;;;;AAepB,eAAsB,UACpB,IACA,GAAG,MACS;CACZ,OAAO,GAAG,GAAG,KAAK;;;;;;;;;;;;;;ACxBpB,SAAgB,KAAQ,WAAkC,IAAsC;CAC9F,QAAQ,UAAa;EACnB,IAAI,UAAU,MAAM,EAClB,OAAO,GAAG,MAAM;EAElB,OAAO;;;;;;;;;;;;;;;;AAiBX,SAAgB,OAAU,WAAkC,IAAsC;CAChG,QAAQ,UAAa;EACnB,IAAI,UAAU,MAAM,EAClB,OAAO;EAET,OAAO,GAAG,MAAM;;;;;;;;;;;;;;;;;AAkBpB,SAAgB,OACd,WACA,QACA,SACiB;CACjB,QAAQ,UAAa;EACnB,IAAI,UAAU,MAAM,EAClB,OAAO,OAAO,MAAM;EAEtB,OAAO,QAAQ,MAAM;;;;;;;;;;;;;;;;;;;AClDzB,SAAgB,IAAO,IAA4C;CACjE,QAAQ,UAAa;EACnB,GAAG,MAAM;EACT,OAAO;;;;;AC8CX,SAAgB,UACd,OACA,GAAG,MACgD;CACnD,OAAO,OAAO,GAAG,SACf,KAAK,OAAyB,OAAO,KAAK,OAAO,GAAG,MAAM,IAAI,EAAE,QAAQ,QAAQ,MAAM,GAAG,KAAK,CAAC,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { clone, cloneDeep, cloneDeepWith, findKey, flattenObject, invert, mapKeys, mapValues, merge, mergeWith, omit, omitBy, pick, pickBy, toCamelCaseKeys, toMerged, toSnakeCaseKeys } from "es-toolkit/object";
|
|
2
|
+
|
|
3
|
+
//#region src/object/evolve.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Applies a spec of transformation functions to matching keys of an object,
|
|
6
|
+
* returning a new object. Keys not in the spec are copied as-is.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* evolve({ name: ' Alice ', age: 29, role: 'admin' }, {
|
|
11
|
+
* name: (s) => s.trim(),
|
|
12
|
+
* age: (n) => n + 1,
|
|
13
|
+
* })
|
|
14
|
+
* // { name: 'Alice', age: 30, role: 'admin' }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
declare function evolve<T extends Record<string, unknown>>(obj: T, spec: { [K in keyof T]?: (value: T[K]) => T[K] }): T;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { toSnakeCaseKeys as _, flattenObject as a, mapValues as c, omit as d, omitBy as f, toMerged as g, toCamelCaseKeys as h, findKey as i, merge as l, pickBy as m, cloneDeep as n, invert as o, pick as p, cloneDeepWith as r, mapKeys as s, clone as t, mergeWith as u, evolve as v };
|
|
20
|
+
//# sourceMappingURL=index-BkyNt8th.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-BkyNt8th.d.mts","names":[],"sources":["../src/object/evolve.ts"],"mappings":";;;;;;AAeA;;;;;;;;;;iBAAgB,MAAA,WAAiB,MAAA,kBAAA,CAC/B,GAAA,EAAK,CAAA,EACL,IAAA,gBAAoB,CAAA,KAAM,KAAA,EAAO,CAAA,CAAE,CAAA,MAAO,CAAA,CAAE,CAAA,MAC3C,CAAA"}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { assert, invariant } from "es-toolkit/util";
|
|
2
|
+
|
|
3
|
+
//#region src/control/types.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Success result containing a value.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const result: Ok<number> = { ok: true, value: 42 }
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
interface Ok<T> {
|
|
13
|
+
readonly ok: true;
|
|
14
|
+
readonly value: T;
|
|
15
|
+
readonly error: null;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Failure result containing an error.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const result: Err = { ok: false, error: new Error('fail') }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
interface Err {
|
|
26
|
+
readonly ok: false;
|
|
27
|
+
readonly value: null;
|
|
28
|
+
readonly error: Error;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Discriminated union representing either success (`Ok`) or failure (`Err`).
|
|
32
|
+
* Inspired by Rust's `Result<T, E>`, but errors are always `Error`.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* function divide(a: number, b: number): Result<number> {
|
|
37
|
+
* return b === 0 ? err('division by zero') : ok(a / b)
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
type Result<T> = Ok<T> | Err;
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/control/attempt.d.ts
|
|
44
|
+
/**
|
|
45
|
+
* Executes a synchronous function and wraps the outcome in a `Result`.
|
|
46
|
+
* Returns `Ok` with the return value on success, `Err` with the thrown value on failure.
|
|
47
|
+
*
|
|
48
|
+
* @param fn - The function to execute
|
|
49
|
+
* @returns A `Result` containing either the value or the error
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* const result = attempt(() => JSON.parse('{"a":1}'))
|
|
54
|
+
* if (isOk(result)) {
|
|
55
|
+
* console.log(result.value) // { a: 1 }
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
declare function attempt<T>(fn: () => T): Result<T>;
|
|
60
|
+
/**
|
|
61
|
+
* Executes an asynchronous function and wraps the outcome in a `Result`.
|
|
62
|
+
* Returns `Ok` with the resolved value on success, `Err` with the rejection reason on failure.
|
|
63
|
+
*
|
|
64
|
+
* @param fn - The async function to execute
|
|
65
|
+
* @returns A promise resolving to a `Result` containing either the value or the error
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* const result = await attemptAsync(() => fetch('/api/data'))
|
|
70
|
+
* if (isErr(result)) {
|
|
71
|
+
* console.error(result.error)
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
declare function attemptAsync<T>(fn: () => Promise<T>): Promise<Result<T>>;
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region src/control/result.d.ts
|
|
78
|
+
/**
|
|
79
|
+
* Creates a success result wrapping the given value.
|
|
80
|
+
*
|
|
81
|
+
* @param value - The success value
|
|
82
|
+
* @returns An `Ok` result containing the value
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* const result = ok(42)
|
|
87
|
+
* // { ok: true, value: 42 }
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
declare function ok<T>(value: T): Ok<T>;
|
|
91
|
+
/**
|
|
92
|
+
* Creates a failure result wrapping the given error.
|
|
93
|
+
*
|
|
94
|
+
* @param error - The error value
|
|
95
|
+
* @returns An `Err` result containing the error
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* const result = err(new Error('fail'))
|
|
100
|
+
* // { ok: false, error: Error('fail') }
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
declare function err(error: unknown): Err;
|
|
104
|
+
/**
|
|
105
|
+
* Type guard that narrows a `Result` to `Ok`.
|
|
106
|
+
*
|
|
107
|
+
* @param result - The result to check
|
|
108
|
+
* @returns `true` if the result is `Ok`
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* const result = attempt(() => JSON.parse('{}'))
|
|
113
|
+
* if (isOk(result)) {
|
|
114
|
+
* console.log(result.value)
|
|
115
|
+
* }
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
declare function isOk<T>(result: Result<T>): result is Ok<T>;
|
|
119
|
+
/**
|
|
120
|
+
* Type guard that narrows a `Result` to `Err`.
|
|
121
|
+
*
|
|
122
|
+
* @param result - The result to check
|
|
123
|
+
* @returns `true` if the result is `Err`
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* const result = attempt(() => JSON.parse('bad'))
|
|
128
|
+
* if (isErr(result)) {
|
|
129
|
+
* console.error(result.error)
|
|
130
|
+
* }
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
declare function isErr<T>(result: Result<T>): result is Err;
|
|
134
|
+
/**
|
|
135
|
+
* Extract the value from an `Ok` result, or throw on `Err`.
|
|
136
|
+
*
|
|
137
|
+
* When called without a message, throws the original error.
|
|
138
|
+
* When called with a message, throws a new Error with that message
|
|
139
|
+
* and the original error as `cause` (like Rust's `expect`).
|
|
140
|
+
*
|
|
141
|
+
* @param result - The result to unwrap
|
|
142
|
+
* @param message - Optional custom error message (Rust `expect` behavior)
|
|
143
|
+
* @returns The unwrapped value
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* const value = unwrap(ok(42)) // 42
|
|
148
|
+
* unwrap(err('fail')) // throws Error('fail')
|
|
149
|
+
* unwrap(err('fail'), 'config required') // throws Error('config required', { cause: Error('fail') })
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
declare function unwrap<T>(result: Result<T>, message?: string): T;
|
|
153
|
+
//#endregion
|
|
154
|
+
export { isOk as a, attempt as c, Ok as d, Result as f, isErr as i, attemptAsync as l, invariant as n, ok as o, err as r, unwrap as s, assert as t, Err as u };
|
|
155
|
+
//# sourceMappingURL=index-Br9PO6mU.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-Br9PO6mU.d.mts","names":[],"sources":["../src/control/types.ts","../src/control/attempt.ts","../src/control/result.ts"],"mappings":";;;;;;AAQA;;;;;UAAiB,EAAA;EAAA,SACN,EAAA;EAAA,SACA,KAAA,EAAO,CAAA;EAAA,SACP,KAAA;AAAA;;AAWX;;;;;;;UAAiB,GAAA;EAAA,SACN,EAAA;EAAA,SACA,KAAA;EAAA,SACA,KAAA,EAAO,KAAA;AAAA;;;;;;;;;;;;KAcN,MAAA,MAAY,EAAA,CAAG,CAAA,IAAK,GAAA;;;;;AA/BhC;;;;;;;;;;;AAcA;;iBCJgB,OAAA,GAAA,CAAW,EAAA,QAAU,CAAA,GAAI,MAAA,CAAO,CAAA;;;;;;;;ADqBhD;;;;;;;;iBCEsB,YAAA,GAAA,CAAgB,EAAA,QAAU,OAAA,CAAQ,CAAA,IAAK,OAAA,CAAQ,MAAA,CAAO,CAAA;;;;;ADjC5E;;;;;;;;;;iBE6BgB,EAAA,GAAA,CAAM,KAAA,EAAO,CAAA,GAAI,EAAA,CAAG,CAAA;AFfpC;;;;;;;;;;AAiBA;;AAjBA,iBE+BgB,GAAA,CAAI,KAAA,YAAiB,GAAA;;;;;;;;;;;;;;;iBAkBrB,IAAA,GAAA,CAAQ,MAAA,EAAQ,MAAA,CAAO,CAAA,IAAK,MAAA,IAAU,EAAA,CAAG,CAAA;;;;;;;;;;;;;;;iBAkBzC,KAAA,GAAA,CAAS,MAAA,EAAQ,MAAA,CAAO,CAAA,IAAK,MAAA,IAAU,GAAA;;;;;;;;;;;;;;;;;;;iBAsBvC,MAAA,GAAA,CAAU,MAAA,EAAQ,MAAA,CAAO,CAAA,GAAI,OAAA,YAAmB,CAAA"}
|