Comparing version 1.4.2 to 1.4.4
@@ -1,1 +0,1 @@ | ||
export { e as at, k as compact, j as concat, g as every, b as filter, c as filterMap, l as flat, d as forEach, f as from, i as has, h as head, m as map, n as next, p as prev, r as reduce, s as some, t as tail, u as uniq } from './arr-61789cba.js'; | ||
export { e as at, k as compact, j as concat, g as every, b as filter, c as filterMap, l as flat, d as forEach, f as from, i as has, h as head, m as map, n as next, p as prev, r as reduce, s as some, t as tail, u as uniq } from './arr-b35d40c1.js'; |
@@ -42,4 +42,4 @@ "use strict"; | ||
let x = f(v, index, arr2); | ||
return x === void 0 ? acc : [...acc, x]; | ||
}, []), forEach = (f) => (arr) => arr.forEach(f), next = (i, loop) => (arr) => arr[(i + 1) % (loop ? arr.length : arr.length - 1)], prev = (i, loop) => (arr) => arr[(i - 1 + (loop ? arr.length : 0)) % arr.length], head = (arr) => arr[0], tail = (arr) => arr[arr.length - 1], at = (i) => (arr) => arr.at(i), uniq = (arr) => [...new Set(arr)], some = (f) => (arr) => arr.some(f), every = (f) => (arr) => arr.every(f), has = (v) => (arr) => arr.includes(v), concat = (...others) => (v) => v.concat(...others), compact = (arr) => arr.filter((v) => v !== void 0), flat = (arr) => arr.flat(); | ||
return x == null ? acc : [...acc, x]; | ||
}, []), forEach = (f) => (arr) => arr.forEach(f), next = (i, loop) => (arr) => arr[(i + 1) % (loop ? arr.length : arr.length - 1)], prev = (i, loop) => (arr) => arr[(i - 1 + (loop ? arr.length : 0)) % arr.length], head = (arr) => arr[0], tail = (arr) => arr[arr.length - 1], at = (i) => (arr) => arr.at(i), uniq = (arr) => [...new Set(arr)], some = (f) => (arr) => arr.some(f), every = (f) => (arr) => arr.every(f), has = (v) => (arr) => arr.includes(v), concat = (...others) => (v) => v.concat(...others), compact = (arr) => arr.filter((v) => v != null), flat = () => (arr) => arr.flat(); | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -46,0 +46,0 @@ 0 && (module.exports = { |
@@ -1,1 +0,1 @@ | ||
export { a as and, n as not, o as or } from './bool-729d4744.js'; | ||
export { a as and, n as not, o as or } from './bool-55837223.js'; |
@@ -0,1 +1,4 @@ | ||
/** | ||
* Performs left-to-right function composition (the first argument must be a value). | ||
*/ | ||
declare function pipe<A, B>(value: A, fn1: (arg: A) => B): B; | ||
@@ -13,2 +16,5 @@ declare function pipe<A, B, C>(value: A, fn1: (arg: A) => B, fn2: (arg: B) => C): C; | ||
declare function pipe<A, B, C, D, E, F, G, H, I, J, K, L, M>(value: A, fn1: (arg: A) => B, fn2: (arg: B) => C, fn3: (arg: C) => D, fn4: (arg: D) => E, fn5: (arg: E) => F, fn6: (arg: F) => G, fn7: (arg: G) => H, fn8: (arg: H) => I, fn9: (arg: I) => J, fn10: (arg: J) => K, fn11: (arg: K) => L, fn12: (arg: L) => M): M; | ||
/** | ||
* Performs left-to-right function composition and returns a new function, the first argument may have any arity, the remaining arguments must be unary. | ||
*/ | ||
declare function flow<A extends any[], B>(fn1: (...args: A) => B): (...args: A) => B; | ||
@@ -23,10 +29,31 @@ declare function flow<A extends any[], B, C>(fn1: (...args: A) => B, fn2: (arg: B) => C): (...args: A) => C; | ||
declare function flow<A extends any[], B, C, D, E, F, G, H, I, J>(fn1: (...args: A) => B, fn2: (arg: B) => C, fn3: (arg: C) => D, fn4: (arg: D) => E, fn5: (arg: E) => F, fn6: (arg: F) => G, fn7: (arg: G) => H, fn8: (arg: H) => I, fn9: (arg: I) => J): (...args: A) => J; | ||
/** | ||
* Invokes the given function with the given value, and then returns the same value. | ||
*/ | ||
declare function tap<T>(fn: (value: T) => void): (value: T) => T; | ||
/** | ||
* Useful for logging values in a pipeline | ||
*/ | ||
declare const log: <T>(label: string, fn?: ((a: T) => any) | undefined) => (v: T) => T; | ||
/** | ||
* Casts a value to a specific type | ||
*/ | ||
declare const cast: <T>(v: any) => T; | ||
/** | ||
* Empty, no-op function | ||
*/ | ||
declare const noop: () => undefined; | ||
/** | ||
* Identity function | ||
*/ | ||
declare const identity: <T>(v: T) => T; | ||
/** | ||
* Returns a function with memoized results | ||
*/ | ||
declare const memo: <T extends (...args: any[]) => any>(fn: T) => T; | ||
/** | ||
* Run a execution in a try/catch block, and return the result of the function or the result of the onError function | ||
*/ | ||
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 { cast, flow, identity, log, memo, noop, pipe, tap, tryCatch }; |
@@ -1,8 +0,8 @@ | ||
export { a as Arr } from './arr-61789cba.js'; | ||
export { b as Bool } from './bool-729d4744.js'; | ||
export { a as Arr } from './arr-b35d40c1.js'; | ||
export { b as Bool } from './bool-55837223.js'; | ||
export { cast, flow, identity, log, memo, noop, pipe, tap, tryCatch } from './func.js'; | ||
export { isArr, isBool, isFunc, isNum, isObj, isStr } from './is.js'; | ||
export { match, orElse, otherwise, when } from './logic.js'; | ||
export { o as Obj } from './obj-40a953aa.js'; | ||
export { o as Opt } from './opt-2faa0ef3.js'; | ||
export { o as Obj } from './obj-90c07fec.js'; | ||
export { o as Opt } from './opt-27fbc4b4.js'; | ||
import './types.js'; |
@@ -9,6 +9,6 @@ "use strict"; | ||
__defProp(target, name, { get: all[name], enumerable: !0 }); | ||
}, __copyProps = (to, from4, except, desc) => { | ||
if (from4 && typeof from4 == "object" || typeof from4 == "function") | ||
for (let key of __getOwnPropNames(from4)) | ||
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from4[key], enumerable: !(desc = __getOwnPropDesc(from4, key)) || desc.enumerable }); | ||
}, __copyProps = (to, from2, except, desc) => { | ||
if (from2 && typeof from2 == "object" || typeof from2 == "function") | ||
for (let key of __getOwnPropNames(from2)) | ||
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from2[key], enumerable: !(desc = __getOwnPropDesc(from2, key)) || desc.enumerable }); | ||
return to; | ||
@@ -71,4 +71,4 @@ }; | ||
let x = f(v, index, arr2); | ||
return x === void 0 ? acc : [...acc, x]; | ||
}, []), forEach = (f) => (arr) => arr.forEach(f), next = (i, loop) => (arr) => arr[(i + 1) % (loop ? arr.length : arr.length - 1)], prev = (i, loop) => (arr) => arr[(i - 1 + (loop ? arr.length : 0)) % arr.length], head = (arr) => arr[0], tail = (arr) => arr[arr.length - 1], at = (i) => (arr) => arr.at(i), uniq = (arr) => [...new Set(arr)], some = (f) => (arr) => arr.some(f), every = (f) => (arr) => arr.every(f), has = (v) => (arr) => arr.includes(v), concat = (...others) => (v) => v.concat(...others), compact = (arr) => arr.filter((v) => v !== void 0), flat = (arr) => arr.flat(); | ||
return x == null ? acc : [...acc, x]; | ||
}, []), forEach = (f) => (arr) => arr.forEach(f), next = (i, loop) => (arr) => arr[(i + 1) % (loop ? arr.length : arr.length - 1)], prev = (i, loop) => (arr) => arr[(i - 1 + (loop ? arr.length : 0)) % arr.length], head = (arr) => arr[0], tail = (arr) => arr[arr.length - 1], at = (i) => (arr) => arr.at(i), uniq = (arr) => [...new Set(arr)], some = (f) => (arr) => arr.some(f), every = (f) => (arr) => arr.every(f), has = (v) => (arr) => arr.includes(v), concat = (...others) => (v) => v.concat(...others), compact = (arr) => arr.filter((v) => v != null), flat = () => (arr) => arr.flat(); | ||
@@ -139,4 +139,2 @@ // src/bool.ts | ||
assignTo: () => assignTo, | ||
bind: () => bind, | ||
clone: () => clone, | ||
compact: () => compact2, | ||
@@ -146,3 +144,2 @@ defaults: () => defaults, | ||
filter: () => filter2, | ||
from: () => from2, | ||
fromEntries: () => fromEntries, | ||
@@ -157,3 +154,3 @@ get: () => get, | ||
}); | ||
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]]); | ||
var fromEntries = (entries2) => Object.fromEntries(entries2), map2 = (f) => (obj) => fromEntries(Object.entries(obj).map(([k, v]) => f(k, v))), entries = (obj) => Object.entries(obj); | ||
function assign(v) { | ||
@@ -165,13 +162,13 @@ return (obj) => ({ | ||
} | ||
var assignTo = (key) => (prev2) => fromEntries([[key, prev2]]), keys = (obj) => Object.keys(obj), filter2 = (f) => (obj) => fromEntries(Object.entries(obj).filter(([k, v]) => f(k, v))), compact2 = (obj) => filter2((_, v) => v !== void 0)(obj), split = (...keys2) => (obj) => { | ||
var assignTo = (key) => (prev2) => fromEntries([[key, prev2]]), keys = (obj) => Object.keys(obj), filter2 = (f) => (obj) => fromEntries(Object.entries(obj).filter(([k, v]) => f(k, v))), compact2 = (obj) => filter2((_, v) => v != null)(obj), split = (...keys2) => (obj) => { | ||
let descriptors = Object.getOwnPropertyDescriptors(obj), dKeys = Object.keys(descriptors), split2 = (k) => { | ||
let clone2 = {}; | ||
let clone = {}; | ||
for (let i = 0; i < k.length; i++) { | ||
let key = k[i]; | ||
descriptors[key] && (Object.defineProperty(clone2, key, descriptors[key]), delete descriptors[key]); | ||
descriptors[key] && (Object.defineProperty(clone, key, descriptors[key]), delete descriptors[key]); | ||
} | ||
return clone2; | ||
return clone; | ||
}, fn = (key) => split2(Array.isArray(key) ? key : dKeys.filter(key)); | ||
return keys2.map(fn).concat(split2(dKeys)); | ||
}, clone = (obj) => structuredClone(obj); | ||
}; | ||
function merge(...args) { | ||
@@ -192,15 +189,12 @@ return (obj) => { | ||
var omit = (keys2) => (obj) => { | ||
let clone2 = { ...obj }; | ||
let clone = { ...obj }; | ||
for (let i = 0; i < keys2.length; i++) | ||
delete clone2[keys2[i]]; | ||
return clone2; | ||
delete clone[keys2[i]]; | ||
return clone; | ||
}, pick = (keys2) => (obj) => { | ||
let clone2 = {}; | ||
let clone = {}; | ||
for (let i = 0; i < keys2.length; i++) | ||
clone2[keys2[i]] = obj[keys2[i]]; | ||
return clone2; | ||
}, bind = (key, fn) => (obj) => cast({ | ||
...obj, | ||
[key]: fn(obj) | ||
}), defaults = (defaults2) => (obj) => cast({ | ||
clone[keys2[i]] = obj[keys2[i]]; | ||
return clone; | ||
}, defaults = (defaults2) => (obj) => cast({ | ||
...defaults2, | ||
@@ -226,5 +220,5 @@ ...obj | ||
flatMap: () => flatMap, | ||
from: () => from3, | ||
fromExecution: () => fromExecution, | ||
fromNullable: () => fromNullable, | ||
fromPredicate: () => fromPredicate, | ||
getOrElse: () => getOrElse, | ||
@@ -241,3 +235,7 @@ getOrThrow: () => getOrThrow, | ||
}); | ||
var none = Object.freeze({ _tag: "None" }), some2 = (value) => Object.freeze({ _tag: "Some", value }), from3 = (value) => some2(value), fromNullable = (value) => value == null ? none : some2(value), fromExecution = (value) => { | ||
var none = Object.freeze({ _tag: "None" }), some2 = (value) => Object.freeze({ _tag: "Some", value }); | ||
function fromPredicate(predicate) { | ||
return (v) => predicate(v) ? some2(v) : none; | ||
} | ||
var fromNullable = (value) => value == null ? none : some2(value), fromExecution = (value) => { | ||
try { | ||
@@ -244,0 +242,0 @@ return fromNullable(value()); |
@@ -0,8 +1,26 @@ | ||
/** | ||
* Whether the value is a boolean | ||
*/ | ||
declare const isBool: (x: any) => x is boolean; | ||
/** | ||
* Whether the value is a number | ||
*/ | ||
declare const isNum: (x: any) => x is number; | ||
/** | ||
* Whether the value is a string | ||
*/ | ||
declare const isStr: (x: any) => x is string; | ||
/** | ||
* Whether the value is an array | ||
*/ | ||
declare const isArr: (x: any) => x is any[]; | ||
/** | ||
* Whether the value is an object | ||
*/ | ||
declare const isObj: (x: any) => x is Record<string, any>; | ||
/** | ||
* Whether the value is a function | ||
*/ | ||
declare const isFunc: (x: any) => x is Function; | ||
export { isArr, isBool, isFunc, isNum, isObj, isStr }; |
import './types.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-40a953aa.js'; | ||
export { a as assign, b as assignTo, d as compact, i as defaults, e as entries, c as filter, f as fromEntries, j as get, k as keys, m as map, g as merge, h as omit, p as pick, s as split } from './obj-90c07fec.js'; |
@@ -9,6 +9,6 @@ "use strict"; | ||
__defProp(target, name, { get: all[name], enumerable: !0 }); | ||
}, __copyProps = (to, from2, except, desc) => { | ||
if (from2 && typeof from2 == "object" || typeof from2 == "function") | ||
for (let key of __getOwnPropNames(from2)) | ||
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from2[key], enumerable: !(desc = __getOwnPropDesc(from2, key)) || desc.enumerable }); | ||
}, __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from == "object" || typeof from == "function") | ||
for (let key of __getOwnPropNames(from)) | ||
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
return to; | ||
@@ -23,4 +23,2 @@ }; | ||
assignTo: () => assignTo, | ||
bind: () => bind, | ||
clone: () => clone, | ||
compact: () => compact, | ||
@@ -30,3 +28,2 @@ defaults: () => defaults, | ||
filter: () => filter, | ||
from: () => from, | ||
fromEntries: () => fromEntries, | ||
@@ -50,3 +47,3 @@ get: () => get, | ||
// src/obj.ts | ||
var fromEntries = (entries2) => Object.fromEntries(entries2), map = (f) => (obj) => fromEntries(Object.entries(obj).map(([k, v]) => f(k, v))), entries = (obj) => Object.entries(obj), from = (key) => (value) => fromEntries([[key, value]]); | ||
var fromEntries = (entries2) => Object.fromEntries(entries2), map = (f) => (obj) => fromEntries(Object.entries(obj).map(([k, v]) => f(k, v))), entries = (obj) => Object.entries(obj); | ||
function assign(v) { | ||
@@ -58,13 +55,13 @@ return (obj) => ({ | ||
} | ||
var assignTo = (key) => (prev) => fromEntries([[key, prev]]), keys = (obj) => Object.keys(obj), filter = (f) => (obj) => fromEntries(Object.entries(obj).filter(([k, v]) => f(k, v))), compact = (obj) => filter((_, v) => v !== void 0)(obj), split = (...keys2) => (obj) => { | ||
var assignTo = (key) => (prev) => fromEntries([[key, prev]]), keys = (obj) => Object.keys(obj), filter = (f) => (obj) => fromEntries(Object.entries(obj).filter(([k, v]) => f(k, v))), compact = (obj) => filter((_, v) => v != null)(obj), split = (...keys2) => (obj) => { | ||
let descriptors = Object.getOwnPropertyDescriptors(obj), dKeys = Object.keys(descriptors), split2 = (k) => { | ||
let clone2 = {}; | ||
let clone = {}; | ||
for (let i = 0; i < k.length; i++) { | ||
let key = k[i]; | ||
descriptors[key] && (Object.defineProperty(clone2, key, descriptors[key]), delete descriptors[key]); | ||
descriptors[key] && (Object.defineProperty(clone, key, descriptors[key]), delete descriptors[key]); | ||
} | ||
return clone2; | ||
return clone; | ||
}, fn = (key) => split2(Array.isArray(key) ? key : dKeys.filter(key)); | ||
return keys2.map(fn).concat(split2(dKeys)); | ||
}, clone = (obj) => structuredClone(obj); | ||
}; | ||
function merge(...args) { | ||
@@ -85,15 +82,12 @@ return (obj) => { | ||
var omit = (keys2) => (obj) => { | ||
let clone2 = { ...obj }; | ||
let clone = { ...obj }; | ||
for (let i = 0; i < keys2.length; i++) | ||
delete clone2[keys2[i]]; | ||
return clone2; | ||
delete clone[keys2[i]]; | ||
return clone; | ||
}, pick = (keys2) => (obj) => { | ||
let clone2 = {}; | ||
let clone = {}; | ||
for (let i = 0; i < keys2.length; i++) | ||
clone2[keys2[i]] = obj[keys2[i]]; | ||
return clone2; | ||
}, bind = (key, fn) => (obj) => cast({ | ||
...obj, | ||
[key]: fn(obj) | ||
}), defaults = (defaults2) => (obj) => cast({ | ||
clone[keys2[i]] = obj[keys2[i]]; | ||
return clone; | ||
}, defaults = (defaults2) => (obj) => cast({ | ||
...defaults2, | ||
@@ -117,4 +111,2 @@ ...obj | ||
assignTo, | ||
bind, | ||
clone, | ||
compact, | ||
@@ -124,3 +116,2 @@ defaults, | ||
filter, | ||
from, | ||
fromEntries, | ||
@@ -127,0 +118,0 @@ get, |
@@ -1,1 +0,1 @@ | ||
export { k as filter, d as flatMap, f as from, b as fromExecution, a as fromNullable, g as getOrElse, e as getOrThrow, i as isNone, c as isSome, m as map, h as match, n as none, j as orElse, s as some, t as tap } from './opt-2faa0ef3.js'; | ||
export { k as filter, d as flatMap, b as fromExecution, a as fromNullable, f as fromPredicate, g as getOrElse, e as getOrThrow, i as isNone, c as isSome, m as map, h as match, n as none, j as orElse, s as some, t as tap } from './opt-27fbc4b4.js'; |
@@ -9,6 +9,6 @@ "use strict"; | ||
__defProp(target, name, { get: all[name], enumerable: !0 }); | ||
}, __copyProps = (to, from2, except, desc) => { | ||
if (from2 && typeof from2 == "object" || typeof from2 == "function") | ||
for (let key of __getOwnPropNames(from2)) | ||
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from2[key], enumerable: !(desc = __getOwnPropDesc(from2, key)) || desc.enumerable }); | ||
}, __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from == "object" || typeof from == "function") | ||
for (let key of __getOwnPropNames(from)) | ||
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
return to; | ||
@@ -23,5 +23,5 @@ }; | ||
flatMap: () => flatMap, | ||
from: () => from, | ||
fromExecution: () => fromExecution, | ||
fromNullable: () => fromNullable, | ||
fromPredicate: () => fromPredicate, | ||
getOrElse: () => getOrElse, | ||
@@ -39,3 +39,7 @@ getOrThrow: () => getOrThrow, | ||
module.exports = __toCommonJS(opt_exports); | ||
var none = Object.freeze({ _tag: "None" }), some = (value) => Object.freeze({ _tag: "Some", value }), from = (value) => some(value), fromNullable = (value) => value == null ? none : some(value), fromExecution = (value) => { | ||
var none = Object.freeze({ _tag: "None" }), some = (value) => Object.freeze({ _tag: "Some", value }); | ||
function fromPredicate(predicate) { | ||
return (v) => predicate(v) ? some(v) : none; | ||
} | ||
var fromNullable = (value) => value == null ? none : some(value), fromExecution = (value) => { | ||
try { | ||
@@ -58,5 +62,5 @@ return fromNullable(value()); | ||
flatMap, | ||
from, | ||
fromExecution, | ||
fromNullable, | ||
fromPredicate, | ||
getOrElse, | ||
@@ -63,0 +67,0 @@ getOrThrow, |
@@ -14,2 +14,3 @@ type Dict = Record<string, any>; | ||
type Awaited<T> = T extends undefined ? T : T extends PromiseLike<infer U> ? U : T; | ||
type Defaults<T extends Dict, K extends Partial<T>> = Simplify<Omit<T, keyof K> & Required<K>>; | ||
type Simplify<T> = T extends any ? { | ||
@@ -41,2 +42,2 @@ [K in keyof T]: T[K]; | ||
export { ArrayPath, Assign, Awaited, Compact, Dict, Path, PathConcat, PathValue, Primitive, PromiseFactory, Simplify, Split, SplitProp }; | ||
export { ArrayPath, Assign, Awaited, Compact, Defaults, Dict, Path, PathConcat, PathValue, Primitive, PromiseFactory, Simplify, Split, SplitProp }; |
{ | ||
"name": "lil-fp", | ||
"version": "1.4.2", | ||
"version": "1.4.4", | ||
"description": "Functional programming utilities for TypeScript", | ||
@@ -105,5 +105,5 @@ "main": "dist/index.js", | ||
"clean": "rm -rf dist", | ||
"release": "pnpm changeset version && pnpm changeset publish", | ||
"release": "pnpm changeset version && pnpm publish --no-git-checks", | ||
"prerelease": "pnpm build" | ||
} | ||
} |
@@ -1,5 +0,10 @@ | ||
export const from = <T>( | ||
v: IterableIterator<T> | Iterable<T> | Set<T> | T[] | ||
): T[] => Array.from(v) | ||
/** | ||
* Creates an array from an iterable object. | ||
*/ | ||
export const from = <T>(v: Iterable<T> | ArrayLike<T> | T[]): T[] => | ||
Array.from(v) | ||
/** | ||
* Returns the elements of an array that meet the condition specified in a callback function. | ||
*/ | ||
export const filter = | ||
@@ -10,2 +15,5 @@ <T>(f: (v: T, index: number, array: T[]) => boolean) => | ||
/** | ||
* Calls a defined callback function on each element of an array, and returns an array that contains the results. | ||
*/ | ||
export const map = | ||
@@ -16,2 +24,8 @@ <T, U>(f: (v: T, index: number, array: T[]) => U) => | ||
/** | ||
* Calls the specified callback function for all the elements in an array. | ||
* | ||
* The return value of the callback function is the accumulated result, | ||
* and is provided as an argument in the next call to the callback function. | ||
*/ | ||
export const reduce = | ||
@@ -22,10 +36,17 @@ <T, U>(f: (acc: U, v: T, index: number, array: T[]) => U, init: U) => | ||
/** | ||
* Calls the specified callback function for all the elements in an array. | ||
* The return value is the non-nullable result of the callback function. | ||
*/ | ||
export const filterMap = | ||
<T, U>(f: (v: T, index: number, array: T[]) => U | undefined) => | ||
<T, U>(f: (v: T, index: number, array: T[]) => U | undefined | null) => | ||
(arr: T[]): U[] => | ||
arr.reduce((acc, v, index, arr) => { | ||
const x = f(v, index, arr) | ||
return x === undefined ? acc : [...acc, x] | ||
return x == null ? acc : [...acc, x] | ||
}, [] as U[]) | ||
/** | ||
* Performs the specified action for each element in an array. | ||
*/ | ||
export const forEach = | ||
@@ -36,2 +57,5 @@ <T>(f: (v: T, index: number, array: T[]) => void) => | ||
/** | ||
* Returns the next element of an array based on the current index. | ||
*/ | ||
export const next = | ||
@@ -42,2 +66,5 @@ <T>(i: number, loop: boolean) => | ||
/** | ||
* Returns the previous element of an array based on the current index. | ||
*/ | ||
export const prev = | ||
@@ -48,6 +75,15 @@ <T>(i: number, loop: boolean) => | ||
/** | ||
* Returns the first element of an array. | ||
*/ | ||
export const head = <T>(arr: T[]): T | undefined => arr[0] | ||
/** | ||
* Returns the last element of an array. | ||
*/ | ||
export const tail = <T>(arr: T[]): T | undefined => arr[arr.length - 1] | ||
/** | ||
* Returns the element of an array at the specified index. | ||
*/ | ||
export const at = | ||
@@ -58,4 +94,10 @@ <T>(i: number) => | ||
/** | ||
* Returns the unique elements of an array. | ||
*/ | ||
export const uniq = <T>(arr: T[]): T[] => [...new Set(arr)] | ||
/** | ||
* Determines whether the specified callback function returns true for any element of an array. | ||
*/ | ||
export const some = | ||
@@ -66,2 +108,5 @@ <T>(f: (v: T, index: number, array: T[]) => boolean) => | ||
/** | ||
* Determines whether the specified callback function returns true for all elements of an array. | ||
*/ | ||
export const every = | ||
@@ -72,2 +117,5 @@ <T>(f: (v: T, index: number, array: T[]) => boolean) => | ||
/** | ||
* Determines whether an array includes a certain element | ||
*/ | ||
export const has = | ||
@@ -78,2 +126,5 @@ <T>(v: T) => | ||
/** | ||
* Combines two or more arrays | ||
*/ | ||
export const concat = | ||
@@ -84,5 +135,14 @@ <T>(...others: T[][]) => | ||
export const compact = <T>(arr: (T | undefined)[]): T[] => | ||
arr.filter((v) => v !== undefined) as T[] | ||
/** | ||
* Removes nullish values from an array | ||
*/ | ||
export const compact = <T>(arr: (T | null | undefined)[]): T[] => | ||
arr.filter((v) => v != null) as T[] | ||
export const flat = <T>(arr: T[][]): T[] => arr.flat() | ||
/** | ||
* Flattens an array of arrays | ||
*/ | ||
export const flat = | ||
<T>() => | ||
(arr: T[][]) => | ||
arr.flat() |
@@ -0,1 +1,4 @@ | ||
/** | ||
* Combines multiple boolean predicates using OR → a || b. | ||
*/ | ||
export function or<A, B extends A, C extends A>( | ||
@@ -20,2 +23,5 @@ a: (v: A) => v is B, | ||
/** | ||
* Inverts a boolean predicate. | ||
*/ | ||
export const not = | ||
@@ -26,2 +32,5 @@ <T>(predicate: (arg: T) => boolean): ((arg: T) => boolean) => | ||
/** | ||
* Combines multiple boolean predicates using AND → a && b. | ||
*/ | ||
export function and<A, B extends A, C extends A, D extends A, E extends A>( | ||
@@ -28,0 +37,0 @@ a: (v: A) => v is B, |
@@ -0,1 +1,4 @@ | ||
/** | ||
* Performs left-to-right function composition (the first argument must be a value). | ||
*/ | ||
export function pipe<A, B>(value: A, fn1: (arg: A) => B): B | ||
@@ -116,2 +119,5 @@ export function pipe<A, B, C>( | ||
/** | ||
* Performs left-to-right function composition and returns a new function, the first argument may have any arity, the remaining arguments must be unary. | ||
*/ | ||
export function flow<A extends any[], B>( | ||
@@ -184,2 +190,5 @@ fn1: (...args: A) => B | ||
/** | ||
* Invokes the given function with the given value, and then returns the same value. | ||
*/ | ||
export function tap<T>(fn: (value: T) => void) { | ||
@@ -192,2 +201,5 @@ return (value: T) => { | ||
/** | ||
* Useful for logging values in a pipeline | ||
*/ | ||
export const log = | ||
@@ -200,8 +212,20 @@ <T>(label: string, fn?: (a: T) => any) => | ||
/** | ||
* Casts a value to a specific type | ||
*/ | ||
export const cast = <T>(v: any): T => v | ||
/** | ||
* Empty, no-op function | ||
*/ | ||
export const noop = () => void 0 | ||
/** | ||
* Identity function | ||
*/ | ||
export const identity = <T>(v: T) => v | ||
/** | ||
* Returns a function with memoized results | ||
*/ | ||
export const memo = <T extends (...args: any[]) => any>(fn: T): T => { | ||
@@ -216,2 +240,5 @@ const cache = Object.create(null) | ||
/** | ||
* Run a execution in a try/catch block, and return the result of the function or the result of the onError function | ||
*/ | ||
export const tryCatch = | ||
@@ -218,0 +245,0 @@ <T, U, K>(fn: (v: T) => U, onError?: (err: unknown, v: T) => K) => |
@@ -0,7 +1,30 @@ | ||
/** | ||
* Whether the value is a boolean | ||
*/ | ||
export const isBool = (x: any): x is boolean => typeof x === 'boolean' | ||
/** | ||
* Whether the value is a number | ||
*/ | ||
export const isNum = (x: any): x is number => typeof x === 'number' | ||
/** | ||
* Whether the value is a string | ||
*/ | ||
export const isStr = (x: any): x is string => typeof x === 'string' | ||
/** | ||
* Whether the value is an array | ||
*/ | ||
export const isArr = (x: any): x is any[] => Array.isArray(x) | ||
/** | ||
* Whether the value is an object | ||
*/ | ||
export const isObj = (x: any): x is Record<string, any> => | ||
x !== null && typeof x === 'object' && !isArr(x) | ||
/** | ||
* Whether the value is a function | ||
*/ | ||
export const isFunc = (x: any): x is Function => typeof x === 'function' |
@@ -20,3 +20,5 @@ export const match = <T, R>(...fns: ((v: T) => R)[]) => { | ||
return (v: any) => { | ||
if (predicate(v)) return fn(v) | ||
if (predicate(v)) { | ||
return fn(v) | ||
} | ||
} | ||
@@ -23,0 +25,0 @@ } |
@@ -6,2 +6,3 @@ import { cast } from './func' | ||
Compact, | ||
Defaults, | ||
Dict, | ||
@@ -14,2 +15,5 @@ Path, | ||
/** | ||
* Returns an object created by key-value entries for properties and methods | ||
*/ | ||
export const fromEntries = <T extends Dict>( | ||
@@ -21,2 +25,5 @@ entries: | ||
/** | ||
* Returns a new object with the key-value pairs from the original object, but the keys transformed by the given function. | ||
*/ | ||
export const map = | ||
@@ -29,10 +36,11 @@ <T extends Dict>( | ||
/** | ||
* Returns the entries of an object as an array of key-value pairs | ||
*/ | ||
export const entries = <T extends Dict>(obj: T): [keyof T, T[keyof T]][] => | ||
Object.entries(obj) | ||
export const from = | ||
<K extends string, T>(key: K) => | ||
(value: T): Record<K, T> => | ||
fromEntries([[key, value]]) | ||
/** | ||
* Assign object properties to an existing object in a pipeline | ||
*/ | ||
export function assign<T extends Dict, K extends Dict>( | ||
@@ -48,2 +56,5 @@ v: K | ((obj: T) => K) | ||
/** | ||
* Assigns a value in a pipeline to a key in an object | ||
*/ | ||
export const assignTo = | ||
@@ -54,5 +65,11 @@ <K extends string, T extends Dict>(key: K) => | ||
/** | ||
* Returns the keys of an object | ||
*/ | ||
export const keys = <T extends Dict>(obj: T): (keyof T)[] => | ||
Object.keys(obj) as string[] | ||
/** | ||
* Filters an object entries by a predicate function | ||
*/ | ||
export const filter = | ||
@@ -63,5 +80,11 @@ <T extends Dict>(f: (key: keyof T, value: T[keyof T]) => boolean) => | ||
/** | ||
* Returns a new object with nullish values removed | ||
*/ | ||
export const compact = <T extends Dict>(obj: T): Compact<T> => | ||
filter((_, v) => v !== undefined)(obj) as any | ||
filter((_, v) => v != null)(obj) as any | ||
/** | ||
* Split an object into multiple objects based on a predicate function | ||
*/ | ||
export const split = | ||
@@ -87,4 +110,5 @@ <T extends Dict, K extends [SplitProp<T>, ...SplitProp<T>[]]>(...keys: K) => | ||
export const clone = <T extends Dict>(obj: T): T => structuredClone(obj) | ||
/** | ||
* Merge multiple objects to an object in a pipeline | ||
*/ | ||
export function merge<T, U>(source: U): (target: T) => Assign<[T, U]> | ||
@@ -120,2 +144,5 @@ export function merge<T, U, V>( | ||
/** | ||
* Omit properties from an object in a pipeline | ||
*/ | ||
export const omit = | ||
@@ -131,2 +158,5 @@ <T extends Dict, K extends keyof T>(keys: K[]) => | ||
/** | ||
* Pick properties from an object in a pipeline | ||
*/ | ||
export const pick = | ||
@@ -142,18 +172,5 @@ <T extends Dict, K extends keyof T>(keys: K[]) => | ||
interface Bind { | ||
<T extends Dict, K extends string, U>(key: K, fn: (value: T) => U): ( | ||
obj: T | ||
) => Assign<[T, Record<K, U>]> | ||
} | ||
export const bind: Bind = (key, fn) => (obj) => | ||
cast({ | ||
...obj, | ||
[key]: fn(obj), | ||
}) | ||
type Defaults<T extends Dict, K extends Partial<T>> = Simplify< | ||
Omit<T, keyof K> & Required<K> | ||
> | ||
/** | ||
* Assign default values to an object in a pipeline | ||
*/ | ||
export const defaults = | ||
@@ -170,2 +187,5 @@ <T extends Dict, K extends Partial<T>>(defaults: K) => | ||
/** | ||
* Get a property from an object by dot notation | ||
*/ | ||
export const get = | ||
@@ -172,0 +192,0 @@ <T extends Dict, K extends Path<T>>(path: K, undef?: T[K]) => |
@@ -0,14 +1,44 @@ | ||
/** | ||
* An Option represents a value that may or may not be present. | ||
*/ | ||
type Option<T> = None | Some<T> | ||
type None = { _tag: 'None' } | ||
type Some<T> = { _tag: 'Some'; value: T } | ||
/** | ||
* The None value represents the absence of a value. | ||
*/ | ||
export const none: Option<never> = Object.freeze({ _tag: 'None' }) | ||
/** | ||
* The Some value represents the presence of a value. | ||
*/ | ||
export const some = <T>(value: T): Option<T> => | ||
Object.freeze({ _tag: 'Some', value }) | ||
export const from = <T>(value: T): Option<T> => some(value) | ||
/** | ||
* Creates an Option from a predicate function. | ||
* If the predicate returns true, the value is wrapped in a Some, otherwise None is returned. | ||
*/ | ||
export function fromPredicate<T, U extends T>( | ||
predicate: (v: T) => v is U | ||
): (v: T) => Option<U> | ||
export function fromPredicate<T>(predicate: (v: T) => boolean) { | ||
return (v: T): Option<T> => (predicate(v) ? some(v) : none) | ||
} | ||
/** | ||
* Creates an Option from a nullable value. | ||
* If the value is null or undefined, None is returned, otherwise the value is wrapped in a Some. | ||
*/ | ||
export const fromNullable = <T>(value: T | null | undefined): Option<T> => | ||
value == null ? none : some(value) | ||
/** | ||
* Creates an Option from a function that may throw an error. | ||
* If the function throws an error, None is returned, otherwise the result is wrapped in a Some. | ||
*/ | ||
export const fromExecution = <T>( | ||
@@ -24,7 +54,21 @@ value: () => T | null | undefined | ||
/** | ||
* Whether the given value is a None | ||
*/ | ||
export const isNone = <T>(o: Option<T>): o is None => o._tag === 'None' | ||
/** | ||
* Whether the given value is a Some | ||
*/ | ||
export const isSome = <T>(o: Option<T>): o is Some<T> => o._tag === 'Some' | ||
/** | ||
* Whether the given value is an Option | ||
*/ | ||
const isOption = <T>(o: any): o is Option<T> => | ||
o._tag === 'None' || o._tag === 'Some' | ||
/** | ||
* Calls the specified callback function on the value of an option if is a Some. | ||
*/ | ||
export const map = | ||
@@ -35,2 +79,6 @@ <T, U>(f: (value: T) => U) => | ||
/** | ||
* Calls the specified callback function on the value of an option if is a Some. | ||
* It also flattens any nested options. | ||
*/ | ||
export const flatMap = | ||
@@ -41,2 +89,5 @@ <T, U>(f: (value: T) => Option<U>) => | ||
/** | ||
* Returns the value of an option if is a Some, otherwise returns the specified default value. | ||
*/ | ||
export const getOrElse = | ||
@@ -47,2 +98,5 @@ <T>(def: () => T) => | ||
/** | ||
* Returns the value of an option if is a Some(value), otherwise it throws an error. | ||
*/ | ||
export const getOrThrow = | ||
@@ -55,2 +109,5 @@ <T>(msg: string) => | ||
/** | ||
* Returns the result of someFn if option is Some, otherwise, returns the result of noneFn. | ||
*/ | ||
export const match = | ||
@@ -61,2 +118,5 @@ <T, U>(onSome: (v: T) => U, onNone: () => U) => | ||
/** | ||
* Returns the value of an option if is a Some, otherwise returns the result of the alternative function. | ||
*/ | ||
export const orElse = | ||
@@ -70,2 +130,6 @@ <T>(v: () => Option<T> | T) => | ||
/** | ||
* Calls the specified callback function on the value of an option if is a Some. | ||
* Returns the original option. | ||
*/ | ||
export const tap = | ||
@@ -78,2 +142,5 @@ <T>(f: (v: T) => void) => | ||
/** | ||
* Returns a None if the option is a Some and the value does not satisfy the predicate. | ||
*/ | ||
export const filter = | ||
@@ -80,0 +147,0 @@ <T>(f: (v: T) => boolean) => |
@@ -26,2 +26,6 @@ export type Dict = Record<string, any> | ||
export type Defaults<T extends Dict, K extends Partial<T>> = Simplify< | ||
Omit<T, keyof K> & Required<K> | ||
> | ||
export type Simplify<T> = T extends any ? { [K in keyof T]: T[K] } : T | ||
@@ -28,0 +32,0 @@ |
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
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
86570
2622