-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
51 lines (43 loc) · 1.76 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
export * from './Registry'
export * from './Scrubs'
export * from './ScrubsInterceptor'
import { Is } from '@nofrills/types'
import { Lincoln, Logger } from './Logging'
import { Registry } from './Registry'
import { Scrubs, ScrubsOptions } from './Scrubs'
import merge = require('lodash.merge')
const log: Lincoln = Logger.extend('scrubslib')
Registry.register<any[]>('array', (value: any[], options: ScrubsOptions, instance: Scrubs): any[] => {
return value.map((v) => instance.scrub(v))
})
Registry.register<any>('object', (value: any, options: ScrubsOptions, instance: Scrubs): any => {
const walk = (object: any): any => {
Object.keys(object).forEach((key: string) => {
if (options.properties.indexOf(key) >= 0) {
object[key] = options.text
} else if (Is.array(object[key]) || Is.object(object[key])) {
object[key] = walk(object[key])
} else if (Is.string(object[key])) {
object[key] = instance.scrub<string>(object[key])
} else {
object[key] = value
}
})
return object
}
const clone: any = walk(merge({}, value))
log.debug('ObjectScrubber', value, clone)
return clone
})
Registry.register<string>('string', (value: string, options: ScrubsOptions): string => {
const regex: RegExp = new RegExp(`([${options.properties.join('|')}]=[\'"]?)\\w+([\'"]?&?)`, 'g')
const transformed: string = value.replace(regex, `$1${options.text}$2`)
log.debug('StringScrubber', value, transformed)
return transformed
})
Registry.register<string>('string', (value: string, options: ScrubsOptions): string => {
const regex: RegExp = /(http[s]:\/\/\w+:)\w+(@.*)/g
const transformed: string = value.replace(regex, `$1${options.text}$2`)
log.debug('StringScrubber', value, transformed)
return transformed
})