This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
43 lines (36 loc) · 1.53 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const pluralRules = new Intl.PluralRules('en', { type: 'ordinal' })
const orindalSuffixes = {
one: 'st',
two: 'nd',
few: 'rd',
other: 'th',
}
const monthNames = 'January February March April May June July August September October November December'.split(' ')
const amountSuffixes = ['', 'K', 'M', 'B', 'T', 'Q']
exports.formatOrdinal = n => {
const rule = pluralRules.select(n)
const suffix = orindalSuffixes[rule]
return `${n}${suffix}`
}
exports.getMonth = n => monthNames[n]
exports.formatDuration = ms => `${
Math.floor(ms/60000)
}:${
Math.floor(ms/1000 % 60).toString().padStart(2, '0')
}`
exports.toClassName = (statics, conditionals) => statics.concat(...Object.keys(conditionals).filter(k => conditionals[k])).join(' ')
// I wrote these before I realized play count isn't returned from the API
// exports.shortAmount = n => {
// const degree = Math.floor(Math.log10(n) / 3)
// const suffix = amountSuffixes[degree]
// let digitStr = (n / 10**(3*degree)).toFixed(1).replace('.0', '')
// if (digitStr.length > 3) digitStr = digitStr.split('.')[0]
// return digitStr + suffix
// }
// exports.delimitedAmount = (n, delimiter = ',') => {
// const sign = Math.sign(n) === -1 ? '-' : ''
// const whole = Math.trunc(Math.abs(n)).toString()
// const decimal = n.toString().includes('.') ? `.${n.toString().split('.')[1]}` : ''
// const delimitedWhole = whole.toString().split('').reverse().map((char, i) => char + (i && !(i % 3) ? delimiter : '')).reverse().join('')
// return `${sign}${delimitedWhole}${decimal}`
// }