UNPKG

@ogcio/o11y-sdk-node

Version:

Opentelemetry standard instrumentation SDK for NodeJS based project

30 lines (29 loc) 1.12 kB
import { BasicRedactor } from "./basic-redactor.js"; export class PpsnRedactor extends BasicRedactor { // iOS < 16.6 compatible: no lookbehind/lookahead static PPSN_REGEX = /\d{7}[a-z]{1,2}/gi; redact(value) { let redactedCounter = 0; // Boundary checking to replace lookbehind/lookahead const redactedValue = value.replace(PpsnRedactor.PPSN_REGEX, (match, offset) => { const before = offset > 0 ? value[offset - 1] : ""; const after = offset + match.length < value.length ? value[offset + match.length] : ""; // Check if surrounded by non-alphanumeric characters const isValidBefore = !before || !/\w/.test(before); const isValidAfter = !after || !/\w/.test(after); if (isValidBefore && isValidAfter) { redactedCounter++; return "[REDACTED PPSN]"; } return match; }); return { redactedValue: redactedValue, context: { redactedCounter, }, }; } }