Files
Piped-Material/src/tools/libpiped.js
T
root bbd1ce30ec Implement PWA, client-side Watch History and cleanup
Squashed commit of the following:

commit a1c18c7903
Author: root <root@git.maharshi.ninja>
Date:   Sun Aug 22 21:46:45 2021 +0530

    Minor UI change

commit 82783a4fb2
Author: root <root@git.maharshi.ninja>
Date:   Sun Aug 22 21:23:15 2021 +0530

    Format date and time the Right Way (™️)

commit 1b0d87e81d
Author: root <root@git.maharshi.ninja>
Date:   Sun Aug 22 21:17:41 2021 +0530

    A few fixes and a basic implementation

commit 7e5d057d37
Author: root <root@git.maharshi.ninja>
Date:   Sun Aug 22 19:58:10 2021 +0530

    Add PouchDB and add the basic design

commit 16a07c10ff
Author: root <root@git.maharshi.ninja>
Date:   Sun Aug 22 18:53:34 2021 +0530

    Delete robots.txt

commit 83e8690dc5
Author: root <root@git.maharshi.ninja>
Date:   Sun Aug 22 18:52:00 2021 +0530

    Add PWA configuration
2021-08-28 03:27:49 +05:30

72 lines
1.5 KiB
JavaScript

import DOMPurify from 'dompurify'
import TimeAgo from 'javascript-time-ago'
import en from 'javascript-time-ago/locale/en'
TimeAgo.addDefaultLocale(en)
const timeAgo = new TimeAgo('en-US')
class LibPiped {
intlDTF = new Intl.DateTimeFormat([], {
dateStyle: 'full',
timeStyle: 'full'
})
timeFormat (duration) {
const pad = function (num, size) {
return ('000' + num).slice(size * -1)
}
const time = parseFloat(duration).toFixed(3)
const hours = Math.floor(time / 60 / 60)
const minutes = Math.floor(time / 60) % 60
const seconds = Math.floor(time - minutes * 60)
let str = ''
if (hours > 0) str += hours + ':'
str += pad(minutes, 2) + ':' + pad(seconds, 2)
return str
}
formatFullDate (date) {
return this.intlDTF.format(date)
}
numberFormat (num) {
const digits = 2
const si = [
{ value: 1, symbol: '' },
{ value: 1e3, symbol: 'K' },
{ value: 1e6, symbol: 'M' },
{ value: 1e9, symbol: 'B' }
]
const rx = /\.0+$|(\.[0-9]*[1-9])0+$/
let i
for (i = si.length - 1; i > 0; i--) {
if (num >= si[i].value) {
break
}
}
return (num / si[i].value).toFixed(digits).replace(rx, '$1') + si[i].symbol
}
addCommas (num) {
num = parseInt(num)
return num.toLocaleString('en-US')
}
purifyHTML (original) {
return DOMPurify.sanitize(original)
}
timeAgo (time) {
return timeAgo.format(time)
}
}
const lp = new LibPiped()
export { lp as LibPiped }