1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.SanitizersImpl = void 0;
|
4 | const validator = require("validator");
|
5 | const sanitization_1 = require("../context-items/sanitization");
|
6 | class SanitizersImpl {
|
7 | constructor(builder, chain) {
|
8 | this.builder = builder;
|
9 | this.chain = chain;
|
10 | }
|
11 |
|
12 | customSanitizer(sanitizer) {
|
13 | this.builder.addItem(new sanitization_1.Sanitization(sanitizer, true));
|
14 | return this.chain;
|
15 | }
|
16 | default(default_value) {
|
17 | return this.customSanitizer(value => [undefined, null, NaN, ''].includes(value) ? default_value : value);
|
18 | }
|
19 | replace(values_to_replace, new_value) {
|
20 | if (!Array.isArray(values_to_replace)) {
|
21 | values_to_replace = [values_to_replace];
|
22 | }
|
23 | return this.customSanitizer(value => (values_to_replace.includes(value) ? new_value : value));
|
24 | }
|
25 |
|
26 | addStandardSanitization(sanitizer, ...options) {
|
27 | this.builder.addItem(new sanitization_1.Sanitization(sanitizer, false, options));
|
28 | return this.chain;
|
29 | }
|
30 | blacklist(chars) {
|
31 | return this.addStandardSanitization(validator.blacklist, chars);
|
32 | }
|
33 | escape() {
|
34 | return this.addStandardSanitization(validator.escape);
|
35 | }
|
36 | unescape() {
|
37 | return this.addStandardSanitization(validator.unescape);
|
38 | }
|
39 | ltrim(chars) {
|
40 | return this.addStandardSanitization(validator.ltrim, chars);
|
41 | }
|
42 | normalizeEmail(options) {
|
43 | return this.addStandardSanitization(validator.normalizeEmail, options);
|
44 | }
|
45 | rtrim(chars) {
|
46 | return this.addStandardSanitization(validator.rtrim, chars);
|
47 | }
|
48 | stripLow(keep_new_lines) {
|
49 | return this.addStandardSanitization(validator.stripLow, keep_new_lines);
|
50 | }
|
51 | toArray() {
|
52 | return this.customSanitizer(value => (value !== undefined && ((Array.isArray(value) && value) || [value])) || []);
|
53 | }
|
54 | toBoolean(strict) {
|
55 | return this.addStandardSanitization(validator.toBoolean, strict);
|
56 | }
|
57 | toDate() {
|
58 | return this.addStandardSanitization(validator.toDate);
|
59 | }
|
60 | toFloat() {
|
61 | return this.addStandardSanitization(validator.toFloat);
|
62 | }
|
63 | toInt(radix) {
|
64 | return this.addStandardSanitization(validator.toInt, radix);
|
65 | }
|
66 | toLowerCase() {
|
67 | return this.customSanitizer(value => (typeof value === 'string' ? value.toLowerCase() : value));
|
68 | }
|
69 | toUpperCase() {
|
70 | return this.customSanitizer(value => (typeof value === 'string' ? value.toUpperCase() : value));
|
71 | }
|
72 | trim(chars) {
|
73 | return this.addStandardSanitization(validator.trim, chars);
|
74 | }
|
75 | whitelist(chars) {
|
76 | return this.addStandardSanitization(validator.whitelist, chars);
|
77 | }
|
78 | }
|
79 | exports.SanitizersImpl = SanitizersImpl;
|