-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding mask formatting example (#287)
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / unit-tests (20)
Check warning on line 59 in examples/mask.js GitHub Actions / unit-tests (20)
Check warning on line 59 in examples/mask.js GitHub Actions / unit-tests (18)
Check warning on line 59 in examples/mask.js GitHub Actions / unit-tests (18)
Check warning on line 59 in examples/mask.js GitHub Actions / unit-tests (16)
|
||
level: 'info', | ||
message: apiResponse | ||
})); |