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 ]
@@ -978,6 +978,12 @@ yesNo('Foo', true); // true
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) => {
const isDescending = arr[0] > arr[arr.length - 1];
const index = arr.findIndex(el => (isDescending ? n >= el : n <= el));
diff --git a/snippets/mask.md b/snippets/mask.md
index c030d0828..11983e0af 100644
--- a/snippets/mask.md
+++ b/snippets/mask.md
@@ -8,13 +8,13 @@ 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);
+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$$$$$$'
+mask(1234567890); // '******7890'
+mask(1234567890, 3); // '*******890'
+mask(1234567890, 4, '$'); // '$$$$$$7890'
+mask(1234567890, -4, '$'); // '1234$$$$$$'
```
diff --git a/tag_database b/tag_database
index 6943467cc..ff8253483 100644
--- a/tag_database
+++ b/tag_database
@@ -88,6 +88,7 @@ last:array
lcm:math
lowercaseKeys:object
mapObject:array
+mask:uncategorized
max:math
median:math
memoize:function