From 6d8b3c325c0fe1d97a29277e5e0f67cbac0a12f8 Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Tue, 23 Jan 2018 19:28:55 +0000 Subject: [PATCH] Travis build: 1356 --- README.md | 27 +++++++++++++++++++++++++++ docs/index.html | 8 +++++++- snippets/nthArg.md | 6 +++--- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3917284fe..d93865803 100644 --- a/README.md +++ b/README.md @@ -375,6 +375,7 @@ average(1, 2, 3); * [`hexToRGB`](#hextorgb-) * [`httpGet`](#httpget) * [`httpPost`](#httppost) +* [`nthArg`](#ntharg) * [`parseCookie`](#parsecookie) * [`prettyBytes`](#prettybytes) * [`randomHexColorCode`](#randomhexcolorcode) @@ -6396,6 +6397,32 @@ Logs: {
[⬆ Back to top](#table-of-contents) +### nthArg + +Creates a function that gets the argument at index `n`. If `n` is negative, the nth argument from the end is returned. + +Use `Array.slice()` to get the desired argument at index `n`. + +```js +const nthArg = n => (...args) => args.slice(n)[0]; +``` + +
+Examples + +```js +const third = nthArg(2); +third(1, 2, 3); // 3 +third(1, 2); // undefined +const last = nthArg(-1); +last(1, 2, 3, 4, 5); // 5 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### parseCookie Parse an HTTP Cookie header string and return an object of all cookie name-value pairs. diff --git a/docs/index.html b/docs/index.html index 110f8eb3c..8c64efc76 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 ]
@@ -1502,6 +1502,12 @@ Logs: {
   "id": 101
 }
 */
+

nthArg

Creates a function that gets the argument at index n. If n is negative, the nth argument from the end is returned.

Use Array.slice() to get the desired argument at index n.

const nthArg = n => (...args) => args.slice(n)[0];
+
const third = nthArg(2);
+third(1, 2, 3); // 3
+third(1, 2); // undefined
+const last = nthArg(-1);
+last(1, 2, 3, 4, 5); // 5
 

parseCookie

Parse an HTTP Cookie header string and return an object of all cookie name-value pairs.

Use String.split(';') to separate key-value pairs from each other. Use Array.map() and String.split('=') to separate keys from values in each pair. Use Array.reduce() and decodeURIComponent() to create an object with all key-value pairs.

const parseCookie = str =>
   str
     .split(';')
diff --git a/snippets/nthArg.md b/snippets/nthArg.md
index c94224326..159975b89 100644
--- a/snippets/nthArg.md
+++ b/snippets/nthArg.md
@@ -10,8 +10,8 @@ const nthArg = n => (...args) => args.slice(n)[0];
 
 ```js
 const third = nthArg(2);
-third(1,2,3); // 3
-third(1,2); // undefined
+third(1, 2, 3); // 3
+third(1, 2); // undefined
 const last = nthArg(-1);
-last(1,2,3,4,5); // 5
+last(1, 2, 3, 4, 5); // 5
 ```