maskdata 1.1.8 → 1.2.0

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/index.js CHANGED
@@ -1,88 +1,30 @@
1
1
  'use strict';
2
2
 
3
- const MaskHelper = require('./lib/helpers/MaskHelper');
4
- const MaskEmail = require('./lib/emailMask/EmailMask');
5
- const MaskCard = require('./lib/cardMask/CardMask');
6
- const {get, set} = require('lodash');
7
-
8
- const defaultPhoneMaskOptions = {
9
- maskWith: "*",
10
- unmaskedStartDigits: 4,
11
- unmaskedEndDigits: 1
12
- };
13
-
14
- const defaultJsonMaskOptions = {
15
- maskWith: "*",
16
- fields: []
17
- };
18
-
19
- const defaultPasswordMaskOptions = {
20
- maskWith: "*",
21
- maxMaskedCharacters: 16,
22
- unmaskedStartCharacters: 0,
23
- unmaskedEndCharacters: 0
24
- };
25
-
26
- const defaultStringMaskOptions = {
27
- maskWith: "*",
28
- maskOnlyFirstOccurance: false,
29
- values: [],
30
- maskAll: false,
31
- maskSpace: true
32
- };
3
+ const MaskEmail = require('./lib/maskService/EmailMask');
4
+ const MaskCard = require('./lib/maskService/CardMask');
5
+ const MaskPhone = require('./lib/maskService/PhoneMask');
6
+ const MaskString = require('./lib/maskService/StringMask');
7
+ const MaskPassword = require('./lib/maskService/PasswordMask');
8
+ const MaskUuid = require('./lib/maskService/UuidMask');
9
+ const JsonMask = require('./lib/maskService/JsonMask');
10
+ const JsonGetSet = require('./lib/helpers/jsonGetSet');
33
11
 
