🌳
pt0/sharedF/getOrSetF.mts
1import { get, set } from 'lodash-es'
4export const compactObjByVals = (inObj: Record<string, unknown>) => {
5 assertPlainObject(inObj, {inObj})
6 return Object.fromEntries(
7 Object.entries(inObj).filter(([, val]) => val)
8 )
9}
10export type imgUrlStr = string & { readonly __brand: unique symbol }
12export const bufferToUrlStr = ({buffer, mimeType}: {buffer: Buffer, mimeType: string}) => {
13 // mimeType image/jpeg
14 return `data:${mimeType};base64,${buffer.toString('base64')}` as imgUrlStr
16// TODO: `if (ret)` truthy check is buggy - should be `if (ret !== undefined)` to handle
17// falsy values like 0, '', false. Also consider throwing if val is undefined to catch
18// caller mistakes (missing default argument).
19export const getOrSet = <T extends object>(obj: T, path: string | Array<string | number>, val: unknown): unknown => {
20 const ret = get(obj, path)
21 if (ret) return ret
22 set(obj, path, val)
23 return val