lil-fp
Advanced tools
Comparing version 1.2.3 to 1.2.4
@@ -24,5 +24,7 @@ declare function pipe<A, B>(value: A, fn1: (arg: A) => B): B; | ||
declare const log: <T>(label: string, fn?: ((a: T) => any) | undefined) => (v: T) => T; | ||
declare const done: () => undefined; | ||
declare const cast: <T>(v: any) => T; | ||
declare const noop: () => undefined; | ||
declare const memo: <T extends (...args: any[]) => any>(fn: T) => T; | ||
declare const tryCatch: <T, U, K>(fn: (v: T) => U, onError?: ((err: unknown, v: T) => K) | undefined) => (v: T) => U | (K extends void ? undefined : K); | ||
export { done, flow, log, memo, pipe, tap }; | ||
export { cast, flow, log, memo, noop, pipe, tap, tryCatch }; |
@@ -20,8 +20,10 @@ "use strict"; | ||
__export(func_exports, { | ||
done: () => done, | ||
cast: () => cast, | ||
flow: () => flow, | ||
log: () => log, | ||
memo: () => memo, | ||
noop: () => noop, | ||
pipe: () => pipe, | ||
tap: () => tap | ||
tap: () => tap, | ||
tryCatch: () => tryCatch | ||
}); | ||
@@ -38,15 +40,23 @@ module.exports = __toCommonJS(func_exports); | ||
} | ||
var log = (label, fn) => (v) => (console.log(`${label}: `, fn?.(v) ?? v), v), done = () => { | ||
var log = (label, fn) => (v) => (console.log(`${label}: `, fn?.(v) ?? v), v), cast = (v) => v, noop = () => { | ||
}, memo = (fn) => { | ||
let cache = /* @__PURE__ */ Object.create(null); | ||
return (arg) => (cache[arg] === void 0 && (cache[arg] = fn(arg)), cache[arg]); | ||
}, tryCatch = (fn, onError) => (v) => { | ||
try { | ||
return fn(v); | ||
} catch (err) { | ||
return cast(onError?.(err, v)); | ||
} | ||
}; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
done, | ||
cast, | ||
flow, | ||
log, | ||
memo, | ||
noop, | ||
pipe, | ||
tap | ||
tap, | ||
tryCatch | ||
}); |
export { a as Arr } from './arr-baeb89ce.js'; | ||
export { b as Bool } from './bool-38e9ed29.js'; | ||
export { done, flow, log, memo, pipe, tap } from './func.js'; | ||
export { o as Obj } from './obj-34f402e3.js'; | ||
export { cast, flow, log, memo, noop, pipe, tap, tryCatch } from './func.js'; | ||
export { isArr, isBool, isFunc, isNum, isObj, isStr } from './is.js'; | ||
export { alt, match, otherwise, when } from './logic.js'; | ||
export { t as Task } from './task-125a47f8.js'; | ||
export { o as Obj } from './obj-0f35c0db.js'; | ||
export { o as Opt } from './option-116b88b4.js'; | ||
import './types.js'; |
@@ -23,5 +23,5 @@ "use strict"; | ||
Obj: () => obj_exports, | ||
Task: () => task_exports, | ||
Opt: () => option_exports, | ||
alt: () => alt, | ||
done: () => done, | ||
cast: () => cast, | ||
flow: () => flow, | ||
@@ -37,5 +37,7 @@ isArr: () => isArr, | ||
memo: () => memo, | ||
noop: () => noop, | ||
otherwise: () => otherwise, | ||
pipe: () => pipe, | ||
tap: () => tap, | ||
tryCatch: () => tryCatch, | ||
when: () => when | ||
@@ -92,8 +94,33 @@ }); | ||
} | ||
var log = (label, fn) => (v) => (console.log(`${label}: `, fn?.(v) ?? v), v), done = () => { | ||
var log = (label, fn) => (v) => (console.log(`${label}: `, fn?.(v) ?? v), v), cast = (v) => v, noop = () => { | ||
}, memo = (fn) => { | ||
let cache = /* @__PURE__ */ Object.create(null); | ||
return (arg) => (cache[arg] === void 0 && (cache[arg] = fn(arg)), cache[arg]); | ||
}, tryCatch = (fn, onError) => (v) => { | ||
try { | ||
return fn(v); | ||
} catch (err) { | ||
return cast(onError?.(err, v)); | ||
} | ||
}; | ||
// src/is.ts | ||
var isBool = (x) => typeof x == "boolean", isNum = (x) => typeof x == "number", isStr = (x) => typeof x == "string", isArr = (x) => Array.isArray(x), isObj = (x) => x !== null && typeof x == "object" && !isArr(x), isFunc = (x) => typeof x == "function"; | ||
// src/logic.ts | ||
var match = (...fns) => (v) => { | ||
for (let fn of fns) { | ||
let x = fn(v); | ||
if (x !== void 0) | ||
return x; | ||
} | ||
}; | ||
function when(predicate, fn) { | ||
return (v) => { | ||
if (predicate(v)) | ||
return fn(v); | ||
}; | ||
} | ||
var otherwise = (fn) => (v) => when(() => !0, fn)(v), alt = (u) => (v) => v === void 0 ? typeof u == "function" ? u() : u : v; | ||
// src/obj.ts | ||
@@ -105,3 +132,2 @@ var obj_exports = {}; | ||
bind: () => bind, | ||
cast: () => cast, | ||
clone: () => clone, | ||
@@ -122,7 +148,2 @@ compact: () => compact2, | ||
}); | ||
// src/is.ts | ||
var isBool = (x) => typeof x == "boolean", isNum = (x) => typeof x == "number", isStr = (x) => typeof x == "string", isArr = (x) => Array.isArray(x), isObj = (x) => x !== null && typeof x == "object" && !isArr(x), isFunc = (x) => typeof x == "function"; | ||
// src/obj.ts | ||
var fromEntries = (entries2) => Object.fromEntries(entries2), map2 = (f) => (obj) => fromEntries(Object.entries(obj).map(([k, v]) => f(k, v))), entries = (obj) => Object.entries(obj), from2 = (key) => (value) => fromEntries([[key, value]]); | ||
@@ -165,3 +186,3 @@ function assign(v) { | ||
return clone2; | ||
}, cast = (v) => v, pick = (keys2) => (obj) => { | ||
}, pick = (keys2) => (obj) => { | ||
let clone2 = {}; | ||
@@ -184,66 +205,28 @@ for (let i = 0; i < keys2.length; i++) | ||
// src/logic.ts | ||
var match = (...fns) => (v) => { | ||
for (let fn of fns) { | ||
let x = fn(v); | ||
if (x !== void 0) | ||
return x; | ||
} | ||
}; | ||
function when(predicate, fn) { | ||
return (v) => { | ||
if (predicate(v)) | ||
return fn(v); | ||
}; | ||
} | ||
var otherwise = (fn) => (v) => when(() => !0, fn)(v), alt = (u) => (v) => v === void 0 ? typeof u == "function" ? u() : u : v; | ||
// src/task.ts | ||
var task_exports = {}; | ||
__export(task_exports, { | ||
bind: () => bind2, | ||
bindTo: () => bindTo, | ||
filterOrElse: () => filterOrElse, | ||
fromCallback: () => fromCallback, | ||
fromPromise: () => fromPromise, | ||
// src/option.ts | ||
var option_exports = {}; | ||
__export(option_exports, { | ||
flatMap: () => flatMap, | ||
fromExecution: () => fromExecution, | ||
fromNullable: () => fromNullable, | ||
getOrElse: () => getOrElse, | ||
makeEmpty: () => makeEmpty, | ||
getOrThrow: () => getOrThrow, | ||
isNone: () => isNone, | ||
isSome: () => isSome, | ||
map: () => map3, | ||
tap: () => tap2, | ||
toError: () => toError, | ||
tryCatch: () => tryCatch | ||
match: () => match2, | ||
none: () => none, | ||
some: () => some2 | ||
}); | ||
var toError = (err2) => err2 instanceof Error ? err2 : new Error(err2), ok = (result) => ({ result, error: null }), err = (error) => ({ result: null, error }), toTask = (result, error) => error != null ? err(error) : ok(result), tryCatch = async (f, onReject) => { | ||
var none = { _tag: "None" }, some2 = (value) => ({ _tag: "Some", value }), fromNullable = (value) => value == null ? none : some2(value), fromExecution = (value) => { | ||
try { | ||
return ok(await f()); | ||
} catch (reason) { | ||
return err(onReject(reason)); | ||
return fromNullable(value()); | ||
} catch { | ||
return none; | ||
} | ||
}, fromPromise = async (promise) => tryCatch(() => promise, toError), fromCallback = async (f) => tryCatch( | ||
() => new Promise((res, rej) => { | ||
f((err2, result) => { | ||
err2 ? rej(err2) : res(result); | ||
}); | ||
}), | ||
toError | ||
), map3 = (f) => async (task) => { | ||
let { result, error } = await task; | ||
return toTask(await f(result), error); | ||
}, getOrElse = (f) => async (task) => pipe( | ||
task, | ||
map3((v) => v ?? f()) | ||
), filterOrElse = (predicate, onFalse) => async (task) => pipe( | ||
task, | ||
map3((v) => predicate(v) ? v : onFalse(v)) | ||
), tap2 = (f) => async (task) => pipe( | ||
task, | ||
map3((v) => (f(v), v)) | ||
), makeEmpty = () => Promise.resolve({}), bind2 = (key, f) => async (task) => pipe( | ||
task, | ||
map3(f), | ||
map3((v) => ({ ...v, [key]: v })) | ||
), bindTo = (key) => (task) => pipe( | ||
task, | ||
map3((v) => ({ [key]: v })) | ||
); | ||
}, isNone = (o) => o._tag === "None", isSome = (o) => o._tag === "Some", map3 = (f) => (o) => isNone(o) ? none : some2(f(o.value)), flatMap = (f) => (o) => isNone(o) ? none : f(o.value), getOrElse = (def) => (o) => isNone(o) ? def() : o.value, getOrThrow = (msg) => (o) => { | ||
if (isNone(o)) | ||
throw new Error(msg); | ||
return o.value; | ||
}, match2 = (onSome, onNone) => (o) => isNone(o) ? onNone() : onSome(o.value); | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -254,5 +237,5 @@ 0 && (module.exports = { | ||
Obj, | ||
Task, | ||
Opt, | ||
alt, | ||
done, | ||
cast, | ||
flow, | ||
@@ -268,6 +251,8 @@ isArr, | ||
memo, | ||
noop, | ||
otherwise, | ||
pipe, | ||
tap, | ||
tryCatch, | ||
when | ||
}); |
import './types.js'; | ||
export { b as assign, c as assignTo, n as bind, l as cast, h as clone, g as compact, q as defaults, e as entries, d as filter, a as from, f as fromEntries, r as get, k as keys, m as map, i as merge, j as omit, p as pick, s as split } from './obj-34f402e3.js'; | ||
export { b as assign, c as assignTo, l as bind, h as clone, g as compact, n as defaults, e as entries, d as filter, a as from, f as fromEntries, q as get, k as keys, m as map, i as merge, j as omit, p as pick, s as split } from './obj-0f35c0db.js'; |
@@ -23,3 +23,2 @@ "use strict"; | ||
bind: () => bind, | ||
cast: () => cast, | ||
clone: () => clone, | ||
@@ -42,2 +41,5 @@ compact: () => compact, | ||
// src/func.ts | ||
var cast = (v) => v; | ||
// src/is.ts | ||
@@ -84,3 +86,3 @@ var isArr = (x) => Array.isArray(x), isObj = (x) => x !== null && typeof x == "object" && !isArr(x), isFunc = (x) => typeof x == "function"; | ||
return clone2; | ||
}, cast = (v) => v, pick = (keys2) => (obj) => { | ||
}, pick = (keys2) => (obj) => { | ||
let clone2 = {}; | ||
@@ -107,3 +109,2 @@ for (let i = 0; i < keys2.length; i++) | ||
bind, | ||
cast, | ||
clone, | ||
@@ -110,0 +111,0 @@ compact, |
{ | ||
"name": "lil-fp", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "Functional programming utilities for TypeScript", | ||
@@ -44,9 +44,37 @@ "main": "dist/index.js", | ||
}, | ||
"./task": { | ||
"types": "./dist/task.d.ts", | ||
"import": "./dist/task.mjs", | ||
"require": "./dist/task.js" | ||
"./option": { | ||
"types": "./dist/option.d.ts", | ||
"import": "./dist/option.mjs", | ||
"require": "./dist/option.js" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
".": [ | ||
"dist/index.d.ts" | ||
], | ||
"arr": [ | ||
"dist/arr.d.ts" | ||
], | ||
"bool": [ | ||
"dist/bool.d.ts" | ||
], | ||
"func": [ | ||
"dist/func.d.ts" | ||
], | ||
"logic": [ | ||
"dist/logic.d.ts" | ||
], | ||
"is": [ | ||
"dist/is.d.ts" | ||
], | ||
"obj": [ | ||
"dist/obj.d.ts" | ||
], | ||
"option": [ | ||
"dist/option.d.ts" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
@@ -53,0 +81,0 @@ "dist", |
@@ -197,4 +197,6 @@ export function pipe<A, B>(value: A, fn1: (arg: A) => B): B | ||
export const done = () => void 0 | ||
export const cast = <T>(v: any): T => v | ||
export const noop = () => void 0 | ||
export const memo = <T extends (...args: any[]) => any>(fn: T): T => { | ||
@@ -208,1 +210,11 @@ const cache = Object.create(null) | ||
} | ||
export const tryCatch = | ||
<T, U, K>(fn: (v: T) => U, onError?: (err: unknown, v: T) => K) => | ||
(v: T): U | (K extends void ? undefined : K) => { | ||
try { | ||
return fn(v) | ||
} catch (err) { | ||
return cast(onError?.(err, v)) | ||
} | ||
} |
export * as Arr from './arr' | ||
export * as Bool from './bool' | ||
export * from './func' | ||
export * as Obj from './obj' | ||
export * from './is' | ||
export * from './logic' | ||
export * as Task from './task' | ||
export * as Obj from './obj' | ||
export * as Opt from './option' |
@@ -0,1 +1,2 @@ | ||
import { cast } from './func' | ||
import { isFunc, isObj } from './is' | ||
@@ -121,4 +122,2 @@ import { | ||
export const cast = <T>(v: any): T => v | ||
export const pick = | ||
@@ -125,0 +124,0 @@ <T extends Dict, K extends keyof T>(keys: K[]) => |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
70762
2066