34
12
  class MaskData {
35
-
13
+
36
14
  static maskPassword(password, options) {
37
- if(!password) {
38
- return password;
39
- }
40
- if(options) {
41
- options = MaskHelper.mapWithDefaultValues(options, defaultPasswordMaskOptions);
42
- MaskHelper.validatePasswordOptions(options);
43
- } else {
44
- options = defaultPasswordMaskOptions;
45
- }
46
- let maskPasswordLength = password.length;
47
- if(password.length > options.maxMaskedCharacters) {
48
- maskPasswordLength = parseInt(options.maxMaskedCharacters);
49
- }
50
- const maskingCharacters = maskPasswordLength - options.unmaskedStartCharacters - options.unmaskedEndCharacters;
15
+ return MaskPassword.maskPassword(password, options);
16
+ }
51
17
 
52
- if(maskingCharacters < 0) {
53
- if(maskPasswordLength <= options.unmaskedStartCharacters) {
54
- return password.substr(0, maskPasswordLength);
55
- } else {
56
- let maskedPassword = password.substr(0, options.unmaskedStartCharacters);
57
- const remainingChars = maskPasswordLength - options.unmaskedStartCharacters;
58
- for(let i = password.length-remainingChars; i < password.length; i++) {
59
- maskedPassword += password[i];
60
- }
61
- return maskedPassword;
62
- }
63
- }
64
- let maskedPassword = "";
65
- maskedPassword = password.substr(0, options.unmaskedStartCharacters);
66
- maskedPassword += `${options.maskWith}`.repeat(maskingCharacters)
67
- for(let i = password.length-options.unmaskedEndCharacters; i < password.length; i++) {
68
- maskedPassword += password[i];
69
- }
70
- return maskedPassword;
18
+ static maskJSONFields(json, options) {
19
+ return JsonMask.maskJSONFields(json, options);
20
+ }
21
+
22
+ static maskJSON2(json, options) {
23
+ return JsonMask.maskJSON2(json, options);
71
24
  }
72
25
 
73
26
  static maskPhone(phone, options) {
74
- if(!phone) return phone;
75
- if(options) {
76
- options = MaskHelper.mapWithDefaultValues(options, defaultPhoneMaskOptions);
77
- MaskHelper.validatePhoneOptions(options);
78
- } else {
79
- options = defaultPhoneMaskOptions;
80
- }
81
- let maskLength = phone.length - options.unmaskedStartDigits - options.unmaskedEndDigits;
82
- if((options.unmaskedStartDigits + options.unmaskedEndDigits) >= phone.length) {
83
- return phone;
84
- }
85
- return phone.substr(0, options.unmaskedStartDigits) + `${options.maskWith}`.repeat(maskLength) + phone.substr(phone.length-options.unmaskedEndDigits);
27
+ return MaskPhone.maskPhone(phone, options);
86
28
  }
87
29
 
88
30
  static maskEmail2(email, options) {
@@ -93,95 +35,20 @@ class MaskData {
93
35
  return MaskCard.maskCard(cardNumber, options);
94
36
  }
95
37
 
96
- static maskJSONFields(obj, options) {
97
- if(options) {
98
- options = MaskHelper.mapWithDefaultValues(options, defaultJsonMaskOptions);
99
- MaskHelper.validateJSONOptions(options);
100
- } else {
101
- options = defaultJsonMaskOptions;
102
- }
103
- let maskedObj = {};
104
- try {
105
- maskedObj = JSON.parse(JSON.stringify(obj));
106
- } catch (ex) {
107
- return obj;
108
- }
109
- const fields = options.fields;
110
-
111
- for(const field of fields) {
112
- try {
113
- if(field.includes('[*].')) {
114
- let [arrayFieldName, subField] = field.split('[*].');
115
- const arrayValue = get(maskedObj, arrayFieldName);
116
- for(const arrayElement of arrayValue) {
117
- const value = arrayElement[subField];
118
- if(value !== undefined && value !== null) {
119
- set(arrayElement, subField, (`${options.maskWith}`.repeat(value.toString().length)))
120
- }
121
- }
122
- } else if(field.includes('.*')) {
123
- let subField = field.split('.*')[0];
124
- const innerObject = get(maskedObj, subField);
125
- for(const innerObjectField of Object.keys(innerObject)) {
126
- const value = innerObject[innerObjectField];
127
- if(value !== undefined && value !== null) {
128
- set(innerObject, innerObjectField, (`${options.maskWith}`.repeat(value.toString().length)))
129
- }
130
- }
131
- } else {
132
- const value = get(maskedObj, field);
133
- if(value !== undefined && value !== null) {
134
- set(maskedObj, field, (`${options.maskWith}`.repeat(value.toString().length)))
135
- }
136
- }
137
- } catch(ex) { continue; }
138
- }
139
- return maskedObj;
38
+ static maskString(str, options) {
39
+ return MaskString.maskString(str, options);
140
40
  }
141
41
 
142
- static maskString(str, options) {
143
- if(!str) return str;
144
- str = str + '';
145
- if(options) {
146
- options = MaskHelper.mapWithDefaultValues(options, defaultStringMaskOptions);
147
- MaskHelper.validateStringOptions(options);
148
- } else {
149
- options = defaultStringMaskOptions;
150
- }
151
- let values = options.values;
152
- if(options.maskAll === true) {
153
- let result = '';
154
- if(options.maskSpace === true) {
155
- result = options.maskWith.repeat(str.length);
156
- } else {
157
- for(let eachChar of str) {
158
- if(eachChar === ' ') {
159
- result += ' ';
160
- } else {
161
- result += options.maskWith;
162
- }
163
- }
164
- }
165
- return result;
166
- }
167
- else if(options.maskOnlyFirstOccurance == true) {
168
- for(const value of Object.values(values)) {
169
- str = str.replace(value, `${options.maskWith}`.repeat(value.length));
170
- }
171
- } else {
172
- for(const value of Object.values(values)) {
173
- str = str.replace(new RegExp(value, 'g'), `${options.maskWith}`.repeat(value.length));
174
- }
175
- }
176
- return str;
42
+ static maskUuid(uuid, options) {
43
+ return MaskUuid.maskUuid(uuid, options);
177
44
  }
178
45
 
179
46
  static getInnerProperty(object, field) {
180
- return get(object, field);
47
+ return JsonGetSet.get(object, field);
181
48
  }
182
49
 
183
50
  static replaceValue(object, field, value) {
184
- return set(object, field, value);
51
+ return JsonGetSet.set(object, field, value);
185
52
  }
186
53
 
187
54
  }
@@ -0,0 +1,72 @@
1
+ const defaultCardMaskOptions = {
2
+ maskWith: "*",
3
+ unmaskedStartDigits: 4,
4
+ unmaskedEndDigits: 1
5
+ };
6
+
7
+ const defaultEmailMask2Options = {
8
+ maskWith: "*",
9
+ unmaskedStartCharactersBeforeAt: 3,
10
+ unmaskedEndCharactersAfterAt: 2,
11
+ maskAtTheRate: false
12
+ };
13
+
14
+ const defaultJsonMaskOptions = {
15
+ maskWith: "*",
16
+ fields: [],
17
+ maxMaskedCharactersStr: -1
18
+ };
19
+
20
+ const defaultPasswordMaskOptions = {
21
+ maskWith: "*",
22
+ maxMaskedCharacters: 16,
23
+ unmaskedStartCharacters: 0,
24
+ unmaskedEndCharacters: 0
25
+ };
26
+
27
+ const defaultPhoneMaskOptions = {
28
+ maskWith: "*",
29
+ unmaskedStartDigits: 4,
30
+ unmaskedEndDigits: 1
31
+ };
32
+
33
+ const defaultStringMaskOptions = {
34
+ maskWith: "*",
35
+ maskOnlyFirstOccurance: false,
36
+ values: [],
37
+ maskAll: false,
38
+ maskSpace: true
39
+ };
40
+
41
+ const defaultUuidMaskOptions = {
42
+ maskWith: "*",
43
+ unmaskedStartCharacters: 0,
44
+ unmaskedEndCharacters: 0
45
+ };
46
+
47
+ const defaultjsonMask2Configs = {
48
+ cardMaskOptions: defaultCardMaskOptions,
49
+ cardFields: [],
50
+ emailMaskOptions: defaultEmailMask2Options,
51
+ emailFields: [],
52
+ passwordMaskOptions: defaultPasswordMaskOptions,
53
+ passwordFields: [],
54
+ phoneMaskOptions: defaultPhoneMaskOptions,
55
+ phoneFields: [],
56
+ stringMaskOptions: defaultStringMaskOptions,
57
+ stringFields: [],
58
+ uuidMaskOptions: defaultUuidMaskOptions,
59
+ uuidFields: []
60
+ };
61
+
62
+
63
+ module.exports = {
64
+ defaultPhoneMaskOptions,
65
+ defaultCardMaskOptions,
66
+ defaultEmailMask2Options,
67
+ defaultJsonMaskOptions,
68
+ defaultPasswordMaskOptions,
69
+ defaultStringMaskOptions,
70
+ defaultUuidMaskOptions,
71
+ defaultjsonMask2Configs
72
+ }
@@ -1,4 +1,5 @@
1
1
  const BadOption = require('../ExceptionsHandler/BadOption');
2
+ const Constants = require('./Constants');
2
3
 
3
4
  class MaskHelper {
4
5
 
@@ -149,6 +150,9 @@ class MaskHelper {
149
150
  if((!options.maskWith) || options.maskWith.toString().length <= 0) {
150
151
  options.maskWith = '*';
151
152
  }
153
+ if(!options.maxMaskedCharactersStr || typeof(options.maxMaskedCharactersStr) !== 'number' || options.maxMaskedCharactersStr <= 0) {
154
+ options.maxMaskedCharactersStr = -1;
155
+ }
152
156
  if(reasons.length > 0) {
153
157
  const badOptions = new BadOption('JSON mask configuration', reasons);
154
158
  badOptions.throwBadOptionException();
@@ -178,6 +182,32 @@ class MaskHelper {
178
182
  }
179
183
  }
180
184
 
185
+ static validateUuidMaskOptions(options) {
186
+ let reasons = [];
187
+ try{
188
+ options.unmaskedStartCharacters = parseInt(options.unmaskedStartCharacters);
189
+ options.unmaskedEndCharacters = parseInt(options.unmaskedEndCharacters);
190
+ if(isNaN(options.unmaskedStartCharacters) || isNaN(options.unmaskedEndCharacters)){
191
+ throw Error();
192
+ };
193
+ }catch(err) {
194
+ reasons.push('unmaskedStartCharacters, unmaskedEndCharacters must be positive integers');
195
+ }
196
+
197
+ if(options.unmaskedStartCharacters > 32 || options.unmaskedEndCharacters > 32) {
198
+ options.unmaskedStartCharacters = 32;
199
+ options.unmaskedEndCharacters = 0;
200
+ }
201
+
202
+ if((!options.maskWith) || options.maskWith.toString().length <= 0) {
203
+ options.maskWith = '*';
204
+ }
205
+ if(reasons.length > 0) {
206
+ const badOptions = new BadOption('Uuid mask configuration: ', reasons);
207
+ badOptions.throwBadOptionException();
208
+ }
209
+ }
210
+
181
211
  static validateCardMaskOptions(options, cardLength) {
182
212
  let reasons = [];
183
213
  try{
@@ -207,6 +237,37 @@ class MaskHelper {
207
237
  badOptions.throwBadOptionException();
208
238
  }
209
239
  }
240
+
241
+ static getValidatedJsonMask2Configs(configs) {
242
+ if(!configs) return JSON.parse(JSON.stringify(Constants.defaultjsonMask2Configs));
243
+
244
+ if(!configs.cardMaskOptions) {
245
+ configs.cardMaskOptions = JSON.parse(JSON.stringify(Constants.defaultCardMaskOptions));
246
+ }
247
+
248
+ if(!configs.emailMaskOptions) {
249
+ configs.emailMaskOptions = JSON.parse(JSON.stringify(Constants.defaultEmailMask2Options));
250
+ }
251
+
252
+ if(!configs.passwordMaskOptions) {
253
+ configs.passwordMaskOptions = JSON.parse(JSON.stringify(Constants.defaultPasswordMaskOptions));
254
+ }
255
+
256
+ if(!configs.phoneMaskOptions) {
257
+ configs.phoneMaskOptions = JSON.parse(JSON.stringify(Constants.defaultPhoneMaskOptions));
258
+ }
259
+
260
+ if(!configs.stringMaskOptions) {
261
+ configs.stringMaskOptions = JSON.parse(JSON.stringify(Constants.defaultStringMaskOptions));
262
+ }
263
+
264
+ if(!configs.uuidMaskOptions) {
265
+ configs.uuidMaskOptions = JSON.parse(JSON.stringify(Constants.defaultUuidMaskOptions));
266
+ }
267
+
268
+ return configs;
269
+ }
270
+
210
271
  }
211
272
 
212
273
  module.exports = MaskHelper;
@@ -0,0 +1,14 @@
1
+ const {get, set} = require('lodash');
2
+
3
+ class JsonGetSet {
4
+ static get(object, field) {
5
+ return get(object, field);
6
+ }
7
+
8
+ static set(object, field, value) {
9
+ return set(object, field, value);
10
+ }
11
+ }
12
+
13
+ module.exports = JsonGetSet
14
+
@@ -1,22 +1,17 @@
1
+ 'use strict';
1
2
  const MaskHelper = require('../helpers/MaskHelper');
2
-
3
- const defaultCardMaskOptions = {
4
- maskWith: "*",
5
- unmaskedStartDigits: 4,
6
- unmaskedEndDigits: 1
7
- };
3
+ const Constants = require('../helpers/Constants');
8
4
 
9
5
  class CardMask {
10
6
  static maskCard(card, options) {
11
7
  if(!card) return card;
12
8
  let cardToProcess = (card + '').trim();
13
9
  if(options) {
14
- options = MaskHelper.mapWithDefaultValues(options, defaultCardMaskOptions);
10
+ options = MaskHelper.mapWithDefaultValues(options, Constants.defaultCardMaskOptions);
15
11
  MaskHelper.validateCardMaskOptions(options, cardToProcess.length);
16
12
  } else {
17
- options = defaultCardMaskOptions;
13
+ options = Constants.defaultCardMaskOptions;
18
14
  }
19
- if(options.unmaskedEndDigits == 0 && options.unmaskedStartDigits == 0) return cardToProcess;
20
15
  let countOfNumbers = 0;
21
16
  let lastIndex = 0;
22
17
  let flag = true;
@@ -1,34 +1,28 @@
1
+ 'use strict';
1
2
  const MaskHelper = require('../helpers/MaskHelper');
2
-
3
- const defaultEmailMask2Options = {
4
- maskWith: "*",
5
- unmaskedStartCharactersBeforeAt: 3,
6
- unmaskedEndCharactersAfterAt: 2,
7
- maskAtTheRate: false
8
- };
3
+ const Constants = require('../helpers/Constants');
9
4
 
10
5
  class EmailMask {
11
6
 
12
7
  static maskEmail2(email, options) {
13
- if(!email) return email;
8
+ if(!email || typeof(email) !== 'string') return email;
14
9
  let maskedEmail = '';
15
- options = options ? MaskHelper.mapWithDefaultValues(options, defaultEmailMask2Options) : defaultEmailMask2Options;
10
+ options = options ? MaskHelper.mapWithDefaultValues(options, Constants.defaultEmailMask2Options)
11
+ : Constants.defaultEmailMask2Options;
16
12
  MaskHelper.validateEmailOptions2(email, options);
17
13
  const indexOfAt = email.indexOf('@');
18
-
19
- if(indexOfAt < 0) return email;
20
-
14
+ // if(indexOfAt < 0) return email; // Not needed since we have the validation for mandatory '@'
21
15
  const [before, after] = email.split('@');
22
16
  const beforeLength = before.length;
23
17
  const afterLength = after.length;
24
18
 
25
19
  maskedEmail += (options.unmaskedStartCharactersBeforeAt >= beforeLength) ? before :
26
- before.substr(0, options.unmaskedStartCharactersBeforeAt) + `${options.maskWith}`.repeat(beforeLength - options.unmaskedStartCharactersBeforeAt);
20
+ before.substring(0, options.unmaskedStartCharactersBeforeAt) + `${options.maskWith}`.repeat(beforeLength - options.unmaskedStartCharactersBeforeAt);
27
21
 
28
22
  maskedEmail += options.maskAtTheRate ? options.maskWith : '@';
29
23
 
30
24
  maskedEmail += (options.unmaskedEndCharactersAfterAt >= afterLength) ? after :
31
- (`${options.maskWith}`.repeat(afterLength-options.unmaskedEndCharactersAfterAt) + after.substr(afterLength - options.unmaskedEndCharactersAfterAt));
25
+ (`${options.maskWith}`.repeat(afterLength-options.unmaskedEndCharactersAfterAt) + after.substring(afterLength - options.unmaskedEndCharactersAfterAt));
32
26
  return maskedEmail;
33
27
  }
34
28
  }
@@ -0,0 +1,118 @@
1
+ 'use strict';
2
+
3
+ const MaskHelper = require('../helpers/MaskHelper');
4
+ const Constants = require('../helpers/Constants');
5
+
6
+ const CardMask = require('./CardMask');
7
+ const EmailMask = require('./EmailMask');
8
+ const PasswordMask = require('./PasswordMask');
9
+ const PhoneMask = require('./PhoneMask');
10
+ const StringMask = require('./StringMask');
11
+ const UuidMask = require('./UuidMask');
12
+ const JsonGetSet = require('../helpers/jsonGetSet');
13
+
14
+ 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
+ }
60
+ }
61
+ } 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
+ }
71
+ }
72
+ } catch(ex) { continue; }
73
+ }
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
+ } 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);
88
+ }
89
+
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
+ }
94
+
95
+ for(const field of validatedConfig.passwordFields) {
96
+ const masked = PasswordMask.maskPassword(JsonGetSet.get(maskedObj, field), validatedConfig.passwordMaskOptions);
97
+ JsonGetSet.set(maskedObj, field, masked);
98
+ }
99
+
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
+ }
104
+
105
+ for(const field of validatedConfig.stringFields) {
106
+ const masked = StringMask.maskString(JsonGetSet.get(maskedObj, field), validatedConfig.stringMaskOptions);
107
+ JsonGetSet.set(maskedObj, field, masked);
108
+ }
109
+
110
+ for(const field of validatedConfig.uuidFields) {
111
+ const masked = UuidMask.maskUuid(JsonGetSet.get(maskedObj, field), validatedConfig.uuidMaskOptions);
112
+ JsonGetSet.set(maskedObj, field, masked);
113
+ }
114
+ return maskedObj;
115
+ }
116
+ }
117
+
118
+ module.exports = JsonMask
@@ -0,0 +1,43 @@
1
+ 'use strict';
2
+ const MaskHelper = require('../helpers/MaskHelper');
3
+ const Constants = require('../helpers/Constants');
4
+
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++) {
37
+ maskedPassword += password[i];
38
+ }
39
+ return maskedPassword;
40
+ }
41
+ }
42
+
43
+ module.exports = PasswordMask
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+ const MaskHelper = require('../helpers/MaskHelper');
3
+ const Constants = require('../helpers/Constants');
4
+
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);
20
+ }
21
+ }
22
+
23
+ module.exports = PhoneMask
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+ const MaskHelper = require('../helpers/MaskHelper');
3
+ const Constants = require('../helpers/Constants');
4
+
5
+ class StringMask {
6
+ static maskString(str, options) {
7
+ if(!str || typeof(str) === 'object') return str;
8
+ str = str + '';
9
+ if(options) {
10
+ options = MaskHelper.mapWithDefaultValues(options, Constants.defaultStringMaskOptions);
11
+ MaskHelper.validateStringOptions(options);
12
+ } else {
13
+ options = Constants.defaultStringMaskOptions;
14
+ }
15
+ let values = options.values;
16
+ if(options.maskAll === true) {
17
+ let result = '';
18
+ if(options.maskSpace === true) {
19
+ result = options.maskWith.repeat(str.length);
20
+ } else {
21
+ for(let eachChar of str) {
22
+ if(eachChar === ' ') {
23
+ result += ' ';
24
+ } else {
25
+ result += options.maskWith;
26
+ }
27
+ }
28
+ }
29
+ return result;
30
+ }
31
+ else if(options.maskOnlyFirstOccurance == true) {
32
+ for(const value of Object.values(values)) {
33
+ str = str.replace(value, `${options.maskWith}`.repeat(value.length));
34
+ }
35
+ } else {
36
+ for(const value of Object.values(values)) {
37
+ str = str.replace(new RegExp(value, 'g'), `${options.maskWith}`.repeat(value.length));
38
+ }
39
+ }
40
+ return str;
41
+ }
42
+ }
43
+
44
+ module.exports = StringMask