Skip to content

Commit 4e65e28

Browse files
committed
refactor(data-masking): reduce cognitive complexity of erase method
1 parent 6598d2d commit 4e65e28

1 file changed

Lines changed: 23 additions & 17 deletions

File tree

packages/data-masking/src/DataMasking.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,33 @@ export class DataMasking {
5151
const copy = this.#deepCopy(data);
5252

5353
if (options.maskingRules) {
54-
for (const [field, rule] of Object.entries(options.maskingRules)) {
55-
for (const path of this.#resolveFieldPaths(
56-
copy as Record<string, unknown>,
57-
field
58-
)) {
59-
const value = getAtPath(copy, path);
60-
if (typeof value !== 'string') {
61-
throw new DataMaskingUnsupportedTypeError(
62-
`Masking rules only support string values, got ${typeof value} at path '${field}'`
63-
);
64-
}
65-
setAtPath(copy, path, applyMaskingRule(value, rule));
54+
this.#applyMaskingRules(copy, options.maskingRules);
55+
} else {
56+
this.#eraseFields(copy, options.fields ?? []);
57+
}
58+
59+
return copy;
60+
}
61+
62+
#applyMaskingRules<T>(copy: T, rules: Record<string, MaskingRule>): void {
63+
for (const [field, rule] of Object.entries(rules)) {
64+
for (const path of this.#resolveFieldPaths(
65+
copy as Record<string, unknown>,
66+
field
67+
)) {
68+
const value = getAtPath(copy, path);
69+
if (typeof value !== 'string') {
70+
throw new DataMaskingUnsupportedTypeError(
71+
`Masking rules only support string values, got ${typeof value} at path '${field}'`
72+
);
6673
}
74+
setAtPath(copy, path, applyMaskingRule(value, rule));
6775
}
68-
69-
return copy;
7076
}
77+
}
7178

72-
for (const field of options.fields ?? []) {
79+
#eraseFields<T>(copy: T, fields: string[]): void {
80+
for (const field of fields) {
7381
const paths = this.#resolveFieldPaths(
7482
copy as Record<string, unknown>,
7583
field
@@ -81,8 +89,6 @@ export class DataMasking {
8189
setAtPath(copy, path, DEFAULT_MASK_VALUE);
8290
}
8391
}
84-
85-
return copy;
8692
}
8793

8894
/**

0 commit comments

Comments
 (0)