HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Mask a JavaScript value

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
maskjavascriptvalue

Problem

One of the most common bits of presentational logic related to string manipulation is masking sensitive information. This is most often seen in relation to saved credit card numbers, but there are other use cases as well. The most common masking format replaces most characters, except for the last few, with a mask character, such as an asterisk (*).
Implementing a helper function to mask any value is pretty simple overall. First, we can use a template literal to convert the value to a string. Then, using String.prototype.slice(), we can grab the portion of the characters that will remain unmasked. Finally, we can use String.prototype.padStart() to fill the beginning of the string with the mask character up to the original length.
Using a negative num argument will place the unmasked characters at the start of the string. Omitting the second argument, num, will keep a default of 4 characters unmasked. Omitting the third argument, mask, will use a default character of '*' for the mask.

Solution

const mask = (cc, num = 4, mask = '*') =>
  `${cc}`.slice(-num).padStart(`${cc}`.length, mask);

mask(1234567890); // '******7890'
mask(1234567890, 3); // '*******890'
mask(1234567890, -4, '

Using a negative num argument will place the unmasked characters at the start of the string. Omitting the second argument, num, will keep a default of 4 characters unmasked. Omitting the third argument, mask, will use a default character of '*' for the mask.); // '$$567890'


Using a negative num argument will place the unmasked characters at the start of the string. Omitting the second argument, num, will keep a default of 4 characters unmasked. Omitting the third argument, mask, will use a default character of '*' for the mask.

Code Snippets

const mask = (cc, num = 4, mask = '*') =>
  `${cc}`.slice(-num).padStart(`${cc}`.length, mask);

mask(1234567890); // '******7890'
mask(1234567890, 3); // '*******890'
mask(1234567890, -4, '$'); // '$$$$567890'

Context

From 30-seconds-of-code: mask-string

Revisions (0)

No revisions yet.