Add a few utilities
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@welshman/lib",
|
"name": "@welshman/lib",
|
||||||
"version": "0.0.33",
|
"version": "0.0.34",
|
||||||
"author": "hodlbod",
|
"author": "hodlbod",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"description": "A collection of utilities.",
|
"description": "A collection of utilities.",
|
||||||
|
|||||||
@@ -62,6 +62,9 @@ export const always = <T>(x: T, ...args: unknown[]) => () => x
|
|||||||
*/
|
*/
|
||||||
export const not = (x: any, ...args: unknown[]) => !x
|
export const not = (x: any, ...args: unknown[]) => !x
|
||||||
|
|
||||||
|
/** Returns a function that returns the boolean negation of the given function */
|
||||||
|
export const complement = <T extends unknown[]>(f: (...args: T) => any) => (...args: T) => !f(...args)
|
||||||
|
|
||||||
/** Converts a Maybe<number> to a number, defaulting to 0 */
|
/** Converts a Maybe<number> to a number, defaulting to 0 */
|
||||||
export const num = (x: Maybe<number>) => x || 0
|
export const num = (x: Maybe<number>) => x || 0
|
||||||
|
|
||||||
@@ -527,6 +530,15 @@ export const nthEq = (i: number, v: any) => (xs: any[], ...args: unknown[]) => x
|
|||||||
/** Returns a function that checks if nth element does not equal value */
|
/** Returns a function that checks if nth element does not equal value */
|
||||||
export const nthNe = (i: number, v: any) => (xs: any[], ...args: unknown[]) => xs[i] !== v
|
export const nthNe = (i: number, v: any) => (xs: any[], ...args: unknown[]) => xs[i] !== v
|
||||||
|
|
||||||
|
/** Returns a function that checks if key/value pairs of x match all pairs in spec */
|
||||||
|
export const spec = (values: Record<string, any>) => (x: Record<string, any>) => {
|
||||||
|
for (const [k, v] of Object.entries(values)) {
|
||||||
|
if (x[k] !== v) return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns a function that checks equality with value */
|
/** Returns a function that checks equality with value */
|
||||||
export const eq = <T>(v: T) => (x: T) => x === v
|
export const eq = <T>(v: T) => (x: T) => x === v
|
||||||
|
|
||||||
@@ -534,7 +546,7 @@ export const eq = <T>(v: T) => (x: T) => x === v
|
|||||||
export const ne = <T>(v: T) => (x: T) => x !== v
|
export const ne = <T>(v: T) => (x: T) => x !== v
|
||||||
|
|
||||||
/** Returns a function that gets property value from object */
|
/** Returns a function that gets property value from object */
|
||||||
export const prop = (k: string) => <T>(x: Record<string, T>) => x[k]
|
export const prop = <T>(k: string) => (x: Record<string, unknown>) => x[k] as T
|
||||||
|
|
||||||
/** Returns a function that adds/updates property on object */
|
/** Returns a function that adds/updates property on object */
|
||||||
export const assoc = <K extends string, T, U>(k: K, v: T) => (o: U) => ({...o, [k as K]: v}) as U & Record<K, T>
|
export const assoc = <K extends string, T, U>(k: K, v: T) => (o: U) => ({...o, [k as K]: v}) as U & Record<K, T>
|
||||||
@@ -572,6 +584,9 @@ export const ensurePlural = <T>(x: T | T[]) => (x instanceof Array ? x : [x])
|
|||||||
/** Converts string or number to number */
|
/** Converts string or number to number */
|
||||||
export const ensureNumber = (x: number | string) => parseFloat(x as string)
|
export const ensureNumber = (x: number | string) => parseFloat(x as string)
|
||||||
|
|
||||||
|
/** Returns a function that gets property value from object */
|
||||||
|
export const pluck = <T>(k: string, xs: Record<string, unknown>[]) => xs.map(x => x[k] as T)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates object from array of key-value pairs
|
* Creates object from array of key-value pairs
|
||||||
* @param pairs - Array of [key, value] tuples
|
* @param pairs - Array of [key, value] tuples
|
||||||
|
|||||||
@@ -113,9 +113,6 @@ export const TOPICS = 10015
|
|||||||
export const EMOJIS = 10030
|
export const EMOJIS = 10030
|
||||||
export const INBOX_RELAYS = 10050
|
export const INBOX_RELAYS = 10050
|
||||||
export const FILE_SERVERS = 10096
|
export const FILE_SERVERS = 10096
|
||||||
export const SEEN_GENERAL = 10115
|
|
||||||
export const SEEN_CONTEXT = 10116
|
|
||||||
export const SEEN_CONVERSATION = 10117
|
|
||||||
export const LIGHTNING_PUB_RPC = 21000
|
export const LIGHTNING_PUB_RPC = 21000
|
||||||
export const CLIENT_AUTH = 22242
|
export const CLIENT_AUTH = 22242
|
||||||
export const AUTH_JOIN = 28934
|
export const AUTH_JOIN = 28934
|
||||||
|
|||||||
Reference in New Issue
Block a user