From cfb1c7b2f4e28e419a86d8dc7abe15e9d59cf5b5 Mon Sep 17 00:00:00 2001 From: Travis CI Date: Mon, 1 Jan 2018 18:14:13 +0000 Subject: [PATCH] Travis build: 822 [ci skip] --- README.md | 81 +++++++++++++++++++++++-------------------------- docs/index.html | 28 ++++++++--------- 2 files changed, 52 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index db686662f..1854ec1a5 100644 --- a/README.md +++ b/README.md @@ -267,6 +267,7 @@ * [`isString`](#isstring) * [`isSymbol`](#issymbol) * [`isValidJSON`](#isvalidjson) +* [`prettyBytes`](#prettybytes) * [`randomHexColorCode`](#randomhexcolorcode) * [`RGBToHex`](#rgbtohex) * [`sdbm`](#sdbm) @@ -278,15 +279,6 @@ -### _Uncategorized_ - -
-View contents - -* [`prettyBytes`](#prettybytes) - -
- --- ## 🔌 Adapter @@ -4740,6 +4732,43 @@ isValidJSON(null); // true
[⬆ Back to top](#table-of-contents) +### prettyBytes + +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. +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 + (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]; +}; +``` + +
+Examples + +```js +prettyBytes(1000); // 1 KB +prettyBytes(123456789); // 123 MB +prettyBytes(-50); // -50 B +prettyBytes(27145424323.5821); // 27.1 GB +prettyBytes(27145424323.5821, 5); // 27.145 GB +prettyBytes(5500, 3, false); // 5.5KB +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### randomHexColorCode Generates a random hexadecimal color code. @@ -4950,40 +4979,6 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) ---- - ## _Uncategorized_ - -### prettyBytes - -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. -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 + (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]; -}; -``` - -```js -prettyBytes(1000); // 1 KB -prettyBytes(123456789); // 123 MB -prettyBytes(-50); // -50 B -prettyBytes(27145424323.5821); // 27.1 GB -prettyBytes(27145424323.5821, 5); // 27.145 GB -prettyBytes(5500, 3, false); // 5.5KB -``` - -
[⬆ back to top](#table-of-contents) - ## Collaborators diff --git a/docs/index.html b/docs/index.html index ad87258f0..81bd9f165 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+    }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -1033,6 +1033,19 @@ isSymbol(Symbol('x')); // true
 
isValidJSON('{"name":"Adam","age":20}'); // true
 isValidJSON('{"name":"Adam",age:"20"}'); // false
 isValidJSON(null); // true
+

prettyBytes

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. 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.

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 + (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];
+};
+
prettyBytes(1000); // 1 KB
+prettyBytes(123456789); // 123 MB
+prettyBytes(-50); // -50 B
+prettyBytes(27145424323.5821); // 27.1 GB
+prettyBytes(27145424323.5821, 5); // 27.145 GB
+prettyBytes(5500, 3, false); // 5.5KB
 

randomHexColorCode

Generates a random hexadecimal color code.

Use Math.random to generate a random 24-bit(6x4bits) hexadecimal number. Use bit shifting and then convert it to an hexadecimal String using toString(16).

const randomHexColorCode = () => {
   let n = ((Math.random() * 0xfffff) | 0).toString(16);
   return '#' + (n.length !== 6 ? ((Math.random() * 0xf) | 0).toString(16) + n : n);
@@ -1081,17 +1094,4 @@ console.log(sdbm('age')); // 808122783
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
-

Uncategorized

prettyBytes

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. 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.

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 + (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];
-};
-
prettyBytes(1000); // 1 KB
-prettyBytes(123456789); // 123 MB
-prettyBytes(-50); // -50 B
-prettyBytes(27145424323.5821); // 27.1 GB
-prettyBytes(27145424323.5821, 5); // 27.145 GB
-prettyBytes(5500, 3, false); // 5.5KB
 

\ No newline at end of file