redact-it 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -4,58 +4,45 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  const percentageValueMasker = (value, mask) => {
6
6
  var _a, _b, _c, _d;
7
-
8
7
  const redactor = (_a = mask.redactWith) !== null && _a !== void 0 ? _a : "•";
9
8
  const percentage = (_b = mask.percentage) !== null && _b !== void 0 ? _b : 100;
10
9
  const complementary = (_c = mask.position === "center" && mask.complementary) !== null && _c !== void 0 ? _c : false;
11
10
  const position = (_d = mask.position) !== null && _d !== void 0 ? _d : "left";
12
-
13
11
  const finalRedactor = p1 => redactor.length > 1 ? redactor : p1.replace(/./g, redactor);
14
-
15
12
  if (percentage === 100) return finalRedactor(value);
16
13
  const stringValue = String(value);
17
14
  const stringLength = stringValue.length;
18
15
  const maskedLength = Math.ceil(stringLength * percentage / 100);
19
16
  const unmaskedLength = stringLength - maskedLength;
20
-
21
17
  const regexString = () => {
22
18
  if (position === "center") {
23
19
  return `(.{${unmaskedLength / 2}})(.{${maskedLength}})(.+)`;
24
20
  }
25
-
26
21
  if (position === "right") {
27
22
  return `(.{${unmaskedLength}})(.{${maskedLength}})`;
28
23
  }
29
-
30
24
  return `(.{${maskedLength}})(.{${unmaskedLength}})`;
31
25
  };
32
-
33
26
  const regex = new RegExp(regexString());
34
-
35
27
  const masoq = (_match, p1, p2, p3) => {
36
28
  if (p3 && complementary) {
37
29
  // Center + complementary
38
30
  return `${finalRedactor(p1)}${p2}${finalRedactor(p3)}`;
39
31
  }
40
-
41
32
  if (p3) {
42
33
  // Center
43
34
  return `${p1}${finalRedactor(p2)}${p3}`;
44
35
  }
45
-
46
36
  if (position === "right") {
47
37
  // Right
48
38
  return `${p1}${finalRedactor(p2)}`;
49
- } // Left
50
-
51
-
39
+ }
40
+ // Left
52
41
  return `${finalRedactor(p1)}${p2}`;
53
42
  };
54
-
55
43
  stringValue.replace(regex, (_match, p1, p2) => `${p1}${finalRedactor(p2)}`);
56
44
  return stringValue.replace(regex, masoq);
57
45
  };
