From afda3af75bba310f6e897f661cd05cf5bcfaa690 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Mon, 1 Jan 2018 16:32:59 +0530 Subject: [PATCH 1/6] add mask --- snippets/mask.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 snippets/mask.md diff --git a/snippets/mask.md b/snippets/mask.md new file mode 100644 index 000000000..3693e5d9a --- /dev/null +++ b/snippets/mask.md @@ -0,0 +1,15 @@ +### mask + +It replace all but the last(first if `num` is negative) `num` (by default it is 4) characters by the provided mask(`'*'` by default). + +```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$$$$$$' +``` From 42f79065b586398a17b847a7590b836bb0251e6a Mon Sep 17 00:00:00 2001 From: atomiks Date: Mon, 1 Jan 2018 22:06:24 +1100 Subject: [PATCH 2/6] Update mask.md --- snippets/mask.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/mask.md b/snippets/mask.md index 3693e5d9a..77c817c4d 100644 --- a/snippets/mask.md +++ b/snippets/mask.md @@ -1,6 +1,6 @@ ### mask -It replace all but the last(first if `num` is negative) `num` (by default it is 4) characters by the provided mask(`'*'` by default). +It replace all but the last `num` (first if `num` is negative, by default `4`) characters by the provided masking character (`'*'` by default). ```js const mask = (cc,num = 4,mask = '*') => From 73c1cf7f5835372118370f978eb3a5f3681f5d3e Mon Sep 17 00:00:00 2001 From: atomiks Date: Mon, 1 Jan 2018 22:09:35 +1100 Subject: [PATCH 3/6] Update mask.md --- snippets/mask.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/snippets/mask.md b/snippets/mask.md index 77c817c4d..7e5875736 100644 --- a/snippets/mask.md +++ b/snippets/mask.md @@ -1,6 +1,8 @@ ### mask -It replace all but the last `num` (first if `num` is negative, by default `4`) characters by the provided masking character (`'*'` by default). +Replaces all but the last `num` of characters (first if `num` is negative, by default `4`) with the provided mask character (`'*'` by default). + +Use `Array.slice()` to grab the portion of the characters that need to be masked and use `Array.replace()` with a regex to replace every character with the mask character. Concatenate the masked characters with the remaining unmasked portion of the string. ```js const mask = (cc,num = 4,mask = '*') => From f811f685479256814711242dd57a3104a6afc375 Mon Sep 17 00:00:00 2001 From: atomiks Date: Mon, 1 Jan 2018 22:10:19 +1100 Subject: [PATCH 4/6] Update mask.md --- snippets/mask.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/mask.md b/snippets/mask.md index 7e5875736..44a74969e 100644 --- a/snippets/mask.md +++ b/snippets/mask.md @@ -2,7 +2,7 @@ Replaces all but the last `num` of characters (first if `num` is negative, by default `4`) with the provided mask character (`'*'` by default). -Use `Array.slice()` to grab the portion of the characters that need to be masked and use `Array.replace()` with a regex 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 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. ```js const mask = (cc,num = 4,mask = '*') => From 629ecb3cff24b5c277da54523a86f4c194cb0a10 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Jan 2018 14:39:25 +0200 Subject: [PATCH 5/6] Update mask.md --- snippets/mask.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/snippets/mask.md b/snippets/mask.md index 44a74969e..09e8a096d 100644 --- a/snippets/mask.md +++ b/snippets/mask.md @@ -1,8 +1,11 @@ ### mask -Replaces all but the last `num` of characters (first if `num` is negative, by default `4`) with the provided mask character (`'*'` by default). +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. +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 = '*') => From 04f12f285ea79faaaf51f61bf8493d5e3179dd5d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Jan 2018 14:40:29 +0200 Subject: [PATCH 6/6] Update mask.md --- snippets/mask.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/mask.md b/snippets/mask.md index 09e8a096d..c030d0828 100644 --- a/snippets/mask.md +++ b/snippets/mask.md @@ -9,7 +9,7 @@ Omit the third argument, `mask`, to use a default character of `'*'` for the mas ```js const mask = (cc,num = 4,mask = '*') => - cc.slice(0, -num).replace(/./g, mask) + cc.slice(-num); + ('' + cc).slice(0, -num).replace(/./g, mask) + ('' + cc).slice(-num); ``` ```js