From bdbc6db294cd721ae525f2077643ce27c5387306 Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Sat, 13 Jan 2018 15:26:47 +0000 Subject: [PATCH] Travis build: 1222 --- README.md | 43 +++++++++++++++++++++++++++++++++++++++++++ docs/index.html | 25 +++++++++++++++++++++++-- snippets/colorize.md | 40 +++++++++++++++++++--------------------- 3 files changed, 85 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 0dc34c6d2..77a637b1f 100644 --- a/README.md +++ b/README.md @@ -246,6 +246,7 @@ average(1, 2, 3);
View contents +* [`colorize`](#colorize) * [`hasFlags`](#hasflags) * [`isTravisCI`](#istravisci) * [`JSONToFile`](#jsontofile) @@ -3731,6 +3732,48 @@ 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. + +```js +const colorize = (...args) => ({ + black: `\x1b[30m${args.join(' ')}`, + red: `\x1b[31m${args.join(' ')}`, + green: `\x1b[32m${args.join(' ')}`, + yellow: `\x1b[33m${args.join(' ')}`, + blue: `\x1b[34m${args.join(' ')}`, + magenta: `\x1b[35m${args.join(' ')}`, + cyan: `\x1b[36m${args.join(' ')}`, + white: `\x1b[37m${args.join(' ')}`, + bgBlack: `\x1b[40m${args.join(' ')}\x1b[0m`, + bgRed: `\x1b[41m${args.join(' ')}\x1b[0m`, + bgGreen: `\x1b[42m${args.join(' ')}\x1b[0m`, + bgYellow: `\x1b[43m${args.join(' ')}\x1b[0m`, + bgBlue: `\x1b[44m${args.join(' ')}\x1b[0m`, + bgMagenta: `\x1b[45m${args.join(' ')}\x1b[0m`, + bgCyan: `\x1b[46m${args.join(' ')}\x1b[0m`, + bgWhite: `\x1b[47m${args.join(' ')}\x1b[0m` +}); +``` + +
+Examples + +```js +console.log(colorize('foo').red); // 'foo' (red letters) +console.log(colorize('foo', 'bar').bgBlue); // 'foo bar' (blue background) +console.log(colorize(colorize('foo').yellow, colorize('foo').green).bgWhite); // 'foo bar' (first word in yellow letters, second word in green letters, white background for both) +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### hasFlags Check if the current process's arguments contain the specified flags. diff --git a/docs/index.html b/docs/index.html index 89d0fe0d1..bce06a724 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 ]
@@ -802,7 +802,28 @@ 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

hasFlags

Check if the current process's arguments contain the specified flags.

Use Array.every() and Array.includes() to check if process.argv contains all the specified flags. Use a regular expression to test if the specified flags are prefixed with - or -- and prefix them accordingly.

const hasFlags = (...flags) =>
+

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) => ({
+  black: `\x1b[30m${args.join(' ')}`,
+  red: `\x1b[31m${args.join(' ')}`,
+  green: `\x1b[32m${args.join(' ')}`,
+  yellow: `\x1b[33m${args.join(' ')}`,
+  blue: `\x1b[34m${args.join(' ')}`,
+  magenta: `\x1b[35m${args.join(' ')}`,
+  cyan: `\x1b[36m${args.join(' ')}`,
+  white: `\x1b[37m${args.join(' ')}`,
+  bgBlack: `\x1b[40m${args.join(' ')}\x1b[0m`,
+  bgRed: `\x1b[41m${args.join(' ')}\x1b[0m`,
+  bgGreen: `\x1b[42m${args.join(' ')}\x1b[0m`,
+  bgYellow: `\x1b[43m${args.join(' ')}\x1b[0m`,
+  bgBlue: `\x1b[44m${args.join(' ')}\x1b[0m`,
+  bgMagenta: `\x1b[45m${args.join(' ')}\x1b[0m`,
+  bgCyan: `\x1b[46m${args.join(' ')}\x1b[0m`,
+  bgWhite: `\x1b[47m${args.join(' ')}\x1b[0m`
+});
+
console.log(colorize('foo').red); // 'foo' (red letters)
+console.log(colorize('foo', 'bar').bgBlue); // 'foo bar' (blue background)
+console.log(colorize(colorize('foo').yellow, colorize('foo').green).bgWhite); // 'foo bar' (first word in yellow letters, second word in green letters, white background for both)
+

hasFlags

Check if the current process's arguments contain the specified flags.

Use Array.every() and Array.includes() to check if process.argv contains all the specified flags. Use a regular expression to test if the specified flags are prefixed with - or -- and prefix them accordingly.

const hasFlags = (...flags) =>
   flags.every(flag => process.argv.includes(/^-{1,2}/.test(flag) ? flag : '--' + flag));
 
// node myScript.js -s --test --cool=true
 hasFlags('-s'); // true
diff --git a/snippets/colorize.md b/snippets/colorize.md
index 054b35cf1..d554ce110 100644
--- a/snippets/colorize.md
+++ b/snippets/colorize.md
@@ -6,30 +6,28 @@ Use template literals and special characters to add the appropriate color code t
 For background colors, add a special character that resets the background color at the end of the string.
 
 ```js
-const colorize = (...args) => (
-  {
-    black: `\x1b[30m${args.join(' ')}`,
-    red: `\x1b[31m${args.join(' ')}`,
-    green: `\x1b[32m${args.join(' ')}`,
-    yellow: `\x1b[33m${args.join(' ')}`,
-    blue: `\x1b[34m${args.join(' ')}`,
-    magenta: `\x1b[35m${args.join(' ')}`,
-    cyan: `\x1b[36m${args.join(' ')}`,
-    white: `\x1b[37m${args.join(' ')}`,
-    bgBlack : `\x1b[40m${args.join(' ')}\x1b[0m`,
-    bgRed : `\x1b[41m${args.join(' ')}\x1b[0m`,
-    bgGreen : `\x1b[42m${args.join(' ')}\x1b[0m`,
-    bgYellow : `\x1b[43m${args.join(' ')}\x1b[0m`,
-    bgBlue : `\x1b[44m${args.join(' ')}\x1b[0m`,
-    bgMagenta : `\x1b[45m${args.join(' ')}\x1b[0m`,
-    bgCyan : `\x1b[46m${args.join(' ')}\x1b[0m`,
-    bgWhite : `\x1b[47m${args.join(' ')}\x1b[0m`
-  }
-)
+const colorize = (...args) => ({
+  black: `\x1b[30m${args.join(' ')}`,
+  red: `\x1b[31m${args.join(' ')}`,
+  green: `\x1b[32m${args.join(' ')}`,
+  yellow: `\x1b[33m${args.join(' ')}`,
+  blue: `\x1b[34m${args.join(' ')}`,
+  magenta: `\x1b[35m${args.join(' ')}`,
+  cyan: `\x1b[36m${args.join(' ')}`,
+  white: `\x1b[37m${args.join(' ')}`,
+  bgBlack: `\x1b[40m${args.join(' ')}\x1b[0m`,
+  bgRed: `\x1b[41m${args.join(' ')}\x1b[0m`,
+  bgGreen: `\x1b[42m${args.join(' ')}\x1b[0m`,
+  bgYellow: `\x1b[43m${args.join(' ')}\x1b[0m`,
+  bgBlue: `\x1b[44m${args.join(' ')}\x1b[0m`,
+  bgMagenta: `\x1b[45m${args.join(' ')}\x1b[0m`,
+  bgCyan: `\x1b[46m${args.join(' ')}\x1b[0m`,
+  bgWhite: `\x1b[47m${args.join(' ')}\x1b[0m`
+});
 ```
 
 ```js
 console.log(colorize('foo').red); // 'foo' (red letters)
-console.log(colorize('foo','bar').bgBlue); // 'foo bar' (blue background)
+console.log(colorize('foo', 'bar').bgBlue); // 'foo bar' (blue background)
 console.log(colorize(colorize('foo').yellow, colorize('foo').green).bgWhite); // 'foo bar' (first word in yellow letters, second word in green letters, white background for both)
 ```