0.0.7 • Published 8 months ago
@erbelion/utils v0.0.7
javascript utils
A collection of simple javascript utility functions for use in browser and node.js apps.
If you have any ideas what functions to add, feel free to open an issue.
Warning: This package is in experimental state, updates may be critical.
Installation
yarn
yarn add @erbelion/utils
npm
npm i @erbelion/utils
Functions
Time
sleep(ms)
import { sleep } from '@erbelion/utils'
await sleep(5000)
// 5 seconds have elapsed
Numbers
compactNumber(number)
import { compactNumber } from '@erbelion/utils'
compactNumber(1555) // 1.6K
compactNumber(1555555) // 1.6M
compactNumber(1555555555) // 1.6B
compactNumber(1555555555555) // 1.6T
random() || random(max) || random(min, max)
import { random } from '@erbelion/utils'
random() // 0 or 1
random(69) // random from 0 to 69
random(69, 420) // random from 69 to 420
URLs
isUrl(url) || isUrl(url, strict)
import { isUrl } from '@erbelion/utils'
isUrl('https://github.com/erbelion/utils') // true
isUrl('bruh://man.com') // false
isUrl('broher') // false
isUrl('github.com/erbelion/utils') // true
isUrl('github.com/erbelion/utils', true) // false
urlParams(url)
import { urlParams } from '@erbelion/utils'
urlParams('https://github.com/erbelion/utils?a=1&b=2') // { a: 1, b: 2 }
urlParams('') // {}
// if no arg is given, then browser url is taken (window.location.href)
urlParams() // {}
updateUrlParams(newParams) || updateUrlParams(newParams, url) || updateUrlParams(newParams, oldParams)
check vue example here
import { updateUrlParams } from '@erbelion/utils'
updateUrlParams({ a: 1 }, 'https://github.com/erbelion/utils?b=2') // { a: 1, b: 2 }
updateUrlParams({ a: 1 }, { b: 2 }) // { a: 1, b: 2 }
// if no second arg is given, then browser url (window.location.href) is updated without reload
updateUrlParams({ a: 1 }) // updates current window url to https://github.com/erbelion/utils?a=1
// remove url params
updateUrlParams({a: ''}, 'https://github.com/erbelion/utils?a=1') // {}