redact-it 0.2.4 → 0.2.8

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/README.md CHANGED
@@ -47,15 +47,15 @@ For the following examples, we are going to use this main object as reference:
47
47
 
48
48
  ```typescript
49
49
  const userInfoToBeLogged = {
50
- password: "123",
51
- name: "foo",
52
- TOKEN: "my-secret-access-token",
50
+ password: '123',
51
+ name: 'foo',
52
+ TOKEN: 'my-secret-access-token',
53
53
  card: {
54
- number: "1234567887654321",
55
- cvv: "123",
56
- expirationDate: "2020-12-20",
54
+ number: '1234567887654321',
55
+ cvv: '123',
56
+ expirationDate: '2020-12-20',
57
57
  },
58
- authorization: "Bearer token",
58
+ authorization: 'Bearer token',
59
59
  };
60
60
  ```
61
61
 
@@ -66,7 +66,7 @@ The default `Mask` is the `replace` with the fixed string `[redacted]`:
66
66
 
67
67
  ```typescript
68
68
  const redactItConfig: RedactItConfig = {
69
- fields: ["password", "cvv", "TOKEN", "authorization"],
69
+ fields: ['password', 'cvv', 'TOKEN', 'authorization'],
70
70
  };
71
71
 
72
72
  const replacerFunction: ReplacerFunction = redactIt(redactItConfig);
@@ -94,31 +94,31 @@ const parsedResult = JSON.parse(stringResult);
94
94
  ```typescript
95
95
  const redactItConfig: RedactItConfig = [
96
96
  {
97
- fields: ["password"], // which fields to redact
97
+ fields: ['password'], // which fields to redact
98
98
  mask: {
99
99
  // How to redact the fields
100
- type: "undefine", // the undefine mask removes the fields
100
+ type: 'undefine', // the undefine mask removes the fields
101
101
  },
102
102
  },
103
103
  {
104
104
  fields: [/token/i], // Will match the regex against all fields
105
105
  },
106
106
  {
107
- fields: ["expirationDate", "number"],
107
+ fields: ['expirationDate', 'number'],
108
108
  mask: {
109
- type: "percentage", // Percentage masks redact data partially
110
- redactWith: "•", // Redacted characters replaced by •
109
+ type: 'percentage', // Percentage masks redact data partially
110
+ redactWith: '•', // Redacted characters replaced by •
111
111
  percentage: 75, // 75% of the value should be redacted
112
112
  },
113
113
  },
114
114
  {
115
- fields: ["authorization"],
115
+ fields: ['authorization'],
116
116
  mask: {
117
- type: "replace", // Replaces the whole value with the `redactWith` string
118
- redactWith: "[REDACTED FOR COMPLIANCE REASONS]",
117
+ type: 'replace', // Replaces the whole value with the `redactWith` string
118
+ redactWith: '[REDACTED FOR COMPLIANCE REASONS]',
119
119
  },
120
120
  },
121
- { fields: ["cvv"] }, // if no mask is passed, fields values are redacted as [redacted]
121
+ { fields: ['cvv'] }, // if no mask is passed, fields values are redacted as [redacted]
122
122
  ];
123
123
 
124
124
  const replacerFunction: ReplacerFunction = redactIt(redactItConfig);
