This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const formatCurrency = (number, currency) => { | |
const options = { | |
minimumFractionDigits: 2, | |
currencyDisplay: 'narrowSymbol', | |
currencySign: 'accounting', | |
} | |
if (currency) { | |
options.style = 'currency' | |
options.currency = currency | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const getLocale = () => { | |
const locale = (navigator.languages && navigator.languages.length) | |
? navigator.languages[0] | |
: navigator.language | |
return locale || 'en-US' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const snippet = (val, len = 5) => | |
(val && val.length > (len * 2)) | |
? val.substring(0, len) + '...' + val.substring(val.length - len) | |
: val |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const isPrimitive = val => { | |
const type = typeof val | |
if (Array.isArray(val)) return false | |
if (type === 'object') return val === null | |
return type !== 'function' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function debounce(func, wait, immediate) { | |
let timeout | |
return function (...args) { | |
clearTimeout(timeout) | |
timeout = setTimeout(() => { | |
timeout = null | |
if (!immediate) func.apply(this, args) | |
}, wait) | |
if (immediate && !timeout) func.apply(this, [...args]) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const copyToClipboard = async (text) => { | |
if (!navigator.clipboard) | |
// Clipboard API not available | |
throw new Error('Your browser doesn\'t allow clipboard access') | |
try { | |
await navigator.clipboard.writeText(text) | |
} catch (e) { | |
log.error('Failed to copy', e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const omit = (obj, fn) => Object.keys(obj) | |
.filter(it => fn(obj[it])) | |
.reduce((acc, it) => ({ ...acc, [it]: obj[it] }), {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const isNumeric = n => | |
!(isNaN(n)) && (typeof n !== 'object') && | |
(n != Number.POSITIVE_INFINITY) && (n != Number.NEGATIVE_INFINITY) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const selectText = el => { | |
if (!window.getSelection || !document.createRange) return | |
const sel = window.getSelection() | |
if (sel.toString() == '') | |
window.setTimeout(() => { | |
const range = document.createRange() | |
range.selectNodeContents(el) | |
sel.removeAllRanges() | |
sel.addRange(range) | |
}, 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Observer<T> { | |
fun onChange(newValue: T?) | |
} | |
class Observable<T>(initialValue: T? = null) { | |
// List ov observers watching this value for changes | |
private val observers = mutableListOf<Observer<T>>() | |
// The real value of this observer |