From b7fb8acf92a0446ac8ae4a0dc45ee8ebb47761d8 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 2 Jan 2018 03:20:09 +1100 Subject: [PATCH 1/9] Add prettyBytes snippet --- snippets/prettyBytes.md | 55 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 snippets/prettyBytes.md diff --git a/snippets/prettyBytes.md b/snippets/prettyBytes.md new file mode 100644 index 000000000..b9be85631 --- /dev/null +++ b/snippets/prettyBytes.md @@ -0,0 +1,55 @@ +### prettyBytes + +Converts a number in bytes to a human-readable string. + +Use an array dictionary of units to be accessed based on the exponent. Return the prettified +string by building it up taking into account whether the supplied options and whether it is +negative or not. + +```js +const prettyBytes = (num, options) => { + options = { precision: 3, addSpace: true, wholeWord: false, ...options }; + const UNITS = [ + ['B', 'Byte'], + ['KB', 'Kilo'], + ['MB', 'Mega'], + ['GB', 'Giga'], + ['TB', 'Tera'], + ['PB', 'Peta'], + ['EB', 'Exa'], + ['ZB', 'Zetta'], + ['YB', 'Yotta'] + ]; + if (num < 0) num = -num; + if (num < 1) return (num < 0 ? '-' : '') + num + ' B'; + const exponent = Math.min(Math.floor(Math.log10(num) / 3), UNITS.length - 1); + const n = Number( + (num / Math.pow(1000, exponent)).toPrecision(options.precision) + ); + return ( + (num < 0 ? '-' : '') + + n + + (options.addSpace ? ' ' : '') + + UNITS[exponent][options.wholeWord ? 1 : 0] + + (options.wholeWord && exponent > 0 ? 'byte' : '') + + (options.wholeWord && n !== 1 ? 's' : '') + ); +}; +``` + +```js +/* +Default options: { + precision: 3, // number of digits + addSpace: true, // add a space between the unit and number? + wholeWord: false // use the whole word or two letters? +} +*/ +prettyBytes(1000); // 1 KB +prettyBytes(123456789); // 123 MB +prettyBytes(-50); // -50 B +prettyBytes(27145424323.5821); // 27.1 GB +prettyBytes(27145424323.5821, { precision: 5 }); // 27.145 GB +prettyBytes(5500, { wholeWord: true }); // 5.5 Kilobytes +prettyBytes(5500, { addSpace: false }); // 5.5KB +``` From 27cf1424084b80ab4ebb23fe4efa83019de55ec9 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 2 Jan 2018 03:21:16 +1100 Subject: [PATCH 2/9] fix typo --- snippets/prettyBytes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/prettyBytes.md b/snippets/prettyBytes.md index b9be85631..80577c118 100644 --- a/snippets/prettyBytes.md +++ b/snippets/prettyBytes.md @@ -3,7 +3,7 @@ Converts a number in bytes to a human-readable string. Use an array dictionary of units to be accessed based on the exponent. Return the prettified -string by building it up taking into account whether the supplied options and whether it is +string by building it up taking into account the supplied options and whether it is negative or not. ```js From 834aaa43003f28fb87d3fe28e6e54f3429cdcfdd Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 2 Jan 2018 03:26:47 +1100 Subject: [PATCH 3/9] use exponent operator --- snippets/prettyBytes.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/snippets/prettyBytes.md b/snippets/prettyBytes.md index 80577c118..8ece6d503 100644 --- a/snippets/prettyBytes.md +++ b/snippets/prettyBytes.md @@ -23,9 +23,7 @@ const prettyBytes = (num, options) => { if (num < 0) num = -num; if (num < 1) return (num < 0 ? '-' : '') + num + ' B'; const exponent = Math.min(Math.floor(Math.log10(num) / 3), UNITS.length - 1); - const n = Number( - (num / Math.pow(1000, exponent)).toPrecision(options.precision) - ); + const n = Number((num / 1000 ** exponent).toPrecision(options.precision)); return ( (num < 0 ? '-' : '') + n + From 6e52930234dfae0029eeed4a9a0e19bf480ec96e Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 2 Jan 2018 03:30:33 +1100 Subject: [PATCH 4/9] Update prettyBytes.md --- snippets/prettyBytes.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/snippets/prettyBytes.md b/snippets/prettyBytes.md index 8ece6d503..9fc6da983 100644 --- a/snippets/prettyBytes.md +++ b/snippets/prettyBytes.md @@ -2,8 +2,9 @@ Converts a number in bytes to a human-readable string. -Use an array dictionary of units to be accessed based on the exponent. Return the prettified -string by building it up taking into account the supplied options and whether it is +Use an array dictionary of units to be accessed based on the exponent. +Use `Number.toPrecision()` to truncate the number to a certain number of digits. +Return the prettified string by building it up, taking into account the supplied options and whether it is negative or not. ```js From 48ce245161bac4c6204a6e80392eb4ff6e321fc9 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Jan 2018 19:58:42 +0200 Subject: [PATCH 5/9] Update prettyBytes.md --- snippets/prettyBytes.md | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/snippets/prettyBytes.md b/snippets/prettyBytes.md index 9fc6da983..c959b66d8 100644 --- a/snippets/prettyBytes.md +++ b/snippets/prettyBytes.md @@ -9,29 +9,15 @@ negative or not. ```js const prettyBytes = (num, options) => { - options = { precision: 3, addSpace: true, wholeWord: false, ...options }; - const UNITS = [ - ['B', 'Byte'], - ['KB', 'Kilo'], - ['MB', 'Mega'], - ['GB', 'Giga'], - ['TB', 'Tera'], - ['PB', 'Peta'], - ['EB', 'Exa'], - ['ZB', 'Zetta'], - ['YB', 'Yotta'] - ]; - if (num < 0) num = -num; + options = { precision: 3, addSpace: true, ...options }; + const UNITS = ['B','KB','MB','GB','TB','PB','EB','ZB','YB']; if (num < 1) return (num < 0 ? '-' : '') + num + ' B'; - const exponent = Math.min(Math.floor(Math.log10(num) / 3), UNITS.length - 1); - const n = Number((num / 1000 ** exponent).toPrecision(options.precision)); + const exponent = Math.min(Math.floor(Math.log10(num < 0 ? -num : num) / 3), UNITS.length - 1); + const n = Number(((num < 0 ? -num : num) / 1000 ** exponent).toPrecision(options.precision)); return ( (num < 0 ? '-' : '') + n + - (options.addSpace ? ' ' : '') + - UNITS[exponent][options.wholeWord ? 1 : 0] + - (options.wholeWord && exponent > 0 ? 'byte' : '') + - (options.wholeWord && n !== 1 ? 's' : '') + (options.addSpace ? ' ' : '') + UNITS[exponent] ); }; ``` From 171ca06a63cc9f2669cd43cd01382ee1deaa14e6 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Jan 2018 20:00:50 +0200 Subject: [PATCH 6/9] Update prettyBytes.md --- snippets/prettyBytes.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/snippets/prettyBytes.md b/snippets/prettyBytes.md index c959b66d8..f8e00917f 100644 --- a/snippets/prettyBytes.md +++ b/snippets/prettyBytes.md @@ -8,8 +8,7 @@ Return the prettified string by building it up, taking into account the supplied negative or not. ```js -const prettyBytes = (num, options) => { - options = { precision: 3, addSpace: true, ...options }; +const prettyBytes = (num, precision = 3, addSpace = true) { const UNITS = ['B','KB','MB','GB','TB','PB','EB','ZB','YB']; if (num < 1) return (num < 0 ? '-' : '') + num + ' B'; const exponent = Math.min(Math.floor(Math.log10(num < 0 ? -num : num) / 3), UNITS.length - 1); From afebcc1c0f1a542aa3c3c0681e2ddf2e04baa285 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Jan 2018 20:01:39 +0200 Subject: [PATCH 7/9] Update prettyBytes.md --- snippets/prettyBytes.md | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/snippets/prettyBytes.md b/snippets/prettyBytes.md index f8e00917f..be18bafde 100644 --- a/snippets/prettyBytes.md +++ b/snippets/prettyBytes.md @@ -22,18 +22,10 @@ const prettyBytes = (num, precision = 3, addSpace = true) { ``` ```js -/* -Default options: { - precision: 3, // number of digits - addSpace: true, // add a space between the unit and number? - wholeWord: false // use the whole word or two letters? -} -*/ prettyBytes(1000); // 1 KB prettyBytes(123456789); // 123 MB prettyBytes(-50); // -50 B prettyBytes(27145424323.5821); // 27.1 GB -prettyBytes(27145424323.5821, { precision: 5 }); // 27.145 GB -prettyBytes(5500, { wholeWord: true }); // 5.5 Kilobytes -prettyBytes(5500, { addSpace: false }); // 5.5KB +prettyBytes(27145424323.5821, 5 ); // 27.145 GB +prettyBytes(5500, 3, false); // 5.5KB ``` From df9b8140e232ebe0fc99606d534341c25003a017 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Jan 2018 20:04:11 +0200 Subject: [PATCH 8/9] Update prettyBytes.md --- snippets/prettyBytes.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/snippets/prettyBytes.md b/snippets/prettyBytes.md index be18bafde..7217cfac5 100644 --- a/snippets/prettyBytes.md +++ b/snippets/prettyBytes.md @@ -8,15 +8,15 @@ Return the prettified string by building it up, taking into account the supplied negative or not. ```js -const prettyBytes = (num, precision = 3, addSpace = true) { +const prettyBytes = (num, precision = 3, addSpace = true) => { const UNITS = ['B','KB','MB','GB','TB','PB','EB','ZB','YB']; - if (num < 1) return (num < 0 ? '-' : '') + num + ' B'; + if (Math.abs(num) < 1) return num + ' B'; const exponent = Math.min(Math.floor(Math.log10(num < 0 ? -num : num) / 3), UNITS.length - 1); - const n = Number(((num < 0 ? -num : num) / 1000 ** exponent).toPrecision(options.precision)); + const n = Number(((num < 0 ? -num : num) / 1000 ** exponent).toPrecision(precision)); return ( (num < 0 ? '-' : '') + n + - (options.addSpace ? ' ' : '') + UNITS[exponent] + (addSpace ? ' ' : '') + UNITS[exponent] ); }; ``` From ef1bf2fa947c2d4f87b4e8ef158a0289070c9dfc Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Jan 2018 20:08:31 +0200 Subject: [PATCH 9/9] Update prettyBytes.md --- snippets/prettyBytes.md | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/snippets/prettyBytes.md b/snippets/prettyBytes.md index 7217cfac5..290c493f8 100644 --- a/snippets/prettyBytes.md +++ b/snippets/prettyBytes.md @@ -4,20 +4,17 @@ Converts a number in bytes to a human-readable string. Use an array dictionary of units to be accessed based on the exponent. Use `Number.toPrecision()` to truncate the number to a certain number of digits. -Return the prettified string by building it up, taking into account the supplied options and whether it is -negative or not. +Return the prettified string by building it up, taking into account the supplied options and whether it is negative or not. +Omit the second argument, `precision`, to use a default precision of `3` digits. +Omit the third argument, `addSpace`, to add space between the number and unit by default. ```js const prettyBytes = (num, precision = 3, addSpace = true) => { const UNITS = ['B','KB','MB','GB','TB','PB','EB','ZB','YB']; - if (Math.abs(num) < 1) return num + ' B'; + if (Math.abs(num) < 1) return num + (addSpace ? ' ' : '') + UNITS[0]; const exponent = Math.min(Math.floor(Math.log10(num < 0 ? -num : num) / 3), UNITS.length - 1); const n = Number(((num < 0 ? -num : num) / 1000 ** exponent).toPrecision(precision)); - return ( - (num < 0 ? '-' : '') + - n + - (addSpace ? ' ' : '') + UNITS[exponent] - ); + return ((num < 0 ? '-' : '') + n + (addSpace ? ' ' : '') + UNITS[exponent]); }; ```