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;