Skip to content

Instantly share code, notes, and snippets.

@koyanloshe
Last active June 6, 2020 16:44
Show Gist options
  • Save koyanloshe/c569a456b58172fcbde50db0b003228a to your computer and use it in GitHub Desktop.
Save koyanloshe/c569a456b58172fcbde50db0b003228a to your computer and use it in GitHub Desktop.
Utility functions
function bytesToSize (bytes) {
if (bytes === 0) return '0 B';
var k = 1024;
var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
var i = Math.floor(Math.log(bytes) / Math.log(k))
return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i]
}
function camelize(str) {
const camelizeRE = /-(\w)/g;
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '');
}
function numberToStringWithComma(number) {
// convert number to string
let str = String(number);
let s = '';
let count = 0;
for (let i = str.length - 1; i >= 0; i--) {
s = str[i] + s
count++
// add a comma to every three numbers
if (count % 3 == 0 && i != 0) {
s = ',' + s
}
}
return s
}
view raw
const stripHTMLTags = str => str.replace(/<[^>]*>/g, '');
const inBrowser = typeof window !== 'undefined'
// get user agent
const UA = inBrowser && window.navigator.userAgent.toLowerCase()
// detect browser
const isIE = UA && /msie|trident/.test(UA)
const isIE9 = UA && UA.indexOf('msie 9.0') > 0
const isEdge = UA && UA.indexOf('edge/') > 0
const isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge
const isPhantomJS = UA && /phantomjs/.test(UA)
const isFF = UA && UA.match(/firefox\/(\d+)/)
// detect OS
const isAndroid = UA && UA.indexOf('android') > 0
const isIOS = UA && /iphone|ipad|ipod|ios/.test(UA)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment