Skip to content

Commit

Permalink
Adding mask formatting example (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
escodel authored Nov 8, 2023
1 parent 8332587 commit ded082a
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions examples/mask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { format } = require('../');

const apiResponse = {
creditCard: 1111222233334444,
ssn: 4873945739834,
customer: {
firstName: 'Jim',
lastName: 'Davis',
address: {
number: '404',
street: 'Poughkeepsie Lane',
city: 'Alpharetta',
state: 'GA'
}
},
accounts: [
{
bank: 'Bank of America',
accountNumber: 1290382430
},
{
bank: 'Chase',
accountNumber: 423523235
},
{
bank: 'Wells Fargo',
accountNumber: 1554235555235460
}
]
};

const maskDataFormat = format((info) => {
const mask = (data, maskCharactersVisible = 0, maskCharacter = '*') => {
Object.keys(data).forEach((key) => {
const strKey = `${data[key]}`;
const strLength = strKey.length;
if ((typeof data[key] === 'object' || Array.isArray(key)) && data[key]) {
mask(data[key], maskCharactersVisible, maskCharacter);
} else if (key === 'accountNumber' || key === 'creditCard') {
if (maskCharactersVisible > 0 && maskCharactersVisible < strLength) {
data[key] =
maskCharacter.repeat(
strKey.slice(0, strLength - maskCharactersVisible).length
) + strKey.slice(strLength - maskCharactersVisible);
} else {
data[key] = maskCharacter.repeat(strLength);
}
}
});

return data;
};

mask(info.message, 4, '%');
return info;
});
const mdf = maskDataFormat();

console.dir(mdf.transform({

Check warning on line 59 in examples/mask.js

View workflow job for this annotation

GitHub Actions / unit-tests (20)

Unexpected console statement

Check warning on line 59 in examples/mask.js

View workflow job for this annotation

GitHub Actions / unit-tests (20)

Unexpected console statement

Check warning on line 59 in examples/mask.js

View workflow job for this annotation

GitHub Actions / unit-tests (18)

Unexpected console statement

Check warning on line 59 in examples/mask.js

View workflow job for this annotation

GitHub Actions / unit-tests (18)

Unexpected console statement

Check warning on line 59 in examples/mask.js

View workflow job for this annotation

GitHub Actions / unit-tests (16)

Unexpected console statement

Check warning on line 59 in examples/mask.js

View workflow job for this annotation

GitHub Actions / unit-tests (16)

Unexpected console statement
level: 'info',
message: apiResponse
}));

0 comments on commit ded082a

Please sign in to comment.