Created
September 28, 2017 09:31
-
-
Save yairEO/4747208b23ab7d4b4db404e86d178652 to your computer and use it in GitHub Desktop.
lodash "formatBytes" mixin
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
_.mixin({ | |
"formatBytes" : function(kiloBytes, decimals){ | |
if(kiloBytes == 0) return ['0', 'KB']; | |
var k = 1024, | |
dm = decimals + 1 || 1, | |
sizes = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], | |
i = Math.floor(Math.log(kiloBytes) / Math.log(k)); | |
return [parseFloat((kiloBytes / Math.pow(k, i)).toFixed(dm)).toLocaleString() , sizes[i]]; // parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + | |
}, | |
"orderByAaBb" : function(arr, prop){ | |
var clonedArr = _.cloneDeep(arr); | |
return clonedArr.sort((a,b) => a[prop].localeCompare(b[prop], 'en-US-u-kf-upper')); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Accepts
number
orstring
which represents a number