@bothrs/util
Advanced tools
Comparing version 1.18.1 to 2.0.0
@@ -151,3 +151,3 @@ import { serialize } from './url' | ||
id: fields._id, | ||
fields: Object.assign(fields, { _id: null, createdTime: null }), | ||
fields: { ...fields, _id: null, createdTime: null }, | ||
createdTime: fields.createdTime, | ||
@@ -158,9 +158,7 @@ } | ||
export function unpack({ id: _id, fields, createdTime }: FieldSet) { | ||
return Object.assign( | ||
{ | ||
_id, | ||
createdTime, | ||
}, | ||
fields | ||
) | ||
return { | ||
_id, | ||
createdTime, | ||
...fields, | ||
} | ||
} | ||
@@ -167,0 +165,0 @@ |
16
async.ts
@@ -1,6 +0,6 @@ | ||
export function timeout(ms: number) { | ||
export function timeout(ms: number): Promise<void> { | ||
return new Promise(resolve => setTimeout(resolve, ms)) | ||
} | ||
export function isPending(promise) { | ||
export function isPending<T>(promise: T) { | ||
const test = {} | ||
@@ -12,1 +12,13 @@ return Promise.race([promise, test]).then( | ||
} | ||
/** | ||
* @summary Convenience wrapper for error handling without try/catch | ||
* @example | ||
* const [ err, items ] = await to(fetchJSON('/api/items')) | ||
* if (err) console.error('Something happened') | ||
*/ | ||
export function to<T>(promise: PromiseLike<T> | T) { | ||
return Promise.resolve(promise) | ||
.then(data => [undefined, data] as [undefined, T]) | ||
.catch(err => [err, undefined] as [Error, undefined]) | ||
} |
30
fetch.ts
@@ -5,21 +5,13 @@ export function fetchJSON(url: string, options?: any) { | ||
} | ||
return fetch( | ||
url, | ||
Object.assign( | ||
{ | ||
method: 'POST', | ||
credentials: 'same-origin', | ||
headers: Object.assign( | ||
{ | ||
accept: 'application/json', | ||
}, | ||
options.method !== 'GET' | ||
? { 'content-type': 'application/json' } | ||
: {}, | ||
options.auth ? { Authorization: 'Bearer ' + options.auth } : {} | ||
), | ||
}, | ||
options | ||
) | ||
) | ||
return fetch(url, { | ||
method: 'POST', | ||
credentials: 'same-origin', | ||
...options, | ||
headers: { | ||
accept: 'application/json', | ||
...(options.method !== 'GET' && { 'content-type': 'application/json' }), | ||
...(options.auth ? { Authorization: 'Bearer ' + options.auth } : {}), | ||
...options.headers, | ||
}, | ||
}) | ||
.then(r => r.json()) | ||
@@ -26,0 +18,0 @@ .catch(e => { |
@@ -1,2 +0,8 @@ | ||
export function ls(key: string, value?) { | ||
/** | ||
* Store a value in localStorage | ||
* | ||
* @param key | ||
* @param [value] - JSON-stringifyable value | ||
*/ | ||
export function ls(key: string, value?: any) { | ||
try { | ||
@@ -3,0 +9,0 @@ if (typeof value === 'undefined') { |
41
memo.ts
@@ -10,5 +10,5 @@ /** | ||
func: T, | ||
timeout? | ||
): (...funcArgs: Parameters<T>) => ReturnType<T> { | ||
const cache = {} | ||
timeout = 0 | ||
): (...args: Parameters<T>) => ReturnType<T> { | ||
const cache: { [key: string]: ReturnType<T> | null } = {} | ||
const f = function (...[a, b, c]: Parameters<T>): ReturnType<T> { | ||
@@ -24,6 +24,8 @@ const key = JSON.stringify([a, b, c]) | ||
} | ||
return cache[key] | ||
return cache[key] as ReturnType<T> | ||
} | ||
// TODO: expire this? | ||
f.set = (key, value) => (cache[JSON.stringify(key)] = value) | ||
f.set = (key: Parameters<T>, value: ReturnType<T>) => | ||
(cache[JSON.stringify(key)] = value) | ||
// @ts-ignore | ||
f.reset = (a, b, c) => (cache[JSON.stringify([a, b, c])] = null) | ||
@@ -36,13 +38,12 @@ return f | ||
* | ||
* @param {Function} func - Expensive function | ||
* @param {number} [timeout] - Milliseconds to wait before running the function again | ||
* @returns {Function} Function that returns optimistic value of `func` | ||
* @param func - Expensive function | ||
* @param [timeout] - Milliseconds to wait before running the function again | ||
* @returns Function that returns optimistic value of `func` | ||
*/ | ||
export function optimist<T extends (...args: any[]) => any>( | ||
func: T, | ||
timeout = 60000 | ||
): (...funcArgs: Parameters<T>) => ReturnType<T> { | ||
const cache = {} | ||
const time = {} | ||
): (...args: Parameters<T>) => ReturnType<T> { | ||
const cache: { [key: string]: ReturnType<T> | null } = {} | ||
const time: { [key: string]: number } = {} | ||
const f = function (...[a, b, c]: Parameters<T>): ReturnType<T> { | ||
@@ -56,11 +57,17 @@ const key = JSON.stringify([a, b, c]) | ||
time[key] = Date.now() + timeout | ||
Promise.resolve(func(a, b, c)) | ||
.then(val => (cache[key] = val)) | ||
.catch(() => console.log('optimist catch', a, b, c)) | ||
const promise = func(a, b, c) | ||
Promise.resolve(promise) | ||
.then(() => (cache[key] = promise)) | ||
.catch(e => { | ||
console.log('optimist bg catch', e?.message || e) | ||
console.log('optimist bg input', a, b, c) | ||
}) | ||
} | ||
return cache[key] | ||
return cache[key] as ReturnType<T> | ||
} | ||
f.set = (key, value) => (cache[JSON.stringify(key)] = value) | ||
f.set = ([a, b, c]: Parameters<T>, value: ReturnType<T>) => | ||
(cache[JSON.stringify([a, b, c])] = value) | ||
// @ts-ignore | ||
f.reset = (a, b, c) => (cache[JSON.stringify([a, b, c])] = null) | ||
return f | ||
} |
{ | ||
"name": "@bothrs/util", | ||
"version": "1.18.1", | ||
"version": "2.0.0", | ||
"description": "Common helper functions", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -22,1 +22,8 @@ export function id(len = 10): string { | ||
} | ||
export function uuidv4() { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => { | ||
const r = (Math.random() * 16) | 0 | ||
return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16) | ||
}) | ||
} |
@@ -81,3 +81,3 @@ ## util | ||
`const hash = md5('test')` => Decode JWT | ||
`const hash = md5('test')` | ||
@@ -84,0 +84,0 @@ #### idle.mjs |
// Usage: | ||
// items.filter(uniq) | ||
export function uniq(v: any, i: number, a: any[]) { | ||
export function uniq<T>(v: T, i: number, a: T[]) { | ||
return a.findIndex(b => b === v) === i | ||
@@ -9,4 +9,5 @@ } | ||
// items.filter(uniqBy('id')) | ||
export function uniqBy(prop: string) { | ||
return (v, i, a) => a.findIndex(v2 => v[prop] === v2[prop]) === i | ||
export function uniqBy<T>(prop: keyof T) { | ||
return (v: T, i: number, a: T[]) => | ||
a.findIndex((v2: T) => v[prop] === v2[prop]) === i | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 4 instances in 1 package
16
68380
66
2259