2018-07-18 19:21:44 +00:00
|
|
|
export const logger = ({ name, color }) => (...args) =>
|
2018-07-14 02:01:24 +00:00
|
|
|
console.log(
|
2018-07-18 19:21:44 +00:00
|
|
|
`%c[${name}]%c`,
|
|
|
|
`color: ${color}; font-weight: bold`,
|
2018-07-14 02:01:24 +00:00
|
|
|
'color: inherit; font-weight: inherit',
|
2018-07-18 19:21:44 +00:00
|
|
|
...args
|
2018-07-16 04:04:43 +00:00
|
|
|
)
|
2018-07-18 19:21:44 +00:00
|
|
|
|
|
|
|
export const log = logger({ name: 'elstat', color: 'purple' })
|
2018-07-16 04:44:59 +00:00
|
|
|
|
2018-08-08 23:43:53 +00:00
|
|
|
export function objectFromEntries(entries) {
|
2018-07-16 04:44:59 +00:00
|
|
|
return entries.reduce(
|
|
|
|
(object, [key, value]) => ({ ...object, [key]: value }),
|
|
|
|
{}
|
|
|
|
)
|
|
|
|
}
|
2018-07-18 21:59:21 +00:00
|
|
|
|
2018-08-09 00:06:35 +00:00
|
|
|
export function truncateToTwoPlaces(number) {
|
|
|
|
// https://stackoverflow.com/a/4187164/2491753
|
|
|
|
return number.toString().match(/^-?\d+(?:\.\d{0,4})?/)[0]
|
|
|
|
}
|
|
|
|
|
2018-08-08 23:43:53 +00:00
|
|
|
export async function strictFetch(...args) {
|
2018-07-18 21:59:21 +00:00
|
|
|
const resp = await fetch(...args)
|
|
|
|
|
|
|
|
if (!resp.ok) {
|
|
|
|
throw new Error(`Failed to fetch: ${resp.status} ${resp.statusText}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|