sensitive.ts 984 B

123456789101112131415161718192021222324252627282930313233
  1. const INVALID_TEXT_VALUES = new Set(['-', 'null', 'undefined']);
  2. const CERT_FIELD_KEYS = new Set([
  3. 'certno',
  4. 'personcertno',
  5. 'othercertno',
  6. 'selfcertno',
  7. 'querycertno',
  8. 'togethercertno',
  9. 'livewithcertno',
  10. 'objidcard',
  11. 'idcard',
  12. 'identitycode',
  13. 'sfz',
  14. ]);
  15. const normalizeSensitiveText = (value?: string | number | null) => {
  16. const text = String(value ?? '').trim();
  17. if (!text || INVALID_TEXT_VALUES.has(text.toLowerCase())) return '';
  18. return text;
  19. };
  20. export const isCertFieldKey = (value?: string | number | null) => {
  21. const text = normalizeSensitiveText(value);
  22. if (!text) return false;
  23. return CERT_FIELD_KEYS.has(text.replace(/[_-]/g, '').toLowerCase());
  24. };
  25. export const maskCertNo = (value?: string | number | null, keepStart = 4, keepEnd = 4) => {
  26. const text = normalizeSensitiveText(value);
  27. if (!text) return '-';
  28. if (text.length <= keepStart + keepEnd) return text;
  29. return `${text.slice(0, keepStart)}****${text.slice(-keepEnd)}`;
  30. };