Skip to content

Instantly share code, notes, and snippets.

@AlaeddineMessadi
Created February 7, 2021 11:42
Show Gist options
  • Save AlaeddineMessadi/a2452a48fa9131be7055a1e4384dca25 to your computer and use it in GitHub Desktop.
Save AlaeddineMessadi/a2452a48fa9131be7055a1e4384dca25 to your computer and use it in GitHub Desktop.
Credit Card Masking #### with RegEx in javascript
function maskify(input) {
return input.replace(/.(?=.{4})/g, "#");
}
/** _Tests__ **/
let assert = require('chai').assert
require('mocha').describe;
require('mocha').it;
describe('Challenge - Maskify', () => {
it('should return a string ouput', () => {
assert.typeOf(maskify(""), 'string');
});
it('should mask the digits of standard credit cards', () => {
assert.equal(maskify("5512103073210694"), "############0694");
});
it('should not mask the digits of short credit cards', () => {
assert.equal(maskify("54321"), "54321");
});
it('should return non digit characters without masking', () => {
assert.equal(maskify("Skippy"), "Skippy");
});
it('should handle the empty string input correctly', () => {
assert.equal(maskify(""), "");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment