diff --git a/README.md b/README.md index 61bcfcf9d..73ee28bfa 100644 --- a/README.md +++ b/README.md @@ -254,6 +254,8 @@ average(1, 2, 3);
View contents +* [`atob`](#atob) +* [`btoa`](#btoa) * [`colorize`](#colorize) * [`hasFlags`](#hasflags) * [`hashNode`](#hashnode) @@ -3870,6 +3872,50 @@ toSafeInteger(Infinity); // 9007199254740991 --- ## 📦 Node +### atob + +Decodes a string of data which has been encoded using base-64 encoding. + +Create a `Buffer` for the given string with base-64 encoding and use `Buffer.toString('binary')` to return the decoded string. + +```js +const atob = str => new Buffer(str, 'base64').toString('binary'); +``` + +
+Examples + +```js +atob('Zm9vYmFy'); // 'foobar' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + +### btoa + +Creates a base-64 encoded ASCII string from a String object in which each character in the string is treated as a byte of binary data. + +Create a `Buffer` for the given string with binary encoding and use `Buffer.toString('base64')` to return the encoded string. + +```js +const btoa = str => new Buffer(str, 'binary').toString('base64'); +``` + +
+Examples + +```js +btoa('foobar'); // 'Zm9vYmFy' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### colorize Add special characters to text to print in color in the console (combined with `console.log()`). diff --git a/docs/index.html b/docs/index.html index cc3507797..7f1126d22 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 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);
+      }

logo 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 ]
@@ -841,7 +841,11 @@ own individual rating by supplying it as the third argument.
   Math.round(Math.max(Math.min(num, Number.MAX_SAFE_INTEGER), Number.MIN_SAFE_INTEGER));
 
toSafeInteger('3.2'); // 3
 toSafeInteger(Infinity); // 9007199254740991
-

Node

colorize

Add special characters to text to print in color in the console (combined with console.log()).

Use template literals and special characters to add the appropriate color code to the string output. For background colors, add a special character that resets the background color at the end of the string.

const colorize = (...args) => ({
+

Node

atob

Decodes a string of data which has been encoded using base-64 encoding.

Create a Buffer for the given string with base-64 encoding and use Buffer.toString('binary') to return the decoded string.

const atob = str => new Buffer(str, 'base64').toString('binary');
+
atob('Zm9vYmFy'); // 'foobar'
+

btoa

Creates a base-64 encoded ASCII string from a String object in which each character in the string is treated as a byte of binary data.

Create a Buffer for the given string with binary encoding and use Buffer.toString('base64') to return the encoded string.

const btoa = str => new Buffer(str, 'binary').toString('base64');
+
btoa('foobar'); // 'Zm9vYmFy'
+

colorize

Add special characters to text to print in color in the console (combined with console.log()).

Use template literals and special characters to add the appropriate color code to the string output. For background colors, add a special character that resets the background color at the end of the string.

const colorize = (...args) => ({
   black: `\x1b[30m${args.join(' ')}`,
   red: `\x1b[31m${args.join(' ')}`,
   green: `\x1b[32m${args.join(' ')}`,