From 5cee65560603a0349fc7a724333df535e6293fd5 Mon Sep 17 00:00:00 2001 From: Travis CI Date: Mon, 1 Jan 2018 12:44:47 +0000 Subject: [PATCH] Travis build: 769 [ci skip] --- README.md | 120 ++++++++++++++++++++++++++---------------------- docs/index.html | 40 ++++++++-------- 2 files changed, 85 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index f7bd918b7..9b8048e10 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ * [`initializeArrayWithRange`](#initializearraywithrange) * [`initializeArrayWithValues`](#initializearraywithvalues) * [`intersection`](#intersection) +* [`join`](#join) * [`last`](#last) * [`mapObject`](#mapobject) * [`nthElement`](#nthelement) @@ -220,6 +221,7 @@ * [`escapeRegExp`](#escaperegexp) * [`fromCamelCase`](#fromcamelcase) * [`isAbsoluteURL`](#isabsoluteurl) +* [`mask`](#mask) * [`palindrome`](#palindrome) * [`repeatString`](#repeatstring) * [`reverseString`](#reversestring) @@ -272,8 +274,6 @@
View contents -* [`join`](#join) -* [`mask`](#mask) * [`sortedIndex`](#sortedindex)
@@ -922,6 +922,40 @@ intersection([1, 2, 3], [4, 3, 2]); // [2,3]
[⬆ Back to top](#table-of-contents) +### join + +Joins all elements of an array into a string and returns this string. Uses a separator and an end separator. + +Use `Array.reduce()` to combine elements into a string. +Omit the second argument, `separator`, to use a default separator of `','`. +Omit the third argument, `end`, to use the same value as `separator` by default. + +```js +const join = (arr, separator = ',', end = separator) => + arr.reduce( + (acc, val, i) => + i == arr.length - 2 + ? acc + val + end + : i == arr.length - 1 ? acc + val : acc + val + separator, + '' + ); +``` + +
+Examples + +```js +join(); // '' +join(['pen', 'pineapple', 'apple', 'pen'], ',', '&'); //"pen,pineapple,apple&pen" +join(['pen', 'pineapple', 'apple', 'pen'], ','); //"pen,pineapple,apple,pen" +join(['pen', 'pineapple', 'apple', 'pen']); //"pen,pineapple,apple,pen" +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### last Returns the last element in an array. @@ -3618,6 +3652,35 @@ isAbsoluteURL('/foo/bar'); // false
[⬆ Back to top](#table-of-contents) +### mask + +Replaces all but the last `num` of characters with the specified mask character. + +Use `String.slice()` to grab the portion of the characters that need to be masked and use `String.replace()` with a regex to replace every character with the mask character. +Concatenate the masked characters with the remaining unmasked portion of the string. +Omit the second argument, `num`, to keep a default of `4` characters unmasked. +Omit the third argument, `mask`, to use a default character of `'*'` for the mask. + +```js +const mask = (cc, num = 4, mask = '*') => + ('' + cc).slice(0, -num).replace(/./g, mask) + ('' + cc).slice(-num); +``` + +
+Examples + +```js +mask(1234567890); // '******7890' +mask(1234567890, 3); // '*******890' +mask(1234567890, 4, '$'); // '$$$$$$7890' +mask(1234567890, -4, '$'); // '1234$$$$$$' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### palindrome Returns `true` if the given string is a palindrome, `false` otherwise. @@ -4591,59 +4654,6 @@ yesNo('Foo', true); // true --- ## _Uncategorized_ -### join - -Joins all elements of an array into a string and returns this string. Uses a separator and an end separator. - -Use `Array.reduce()` to combine elements into a string. -Omit the second argument, `separator`, to use a default separator of `','`. -Omit the third argument, `end`, to use the same value as `separator` by default. - -```js -const join = (arr, separator = ',', end = separator) => - arr.reduce( - (acc, val, i) => - i == arr.length - 2 - ? acc + val + end - : i == arr.length - 1 ? acc + val : acc + val + separator, - '' - ); -``` - -```js -join(); // '' -join(['pen', 'pineapple', 'apple', 'pen'], ',', '&'); //"pen,pineapple,apple&pen" -join(['pen', 'pineapple', 'apple', 'pen'], ','); //"pen,pineapple,apple,pen" -join(['pen', 'pineapple', 'apple', 'pen']); //"pen,pineapple,apple,pen" -``` - -
[⬆ back to top](#table-of-contents) - - -### mask - -Replaces all but the last `num` of characters with the specified mask character. - -Use `String.slice()` to grab the portion of the characters that need to be masked and use `String.replace()` with a regex to replace every character with the mask character. -Concatenate the masked characters with the remaining unmasked portion of the string. -Omit the second argument, `num`, to keep a default of `4` characters unmasked. -Omit the third argument, `mask`, to use a default character of `'*'` for the mask. - -```js -const mask = (cc, num = 4, mask = '*') => - ('' + cc).slice(0, -num).replace(/./g, mask) + ('' + cc).slice(-num); -``` - -```js -mask(1234567890); // '******7890' -mask(1234567890, 3); // '*******890' -mask(1234567890, 4, '$'); // '$$$$$$7890' -mask(1234567890, -4, '$'); // '1234$$$$$$' -``` - -
[⬆ back to top](#table-of-contents) - - ### sortedIndex Returns the lowest index at which value should be inserted into array in order to maintain its sort order. diff --git a/docs/index.html b/docs/index.html index 3f4346954..c1a7a5fc3 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 ]
@@ -163,6 +163,18 @@ initializeArrayWithRange(7, 3); // [3,4,5,6,7]
   return a.filter(x => s.has(x));
 };
 
intersection([1, 2, 3], [4, 3, 2]); // [2,3]
+

join

Joins all elements of an array into a string and returns this string. Uses a separator and an end separator.

Use Array.reduce() to combine elements into a string. Omit the second argument, separator, to use a default separator of ','. Omit the third argument, end, to use the same value as separator by default.

const join = (arr, separator = ',', end = separator) =>
+  arr.reduce(
+    (acc, val, i) =>
+      i == arr.length - 2
+        ? acc + val + end
+        : i == arr.length - 1 ? acc + val : acc + val + separator,
+    ''
+  );
+
join(); // ''
+join(['pen', 'pineapple', 'apple', 'pen'], ',', '&'); //"pen,pineapple,apple&pen"
+join(['pen', 'pineapple', 'apple', 'pen'], ','); //"pen,pineapple,apple,pen"
+join(['pen', 'pineapple', 'apple', 'pen']); //"pen,pineapple,apple,pen"
 

last

Returns the last element in an array.

Use arr.length - 1 to compute the index of the last element of the given array and returning it.

const last = arr => arr[arr.length - 1];
 
last([1, 2, 3]); // 3
 

mapObject

Maps the values of an array to an object using a function, where the key-value pairs consist of the original value as the key and the mapped value.

Use an anonymous inner function scope to declare an undefined memory space, using closures to store a return value. Use a new Array to store the array with a map of the function over its data set and a comma operator to return a second step, without needing to move from one context to another (due to closures and order of operations).

const mapObject = (arr, fn) =>
@@ -728,6 +740,12 @@ fromCamelCase('someJavascriptProperty', '_'); // 'some_javascript_property'
 
isAbsoluteURL('https://google.com'); // true
 isAbsoluteURL('ftp://www.myserver.net'); // true
 isAbsoluteURL('/foo/bar'); // false
+

mask

Replaces all but the last num of characters with the specified mask character.

Use String.slice() to grab the portion of the characters that need to be masked and use String.replace() with a regex to replace every character with the mask character. Concatenate the masked characters with the remaining unmasked portion of the string. Omit the second argument, num, to keep a default of 4 characters unmasked. Omit the third argument, mask, to use a default character of '*' for the mask.

const mask = (cc, num = 4, mask = '*') =>
+  ('' + cc).slice(0, -num).replace(/./g, mask) + ('' + cc).slice(-num);
+
mask(1234567890); // '******7890'
+mask(1234567890, 3); // '*******890'
+mask(1234567890, 4, '$'); // '$$$$$$7890'
+mask(1234567890, -4, '$'); // '1234$$$$$$'
 

palindrome

Returns true if the given string is a palindrome, false otherwise.

Convert string toLowerCase() and use replace() to remove non-alphanumeric characters from it. Then, split('') into individual characters, reverse(), join('') and compare to the original, unreversed string, after converting it tolowerCase().

const palindrome = str => {
   const s = str.toLowerCase().replace(/[\W_]/g, '');
   return (
@@ -966,25 +984,7 @@ console.log(sdbm('age')); // 808122783
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
-

Uncategorized

join

Joins all elements of an array into a string and returns this string. Uses a separator and an end separator.

Use Array.reduce() to combine elements into a string. Omit the second argument, separator, to use a default separator of ','. Omit the third argument, end, to use the same value as separator by default.

const join = (arr, separator = ',', end = separator) =>
-  arr.reduce(
-    (acc, val, i) =>
-      i == arr.length - 2
-        ? acc + val + end
-        : i == arr.length - 1 ? acc + val : acc + val + separator,
-    ''
-  );
-
join(); // ''
-join(['pen', 'pineapple', 'apple', 'pen'], ',', '&'); //"pen,pineapple,apple&pen"
-join(['pen', 'pineapple', 'apple', 'pen'], ','); //"pen,pineapple,apple,pen"
-join(['pen', 'pineapple', 'apple', 'pen']); //"pen,pineapple,apple,pen"
-

mask

Replaces all but the last num of characters with the specified mask character.

Use String.slice() to grab the portion of the characters that need to be masked and use String.replace() with a regex to replace every character with the mask character. Concatenate the masked characters with the remaining unmasked portion of the string. Omit the second argument, num, to keep a default of 4 characters unmasked. Omit the third argument, mask, to use a default character of '*' for the mask.

const mask = (cc, num = 4, mask = '*') =>
-  ('' + cc).slice(0, -num).replace(/./g, mask) + ('' + cc).slice(-num);
-
mask(1234567890); // '******7890'
-mask(1234567890, 3); // '*******890'
-mask(1234567890, 4, '$'); // '$$$$$$7890'
-mask(1234567890, -4, '$'); // '1234$$$$$$'
-

sortedIndex

Returns the lowest index at which value should be inserted into array in order to maintain its sort order.

Check if the array is sorted in descending order (loosely). Use Array.findIndex() to find the appropriate index where the element should be inserted.

const sortedIndex = (arr, n) => {
+

Uncategorized

sortedIndex

Returns the lowest index at which value should be inserted into array in order to maintain its sort order.

Check if the array is sorted in descending order (loosely). Use Array.findIndex() to find the appropriate index where the element should be inserted.

const sortedIndex = (arr, n) => {
   const isDescending = arr[0] > arr[arr.length - 1];
   const index = arr.findIndex(el => (isDescending ? n >= el : n <= el));
   return index === -1 ? arr.length : index;