You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.1 KiB
JavaScript

function clone (x) {
return JSON.parse(JSON.stringify(x))
}
function fromQueryString (query = window.location.search) {
return query
.slice(1) // remove leading '?'
.split('&') // split params
.reduce((result, kv) => { // make an object of key value pairs
const [key, value] = kv.split('=')
return { ...result, [key]: decodeURIComponent(value) }
}, {})
}
function toQueryString (params) {
return Object.entries(params)
.filter(([k, v]) => !!v)
.map(([k, v]) => [k, stringify(v)]
.map(encodeURIComponent)
.join('='))
.join('&')
}
function wait (milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
function round (val, decimals = 2) {
const fac = 10 ** decimals
return Math.round(val * fac) / fac
}
function stringify (v) {
if (v === undefined)
return ''
else if (v instanceof Date)
return v.toISOString()
else if (v instanceof Object)
return JSON.stringify(v)
else
return v.toString()
}
module.exports = {
clone,
round,
stringify,
toQueryString,
fromQueryString,
wait,
}