11import { createHash } from 'node:crypto' ;
22import {
33 BUILTIN_REGEX_PATTERNS ,
4- DEFAULT_REDACTION_PATTERNS ,
54 type BuiltinPatternName ,
5+ DEFAULT_REDACTION_PATTERNS ,
66 type RedactionConfig ,
77 type RedactionOptions ,
88 type RedactionResult ,
@@ -13,22 +13,24 @@ import {
1313 * Supports built-in pattern names and custom regex strings
1414 */
1515export function resolvePatterns ( patterns : string [ ] ) : { regex : RegExp ; name : string } [ ] {
16- return patterns . map ( ( pattern ) => {
17- // Check if it's a built-in pattern name
18- if ( pattern in BUILTIN_REGEX_PATTERNS ) {
19- const regex = BUILTIN_REGEX_PATTERNS [ pattern as BuiltinPatternName ] ;
20- return { regex : new RegExp ( regex . source , regex . flags ) , name : pattern } ;
21- }
16+ return patterns
17+ . map ( ( pattern ) => {
18+ // Check if it's a built-in pattern name
19+ if ( pattern in BUILTIN_REGEX_PATTERNS ) {
20+ const regex = BUILTIN_REGEX_PATTERNS [ pattern as BuiltinPatternName ] ;
21+ return { regex : new RegExp ( regex . source , regex . flags ) , name : pattern } ;
22+ }
2223
23- // Treat as custom regex
24- try {
25- return { regex : new RegExp ( pattern , 'g' ) , name : `custom:${ pattern . slice ( 0 , 20 ) } ` } ;
26- } catch {
27- // Invalid regex, skip it
28- console . warn ( `Invalid redaction pattern: ${ pattern } ` ) ;
29- return null ;
30- }
31- } ) . filter ( ( p ) : p is { regex : RegExp ; name : string } => p !== null ) ;
24+ // Treat as custom regex
25+ try {
26+ return { regex : new RegExp ( pattern , 'g' ) , name : `custom:${ pattern . slice ( 0 , 20 ) } ` } ;
27+ } catch {
28+ // Invalid regex, skip it
29+ console . warn ( `Invalid redaction pattern: ${ pattern } ` ) ;
30+ return null ;
31+ }
32+ } )
33+ . filter ( ( p ) : p is { regex : RegExp ; name : string } => p !== null ) ;
3234}
3335
3436/**
@@ -54,7 +56,7 @@ export function createRedactionOptions(config: RedactionConfig): RedactionOption
5456export function redactText (
5557 text : string ,
5658 patterns : ( string | RegExp ) [ ] ,
57- replacement : string = '[REDACTED]'
59+ replacement = '[REDACTED]'
5860) : RedactionResult {
5961 if ( ! text || patterns . length === 0 ) {
6062 return {
@@ -102,7 +104,7 @@ export function hashText(text: string): string {
102104export function redactWithHash (
103105 text : string ,
104106 patterns : ( string | RegExp ) [ ] ,
105- replacement : string = '[REDACTED]'
107+ replacement = '[REDACTED]'
106108) : RedactionResult & { originalHash : string } {
107109 const originalHash = hashText ( text ) ;
108110 const result = redactText ( text , patterns , replacement ) ;
0 commit comments