Last active
June 6, 2020 16:44
-
-
Save koyanloshe/c569a456b58172fcbde50db0b003228a to your computer and use it in GitHub Desktop.
Utility functions
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
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] | |
} |
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
function camelize(str) { | |
const camelizeRE = /-(\w)/g; | |
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : ''); | |
} |
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
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 |
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
const stripHTMLTags = str => str.replace(/<[^>]*>/g, ''); |
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
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