maskdata 1.2.2 → 1.2.4

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.
@@ -3,59 +3,59 @@ const MaskHelper = require('../helpers/MaskHelper');
3
3
  const Constants = require('../helpers/Constants');
4
4
 
5
5
  class CardMask {
6
- static maskCard(card, options) {
7
- if(!card) return card;
8
- let cardToProcess = (card + '').trim();
9
- if(options) {
10
- options = MaskHelper.mapWithDefaultValues(options, Constants.defaultCardMaskOptions);
11
- MaskHelper.validateCardMaskOptions(options, cardToProcess.length);
12
- } else {
13
- options = Constants.defaultCardMaskOptions;
14
- }
15
- let countOfNumbers = 0;
16
- let lastIndex = 0;
17
- let flag = true;
18
- for(let i = cardToProcess.length-1; i >= 0; i--) {
19
- if(!isNaN(cardToProcess[i])) {
20
- if(flag && countOfNumbers === options.unmaskedEndDigits) {
21
- lastIndex = i;
22
- flag = false;
23
- }
24
- countOfNumbers++;
25
- }
26
- }
27
- if((options.unmaskedStartDigits + options.unmaskedEndDigits) >= countOfNumbers) {
28
- return cardToProcess;
29
- }
30
- let maskedCard = '';
31
- let i = 0;
32
- let j = 0;
33
- while(i < options.unmaskedStartDigits) {
34
- if(!isNaN(cardToProcess[j])) {
35
- i++;
36
- }
37
- maskedCard += cardToProcess[j++];
6
+ static maskCard(card, options) {
7
+ if (!card) return card;
8
+ let cardToProcess = (card + '').trim();
9
+ if (options) {
10
+ options = MaskHelper.mapWithDefaultValues(options, Constants.defaultCardMaskOptions);
11
+ MaskHelper.validateCardMaskOptions(options, cardToProcess.length);
12
+ } else {
13
+ options = Constants.defaultCardMaskOptions;
14
+ }
15
+ let countOfNumbers = 0;
16
+ let lastIndex = 0;
17
+ let flag = true;
18
+ for (let i = cardToProcess.length - 1; i >= 0; i--) {
19
+ if (!isNaN(cardToProcess[i])) {
20
+ if (flag && countOfNumbers === options.unmaskedEndDigits) {
21
+ lastIndex = i;
22
+ flag = false;
38
23
  }
24
+ countOfNumbers++;
25
+ }
26
+ }
27
+ if (options.unmaskedStartDigits + options.unmaskedEndDigits >= countOfNumbers) {
28
+ return cardToProcess;
29
+ }
30
+ let maskedCard = '';
31
+ let i = 0;
32
+ let j = 0;
33
+ while (i < options.unmaskedStartDigits) {
34
+ if (!isNaN(cardToProcess[j])) {
35
+ i++;
36
+ }
37
+ maskedCard += cardToProcess[j++];
38
+ }
39
39
 
40
- while(j <= lastIndex) {
41
- if(isNaN(parseInt(cardToProcess[j]))) {
42
- maskedCard += cardToProcess[j];
43
- } else {
44
- maskedCard += options.maskWith;
45
- }
46
- j++;
47
- }
48
-
49
- i = 0;
50
- j = lastIndex+1;
51
- while(i < options.unmaskedEndDigits) {
52
- if(!isNaN(cardToProcess[j])) {
53
- i++;
54
- }
55
- maskedCard += cardToProcess[j++];
56
- }
57
- return maskedCard;
40
+ while (j <= lastIndex) {
41
+ if (isNaN(parseInt(cardToProcess[j]))) {
42
+ maskedCard += cardToProcess[j];
43
+ } else {
44
+ maskedCard += options.maskWith;
45
+ }
46
+ j++;
47
+ }
48
+
49
+ i = 0;
50
+ j = lastIndex + 1;
51
+ while (i < options.unmaskedEndDigits) {
52
+ if (!isNaN(cardToProcess[j])) {
53
+ i++;
54
+ }
55
+ maskedCard += cardToProcess[j++];
58
56
  }
57
+ return maskedCard;
58
+ }
59
59
  }
60
60
 
61
- module.exports = CardMask
61
+ module.exports = CardMask;
@@ -3,28 +3,34 @@ const MaskHelper = require('../helpers/MaskHelper');
3
3
  const Constants = require('../helpers/Constants');
4
4
 
5
5
  class EmailMask {
6
+ static maskEmail2(email, options) {
7
+ if (!email || typeof email !== 'string') return email;
8
+ let maskedEmail = '';
9
+ options = options
10
+ ? MaskHelper.mapWithDefaultValues(options, Constants.defaultEmailMask2Options)
11
+ : Constants.defaultEmailMask2Options;
12
+ MaskHelper.validateEmailOptions2(email, options);
13
+ const indexOfAt = email.indexOf('@');
14
+ // if(indexOfAt < 0) return email; // Not needed since we have the validation for mandatory '@'
15
+ const [before, after] = email.split('@');
16
+ const beforeLength = before.length;
17
+ const afterLength = after.length;
6
18
 
7
- static maskEmail2(email, options) {
8
- if(!email || typeof(email) !== 'string') return email;
9
- let maskedEmail = '';
10
- options = options ? MaskHelper.mapWithDefaultValues(options, Constants.defaultEmailMask2Options)
11
- : Constants.defaultEmailMask2Options;
12
- MaskHelper.validateEmailOptions2(email, options);
13
- const indexOfAt = email.indexOf('@');
14
- // if(indexOfAt < 0) return email; // Not needed since we have the validation for mandatory '@'
15
- const [before, after] = email.split('@');
16
- const beforeLength = before.length;
17
- const afterLength = after.length;
19
+ maskedEmail +=
20
+ options.unmaskedStartCharactersBeforeAt >= beforeLength
21
+ ? before
22
+ : before.substring(0, options.unmaskedStartCharactersBeforeAt) +
23
+ `${options.maskWith}`.repeat(beforeLength - options.unmaskedStartCharactersBeforeAt);
18
24
 
19
- maskedEmail += (options.unmaskedStartCharactersBeforeAt >= beforeLength) ? before :
20
- before.substring(0, options.unmaskedStartCharactersBeforeAt) + `${options.maskWith}`.repeat(beforeLength - options.unmaskedStartCharactersBeforeAt);
21
-
22
- maskedEmail += options.maskAtTheRate ? options.maskWith : '@';
25
+ maskedEmail += options.maskAtTheRate ? options.maskWith : '@';
23
26
 
24
- maskedEmail += (options.unmaskedEndCharactersAfterAt >= afterLength) ? after :
25
- (`${options.maskWith}`.repeat(afterLength-options.unmaskedEndCharactersAfterAt) + after.substring(afterLength - options.unmaskedEndCharactersAfterAt));
26
- return maskedEmail;
27
- }
27
+ maskedEmail +=
28
+ options.unmaskedEndCharactersAfterAt >= afterLength
29
+ ? after
30
+ : `${options.maskWith}`.repeat(afterLength - options.unmaskedEndCharactersAfterAt) +
31
+ after.substring(afterLength - options.unmaskedEndCharactersAfterAt);
32
+ return maskedEmail;
33
+ }
28
34
  }
29
35
 
30
- module.exports = EmailMask
36
+ module.exports = EmailMask;
@@ -9,110 +9,189 @@ const PasswordMask = require('./PasswordMask');
9
9
  const PhoneMask = require('./PhoneMask');
10
10
  const StringMask = require('./StringMask');
11
11
  const UuidMask = require('./UuidMask');
12
+ const JwtMask = require('./JwtMask');
12
13
  const JsonGetSet = require('../helpers/jsonGetSet');
13
14
 
15
+ const typeToFunctionMap = {
16
+ card: [CardMask.maskCard, 'cardFields', 'cardMaskOptions'],
17
+ email: [EmailMask.maskEmail2, 'emailFields', 'emailMaskOptions'],
18
+ password: [PasswordMask.maskPassword, 'passwordFields', 'passwordMaskOptions'],
19
+ phone: [PhoneMask.maskPhone, 'phoneFields', 'phoneMaskOptions'],
20
+ string: [StringMask.maskString, 'stringFields', 'stringMaskOptions'],
21
+ uuid: [UuidMask.maskUuid, 'uuidFields', 'uuidMaskOptions'],
22
+ jwt: [JwtMask.maskJwt, 'jwtFields', 'jwtMaskOptions']
23
+ };
24
+
14
25
  class JsonMask {
15
- static maskJSONFields(obj, options) {
16
- if(options) {
17
- options = MaskHelper.mapWithDefaultValues(options, Constants.defaultJsonMaskOptions);
18
- MaskHelper.validateJSONOptions(options);
19
- } else {
20
- options = Constants.defaultJsonMaskOptions;
21
- }
22
- let maskedObj = {};
23
- try {
24
- maskedObj = JSON.parse(JSON.stringify(obj));
25
- } catch (ex) {
26
- return obj;
27
- }
28
- const fields = options.fields;
29
- for(const field of fields) {
30
- try {
31
- if(field.includes('[*].')) {
32
- let [arrayFieldName, subField] = field.split('[*].');
33
- const arrayValue = JsonGetSet.get(maskedObj, arrayFieldName);
34
- for(const arrayElement of arrayValue) {
35
- const value = arrayElement[subField];
36
- if(value === undefined || value === null) {
37
- continue;
38
- } else {
39
- if(typeof(value) == 'string' && options.maxMaskedCharactersStr != -1 && options.maxMaskedCharactersStr < value.length) {
40
- JsonGetSet.set(arrayElement, subField, (`${options.maskWith}`.repeat(options.maxMaskedCharactersStr)))
41
- } else {
42
- JsonGetSet.set(arrayElement, subField, (`${options.maskWith}`.repeat(value.toString().length)));
43
- }
44
- }
45
- }
46
- } else if(field.includes('.*')) {
47
- let subField = field.split('.*')[0];
48
- const innerObject = JsonGetSet.get(maskedObj, subField);
49
- for(const innerObjectField of Object.keys(innerObject)) {
50
- const value = innerObject[innerObjectField];
51
- if(value === undefined || value === null) {
52
- continue;
53
- } else {
54
- if(typeof(value) == 'string' && options.maxMaskedCharactersStr != -1 && options.maxMaskedCharactersStr < value.length) {
55
- JsonGetSet.set(innerObject, innerObjectField, (`${options.maskWith}`.repeat(options.maxMaskedCharactersStr)))
56
- } else {
57
- JsonGetSet.set(innerObject, innerObjectField, (`${options.maskWith}`.repeat(value.toString().length)));
58
- }
59
- }
26
+ static maskJSONFields(obj, options) {
27
+ if (options) {
28
+ options = MaskHelper.mapWithDefaultValues(options, Constants.defaultJsonMaskOptions);
29
+ MaskHelper.validateJSONOptions(options);
30
+ } else {
31
+ options = Constants.defaultJsonMaskOptions;
32
+ }
33
+ let maskedObj = {};
34
+ try {
35
+ maskedObj = JSON.parse(JSON.stringify(obj));
36
+ } catch (ex) {
37
+ return obj;
38
+ }
39
+ const fields = options.fields;
40
+ for (const field of fields) {
41
+ try {
42
+ if (field.includes('[*].')) {
43
+ let [arrayFieldName, subField] = field.split('[*].');
44
+ const arrayValue = JsonGetSet.get(maskedObj, arrayFieldName);
45
+ for (const arrayElement of arrayValue) {
46
+ const value = arrayElement[subField];
47
+ if (value === undefined || value === null) {
48
+ continue;
49
+ } else {
50
+ if (
51
+ typeof value == 'string' &&
52
+ options.maxMaskedCharactersStr != -1 &&
53
+ options.maxMaskedCharactersStr < value.length
54
+ ) {
55
+ JsonGetSet.set(
56
+ arrayElement,
57
+ subField,
58
+ `${options.maskWith}`.repeat(options.maxMaskedCharactersStr)
59
+ );
60
+ } else {
61
+ JsonGetSet.set(
62
+ arrayElement,
63
+ subField,
64
+ `${options.maskWith}`.repeat(value.toString().length)
65
+ );
60
66
  }
67
+ }
68
+ }
69
+ } else if (field.includes('.*')) {
70
+ let subField = field.split('.*')[0];
71
+ const innerObject = JsonGetSet.get(maskedObj, subField);
72
+ for (const innerObjectField of Object.keys(innerObject)) {
73
+ const value = innerObject[innerObjectField];
74
+ if (value === undefined || value === null) {
75
+ continue;
61
76
  } else {
62
- const value = JsonGetSet.get(maskedObj, field);
63
- if(value === undefined || value === null) {
64
- continue;
65
- }
66
- if(typeof(value) == 'string' && options.maxMaskedCharactersStr != -1 && options.maxMaskedCharactersStr < value.length) {
67
- JsonGetSet.set(maskedObj, field, (`${options.maskWith}`.repeat(options.maxMaskedCharactersStr)))
68
- } else {
69
- JsonGetSet.set(maskedObj, field, (`${options.maskWith}`.repeat(value.toString().length)))
70
- }
77
+ if (
78
+ typeof value == 'string' &&
79
+ options.maxMaskedCharactersStr != -1 &&
80
+ options.maxMaskedCharactersStr < value.length
81
+ ) {
82
+ JsonGetSet.set(
83
+ innerObject,
84
+ innerObjectField,
85
+ `${options.maskWith}`.repeat(options.maxMaskedCharactersStr)
86
+ );
87
+ } else {
88
+ JsonGetSet.set(
89
+ innerObject,
90
+ innerObjectField,
91
+ `${options.maskWith}`.repeat(value.toString().length)
92
+ );
93
+ }
71
94
  }
72
- } catch(ex) { continue; }
95
+ }
96
+ } else {
97
+ const value = JsonGetSet.get(maskedObj, field);
98
+ if (value === undefined || value === null) {
99
+ continue;
100
+ }
101
+ if (
102
+ typeof value == 'string' &&
103
+ options.maxMaskedCharactersStr != -1 &&
104
+ options.maxMaskedCharactersStr < value.length
105
+ ) {
106
+ JsonGetSet.set(
107
+ maskedObj,
108
+ field,
109
+ `${options.maskWith}`.repeat(options.maxMaskedCharactersStr)
110
+ );
111
+ } else {
112
+ JsonGetSet.set(maskedObj, field, `${options.maskWith}`.repeat(value.toString().length));
113
+ }
73
114
  }
74
- return maskedObj;
75
- }
76
-
77
- static maskJSON2(obj, config) {
78
- const validatedConfig = MaskHelper.getValidatedJsonMask2Configs(config)
79
- let maskedObj = {};
80
- try {
81
- maskedObj = JSON.parse(JSON.stringify(obj));
82
115
  } catch (ex) {
83
- return obj;
84
- }
85
- for(const field of validatedConfig.cardFields) {
86
- const masked = CardMask.maskCard(JsonGetSet.get(maskedObj, field), validatedConfig.cardMaskOptions);
87
- JsonGetSet.set(maskedObj, field, masked);
116
+ continue;
88
117
  }
118
+ }
119
+ return maskedObj;
120
+ }
89
121
 
90
- for(const field of validatedConfig.emailFields) {
91
- const masked = EmailMask.maskEmail2(JsonGetSet.get(maskedObj, field), validatedConfig.emailMaskOptions);
92
- JsonGetSet.set(maskedObj, field, masked);
93
- }
122
+ static maskJSON2(obj, config) {
123
+ const validatedConfig = MaskHelper.getValidatedJsonMask2Configs(config);
124
+ let maskedObj = {};
125
+ try {
126
+ maskedObj = JSON.parse(JSON.stringify(obj));
127
+ } catch (ex) {
128
+ return obj;
129
+ }
94
130
 
95
- for(const field of validatedConfig.passwordFields) {
96
- const masked = PasswordMask.maskPassword(JsonGetSet.get(maskedObj, field), validatedConfig.passwordMaskOptions);
97
- JsonGetSet.set(maskedObj, field, masked);
131
+ for (const key of Object.keys(typeToFunctionMap)) {
132
+ if (!validatedConfig[typeToFunctionMap[key][1]]) continue;
133
+ for (const field of validatedConfig[typeToFunctionMap[key][1]]) {
134
+ this.mask(maskedObj, field, key, validatedConfig[typeToFunctionMap[key][2]]);
98
135
  }
136
+ }
137
+ return maskedObj;
138
+ }
99
139
 
100
- for(const field of validatedConfig.phoneFields) {
101
- const masked = PhoneMask.maskPhone(JsonGetSet.get(maskedObj, field), validatedConfig.phoneMaskOptions);
102
- JsonGetSet.set(maskedObj, field, masked);
103
- }
140
+ static mask(json, field, type, maskConfig) {
141
+ if (field.includes('.*') && !field.endsWith('.*')) {
142
+ console.warn('Ignoring the invalid field: ', field);
143
+ return;
144
+ }
104
145
 
105
- for(const field of validatedConfig.stringFields) {
106
- const masked = StringMask.maskString(JsonGetSet.get(maskedObj, field), validatedConfig.stringMaskOptions);
107
- JsonGetSet.set(maskedObj, field, masked);
146
+ if (field.includes('[*].')) {
147
+ const index = field.indexOf('[*]');
148
+ const left = field.substring(0, index);
149
+ const right = field.substring(index + 3);
150
+ const count = JsonGetSet.get(json, left);
151
+ if (count === undefined || count === null || count === '') return;
152
+ let i = 0;
153
+ for (let i = 0; i < count.length; i++) {
154
+ this.mask(json, `${left}[${i}]${right}`, type, maskConfig);
155
+ }
156
+ } else if (field.includes('.*')) {
157
+ const index = field.indexOf('.*');
158
+ const left = field.substring(0, index);
159
+ const keys = this.getNestedKeys(JsonGetSet.get(json, left), left);
160
+ for (const key of keys) {
161
+ this.mask(json, key, type, maskConfig);
108
162
  }
163
+ } else {
164
+ const value = JsonGetSet.get(json, field);
165
+ if (value !== null && value !== undefined) {
166
+ const masked = typeToFunctionMap[type][0](value, maskConfig);
167
+ JsonGetSet.set(json, field, masked);
168
+ return json;
169
+ }
170
+ }
171
+ }
109
172
 
110
- for(const field of validatedConfig.uuidFields) {
111
- const masked = UuidMask.maskUuid(JsonGetSet.get(maskedObj, field), validatedConfig.uuidMaskOptions);
112
- JsonGetSet.set(maskedObj, field, masked);
173
+ static getNestedKeys(obj, prefix = '') {
174
+ let keys = [];
175
+ if (typeof obj === 'string') {
176
+ keys.push(prefix);
177
+ return keys;
178
+ }
179
+ for (let key in obj) {
180
+ if (obj.hasOwnProperty(key)) {
181
+ const fullPath = prefix ? `${prefix}.${key}` : key;
182
+ if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) {
183
+ keys = keys.concat(this.getNestedKeys(obj[key], fullPath));
184
+ } else if (Array.isArray(obj[key])) {
185
+ for (let i = 0; i < obj[key].length; i++) {
186
+ keys = keys.concat(this.getNestedKeys(obj[key][i], `${fullPath}[${i}]`));
187
+ }
188
+ } else {
189
+ keys.push(fullPath);
190
+ }
113
191
  }
114
- return maskedObj;
115
192
  }
193
+ return keys;
194
+ }
116
195
  }
117
196
 
118
- module.exports = JsonMask
197
+ module.exports = JsonMask;
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+ const MaskHelper = require('../helpers/MaskHelper');
3
+ const Constants = require('../helpers/Constants');
4
+
5
+ class JwtMask {
6
+ static maskJwt(jwt, options) {
7
+ if (!jwt || typeof jwt !== 'string' || jwt.length < 5) return jwt;
8
+ const countOfDots = jwt.split('.').length - 1;
9
+ if (countOfDots != 2) return jwt;
10
+ let maskedJwt = '';
11
+ options = options
12
+ ? MaskHelper.mapWithDefaultValues(options, Constants.defaultJwtMaskOptions)
13
+ : Constants.defaultJwtMaskOptions;
14
+ MaskHelper.validateJwtMaskOptions(options);
15
+ const indexOfFirstDot = jwt.indexOf('.');
16
+ const indexOfSecondDot = jwt.indexOf('.', indexOfFirstDot + 1);
17
+
18
+ if (!indexOfFirstDot || !indexOfSecondDot || indexOfFirstDot <= 0 || indexOfSecondDot <= 0) {
19
+ return jwt;
20
+ }
21
+
22
+ const part1 = jwt.substring(0, indexOfFirstDot);
23
+ const part2 = jwt.substring(indexOfFirstDot + 1, indexOfSecondDot);
24
+ const part3 = jwt.substring(indexOfSecondDot + 1);
25
+
26
+ const maxMasked =
27
+ options.maxMaskedCharacters > 2
28
+ ? options.maxMaskedCharacters - (options.maskDot === true ? 2 : 0)
29
+ : options.maxMaskedCharacters;
30
+
31
+ let maskpartcnt = 0;
32
+ let maskLength = 0;
33
+ if (options.maskHeader === true) {
34
+ maskpartcnt++;
35
+ maskLength += part1.length;
36
+ }
37
+ if (options.maskPayload === true) {
38
+ maskpartcnt++;
39
+ maskLength += part2.length;
40
+ }
41
+ if (options.maskSignature === true) {
42
+ maskpartcnt++;
43
+ maskLength += part3.length;
44
+ }
45
+
46
+ let residual = 0;
47
+ let eachMask = 0;
48
+
49
+ const isJwtLengthGreater = maskLength > maxMasked;
50
+
51
+ if (isJwtLengthGreater) {
52
+ residual = maxMasked % maskpartcnt;
53
+ eachMask = maxMasked / maskpartcnt;
54
+ }
55
+
56
+ let maskedCharCount = 0;
57
+
58
+ if (options.maskHeader === false) {
59
+ maskedJwt += part1;
60
+ maskedJwt += options.maskDot === true ? options.maskWith : '.';
61
+ } else {
62
+ maskedJwt += isJwtLengthGreater
63
+ ? options.maskWith.repeat(residual + eachMask)
64
+ : options.maskWith.repeat(part1.length);
65
+ residual = 0;
66
+ maskedCharCount += part1.length;
67
+ maskedJwt += options.maskDot === true ? options.maskWith : '.';
68
+ }
69
+
70
+ if (options.maskPayload === false) {
71
+ maskedJwt += part2;
72
+ maskedJwt += options.maskDot === true ? options.maskWith : '.';
73
+ } else {
74
+ maskedJwt += isJwtLengthGreater
75
+ ? options.maskWith.repeat(residual + eachMask)
76
+ : options.maskWith.repeat(part2.length);
77
+ residual = 0;
78
+ maskedJwt += options.maskDot === true ? options.maskWith : '.';
79
+ }
80
+
81
+ if (options.maskSignature === false) {
82
+ maskedJwt += part3;
83
+ } else {
84
+ maskedJwt += isJwtLengthGreater
85
+ ? options.maskWith.repeat(residual + eachMask)
86
+ : options.maskWith.repeat(part3.length);
87
+ }
88
+ return maskedJwt;
89
+ }
90
+ }
91
+
92
+ module.exports = JwtMask;
@@ -3,41 +3,42 @@ const MaskHelper = require('../helpers/MaskHelper');
3
3
  const Constants = require('../helpers/Constants');
4
4
 
5
5
  class PasswordMask {
6
- static maskPassword(password, options) {
7
- if(!password || typeof(password) === 'object') return password;
8
- password = password + ''
9
- if(options) {
10
- options = MaskHelper.mapWithDefaultValues(options, Constants.defaultPasswordMaskOptions);
11
- MaskHelper.validatePasswordOptions(options);
12
- } else {
13
- options = Constants.defaultPasswordMaskOptions;
14
- }
15
- let maskPasswordLength = password.length;
16
- if(password.length > options.maxMaskedCharacters) {
17
- maskPasswordLength = parseInt(options.maxMaskedCharacters);
18
- }
19
- const maskingCharacters = maskPasswordLength - options.unmaskedStartCharacters - options.unmaskedEndCharacters;
20
-
21
- if(maskingCharacters < 0) {
22
- if(maskPasswordLength <= options.unmaskedStartCharacters) {
23
- return password.substring(0, maskPasswordLength);
24
- } else {
25
- let maskedPassword = password.substring(0, options.unmaskedStartCharacters);
26
- const remainingChars = maskPasswordLength - options.unmaskedStartCharacters;
27
- for(let i = password.length-remainingChars; i < password.length; i++) {
28
- maskedPassword += password[i];
29
- }
30
- return maskedPassword;
31
- }
32
- }
33
- let maskedPassword = "";
34
- maskedPassword = password.substring(0, options.unmaskedStartCharacters);
35
- maskedPassword += `${options.maskWith}`.repeat(maskingCharacters)
36
- for(let i = password.length-options.unmaskedEndCharacters; i < password.length; i++) {
6
+ static maskPassword(password, options) {
7
+ if (!password || typeof password === 'object') return password;
8
+ password = password + '';
9
+ if (options) {
10
+ options = MaskHelper.mapWithDefaultValues(options, Constants.defaultPasswordMaskOptions);
11
+ MaskHelper.validatePasswordOptions(options);
12
+ } else {
13
+ options = Constants.defaultPasswordMaskOptions;
14
+ }
15
+ let maskPasswordLength = password.length;
16
+ if (password.length > options.maxMaskedCharacters) {
17
+ maskPasswordLength = parseInt(options.maxMaskedCharacters);
18
+ }
19
+ const maskingCharacters =
20
+ maskPasswordLength - options.unmaskedStartCharacters - options.unmaskedEndCharacters;
21
+
22
+ if (maskingCharacters < 0) {
23
+ if (maskPasswordLength <= options.unmaskedStartCharacters) {
24
+ return password.substring(0, maskPasswordLength);
25
+ } else {
26
+ let maskedPassword = password.substring(0, options.unmaskedStartCharacters);
27
+ const remainingChars = maskPasswordLength - options.unmaskedStartCharacters;
28
+ for (let i = password.length - remainingChars; i < password.length; i++) {
37
29
  maskedPassword += password[i];
38
- }
30
+ }
39
31
  return maskedPassword;
32
+ }
33
+ }
34
+ let maskedPassword = '';
35
+ maskedPassword = password.substring(0, options.unmaskedStartCharacters);
36
+ maskedPassword += `${options.maskWith}`.repeat(maskingCharacters);
37
+ for (let i = password.length - options.unmaskedEndCharacters; i < password.length; i++) {
38
+ maskedPassword += password[i];
40
39
  }
40
+ return maskedPassword;
41
+ }
41
42
  }
42
43
 
43
- module.exports = PasswordMask
44
+ module.exports = PasswordMask;
@@ -3,21 +3,25 @@ const MaskHelper = require('../helpers/MaskHelper');
3
3
  const Constants = require('../helpers/Constants');
4
4
 
5
5
  class PhoneMask {
6
- static maskPhone(phone, options) {
7
- if(!phone || typeof(phone) === 'object') return phone;
8
- phone += '';
9
- if(options) {
10
- options = MaskHelper.mapWithDefaultValues(options, Constants.defaultPhoneMaskOptions);
11
- MaskHelper.validatePhoneOptions(options);
12
- } else {
13
- options = Constants.defaultPhoneMaskOptions;
14
- }
15
- let maskLength = phone.length - options.unmaskedStartDigits - options.unmaskedEndDigits;
16
- if((options.unmaskedStartDigits + options.unmaskedEndDigits) >= phone.length) {
17
- return phone;
18
- }
19
- return phone.substring(0, options.unmaskedStartDigits) + `${options.maskWith}`.repeat(maskLength) + phone.substring(phone.length-options.unmaskedEndDigits);
6
+ static maskPhone(phone, options) {
7
+ if (!phone || typeof phone === 'object') return phone;
8
+ phone += '';
9
+ if (options) {
10
+ options = MaskHelper.mapWithDefaultValues(options, Constants.defaultPhoneMaskOptions);
11
+ MaskHelper.validatePhoneOptions(options);
12
+ } else {
13
+ options = Constants.defaultPhoneMaskOptions;
20
14
  }
15
+ let maskLength = phone.length - options.unmaskedStartDigits - options.unmaskedEndDigits;
16
+ if (options.unmaskedStartDigits + options.unmaskedEndDigits >= phone.length) {
17
+ return phone;
18
+ }
19
+ return (
20
+ phone.substring(0, options.unmaskedStartDigits) +
21
+ `${options.maskWith}`.repeat(maskLength) +
22
+ phone.substring(phone.length - options.unmaskedEndDigits)
23
+ );
24
+ }
21
25
  }
22
26
 
23
- module.exports = PhoneMask
27
+ module.exports = PhoneMask;