diff --git a/packages/lib/package.json b/packages/lib/package.json index c19bcdb..4ff59ea 100644 --- a/packages/lib/package.json +++ b/packages/lib/package.json @@ -1,6 +1,6 @@ { "name": "@welshman/lib", - "version": "0.0.33", + "version": "0.0.34", "author": "hodlbod", "license": "MIT", "description": "A collection of utilities.", diff --git a/packages/lib/src/Tools.ts b/packages/lib/src/Tools.ts index 8f3342b..e4da6ca 100644 --- a/packages/lib/src/Tools.ts +++ b/packages/lib/src/Tools.ts @@ -62,6 +62,9 @@ export const always = (x: T, ...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 = (f: (...args: T) => any) => (...args: T) => !f(...args) + /** Converts a Maybe to a number, defaulting to 0 */ export const num = (x: Maybe) => 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 */ 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) => (x: Record) => { + for (const [k, v] of Object.entries(values)) { + if (x[k] !== v) return false + } + + return true +} + /** Returns a function that checks equality with value */ export const eq = (v: T) => (x: T) => x === v @@ -534,7 +546,7 @@ export const eq = (v: T) => (x: T) => x === v export const ne = (v: T) => (x: T) => x !== v /** Returns a function that gets property value from object */ -export const prop = (k: string) => (x: Record) => x[k] +export const prop = (k: string) => (x: Record) => x[k] as T /** Returns a function that adds/updates property on object */ export const assoc = (k: K, v: T) => (o: U) => ({...o, [k as K]: v}) as U & Record @@ -572,6 +584,9 @@ export const ensurePlural = (x: T | T[]) => (x instanceof Array ? x : [x]) /** Converts string or number to number */ export const ensureNumber = (x: number | string) => parseFloat(x as string) +/** Returns a function that gets property value from object */ +export const pluck = (k: string, xs: Record[]) => xs.map(x => x[k] as T) + /** * Creates object from array of key-value pairs * @param pairs - Array of [key, value] tuples diff --git a/packages/util/src/Kinds.ts b/packages/util/src/Kinds.ts index 55b6e82..e227948 100644 --- a/packages/util/src/Kinds.ts +++ b/packages/util/src/Kinds.ts @@ -113,9 +113,6 @@ export const TOPICS = 10015 export const EMOJIS = 10030 export const INBOX_RELAYS = 10050 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 CLIENT_AUTH = 22242 export const AUTH_JOIN = 28934