Add domain package

This commit is contained in:
Jon Staab
2024-08-13 08:31:53 -07:00
parent d561039606
commit 0576c3c0e0
14 changed files with 378 additions and 1 deletions
+22
View File
@@ -121,6 +121,16 @@ export const toggle = <T>(x: T, xs: T[]) => xs.includes(x) ? remove(x, xs) : app
export const clamp = ([min, max]: [number, number], n: number) => Math.min(max, Math.max(min, n))
export const parseJson = (json: string | Nil) => {
if (!json) return null
try {
return JSON.parse(json)
} catch (e) {
return null
}
}
export const tryCatch = <T>(f: () => T, onError?: (e: Error) => void): T | undefined => {
try {
const r = f()
@@ -137,6 +147,18 @@ export const tryCatch = <T>(f: () => T, onError?: (e: Error) => void): T | undef
return undefined
}
export const ellipsize = (s: string, l: number, suffix = '...') => {
if (s.length < l * 1.1) {
return s
}
while (s.length > l && s.includes(' ')) {
s = s.split(' ').slice(0, -1).join(' ')
}
return s + suffix
}
export const isPojo = (obj: any) => {
const hasOwnProperty = Object.prototype.hasOwnProperty
const toString = Object.prototype.toString