Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding mask formatting example #287

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}));
Loading