diff --git a/packages/lib/package.json b/packages/lib/package.json index c77e040..46244e9 100644 --- a/packages/lib/package.json +++ b/packages/lib/package.json @@ -26,8 +26,6 @@ "fix": "gts fix" }, "dependencies": { - "@scure/base": "^1.1.6", - "@types/events": "^3.0.3", - "events": "^3.3.0" + "@scure/base": "^1.1.6" } } diff --git a/packages/lib/src/Tools.ts b/packages/lib/src/Tools.ts index e1b52e6..6074977 100644 --- a/packages/lib/src/Tools.ts +++ b/packages/lib/src/Tools.ts @@ -9,6 +9,9 @@ export const isNil = (x: any) => [null, undefined].includes(x) /** Type representing an optional value */ export type Maybe = T | undefined +/** Type that is shorthand for Record */ +export type Obj = Record + /** * Executes a function if the value is defined * @param x - The value to check @@ -139,7 +142,7 @@ export const take = (n: number, xs: T[]) => xs.slice(0, n) * @param x - Source object * @returns New object without specified keys */ -export const omit = >(ks: string[], x: T) => { +export const omit = (ks: string[], x: T) => { const r: T = {...x} for (const k of ks) { @@ -155,8 +158,8 @@ export const omit = >(ks: string[], x: T) => { * @param x - Source object * @returns New object without entries containing specified values */ -export const omitVals = >(xs: any[], x: T) => { - const r: Record = {} +export const omitVals = (xs: any[], x: T) => { + const r: Obj = {} for (const [k, v] of Object.entries(x)) { if (!xs.includes(v)) { @@ -173,7 +176,7 @@ export const omitVals = >(xs: any[], x: T) => { * @param x - Source object * @returns New object with only specified keys */ -export const pick = >(ks: string[], x: T) => { +export const pick = (ks: string[], x: T) => { const r: T = {...x} for (const k of Object.keys(x)) { @@ -204,8 +207,8 @@ export function* range(a: number, b: number, step = 1) { * @param x - Source object * @returns Object with transformed keys */ -export const mapKeys = >(f: (v: string) => string, x: T) => { - const r: Record = {} +export const mapKeys = (f: (v: string) => string, x: T) => { + const r: Obj = {} for (const [k, v] of Object.entries(x)) { r[f(k)] = v @@ -236,7 +239,7 @@ export const mapVals = (f: (v: V) => U, x: Record) => { * @param b - Right object * @returns Merged object with a"s properties overriding b"s */ -export const mergeLeft = >(a: T, b: T) => ({ +export const mergeLeft = (a: T, b: T) => ({ ...b, ...a, }) @@ -247,11 +250,29 @@ export const mergeLeft = >(a: T, b: T) => ({ * @param b - Right object * @returns Merged object with b"s properties overriding a"s */ -export const mergeRight = >(a: T, b: T) => ({ +export const mergeRight = (a: T, b: T) => ({ ...a, ...b, }) +/** Deep merge two objects, prioritizing the first argument. */ +export const deepMergeLeft = (a: Obj, b: Obj) => deepMergeRight(b, a) + +/** Deep merge two objects, prioritizing the second argument. */ +export const deepMergeRight = (a: Obj, b: Obj) => { + a = {...a} + + for (const [k, v] of Object.entries(b)) { + if (isPojo(v) && isPojo(a[k])) { + a[k] = deepMergeRight(a[k], v) + } else { + a[k] = v + } + } + + return a +} + /** * Checks if a number is between two values (exclusive) * @param bounds - Lower and upper bounds @@ -556,7 +577,7 @@ export const nthNe = 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) => { +export const spec = (values: Obj) => (x: Obj) => { for (const [k, v] of Object.entries(values)) { if (x[k] !== v) return false } @@ -1140,4 +1161,4 @@ export const hexToBech32 = (prefix: string, hex: string) => * @returns Hex encoded string */ export const bech32ToHex = (b32: string) => - utf8.encode(bech32.fromWords(bech32.decode(b32, false).words)) + utf8.encode(bech32.fromWords(bech32.decode(b32 as any, false).words))