package/dist/index.cjs.js CHANGED
@@ -1,87 +1,66 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  const percentageValueMasker = (value, mask) => {
6
- var _a, _b, _c, _d;
7
-
8
- const redactor = (_a = mask.redactWith) !== null && _a !== void 0 ? _a : "•";
9
- const percentage = (_b = mask.percentage) !== null && _b !== void 0 ? _b : 100;
10
- const complementary = (_c = mask.position === "center" && mask.complementary) !== null && _c !== void 0 ? _c : false;
11
- const position = (_d = mask.position) !== null && _d !== void 0 ? _d : "left";
12
-
4
+ const redactor = mask.redactWith ?? '•';
5
+ const percentage = mask.percentage ?? 100;
6
+ const complementary = (mask.position === 'center' && mask.complementary) ?? false;
7
+ const position = mask.position ?? 'left';
13
8
  const finalRedactor = p1 => redactor.length > 1 ? redactor : p1.replace(/./g, redactor);
14
-
15
9
  if (percentage === 100) return finalRedactor(value);
16
10
  const stringValue = String(value);
17
11
  const stringLength = stringValue.length;
18
12
  const maskedLength = Math.ceil(stringLength * percentage / 100);
19
13
  const unmaskedLength = stringLength - maskedLength;
20
-
21
14
  const regexString = () => {
22
- if (position === "center") {
15
+ if (position === 'center') {
23
16
  return `(.{${unmaskedLength / 2}})(.{${maskedLength}})(.+)`;
24
17
  }
25
-
26
- if (position === "right") {
18
+ if (position === 'right') {
27
19
  return `(.{${unmaskedLength}})(.{${maskedLength}})`;
28
20
  }
29
-
30
21
  return `(.{${maskedLength}})(.{${unmaskedLength}})`;
31
22
  };
32
-
33
23
  const regex = new RegExp(regexString());
34
-
35
24
  const masoq = (_match, p1, p2, p3) => {
36
25
  if (p3 && complementary) {
37
26
  // Center + complementary
38
27
  return `${finalRedactor(p1)}${p2}${finalRedactor(p3)}`;
39
28
  }
40
-
41
29
  if (p3) {
42
30
  // Center
43
31
  return `${p1}${finalRedactor(p2)}${p3}`;
44
32
  }
45
-
46
- if (position === "right") {
33
+ if (position === 'right') {
47
34
  // Right
48
35
  return `${p1}${finalRedactor(p2)}`;
49
- } // Left
50
-
51
-
36
+ }
37
+ // Left
52
38
  return `${finalRedactor(p1)}${p2}`;
53
39
  };
54
-
55
40
  stringValue.replace(regex, (_match, p1, p2) => `${p1}${finalRedactor(p2)}`);
56
41
  return stringValue.replace(regex, masoq);
57
42
  };
58
-
59
43
  const redactIt = configs => {
60
44
  const defaultMask = {
61
- type: "replace",
62
- redactWith: "[redacted]"
45
+ type: 'replace',
46
+ redactWith: '[redacted]'
63
47
  };
64
48
  const defaultOptions = {
65
- fields: ["password"],
49
+ fields: ['password'],
66
50
  mask: defaultMask
67
51
  };
68
52
  const mappedFields = new Map();
69
- const optionsArray = Array.isArray(configs) ? configs : [configs !== null && configs !== void 0 ? configs : defaultOptions];
53
+ const optionsArray = Array.isArray(configs) ? configs : [configs ?? defaultOptions];
70
54
  optionsArray.forEach(option => {
71
55
  option.fields.forEach(field => {
72
- var _a;
73
-
74
- mappedFields.set(field, (_a = option.mask) !== null && _a !== void 0 ? _a : defaultMask);
56
+ mappedFields.set(field, option.mask ?? defaultMask);
75
57
  });
76
58
  });
77
-
78
59
  const getMaskForKey = key => {
79
60
  const mask = mappedFields.get(key);
80
-
81
61
  if (mask) {
82
62
  return mask;
83
63
  }
84
-
85
64
  for (const [matcher, mask] of mappedFields.entries()) {
86
65
  if (matcher instanceof RegExp) {
87
66
  if (matcher.test(key)) {
@@ -91,29 +70,23 @@ const redactIt = configs => {
91
70
  return mask;
92
71
  }
93
72
  }
94
-
95
73
  return null;
96
74
  };
97
-
98
75
  const replacer = (key, value) => {
99
76
  const mask = getMaskForKey(key);
100
-
101
77
  if (mask) {
102
- if (mask.type === "replace") {
78
+ if (mask.type === 'replace') {
103
79
  return mask.redactWith;
104
80
  }
105
-
106
- if (mask.type === "undefine") {
81
+ if (mask.type === 'undefine') {
107
82
  return undefined;
108
83
  }
109
-
110
84
  return percentageValueMasker(value, mask);
111
85
  }
112
-
113
86
  return value;
114
87
  };
115
-
116
88
  return replacer;
117
89
  };
118
90
 
119
91
  exports.redactIt = redactIt;
92
+ //# sourceMappingURL=index.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs.js","sources":["../src/redact-it.ts"],"sourcesContent":["import type {\n Mask,\n RedactIt,\n RedactItConfig,\n ReplacerFunction,\n PercentageMask,\n CenterPercentageMask,\n} from '../typings';\n\nconst percentageValueMasker = (\n value: any,\n mask: PercentageMask | CenterPercentageMask,\n): string | undefined => {\n const redactor = mask.redactWith ?? '•';\n const percentage = mask.percentage ?? 100;\n const complementary = (mask.position === 'center' && mask.complementary) ?? false;\n const position = mask.position ?? 'left';\n\n const finalRedactor = (p1: string): string =>\n redactor.length > 1 ? redactor : p1.replace(/./g, redactor);\n\n if (percentage === 100) return finalRedactor(value);\n\n const stringValue = String(value);\n const stringLength = stringValue.length;\n const maskedLength = Math.ceil((stringLength * percentage) / 100);\n const unmaskedLength = stringLength - maskedLength;\n\n const regexString = (): string => {\n if (position === 'center') {\n return `(.{${unmaskedLength / 2}})(.{${maskedLength}})(.+)`;\n }\n if (position === 'right') {\n return `(.{${unmaskedLength}})(.{${maskedLength}})`;\n }\n return `(.{${maskedLength}})(.{${unmaskedLength}})`;\n };\n\n const regex = new RegExp(regexString());\n\n const masoq = (_match: any, p1: string, p2: string, p3?: string): string => {\n if (p3 && complementary) {\n // Center + complementary\n return `${finalRedactor(p1)}${p2}${finalRedactor(p3)}`;\n }\n if (p3) {\n // Center\n return `${p1}${finalRedactor(p2)}${p3}`;\n }\n if (position === 'right') {\n // Right\n return `${p1}${finalRedactor(p2)}`;\n }\n // Left\n return `${finalRedactor(p1)}${p2}`;\n };\n\n stringValue.replace(regex, (_match, p1, p2) => `${p1}${finalRedactor(p2)}`);\n\n return stringValue.replace(regex, masoq);\n};\n\nexport const redactIt: RedactIt = (\n configs?: RedactItConfig | RedactItConfig[],\n): ReplacerFunction => {\n const defaultMask: Mask = {\n type: 'replace',\n redactWith: '[redacted]',\n };\n\n const defaultOptions: RedactItConfig = {\n fields: ['password'],\n mask: defaultMask,\n };\n\n const mappedFields: Map<string | RegExp, Mask> = new Map();\n\n const optionsArray: RedactItConfig[] = Array.isArray(configs)\n ? configs\n : [configs ?? defaultOptions];\n\n optionsArray.forEach((option: RedactItConfig) => {\n option.fields.forEach((field) => {\n mappedFields.set(field, option.mask ?? defaultMask);\n });\n });\n\n const getMaskForKey = (key: any): Mask | null => {\n const mask = mappedFields.get(key);\n if (mask) {\n return mask;\n }\n\n for (const [matcher, mask] of mappedFields.entries()) {\n if (matcher instanceof RegExp) {\n if (matcher.test(key)) {\n return mask;\n }\n } else if (matcher === key) {\n return mask;\n }\n }\n return null;\n };\n\n const replacer = (key: any, value: any): any => {\n const mask = getMaskForKey(key);\n if (mask) {\n if (mask.type === 'replace') {\n return mask.redactWith;\n }\n if (mask.type === 'undefine') {\n return undefined;\n }\n return percentageValueMasker(value, mask);\n }\n return value;\n };\n return replacer;\n};\n"],"names":[],"mappings":";;AASA,MAAM,qBAAqB,GAAG,CAC5B,KAAU,EACV,IAA2C,KACpB;AACvB,EAAA,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,IAAI,GAAG;AACvC,EAAA,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,IAAI,GAAG;AACzC,EAAA,MAAM,aAAa,GAAG,CAAC,IAAI,CAAC,QAAQ,KAAK,QAAQ,IAAI,IAAI,C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;EAEF,MAAM,aAAa,GAAI,GAAQ,IAAkB;AAC/C,IAAA,MAAM,IAAI,GAAG,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC;AAClC,IAAA,IAAI,IAAI,EAAE;AACR,MAAA,OAAO,IAAI;AACb,IAAA;AAEA,IAAA,KAAK,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,IAAI,YAAY,CAAC,OAAO,EAAE,EAAE;MACpD,IAAI,OAAO,YAAY,MAAM,EAAE;AAC7B,QAAA,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;AACrB,UAAA,OAAO,IAAI;AACb,QAAA;AACF,MAAA,CAAC,MAAM,IAAI,OAAO,KAAK,GAAG,EAAE;AAC1B,QAAA,OAAO,IAAI;AACb,MAAA;AACF,IAAA;AACA,IAAA,OAAO,IAAI;EACb,CAAC;AAED,EAAA,MAAM,QAAQ,GAAG,CAAC,GAAQ,EAAE,KAAU,KAAU;AAC9C,IAAA,MAAM,IAAI,GAAG,aAAa,CAAC,GAAG,CAAC;AAC/B,IAAA,IAAI,IAAI,EAAE;AACR,MAAA,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE;QAC3B,OAAO,IAAI,CAAC,UAAU;AACxB,MAAA;AACA,MAAA,IAAI,IAAI,CAAC,IAAI,KAAK,UAAU,EAAE;AAC5B,QAAA,OAAO,SAAS;AAClB,MAAA;AACA,MAAA,OAAO,qBAAqB,CAAC,KAAK,EAAE,IAAI,CAAC;AAC3C,IAAA;AACA,IAAA,OAAO,KAAK;EACd,CAAC;AACD,EAAA,OAAO,QAAQ;AACjB;;"}
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { redactIt } from "./src/redact-it";
2
- export * from "./typings";
1
+ export { redactIt } from './src/redact-it';
2
+ export type * from './typings';
package/dist/index.esm.js CHANGED
@@ -1,83 +1,64 @@
1
1
  const percentageValueMasker = (value, mask) => {
2
- var _a, _b, _c, _d;
3
-
4
- const redactor = (_a = mask.redactWith) !== null && _a !== void 0 ? _a : "•";
5
- const percentage = (_b = mask.percentage) !== null && _b !== void 0 ? _b : 100;
6
- const complementary = (_c = mask.position === "center" && mask.complementary) !== null && _c !== void 0 ? _c : false;
7
- const position = (_d = mask.position) !== null && _d !== void 0 ? _d : "left";
8
-
2
+ const redactor = mask.redactWith ?? '•';
3
+ const percentage = mask.percentage ?? 100;
4
+ const complementary = (mask.position === 'center' && mask.complementary) ?? false;
5
+ const position = mask.position ?? 'left';
9
6
  const finalRedactor = p1 => redactor.length > 1 ? redactor : p1.replace(/./g, redactor);
10
-
11
7
  if (percentage === 100) return finalRedactor(value);
12
8
  const stringValue = String(value);
13
9
  const stringLength = stringValue.length;
14
10
  const maskedLength = Math.ceil(stringLength * percentage / 100);
15
11
  const unmaskedLength = stringLength - maskedLength;
16
-
17
12
  const regexString = () => {
18
- if (position === "center") {
13
+ if (position === 'center') {
19
14
  return `(.{${unmaskedLength / 2}})(.{${maskedLength}})(.+)`;
20
15
  }
21
-
22
- if (position === "right") {
16
+ if (position === 'right') {
23
17
  return `(.{${unmaskedLength}})(.{${maskedLength}})`;
24
18
  }
25
-
26
19
  return `(.{${maskedLength}})(.{${unmaskedLength}})`;
27
20
  };
28
-
29
21
  const regex = new RegExp(regexString());
30
-
31
22
  const masoq = (_match, p1, p2, p3) => {
32
23
  if (p3 && complementary) {
33
24
  // Center + complementary
34
25
  return `${finalRedactor(p1)}${p2}${finalRedactor(p3)}`;
35
26
  }
36
-
37
27
  if (p3) {
38
28
  // Center
39
29
  return `${p1}${finalRedactor(p2)}${p3}`;
40
30
  }
41
-
42
- if (position === "right") {
31
+ if (position === 'right') {
43
32
  // Right
44
33
  return `${p1}${finalRedactor(p2)}`;
45
- } // Left
46
-
47
-
34
+ }
35
+ // Left
48
36
  return `${finalRedactor(p1)}${p2}`;
49
37
  };
50
-
51
38
  stringValue.replace(regex, (_match, p1, p2) => `${p1}${finalRedactor(p2)}`);
52
39
  return stringValue.replace(regex, masoq);
53
40
  };
54
-
55
41
  const redactIt = configs => {
56
42
  const defaultMask = {
57
- type: "replace",
58
- redactWith: "[redacted]"
43
+ type: 'replace',
44
+ redactWith: '[redacted]'
59
45
  };
60
46
  const defaultOptions = {
61
- fields: ["password"],
47
+ fields: ['password'],
62
48
  mask: defaultMask
63
49
  };
64
50
  const mappedFields = new Map();
65
- const optionsArray = Array.isArray(configs) ? configs : [configs !== null && configs !== void 0 ? configs : defaultOptions];
51
+ const optionsArray = Array.isArray(configs) ? configs : [configs ?? defaultOptions];
66
52
  optionsArray.forEach(option => {
67
53
  option.fields.forEach(field => {
68
- var _a;
69
-
70
- mappedFields.set(field, (_a = option.mask) !== null && _a !== void 0 ? _a : defaultMask);
54
+ mappedFields.set(field, option.mask ?? defaultMask);
71
55
  });
72
56
  });
73
-
74
57
  const getMaskForKey = key => {
75
58
  const mask = mappedFields.get(key);
76
-
77
59
  if (mask) {
78
60
  return mask;
79
61
  }
80
-
81
62
  for (const [matcher, mask] of mappedFields.entries()) {
82
63
  if (matcher instanceof RegExp) {
83
64
  if (matcher.test(key)) {
@@ -87,29 +68,23 @@ const redactIt = configs => {
87
68
  return mask;
88
69
  }
89
70
  }
90
-
91
71
  return null;
92
72
  };
93
-
94
73
  const replacer = (key, value) => {
95
74
  const mask = getMaskForKey(key);
96
-
97
75
  if (mask) {
98
- if (mask.type === "replace") {
76
+ if (mask.type === 'replace') {
99
77
  return mask.redactWith;
100
78
  }
101
-
102
- if (mask.type === "undefine") {
79
+ if (mask.type === 'undefine') {
103
80
  return undefined;
104
81
  }
105
-
106
82
  return percentageValueMasker(value, mask);
107
83
  }
108
-
109
84
  return value;
110
85
  };
111
-
112
86
  return replacer;
113
87
  };
114
88
 
115
89
  export { redactIt };
90
+ //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.esm.js","sources":["../src/redact-it.ts"],"sourcesContent":["import type {\n Mask,\n RedactIt,\n RedactItConfig,\n ReplacerFunction,\n PercentageMask,\n CenterPercentageMask,\n} from '../typings';\n\nconst percentageValueMasker = (\n value: any,\n mask: PercentageMask | CenterPercentageMask,\n): string | undefined => {\n const redactor = mask.redactWith ?? '•';\n const percentage = mask.percentage ?? 100;\n const complementary = (mask.position === 'center' && mask.complementary) ?? false;\n const position = mask.position ?? 'left';\n\n const finalRedactor = (p1: string): string =>\n redactor.length > 1 ? redactor : p1.replace(/./g, redactor);\n\n if (percentage === 100) return finalRedactor(value);\n\n const stringValue = String(value);\n const stringLength = stringValue.length;\n const maskedLength = Math.ceil((stringLength * percentage) / 100);\n const unmaskedLength = stringLength - maskedLength;\n\n const regexString = (): string => {\n if (position === 'center') {\n return `(.{${unmaskedLength / 2}})(.{${maskedLength}})(.+)`;\n }\n if (position === 'right') {\n return `(.{${unmaskedLength}})(.{${maskedLength}})`;\n }\n return `(.{${maskedLength}})(.{${unmaskedLength}})`;\n };\n\n const regex = new RegExp(regexString());\n\n const masoq = (_match: any, p1: string, p2: string, p3?: string): string => {\n if (p3 && complementary) {\n // Center + complementary\n return `${finalRedactor(p1)}${p2}${finalRedactor(p3)}`;\n }\n if (p3) {\n // Center\n return `${p1}${finalRedactor(p2)}${p3}`;\n }\n if (position === 'right') {\n // Right\n return `${p1}${finalRedactor(p2)}`;\n }\n // Left\n return `${finalRedactor(p1)}${p2}`;\n };\n\n stringValue.replace(regex, (_match, p1, p2) => `${p1}${finalRedactor(p2)}`);\n\n return stringValue.replace(regex, masoq);\n};\n\nexport const redactIt: RedactIt = (\n configs?: RedactItConfig | RedactItConfig[],\n): ReplacerFunction => {\n const defaultMask: Mask = {\n type: 'replace',\n redactWith: '[redacted]',\n };\n\n const defaultOptions: RedactItConfig = {\n fields: ['password'],\n mask: defaultMask,\n };\n\n const mappedFields: Map<string | RegExp, Mask> = new Map();\n\n const optionsArray: RedactItConfig[] = Array.isArray(configs)\n ? configs\n : [configs ?? defaultOptions];\n\n optionsArray.forEach((option: RedactItConfig) => {\n option.fields.forEach((field) => {\n mappedFields.set(field, option.mask ?? defaultMask);\n });\n });\n\n const getMaskForKey = (key: any): Mask | null => {\n const mask = mappedFields.get(key);\n if (mask) {\n return mask;\n }\n\n for (const [matcher, mask] of mappedFields.entries()) {\n if (matcher instanceof RegExp) {\n if (matcher.test(key)) {\n return mask;\n }\n } else if (matcher === key) {\n return mask;\n }\n }\n return null;\n };\n\n const replacer = (key: any, value: any): any => {\n const mask = getMaskForKey(key);\n if (mask) {\n if (mask.type === 'replace') {\n return mask.redactWith;\n }\n if (mask.type === 'undefine') {\n return undefined;\n }\n return percentageValueMasker(value, mask);\n }\n return value;\n };\n return replacer;\n};\n"],"names":[],"mappings":"AASA,MAAM,qBAAqB,GAAG,CAC5B,KAAU,EACV,IAA2C,KACpB;AACvB,EAAA,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,IAAI,GAAG;AACvC,EAAA,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,IAAI,GAAG;AACzC,EAAA,MAAM,aAAa,GAAG,CAAC,IAAI,CAAC,QAAQ,KAAK,QAAQ,IAAI,IAAI,CAAC,aAAa,KAAK,KAAK;AACjF,EAAA,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,IAAI,MAAM;AAExC,EAAA,MAAM,aAAa,GAAI,EAAU,IAC/B,QAAQ,CAAC,MAAM,GAAG,CAAC,GAAG,QAAQ,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC;EAE7D,IAAI,UAAU,KAAK,GAAG,EAAE,OAAO,aAAa,CAAC,KAAK,CAAC;AAEnD,EAAA,MAAM,WAAW,GAAG,MAAM,CAAC,KAAK,CAAC;AACjC,EAAA,MAAM,YAAY,GAAG,WAAW,CAAC,MAAM;EACvC,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,CAAE,YAAY,GAAG,UAAU,GAAI,GAAG,CAAC;AACjE,EAAA,MAAM,cAAc,GAAG,YAAY,GAAG,YAAY;EAElD,MAAM,WAAW,GAAG,MAAc;IAChC,IAAI,QAAQ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AU,KAAU;AAC9C,IAAA,MAAM,IAAI,GAAG,aAAa,CAAC,GAAG,CAAC;AAC/B,IAAA,IAAI,IAAI,EAAE;AACR,MAAA,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE;QAC3B,OAAO,IAAI,CAAC,UAAU;AACxB,MAAA;AACA,MAAA,IAAI,IAAI,CAAC,IAAI,KAAK,UAAU,EAAE;AAC5B,QAAA,OAAO,SAAS;AAClB,MAAA;AACA,MAAA,OAAO,qBAAqB,CAAC,KAAK,EAAE,IAAI,CAAC;AAC3C,IAAA;AACA,IAAA,OAAO,KAAK;EACd,CAAC;AACD,EAAA,OAAO,QAAQ;AACjB;;"}
@@ -1,2 +1,2 @@
1
- import { RedactIt } from "../typings";
1
+ import type { RedactIt } from '../typings';
2
2
  export declare const redactIt: RedactIt;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,11 +1,11 @@
1
- export declare type Mask = PercentageMask | CenterPercentageMask | UndefineMask | ReplaceMask;
1
+ export type Mask = PercentageMask | CenterPercentageMask | UndefineMask | ReplaceMask;
2
2
  export interface PercentageMask {
3
- type: "percentage";
3
+ type: 'percentage';
4
4
  /**
5
5
  * Character to be used as the redacted part
6
6
  * @default "•"
7
7
  */
8
- redactWith?: "*" | "•" | string;
8
+ redactWith?: '*' | '•' | string;
9
9
  /**
10
10
  * Percentage of the value to apply the mask on
11
11
  * @default 100
@@ -15,16 +15,16 @@ export interface PercentageMask {
15
15
  * Which part of the value to redact
16
16
  * @default "left"
17
17
  */
18
- position?: "left" | "right";
18
+ position?: 'left' | 'right';
19
19
  }
20
20
  export interface CenterPercentageMask {
21
- type: "percentage";
22
- position: "center";
21
+ type: 'percentage';
22
+ position: 'center';
23
23
  /**
24
24
  * Character to be used as the redacted part
25
25
  * @default "•"
26
26
  */
27
- redactWith?: "*" | "•" | string;
27
+ redactWith?: '*' | '•' | string;
28
28
  /**
29
29
  * Percentage of the value to apply the mask on
30
30
  * @default 100
@@ -37,15 +37,15 @@ export interface CenterPercentageMask {
37
37
  complementary?: boolean;
38
38
  }
39
39
  export interface UndefineMask {
40
- type: "undefine";
40
+ type: 'undefine';
41
41
  }
42
42
  export interface ReplaceMask {
43
- type: "replace";
43
+ type: 'replace';
44
44
  /**
45
45
  * Replace the value entirely with this string
46
46
  * @default "[redacted]"
47
47
  */
48
- redactWith: "[redacted]" | string;
48
+ redactWith: '[redacted]' | string;
49
49
  }
50
50
  /** Redact-it configs to customize how and which fields are going to be redacted */
51
51
  export interface RedactItConfig {
@@ -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.8",
4
4
  "description": "A flexible tool to redact JS objects' data",
5
5
  "author": "AlanPCS <alanpcsilva+npm@gmail.com>",
6
6
  "license": "MIT",
@@ -10,47 +10,48 @@
10
10
  "files": [
11
11
  "dist"
12
12
  ],
13
- "devDependencies": {
14
- "@babel/core": "^7.14.6",
15
- "@babel/plugin-transform-runtime": "^7.14.5",
16
- "@babel/preset-typescript": "^7.14.5",
17
- "@babel/runtime": "^7.14.6",
18
- "@rollup/plugin-babel": "^5.3.0",
19
- "@rollup/plugin-node-resolve": "^13.0.0",
20
- "@types/chai": "^4.2.19",
21
- "@types/mocha": "^8.2.2",
22
- "@types/node": "^15.12.5",
23
- "@types/node-fetch": "^2.5.10",
24
- "@typescript-eslint/eslint-plugin": "^4.28.1",
25
- "@typescript-eslint/parser": "^4.28.1",
26
- "chai": "^4.3.4",
27
- "eslint": "^7.29.0",
28
- "karma-nyan-reporter": "^0.2.5",
29
- "mocha": "^9.0.1",
30
- "npm-run-all": "^4.1.5",
31
- "nyc": "^15.0.1",
32
- "prettier": "^2.3.2",
33
- "rollup": "^2.52.4",
34
- "rollup-plugin-typescript2": "^0.30.0",
35
- "ts-mocha": "^8.0.0",
36
- "ts-node": "^10.0.0",
37
- "tslint": "^6.1.3",
38
- "typescript": "^4.3.4"
39
- },
13
+ "packageManager": "pnpm@12.16.1",
40
14
  "scripts": {
41
- "test": "run-p lint test:compile test:mocha",
42
- "test:ci": "run-p lint test:compile test:coverage",
43
- "test:compile": "tsc",
44
- "test:mocha": "ts-mocha --config test/.mocharc.json",
45
- "test:watch": "yarn test:mocha --watch -R min",
46
- "test:coverage": "nyc --reporter=text yarn test:mocha -R spec",
47
- "lint": "eslint 'src/**/*.ts' 'test/**/*.ts'",
48
- "lint:fix": "eslint 'src/**/*.ts' 'test/**/*.ts' --fix",
49
- "build": "rm -rf dist && rollup -c rollup.config.ts && tsc --emitDeclarationOnly",
50
- "deploy": "yarn build && npm publish"
15
+ "test": "jest",
16
+ "test:ci": "pnpm lint && pnpm test:compile && pnpm test:coverage",
17
+ "test:compile": "tsc --noEmit",
18
+ "test:watch": "pnpm test --watch -R min",
19
+ "test:coverage": "jest --coverage",
20
+ "lint": "eslint .",
21
+ "lint:fix": "pnpm lint --fix",
22
+ "format": "prettier --write .",
23
+ "format:check": "prettier --check .",
24
+ "build": "rm -rf dist && rollup -c rollup.config.ts --bundleConfigAsCjs && tsc --emitDeclarationOnly",
25
+ "deploy": "pnpm build && npm publish",
26
+ "bless": "pnpm peers check && pnpm format:check && pnpm lint && pnpm test:compile && pnpm test:coverage && pnpm knip && pnpm build && pnpm audit",
27
+ "knip": "knip"
28
+ },
29
+ "devDependencies": {
30
+ "@babel/core": "^8.1.4",
31
+ "@babel/preset-env": "^8.1.4",
32
+ "@babel/preset-typescript": "^8.0.7",
33
+ "@eslint/js": "^10.0.1",
34
+ "@rollup/plugin-babel": "^7.2.0",
35
+ "@rollup/plugin-node-resolve": "^16.0.3",
36
+ "@types/jest": "^30.0.0",
37
+ "@types/node": "^26.6.11",
38
+ "@typescript-eslint/eslint-plugin": "^8.74.0",
39
+ "@typescript-eslint/parser": "^8.74.0",
40
+ "@typescript/native": "npm:typescript@^7.0.2",
41
+ "eslint": "^10.16.0",
42
+ "eslint-plugin-jest": "^29.17.2",
43
+ "jest": "^30.5.2",
44
+ "jiti": "^2.7.0",
45
+ "knip": "^6.49.0",
46
+ "prettier": "^3.10.1",
47
+ "rollup": "^4.69.2",
48
+ "typescript": "npm:@typescript/typescript6@^6.0.2"
51
49
  },
52
50
  "repository": {
53
51
  "type": "git",
54
- "url": "ssh://git@github.com/alanpcs/redact-it.git"
52
+ "url": "git+https://github.com/alanpcs/redact-it.git"
53
+ },
54
+ "engines": {
55
+ "node": "26"
55
56
  }
56
57
  }