-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sanitization.ts
40 lines (36 loc) · 1.43 KB
/
Sanitization.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
export enum SanitizationType {
Trim, Alpha, Numeric, AlphaNumeric, UpperCase, LowerCase, NoSpaces
}
export class Sanitization {
private static _sanitizationRules: any = {
Trim: ( value: string ) => {
return (typeof value !== "string") ? value: value.trim();
},
UpperCase: ( value: string ) => {
return (typeof value !== "string") ? value: value.toUpperCase();
},
LowerCase: ( value: string ) => {
return (typeof value !== "string") ? value: value.toLowerCase();
},
Numeric: ( value: string ) => {
return (typeof value !== "string") ? value: value.replace( /[^0-9.\- ]/g, '' );
},
Alpha: ( value: string ) => {
return (typeof value !== "string") ? value: value.replace( /[^a-zA-Z ]/g, '' );
},
AlphaNumeric: ( value: string ) => {
return (typeof value !== "string") ? value: value.replace( /[^a-zA-Z0-9 ]/g, '' );
},
NoSpaces: ( value: string ) => {
return (typeof value !== "string") ? value: value.replace( /[\n\t\r ]/g, '' );
}
};
constructor( private _sanitization: SanitizationType[] ) {
}
Sanitize( value: any ): any {
for( var i = 0; i < this._sanitization.length; i++ ) {
value = Sanitization._sanitizationRules[ SanitizationType[ this._sanitization[i] ] ]( <any>value );
}
return value;
}
}