Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bothrs/util

Package Overview
Dependencies
Maintainers
5
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bothrs/util - npm Package Compare versions

Comparing version 1.18.1 to 2.0.0

hash.ts

14

airtable.ts

@@ -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 @@

@@ -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])
}

@@ -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') {

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc