From 77712e2e9af16c05811bb59f120dc53fe2d1a14a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Barrag=C3=A1n=20Merino?= Date: Tue, 29 Jul 2014 14:43:21 +0200 Subject: [PATCH] Add a function to formated file size to 'utils' module --- app/coffee/utils.coffee | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/coffee/utils.coffee b/app/coffee/utils.coffee index 7be76d89..ba3d6a6f 100644 --- a/app/coffee/utils.coffee +++ b/app/coffee/utils.coffee @@ -105,6 +105,22 @@ debounce = (wait, func) -> startswith = (str1, str2) -> return _.str.startsWith(str1, str2) + +sizeFormat = (input, precision=1) -> + if isNaN(parseFloat(input)) or not isFinite(input) + return "-" + + if input == 0 + return "0 bytes" + + units = ["bytes", "KB", "MB", "GB", "TB", "PB"] + number = Math.floor(Math.log(input) / Math.log(1024)) + if number > 5 + number = 5 + size = (input / Math.pow(1024, number)).toFixed(precision) + return "#{size} #{units[number]}" + + taiga = @.taiga taiga.bindOnce = bindOnce taiga.mixOf = mixOf @@ -120,3 +136,4 @@ taiga.toString = toString taiga.joinStr = joinStr taiga.debounce = debounce taiga.startswith = startswith +taiga.sizeFormat = sizeFormat