58
-
59
46
  const redactIt = configs => {
60
47
  const defaultMask = {
61
48
  type: "replace",
@@ -70,18 +57,14 @@ const redactIt = configs => {
70
57
  optionsArray.forEach(option => {
71
58
  option.fields.forEach(field => {
72
59
  var _a;
73
-
74
60
  mappedFields.set(field, (_a = option.mask) !== null && _a !== void 0 ? _a : defaultMask);
75
61
  });
76
62
  });
77
-
78
63
  const getMaskForKey = key => {
79
64
  const mask = mappedFields.get(key);
80
-
81
65
  if (mask) {
82
66
  return mask;
83
67
  }
84
-
85
68
  for (const [matcher, mask] of mappedFields.entries()) {
86
69
  if (matcher instanceof RegExp) {
87
70
  if (matcher.test(key)) {
@@ -91,28 +74,21 @@ const redactIt = configs => {
91
74
  return mask;
92
75
  }
93
76
  }
94
-
95
77
  return null;
96
78
  };
97
-
98
79
  const replacer = (key, value) => {
99
80
  const mask = getMaskForKey(key);
100
-
101
81
  if (mask) {
102
82
  if (mask.type === "replace") {
103
83
  return mask.redactWith;
104
84
  }
105
-
106
85
  if (mask.type === "undefine") {
107
86
  return undefined;
108
87
  }
109
-
110
88
  return percentageValueMasker(value, mask);
111
89
  }
112
-
113
90
  return value;
114
91
  };
115
-
116
92
  return replacer;
117
93
  };
118
94
 
package/dist/index.esm.js CHANGED
@@ -1,57 +1,44 @@
1
1
  const percentageValueMasker = (value, mask) => {
2
2
  var _a, _b, _c, _d;
3
-
4
3
  const redactor = (_a = mask.redactWith) !== null && _a !== void 0 ? _a : "•";
5
4
  const percentage = (_b = mask.percentage) !== null && _b !== void 0 ? _b : 100;
6
5
  const complementary = (_c = mask.position === "center" && mask.complementary) !== null && _c !== void 0 ? _c : false;
7
6
  const position = (_d = mask.position) !== null && _d !== void 0 ? _d : "left";
8
-
9
7
  const finalRedactor = p1 => redactor.length > 1 ? redactor : p1.replace(/./g, redactor);
10
-
11
8
  if (percentage === 100) return finalRedactor(value);
12
9
  const stringValue = String(value);
13
10
  const stringLength = stringValue.length;
14
11
  const maskedLength = Math.ceil(stringLength * percentage / 100);
15
12
  const unmaskedLength = stringLength - maskedLength;
16
-
17
13
  const regexString = () => {
18
14
  if (position === "center") {
19
15
  return `(.{${unmaskedLength / 2}})(.{${maskedLength}})(.+)`;
20
16
  }
21
-
22
17
  if (position === "right") {
23
18
  return `(.{${unmaskedLength}})(.{${maskedLength}})`;
24
19
  }
25
-
26
20
  return `(.{${maskedLength}})(.{${unmaskedLength}})`;
27
21
  };
28
-
29
22
  const regex = new RegExp(regexString());
30
-
31
23
  const masoq = (_match, p1, p2, p3) => {
32
24
  if (p3 && complementary) {
33
25
  // Center + complementary
34
26
  return `${finalRedactor(p1)}${p2}${finalRedactor(p3)}`;
35
27
  }
36
-
37
28
  if (p3) {
38
29
  // Center
39
30
  return `${p1}${finalRedactor(p2)}${p3}`;
40
31
  }
41
-
42
32
  if (position === "right") {
43
33
  // Right
44
34
  return `${p1}${finalRedactor(p2)}`;
45
- } // Left
46
-
47
-
35
+ }
36
+ // Left
48
37
  return `${finalRedactor(p1)}${p2}`;
49
38
  };
50
-
51
39
  stringValue.replace(regex, (_match, p1, p2) => `${p1}${finalRedactor(p2)}`);
52
40
  return stringValue.replace(regex, masoq);
53
41
  };
54
-
55
42
  const redactIt = configs => {
56
43
  const defaultMask = {
57
44
  type: "replace",
@@ -66,18 +53,14 @@ const redactIt = configs => {
66
53
  optionsArray.forEach(option => {
67
54
  option.fields.forEach(field => {
68
55
  var _a;
69
-
70
56
  mappedFields.set(field, (_a = option.mask) !== null && _a !== void 0 ? _a : defaultMask);
71
57
  });
72
58
  });
73
-
74
59
  const getMaskForKey = key => {
75
60
  const mask = mappedFields.get(key);
76
-
77
61
  if (mask) {
78
62
  return mask;
79
63
  }
80
-
81
64
  for (const [matcher, mask] of mappedFields.entries()) {
82
65
  if (matcher instanceof RegExp) {
83
66
  if (matcher.test(key)) {
@@ -87,28 +70,21 @@ const redactIt = configs => {
87
70
  return mask;
88
71
  }
89
72
  }
90
-
91
73
  return null;
92
74
  };
93
-
94
75
  const replacer = (key, value) => {
95
76
  const mask = getMaskForKey(key);
96
-
97
77
  if (mask) {
98
78
  if (mask.type === "replace") {
99
79
  return mask.redactWith;
100
80
  }
101
-
102
81
  if (mask.type === "undefine") {
103
82
  return undefined;
104
83
  }
105
-
106
84
  return percentageValueMasker(value, mask);
107
85
  }
108
-
109
86
  return value;
110
87
  };
111
-
112
88
  return replacer;
113
89
  };
114
90
 
@@ -1,4 +1,4 @@
1
- export declare type Mask = PercentageMask | CenterPercentageMask | UndefineMask | ReplaceMask;
1
+ export type Mask = PercentageMask | CenterPercentageMask | UndefineMask | ReplaceMask;
2
2
  export interface PercentageMask {
3
3
  type: "percentage";
4
4
  /**
@@ -55,11 +55,11 @@ export interface RedactItConfig {
55
55
  mask?: Mask;
56
56
  }
57
57
  /** A replacer function compatible with JSON.stringify */
58
- export declare type ReplacerFunction = (key: any, value: any) => any;
58
+ export type ReplacerFunction = (key: any, value: any) => any;
59
59
  /**
60
60
  * A function that takes the argument and creates a replacer function
61
61
  * The argument may be a single object of the RedactItConfig type or an array of these objects
62
62
  * @param {RedactItConfig | RedactItConfig[]} configs - RedactItConfig to customize the redact function
63
63
  * @return {ReplacerFunction} replacer - A replacer function compatible with JSON.stringify
64
64
  */
65
- export declare type RedactIt = (configs?: RedactItConfig | RedactItConfig[]) => ReplacerFunction;
65
+ export type RedactIt = (configs?: RedactItConfig | RedactItConfig[]) => ReplacerFunction;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "redact-it",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "A flexible tool to redact JS objects' data",
5
5
  "author": "AlanPCS <alanpcsilva+npm@gmail.com>",
6
6
  "license": "MIT",
@@ -26,13 +26,13 @@
26
26
  "chai": "^4.3.4",
27
27
  "eslint": "^7.29.0",
28
28
  "karma-nyan-reporter": "^0.2.5",
29
- "mocha": "^9.0.1",
29
+ "mocha": "^10.2.0",
30
30
  "npm-run-all": "^4.1.5",
31
31
  "nyc": "^15.0.1",
32
32
  "prettier": "^2.3.2",
33
33
  "rollup": "^2.52.4",
34
- "rollup-plugin-typescript2": "^0.30.0",
35
- "ts-mocha": "^8.0.0",
34
+ "rollup-plugin-typescript2": "^0.36.0",
35
+ "ts-mocha": "^10.0.0",
36
36
  "ts-node": "^10.0.0",
37
37
  "tslint": "^6.1.3",
38
38
  "typescript": "^4.3.4"