From 4cbb97733ba09283f68c46e4869fa8148f083513 Mon Sep 17 00:00:00 2001 From: Theo Menkes Reis Date: Tue, 25 Sep 2018 00:21:18 +0000 Subject: [PATCH 1/2] simpler solution to mask using padStart --- snippets/mask.md | 5 ++--- test/mask/mask.js | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/snippets/mask.md b/snippets/mask.md index d7eec0417..472cd3ed0 100644 --- a/snippets/mask.md +++ b/snippets/mask.md @@ -2,14 +2,13 @@ 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 regexp to replace every character with the mask character. -Concatenate the masked characters with the remaining unmasked portion of the string. +Use `String.slice()` to grab the portion of the characters that will remain unmasked and use `String.padStart()` to fill the beginning of the string with the mask character up to the original length. Omit the second argument, `num`, to keep a default of `4` characters unmasked. If `num` is negative, the unmasked characters will be at the start of the string. 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); + ('' + cc).slice(-num).padStart(('' + cc).length, mask); ``` ```js diff --git a/test/mask/mask.js b/test/mask/mask.js index 8f3b55140..92709f034 100644 --- a/test/mask/mask.js +++ b/test/mask/mask.js @@ -1,3 +1,3 @@ const mask = (cc, num = 4, mask = '*') => - ('' + cc).slice(0, -num).replace(/./g, mask) + ('' + cc).slice(-num); + ('' + cc).slice(-num).padStart(('' + cc).length, mask); module.exports = mask; From aa22ed3bbb26ad8250e7f476d2507d1773d7f5dd Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 26 Sep 2018 12:27:04 +0300 Subject: [PATCH 2/2] Update mask.md --- snippets/mask.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/snippets/mask.md b/snippets/mask.md index 472cd3ed0..46d31bbad 100644 --- a/snippets/mask.md +++ b/snippets/mask.md @@ -7,8 +7,7 @@ Omit the second argument, `num`, to keep a default of `4` characters unmasked. I Omit the third argument, `mask`, to use a default character of `'*'` for the mask. ```js -const mask = (cc, num = 4, mask = '*') => - ('' + cc).slice(-num).padStart(('' + cc).length, mask); +const mask = (cc, num = 4, mask = '*') => `${cc}`.slice(-num).padStart(`${cc}`.length, mask); ``` ```js