maskdata 1.0.17 → 1.1.2
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 +78 -53
- package/index.js +16 -90
- package/lib/cardMask/CardMask.js +66 -0
- package/lib/emailMask/EmailMask.js +35 -0
- package/lib/helpers/MaskHelper.js +38 -4
- package/package.json +1 -2
- package/test.js +42 -28
package/README.md
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
maskdata is a Node.js module to mask various kinds of data.
|
|
1
|
+
maskdata is a Node.js module to mask various kinds of data. With the help of maskdata, you can mask email, phone number, card number, JSON fields, password etc..
|
|
2
|
+
<br/>Also, it provides utility methods to get a field, or replace a field from any complex/nested json.
|
|
2
3
|
|
|
3
4
|
# Table of Contents
|
|
4
5
|
- [Features](#features)
|
|
5
6
|
- [Install maskdata](#install-maskdata)
|
|
7
|
+
- [Version 1.1.1 Features](#release-features)
|
|
6
8
|
- [How to Use](#how-to-use)
|
|
7
9
|
- [Mask Phone Number](#mask-phone-number)
|
|
8
10
|
- [Mask Phone Number with the default configuration](#mask-phone-number-with-the-default-configuration)
|
|
@@ -20,27 +22,35 @@ maskdata is a Node.js module to mask various kinds of data.
|
|
|
20
22
|
- [LICENSE - "MIT"](#license---mit)
|
|
21
23
|
|
|
22
24
|
# Features
|
|
23
|
-
* Mask password
|
|
24
|
-
* Simple password mask(Mask using default configurations)
|
|
25
|
-
* Mask password data with the desired configuration
|
|
25
|
+
* Mask password
|
|
26
26
|
* Mask Phone numbers
|
|
27
|
-
* Simple phone number mask(Mask using default configurations)
|
|
28
|
-
* Mask phone number data with the desired configuration
|
|
29
27
|
* Mask Email
|
|
30
|
-
* Simple Email mask(Mask using default configurations)
|
|
31
|
-
* Mask Email data with the desired configuration
|
|
32
28
|
* Mask desired fields in a JSON
|
|
33
|
-
* Mask nested onject fields
|
|
34
29
|
* Mask the given substring from throughout a String
|
|
35
30
|
* Mask the card number
|
|
36
31
|
|
|
37
32
|
# Install maskdata
|
|
38
33
|
> npm i maskdata
|
|
39
34
|
|
|
35
|
+
# Release Features
|
|
36
|
+
### Version: 1.1.1
|
|
37
|
+
Removed the below deprecated methods
|
|
38
|
+
- **simplePasswordMask()** method -> To mask with default configs, simply dont pass the config argument to **maskPassword()** method.
|
|
39
|
+
|
|
40
|
+
- **simplePhoneMask()** method -> To mask with default configs, simply dont pass the config argument to **maskPhone()** method.
|
|
41
|
+
|
|
42
|
+
- **simpleEmailMask()** method -> To mask with default configs, simply dont pass the config argument to **maskEmail()** method.
|
|
43
|
+
|
|
44
|
+
- **maskEmail()** method due to its complications in using the configs and some bugs -> To mask email, start using **maskEmail2()** method with the configs mentioned for maskEmail2 method
|
|
45
|
+
|
|
46
|
+
### Version: 1.1.2
|
|
47
|
+
- Bug fix in maskCard function. Issue: https://github.com/Sumukha1496/maskdata/issues/10
|
|
48
|
+
|
|
40
49
|
# How to Use
|
|
41
50
|
```javascript
|
|
42
51
|
const MaskData = require('./maskdata');
|
|
43
52
|
```
|
|
53
|
+
|
|
44
54
|
## Mask Phone Number
|
|
45
55
|
```javascript
|
|
46
56
|
const MaskData = require('./maskdata');
|
|
@@ -69,6 +79,7 @@ const maskedPhoneNumber = MaskData.maskPhone(phoneNumber, maskPhoneOptions);
|
|
|
69
79
|
```
|
|
70
80
|
|
|
71
81
|
## Mask Phone Number with the default configuration
|
|
82
|
+
To mask with the default options, dont pass the configurations.
|
|
72
83
|
```javascript
|
|
73
84
|
const MaskData = require('./maskdata');
|
|
74
85
|
|
|
@@ -79,12 +90,13 @@ const MaskData = require('./maskdata');
|
|
|
79
90
|
**/
|
|
80
91
|
const phoneNumber = "+111234567890";
|
|
81
92
|
|
|
82
|
-
const maskedPhoneNumber = MaskData.
|
|
93
|
+
const maskedPhoneNumber = MaskData.maskPhone(phoneNumber);
|
|
83
94
|
|
|
84
95
|
//Output: +911********0
|
|
85
96
|
|
|
86
97
|
```
|
|
87
98
|
|
|
99
|
+
|
|
88
100
|
## Mask Password
|
|
89
101
|
```javascript
|
|
90
102
|
const MaskData = require('./maskdata');
|
|
@@ -108,6 +120,7 @@ const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
|
|
|
108
120
|
```
|
|
109
121
|
|
|
110
122
|
## Mask Password with the default configuration
|
|
123
|
+
To mask with the default options, dont pass the configurations.
|
|
111
124
|
```javascript
|
|
112
125
|
const MaskData = require('./maskdata');
|
|
113
126
|
|
|
@@ -118,70 +131,81 @@ const MaskData = require('./maskdata');
|
|
|
118
131
|
|
|
119
132
|
const password = "Password1$";
|
|
120
133
|
|
|
121
|
-
const maskedPassword = MaskData.
|
|
134
|
+
const maskedPassword = MaskData.maskPassword(password)
|
|
122
135
|
|
|
123
136
|
```
|
|
124
137
|
|
|
125
|
-
|
|
126
|
-
Mask
|
|
138
|
+
|
|
139
|
+
## Mask Email id
|
|
140
|
+
Use this method instead of maskEmail(). To mask with the default options, dont pass the configurations.
|
|
127
141
|
```javascript
|
|
128
142
|
const MaskData = require('./maskdata');
|
|
129
143
|
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
144
|
+
const emailMask2Options = {
|
|
145
|
+
maskWith: "*",
|
|
146
|
+
unmaskedStartCharactersBeforeAt: 3,
|
|
147
|
+
unmaskedEndCharactersAfterAt: 2,
|
|
148
|
+
maskAtTheRate: false
|
|
149
|
+
};
|
|
133
150
|
|
|
134
|
-
|
|
135
|
-
// If the starting 'n' characters needs to be unmasked. Default value is 3
|
|
136
|
-
unmaskedStartCharacters: 1,
|
|
151
|
+
const email = "my.test.email@testEmail.com";
|
|
137
152
|
|
|
138
|
-
|
|
139
|
-
// If the ending 'n' characters needs to be unmasked. Default value is 2
|
|
140
|
-
unmaskedEndCharacters: 2,
|
|
153
|
+
const maskedEmail = MaskData.maskEmail2(email, emailMask2Options);
|
|
141
154
|
|
|
142
|
-
|
|
143
|
-
//If '@' needs to be masked. Default value is false(Will not mask)
|
|
144
|
-
maskAtTheRate: false,
|
|
155
|
+
//Output: my.********@**********om
|
|
145
156
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
157
|
+
```
|
|
158
|
+
### Consider an email: abcd@email.com
|
|
159
|
+
Here,
|
|
160
|
+
- number of characters before @ -> 4
|
|
161
|
+
- number of characters after @ --> 9
|
|
149
162
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
163
|
+
1. **unmaskedStartCharactersBeforeAt** --> number of starting characters (before @)not to be masked.
|
|
164
|
+
* If unmaskedStartCharactersBeforeAt > number of characters before @, then it will not mask the characters before @
|
|
165
|
+
2. **unmaskedEndCharactersAfterAt** --> number of characters not to be masked starting from the end till @.
|
|
166
|
+
* If unmaskedEndCharactersAfterAt > number of characters after @, then it will not mask the characters after @
|
|
167
|
+
|
|
168
|
+
### Mask only the characters before '@'
|
|
169
|
+
```javascript
|
|
170
|
+
const MaskData = require('./maskdata');
|
|
171
|
+
|
|
172
|
+
const emailMask2Options = {
|
|
173
|
+
maskWith: "*",
|
|
174
|
+
unmaskedStartCharactersBeforeAt: 0,
|
|
175
|
+
unmaskedEndCharactersAfterAt: 257, // Give a number which is more than the characters after @
|
|
176
|
+
maskAtTheRate: false
|
|
153
177
|
};
|
|
154
178
|
|
|
155
|
-
const email = "
|
|
179
|
+
const email = "abcd@email.com";
|
|
156
180
|
|
|
157
|
-
const maskedEmail = MaskData.
|
|
181
|
+
const maskedEmail = MaskData.maskEmail2(email, emailMask2Options);
|
|
158
182
|
|
|
159
|
-
//Output:
|
|
183
|
+
//Output: ****@email.com
|
|
160
184
|
|
|
161
185
|
```
|
|
162
186
|
|
|
163
187
|
## Mask Email id with the default configuration
|
|
188
|
+
To mask with the default options, dont pass the configurations.
|
|
164
189
|
```javascript
|
|
165
190
|
const MaskData = require('./maskdata');
|
|
166
191
|
|
|
167
192
|
/** Default Options
|
|
168
|
-
maskWith: "*"
|
|
169
|
-
|
|
170
|
-
|
|
193
|
+
maskWith: "*",
|
|
194
|
+
unmaskedStartCharactersBeforeAt: 3,
|
|
195
|
+
unmaskedEndCharactersAfterAt: 2,
|
|
171
196
|
maskAtTheRate: false
|
|
172
|
-
maxMaskedCharactersBeforeAtTheRate: 10
|
|
173
|
-
maxMaskedCharactersAfterAtTheRate: 10
|
|
174
197
|
**/
|
|
175
198
|
const email = "my.test.email@testEmail.com";
|
|
176
199
|
|
|
177
|
-
const maskedEmail = MaskData.
|
|
200
|
+
const maskedEmail = MaskData.maskEmail2(email);
|
|
178
201
|
|
|
179
202
|
//Output: my.********@**********om
|
|
180
203
|
|
|
181
204
|
```
|
|
182
205
|
|
|
206
|
+
|
|
183
207
|
## Get Nested Json Property
|
|
184
|
-
This
|
|
208
|
+
This method returns the value of nested Json property if exists. Otherwise it returns ```undefined```
|
|
185
209
|
```javascript
|
|
186
210
|
const MaskData = require('./maskdata');
|
|
187
211
|
|
|
@@ -207,6 +231,7 @@ const innerPropety = Maskdata.getInnerProperty(nestedObject, 'level1.level2.leve
|
|
|
207
231
|
|
|
208
232
|
```
|
|
209
233
|
|
|
234
|
+
|
|
210
235
|
## Replace the value of a json field
|
|
211
236
|
To replace a value by keeping the type.
|
|
212
237
|
```javascript
|
|
@@ -217,26 +242,22 @@ const input = {
|
|
|
217
242
|
married: true
|
|
218
243
|
}
|
|
219
244
|
|
|
220
|
-
console.log("Before replacing: " + JSON.stringify(input));
|
|
221
|
-
console.log("========================================");
|
|
222
245
|
let afterReplacing = MaskData.replaceValue(input, 'age', 99);
|
|
223
246
|
afterReplacing = MaskData.replaceValue(input, 'married', false);
|
|
224
|
-
console.log("After replacing: " + JSON.stringify(afterReplacing));
|
|
225
|
-
console.log("Type of age: "+ typeof(afterReplacing.age));
|
|
226
|
-
console.log("Type of married: "+ typeof(afterReplacing.married));
|
|
227
247
|
|
|
228
248
|
//Output:
|
|
229
249
|
|
|
230
250
|
Before replacing: {"name":"John","age":33,"married":true}
|
|
231
|
-
========================================
|
|
232
251
|
After replacing: {"name":"John","age":99,"married":false}
|
|
252
|
+
|
|
233
253
|
Type of age: number
|
|
234
254
|
Type of married: boolean
|
|
235
255
|
|
|
236
256
|
```
|
|
237
257
|
|
|
258
|
+
|
|
238
259
|
## Mask fields in a JSON
|
|
239
|
-
This
|
|
260
|
+
This method masks the field value if present in the given object
|
|
240
261
|
```javascript
|
|
241
262
|
const MaskData = require('./maskdata');
|
|
242
263
|
|
|
@@ -259,8 +280,10 @@ const maskedObj = MaskData.maskJSONFields(obj, maskJSONOptions);
|
|
|
259
280
|
//Output: { password: '************', firstName: '***', lastName: 'Snoww' }
|
|
260
281
|
|
|
261
282
|
```
|
|
283
|
+
|
|
284
|
+
|
|
262
285
|
## Mask fields of a nested Object
|
|
263
|
-
This
|
|
286
|
+
This method masks the field value if present in the given object
|
|
264
287
|
|
|
265
288
|
The masked value type will always be string. Won't mask if the value is ```null```.
|
|
266
289
|
|
|
@@ -339,6 +362,7 @@ const maskedObject = MaskData.maskJSONFields(nestedObject, maskAllFields);
|
|
|
339
362
|
|
|
340
363
|
```
|
|
341
364
|
|
|
365
|
+
|
|
342
366
|
## Mask the exact substring from throughout the string
|
|
343
367
|
This will mask the field value if present in the given object
|
|
344
368
|
```javascript
|
|
@@ -365,8 +389,9 @@ const strAfterMasking = MaskData.maskString(str, maskStringOptions);
|
|
|
365
389
|
|
|
366
390
|
```
|
|
367
391
|
|
|
392
|
+
|
|
368
393
|
## Mask card number
|
|
369
|
-
This will mask the card numbers
|
|
394
|
+
This will mask the digits in a card numbers.<br/>This will mask only the numerical data and not any non numeric delimeters, alphabets or any other types of data
|
|
370
395
|
```javascript
|
|
371
396
|
const MaskData = require('./maskdata');
|
|
372
397
|
|
|
@@ -375,14 +400,13 @@ const maskCardOptions = {
|
|
|
375
400
|
maskWith: "X",
|
|
376
401
|
|
|
377
402
|
// Should be positive Integer
|
|
378
|
-
// If the starting 'n'
|
|
403
|
+
// If the starting 'n' digits needs to be unmasked
|
|
379
404
|
// Default value is 4
|
|
380
405
|
unmaskedStartDigits: 4,
|
|
381
406
|
|
|
382
407
|
//Should be positive Integer
|
|
383
|
-
//If the ending 'n'
|
|
408
|
+
//If the ending 'n' digits needs to be unmasked
|
|
384
409
|
// Default value is 1.
|
|
385
|
-
// Max possible value is 4
|
|
386
410
|
unmaskedEndDigits: 1
|
|
387
411
|
};
|
|
388
412
|
|
|
@@ -394,6 +418,7 @@ const cardAfterMasking = MaskData.maskCard(cardNumber, maskCardOptions);
|
|
|
394
418
|
|
|
395
419
|
```
|
|
396
420
|
|
|
421
|
+
|
|
397
422
|
# Report Bugs
|
|
398
423
|
Please raise an issue in github: https://github.com/Sumukha1496/maskdata/issues
|
|
399
424
|
|
package/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const MaskHelper = require('./lib/helpers/MaskHelper');
|
|
4
|
+
const MaskEmail = require('./lib/emailMask/EmailMask');
|
|
5
|
+
const MaskCard = require('./lib/cardMask/CardMask');
|
|
4
6
|
const get = require('lodash.get');
|
|
5
7
|
const set = require('lodash.set');
|
|
6
8
|
|
|
@@ -10,15 +12,6 @@ const defaultPhoneMaskOptions = {
|
|
|
10
12
|
unmaskedEndDigits: 1
|
|
11
13
|
};
|
|
12
14
|
|
|
13
|
-
const defaultEmailMaskOptions = {
|
|
14
|
-
maskWith: "*",
|
|
15
|
-
unmaskedStartCharacters: 3,
|
|
16
|
-
unmaskedEndCharacters: 2,
|
|
17
|
-
maskAtTheRate: false,
|
|
18
|
-
maxMaskedCharactersBeforeAtTheRate: 10,
|
|
19
|
-
maxMaskedCharactersAfterAtTheRate: 10,
|
|
20
|
-
};
|
|
21
|
-
|
|
22
15
|
const defaultJsonMaskOptions = {
|
|
23
16
|
maskWith: "*",
|
|
24
17
|
fields: []
|
|
@@ -35,22 +28,14 @@ const defaultStringMaskOptions = {
|
|
|
35
28
|
values: []
|
|
36
29
|
};
|
|
37
30
|
|
|
38
|
-
const defaultCardMaskOptions = {
|
|
39
|
-
maskWith: "*",
|
|
40
|
-
unmaskedStartDigits: 4,
|
|
41
|
-
unmaskedEndDigits: 1
|
|
42
|
-
};
|
|
43
|
-
|
|
44
31
|
class MaskData {
|
|
45
32
|
|
|
46
|
-
static simplePasswordMask(password) {
|
|
47
|
-
return MaskData.maskPassword(password, defaultPasswordMaskOptions);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
33
|
static maskPassword(password, options) {
|
|
51
34
|
if(options) {
|
|
52
35
|
options = MaskHelper.mapWithDefaultValues(options, defaultPasswordMaskOptions);
|
|
53
36
|
MaskHelper.validatePasswordOptions(options);
|
|
37
|
+
} else {
|
|
38
|
+
options = defaultPasswordMaskOptions;
|
|
54
39
|
}
|
|
55
40
|
let maskPasswordLength = password.length;
|
|
56
41
|
if(password.length > options.maxMaskedCharacters) {
|
|
@@ -59,64 +44,34 @@ class MaskData {
|
|
|
59
44
|
return `${options.maskWith}`.repeat(maskPasswordLength);
|
|
60
45
|
}
|
|
61
46
|
|
|
62
|
-
static simplePhoneMask(phone) {
|
|
63
|
-
return MaskData.maskPhone(phone, defaultPhoneMaskOptions);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
47
|
static maskPhone(phone, options) {
|
|
67
|
-
let maskedPhone = null;
|
|
68
48
|
if(options) {
|
|
69
49
|
options = MaskHelper.mapWithDefaultValues(options, defaultPhoneMaskOptions);
|
|
70
50
|
MaskHelper.validatePhoneOptions(options);
|
|
51
|
+
} else {
|
|
52
|
+
options = defaultPhoneMaskOptions;
|
|
71
53
|
}
|
|
72
54
|
let maskLength = phone.length - options.unmaskedStartDigits - options.unmaskedEndDigits;
|
|
73
55
|
if((options.unmaskedStartDigits + options.unmaskedEndDigits) >= phone.length) {
|
|
74
|
-
|
|
75
|
-
maskedPhone = phone;
|
|
56
|
+
return phone;
|
|
76
57
|
}
|
|
77
|
-
|
|
78
|
-
maskedPhone = phone.substr(0, options.unmaskedStartDigits) + `${options.maskWith}`.repeat(maskLength) + phone.substr(phone.length-options.unmaskedEndDigits);
|
|
79
|
-
}
|
|
80
|
-
return maskedPhone;
|
|
58
|
+
return phone.substr(0, options.unmaskedStartDigits) + `${options.maskWith}`.repeat(maskLength) + phone.substr(phone.length-options.unmaskedEndDigits);
|
|
81
59
|
}
|
|
82
60
|
|
|
83
|
-
static
|
|
84
|
-
return
|
|
61
|
+
static maskEmail2(email, options) {
|
|
62
|
+
return MaskEmail.maskEmail2(email, options);
|
|
85
63
|
}
|
|
86
64
|
|
|
87
|
-
static
|
|
88
|
-
|
|
89
|
-
if(options) {
|
|
90
|
-
options = MaskHelper.mapWithDefaultValues(options, defaultEmailMaskOptions);
|
|
91
|
-
MaskHelper.validateEmailOptions(email, options);
|
|
92
|
-
}
|
|
93
|
-
const indexOfAt = email.indexOf('@');
|
|
94
|
-
let maskLengthBeforeAtTheRate = indexOfAt - options.unmaskedStartCharacters;
|
|
95
|
-
let maskLengthAfterAtTheRate = email.length - indexOfAt - options.unmaskedEndCharacters;
|
|
96
|
-
|
|
97
|
-
if(maskLengthBeforeAtTheRate > options.maxMaskedCharactersBeforeAtTheRate) {
|
|
98
|
-
maskLengthBeforeAtTheRate = options.maxMaskedCharactersBeforeAtTheRate;
|
|
99
|
-
}
|
|
100
|
-
if(maskLengthAfterAtTheRate > options.maxMaskedCharactersAfterAtTheRate) {
|
|
101
|
-
maskLengthAfterAtTheRate = options.maxMaskedCharactersAfterAtTheRate;
|
|
102
|
-
}
|
|
103
|
-
if((options.unmaskedStartCharacters + options.unmaskedEndCharacters + 1) >= email.length) {
|
|
104
|
-
maskLengthBeforeAtTheRate = 0;
|
|
105
|
-
maskLengthAfterAtTheRate = 0;
|
|
106
|
-
maskedEmail = email;
|
|
107
|
-
}
|
|
108
|
-
if(!maskedEmail) {
|
|
109
|
-
if (maskLengthBeforeAtTheRate < 0) maskLengthBeforeAtTheRate = 0
|
|
110
|
-
maskedEmail = email.substr(0, options.unmaskedStartCharacters) + `${options.maskWith}`.repeat(maskLengthBeforeAtTheRate)
|
|
111
|
-
+ '@' + `${options.maskWith}`.repeat(maskLengthAfterAtTheRate) + email.substr(email.length - options.unmaskedEndCharacters);
|
|
112
|
-
}
|
|
113
|
-
return maskedEmail;
|
|
65
|
+
static maskCard(cardNumber, options) {
|
|
66
|
+
return MaskCard.maskCard(cardNumber, options);
|
|
114
67
|
}
|
|
115
68
|
|
|
116
69
|
static maskJSONFields(obj, options) {
|
|
117
70
|
if(options) {
|
|
118
71
|
options = MaskHelper.mapWithDefaultValues(options, defaultJsonMaskOptions);
|
|
119
72
|
MaskHelper.validateJSONOptions(options);
|
|
73
|
+
} else {
|
|
74
|
+
options = defaultJsonMaskOptions;
|
|
120
75
|
}
|
|
121
76
|
let maskedObj = {};
|
|
122
77
|
try {
|
|
@@ -162,6 +117,8 @@ class MaskData {
|
|
|
162
117
|
if(options) {
|
|
163
118
|
options = MaskHelper.mapWithDefaultValues(options, defaultStringMaskOptions);
|
|
164
119
|
MaskHelper.validateStringOptions(options);
|
|
120
|
+
} else {
|
|
121
|
+
options = defaultStringMaskOptions;
|
|
165
122
|
}
|
|
166
123
|
let values = options.values;
|
|
167
124
|
if(options.maskOnlyFirstOccurance == true) {
|
|
@@ -176,37 +133,6 @@ class MaskData {
|
|
|
176
133
|
return str;
|
|
177
134
|
}
|
|
178
135
|
|
|
179
|
-
static maskCard(card, options) {
|
|
180
|
-
card = card + '';
|
|
181
|
-
card = card.trim();
|
|
182
|
-
if(options) {
|
|
183
|
-
options = MaskHelper.mapWithDefaultValues(options, defaultCardMaskOptions);
|
|
184
|
-
MaskHelper.validateCardMaskOptions(options);
|
|
185
|
-
}
|
|
186
|
-
let maskedCard;
|
|
187
|
-
if((options.unmaskedStartDigits + options.unmaskedEndDigits) >= card.length) {
|
|
188
|
-
return card;
|
|
189
|
-
}
|
|
190
|
-
if(!maskedCard) {
|
|
191
|
-
maskedCard = '';
|
|
192
|
-
for(let i = 0; i < options.unmaskedStartDigits; i++) {
|
|
193
|
-
maskedCard += card[i];
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
for(let i = options.unmaskedStartDigits; i < (card.length-options.unmaskedEndDigits); i++) {
|
|
197
|
-
if(isNaN(parseInt(card[i]))) {
|
|
198
|
-
maskedCard += card[i];
|
|
199
|
-
} else {
|
|
200
|
-
maskedCard += options.maskWith;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
for(let i = (card.length-options.unmaskedEndDigits); i < card.length; i++) {
|
|
204
|
-
maskedCard += card[i];
|
|
205
|
-
}
|
|
206
|
-
return maskedCard;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
136
|
static getInnerProperty(object, field) {
|
|
211
137
|
return get(object, field);
|
|
212
138
|
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const MaskHelper = require('../helpers/MaskHelper');
|
|
2
|
+
|
|
3
|
+
const defaultCardMaskOptions = {
|
|
4
|
+
maskWith: "*",
|
|
5
|
+
unmaskedStartDigits: 4,
|
|
6
|
+
unmaskedEndDigits: 1
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
class CardMask {
|
|
10
|
+
static maskCard(card, options) {
|
|
11
|
+
if(!card) return card;
|
|
12
|
+
let cardToProcess = (card + '').trim();
|
|
13
|
+
if(options) {
|
|
14
|
+
options = MaskHelper.mapWithDefaultValues(options, defaultCardMaskOptions);
|
|
15
|
+
MaskHelper.validateCardMaskOptions(options, cardToProcess.length);
|
|
16
|
+
} else {
|
|
17
|
+
options = defaultCardMaskOptions;
|
|
18
|
+
}
|
|
19
|
+
if(options.unmaskedEndDigits == 0 && options.unmaskedStartDigits == 0) return cardToProcess;
|
|
20
|
+
let countOfNumbers = 0;
|
|
21
|
+
let lastIndex = 0;
|
|
22
|
+
let flag = true;
|
|
23
|
+
for(let i = cardToProcess.length-1; i >= 0; i--) {
|
|
24
|
+
if(!isNaN(cardToProcess[i])) {
|
|
25
|
+
if(flag && countOfNumbers === options.unmaskedEndDigits) {
|
|
26
|
+
lastIndex = i;
|
|
27
|
+
flag = false;
|
|
28
|
+
}
|
|
29
|
+
countOfNumbers++;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if((options.unmaskedStartDigits + options.unmaskedEndDigits) >= countOfNumbers) {
|
|
33
|
+
return cardToProcess;
|
|
34
|
+
}
|
|
35
|
+
let maskedCard = '';
|
|
36
|
+
let i = 0;
|
|
37
|
+
let j = 0;
|
|
38
|
+
while(i < options.unmaskedStartDigits) {
|
|
39
|
+
if(!isNaN(cardToProcess[j])) {
|
|
40
|
+
i++;
|
|
41
|
+
}
|
|
42
|
+
maskedCard += cardToProcess[j++];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
while(j <= lastIndex) {
|
|
46
|
+
if(isNaN(parseInt(cardToProcess[j]))) {
|
|
47
|
+
maskedCard += cardToProcess[j];
|
|
48
|
+
} else {
|
|
49
|
+
maskedCard += options.maskWith;
|
|
50
|
+
}
|
|
51
|
+
j++;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
i = 0;
|
|
55
|
+
j = lastIndex+1;
|
|
56
|
+
while(i < options.unmaskedEndDigits) {
|
|
57
|
+
if(!isNaN(cardToProcess[j])) {
|
|
58
|
+
i++;
|
|
59
|
+
}
|
|
60
|
+
maskedCard += cardToProcess[j++];
|
|
61
|
+
}
|
|
62
|
+
return maskedCard;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
module.exports = CardMask
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const MaskHelper = require('../helpers/MaskHelper');
|
|
2
|
+
|
|
3
|
+
const defaultEmailMask2Options = {
|
|
4
|
+
maskWith: "*",
|
|
5
|
+
unmaskedStartCharactersBeforeAt: 3,
|
|
6
|
+
unmaskedEndCharactersAfterAt: 2,
|
|
7
|
+
maskAtTheRate: false
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
class EmailMask {
|
|
11
|
+
|
|
12
|
+
static maskEmail2(email, options) {
|
|
13
|
+
let maskedEmail = '';
|
|
14
|
+
options = options ? MaskHelper.mapWithDefaultValues(options, defaultEmailMask2Options) : defaultEmailMask2Options;
|
|
15
|
+
MaskHelper.validateEmailOptions2(email, options);
|
|
16
|
+
const indexOfAt = email.indexOf('@');
|
|
17
|
+
|
|
18
|
+
if(indexOfAt < 0) return email;
|
|
19
|
+
|
|
20
|
+
const [before, after] = email.split('@');
|
|
21
|
+
const beforeLength = before.length;
|
|
22
|
+
const afterLength = after.length;
|
|
23
|
+
|
|
24
|
+
maskedEmail += (options.unmaskedStartCharactersBeforeAt >= beforeLength) ? before :
|
|
25
|
+
before.substr(0, options.unmaskedStartCharactersBeforeAt) + `${options.maskWith}`.repeat(beforeLength - options.unmaskedStartCharactersBeforeAt);
|
|
26
|
+
|
|
27
|
+
maskedEmail += options.maskAtTheRate ? options.maskWith : '@';
|
|
28
|
+
|
|
29
|
+
maskedEmail += (options.unmaskedEndCharactersAfterAt >= afterLength) ? after :
|
|
30
|
+
(`${options.maskWith}`.repeat(afterLength-options.unmaskedEndCharactersAfterAt) + after.substr(afterLength - options.unmaskedEndCharactersAfterAt));
|
|
31
|
+
return maskedEmail;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = EmailMask
|
|
@@ -4,7 +4,7 @@ class MaskHelper {
|
|
|
4
4
|
|
|
5
5
|
static mapWithDefaultValues(options, defaultOptions) {
|
|
6
6
|
for(let key of Object.keys(defaultOptions)) {
|
|
7
|
-
if(
|
|
7
|
+
if(options[key] === undefined || options[key] === null) {
|
|
8
8
|
options[key] = defaultOptions[key];
|
|
9
9
|
}
|
|
10
10
|
}
|
|
@@ -99,6 +99,38 @@ class MaskHelper {
|
|
|
99
99
|
badOptions.throwBadOptionException();
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
|
+
|
|
103
|
+
static validateEmailOptions2(email, options) {
|
|
104
|
+
let reasons = [];
|
|
105
|
+
if(!email || email.indexOf('@') < 0) {
|
|
106
|
+
reasons.push(`Email must contain one '@'`);
|
|
107
|
+
}
|
|
108
|
+
try{
|
|
109
|
+
options.unmaskedStartCharactersBeforeAt = parseInt(options.unmaskedStartCharactersBeforeAt);
|
|
110
|
+
options.unmaskedEndCharactersAfterAt = parseInt(options.unmaskedEndCharactersAfterAt);
|
|
111
|
+
if(isNaN(options.unmaskedStartCharactersBeforeAt) || isNaN(options.unmaskedEndCharactersAfterAt)){
|
|
112
|
+
throw Error();
|
|
113
|
+
};
|
|
114
|
+
}catch(err) {
|
|
115
|
+
reasons.push('unmaskedStartCharactersBeforeAt, unmaskedEndCharactersAfterAt must be positive integers');
|
|
116
|
+
}
|
|
117
|
+
if(options.unmaskedStartCharactersBeforeAt < 0) {
|
|
118
|
+
options.unmaskedStartCharactersBeforeAt = 0;
|
|
119
|
+
}
|
|
120
|
+
if(options.unmaskedEndCharactersAfterAt < 0) {
|
|
121
|
+
options.unmaskedEndCharactersAfterAt = 0;
|
|
122
|
+
}
|
|
123
|
+
if((!options.maskWith) || options.maskWith.toString().length <= 0) {
|
|
124
|
+
options.maskWith = '*';
|
|
125
|
+
}
|
|
126
|
+
if(typeof(options.maskAtTheRate) !== 'boolean') {
|
|
127
|
+
reasons.push('maskAtTheRate should be a boolean field');
|
|
128
|
+
}
|
|
129
|
+
if(reasons.length > 0) {
|
|
130
|
+
const badOptions = new BadOption('Email mask configuration', reasons);
|
|
131
|
+
badOptions.throwBadOptionException();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
102
134
|
|
|
103
135
|
static validateJSONOptions(options) {
|
|
104
136
|
let reasons = [];
|
|
@@ -131,7 +163,7 @@ class MaskHelper {
|
|
|
131
163
|
}
|
|
132
164
|
}
|
|
133
165
|
|
|
134
|
-
static validateCardMaskOptions(options) {
|
|
166
|
+
static validateCardMaskOptions(options, cardLength) {
|
|
135
167
|
let reasons = [];
|
|
136
168
|
try{
|
|
137
169
|
options.unmaskedStartDigits = parseInt(options.unmaskedStartDigits);
|
|
@@ -144,11 +176,13 @@ class MaskHelper {
|
|
|
144
176
|
}
|
|
145
177
|
if(options.unmaskedStartDigits < 0) {
|
|
146
178
|
options.unmaskedStartDigits = 0;
|
|
179
|
+
} else if(options.unmaskedStartDigits > cardLength) {
|
|
180
|
+
options.unmaskedStartDigits = cardLength;
|
|
147
181
|
}
|
|
148
182
|
if(options.unmaskedEndDigits < 0) {
|
|
149
183
|
options.unmaskedEndDigits = 0;
|
|
150
|
-
} else if(options.unmaskedEndDigits >
|
|
151
|
-
options.unmaskedEndDigits =
|
|
184
|
+
} else if(options.unmaskedEndDigits > cardLength) {
|
|
185
|
+
options.unmaskedEndDigits = cardLength;
|
|
152
186
|
}
|
|
153
187
|
if((!options.maskWith) || options.maskWith.toString().length <= 0) {
|
|
154
188
|
options.maskWith = '*';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "maskdata",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "Node module to mask various kinds of data in required formats",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
@@ -22,7 +22,6 @@
|
|
|
22
22
|
"mask card",
|
|
23
23
|
"mask json",
|
|
24
24
|
"mask string",
|
|
25
|
-
"mask json",
|
|
26
25
|
"mask object",
|
|
27
26
|
"Get json fields",
|
|
28
27
|
"Set json fields",
|
package/test.js
CHANGED
|
@@ -1,50 +1,54 @@
|
|
|
1
1
|
const MaskData = require('./index');
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const passwordMaskOptions = {
|
|
4
4
|
maxMaskedCharacters: 10,
|
|
5
5
|
maskWith: ""
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
const password = "Password1$";
|
|
9
9
|
console.log(`Unmasked Password: ${password}`);
|
|
10
|
-
console.log(`Password after masking: ${MaskData.maskPassword(password,
|
|
10
|
+
console.log(`Password after masking: ${MaskData.maskPassword(password, passwordMaskOptions)}`);
|
|
11
|
+
console.log("========================================");
|
|
12
|
+
console.log(`Password after masking with default options: ${MaskData.maskPassword(password)}`);
|
|
11
13
|
console.log("========================================");
|
|
12
14
|
|
|
13
15
|
|
|
14
|
-
const
|
|
16
|
+
const phoneMaskOptions = {
|
|
15
17
|
maskWith: "*",
|
|
16
|
-
unmaskedStartDigits:
|
|
18
|
+
unmaskedStartDigits: 5,
|
|
17
19
|
unmaskedEndDigits: 1
|
|
18
20
|
};
|
|
19
21
|
|
|
20
22
|
const phone = "+91123456789";
|
|
21
23
|
|
|
22
24
|
console.log(`Unmasked phone: ${phone}`);
|
|
23
|
-
console.log(`phone after masking: ${MaskData.maskPhone(phone,
|
|
25
|
+
console.log(`phone after masking: ${MaskData.maskPhone(phone, phoneMaskOptions)}`);
|
|
26
|
+
console.log("========================================");
|
|
27
|
+
console.log(`phone after masking with default options: ${MaskData.maskPhone(phone)}`);
|
|
24
28
|
console.log("========================================");
|
|
25
29
|
|
|
26
|
-
const
|
|
30
|
+
const emailMask2Options = {
|
|
27
31
|
maskWith: "*",
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
maskAtTheRate: false
|
|
31
|
-
maxMaskedCharactersBeforeAtTheRate: 10,
|
|
32
|
-
maxMaskedCharactersAfterAtTheRate: 10,
|
|
32
|
+
unmaskedStartCharactersBeforeAt: 0,
|
|
33
|
+
unmaskedEndCharactersAfterAt: 99,
|
|
34
|
+
maskAtTheRate: false
|
|
33
35
|
};
|
|
34
36
|
|
|
35
37
|
console.log("========================================");
|
|
36
38
|
const email = "my.testEmail@testMail.com";
|
|
37
39
|
console.log(`Unmasked email: ${email}`);
|
|
38
|
-
console.log(`Email after masking: ${MaskData.
|
|
40
|
+
console.log(`Email after masking: ${MaskData.maskEmail2(email, emailMask2Options)}`);
|
|
41
|
+
console.log("========================================");
|
|
42
|
+
console.log(`Email after masking with default options: ${MaskData.maskEmail2(email)}`);
|
|
39
43
|
console.log("========================================");
|
|
40
44
|
|
|
41
45
|
console.log("========================================");
|
|
42
|
-
const shortEmail = "
|
|
46
|
+
const shortEmail = "a@b.c";
|
|
43
47
|
console.log(`Unmasked shortEmail: ${shortEmail}`);
|
|
44
|
-
console.log(`shortEmail after masking: ${MaskData.
|
|
48
|
+
console.log(`shortEmail after masking: ${MaskData.maskEmail2(shortEmail, emailMask2Options)}`);
|
|
45
49
|
console.log("========================================");
|
|
46
50
|
|
|
47
|
-
const
|
|
51
|
+
const jsonMaskOptions = {
|
|
48
52
|
fields: ['password', 'firstName']
|
|
49
53
|
};
|
|
50
54
|
|
|
@@ -57,16 +61,16 @@ const json = {
|
|
|
57
61
|
console.log(`Unmasked object: `);
|
|
58
62
|
console.log(JSON.stringify(json));
|
|
59
63
|
console.log(`Object after masking:`);
|
|
60
|
-
console.log(JSON.stringify(MaskData.maskJSONFields(json,
|
|
64
|
+
console.log(JSON.stringify(MaskData.maskJSONFields(json, jsonMaskOptions)));
|
|
61
65
|
console.log("========================================");
|
|
62
66
|
console.log(JSON.stringify(json));
|
|
63
67
|
console.log("========================================");
|
|
64
68
|
|
|
65
|
-
const
|
|
69
|
+
const jsonMaskOptions2 = {
|
|
66
70
|
fields : ['level1.level2.level3.field3', 'level1.level2.field2', 'level1.field1', 'value1', 'level1.level2.level3.field4[0].Hello', 'level1.level2.level3.field4[2]']
|
|
67
71
|
};
|
|
68
72
|
|
|
69
|
-
const
|
|
73
|
+
const maskAllFieldsOptions = {
|
|
70
74
|
fields : ['level1.level2.field3[*].Hello', 'level1.level2.level3.*']
|
|
71
75
|
};
|
|
72
76
|
|
|
@@ -88,7 +92,7 @@ const nestedAllObject = {
|
|
|
88
92
|
console.log(`Unmasked nested object: `);
|
|
89
93
|
console.log(JSON.stringify(nestedAllObject));
|
|
90
94
|
console.log(`Nested Object after masking all:`);
|
|
91
|
-
console.log(JSON.stringify(MaskData.maskJSONFields(nestedAllObject,
|
|
95
|
+
console.log(JSON.stringify(MaskData.maskJSONFields(nestedAllObject, maskAllFieldsOptions)));
|
|
92
96
|
console.log("========================================");
|
|
93
97
|
|
|
94
98
|
const nestedObject = {
|
|
@@ -108,35 +112,35 @@ const nestedObject = {
|
|
|
108
112
|
console.log(`Unmasked nested object: `);
|
|
109
113
|
console.log(JSON.stringify(nestedObject));
|
|
110
114
|
console.log(`Nested Object after masking:`);
|
|
111
|
-
console.log(JSON.stringify(MaskData.maskJSONFields(nestedObject,
|
|
115
|
+
console.log(JSON.stringify(MaskData.maskJSONFields(nestedObject, jsonMaskOptions2)));
|
|
112
116
|
console.log("========================================");
|
|
113
117
|
|
|
114
118
|
|
|
115
|
-
const
|
|
119
|
+
const stringMaskOptions = {
|
|
116
120
|
maskWith: "*",
|
|
117
121
|
values: ['is', 'API']
|
|
118
122
|
};
|
|
119
123
|
|
|
120
124
|
let str = "This is a testingAPI String";
|
|
121
125
|
console.log(`Unmasked string: ${str}`);
|
|
122
|
-
console.log(`String after masking: ${MaskData.maskString(str,
|
|
126
|
+
console.log(`String after masking: ${MaskData.maskString(str, stringMaskOptions)}`);
|
|
123
127
|
console.log("========================================");
|
|
124
128
|
|
|
125
129
|
str = "This is a testingAPI String";
|
|
126
|
-
|
|
130
|
+
stringMaskOptions.maskOnlyFirstOccurance = true;
|
|
127
131
|
console.log(`Unmasked string: ${str}`);
|
|
128
|
-
console.log(`String after masking only first occurances: ${MaskData.maskString(str,
|
|
132
|
+
console.log(`String after masking only first occurances: ${MaskData.maskString(str, stringMaskOptions)}`);
|
|
129
133
|
console.log("========================================");
|
|
130
134
|
|
|
131
|
-
const
|
|
135
|
+
const cardMaskOptions = {
|
|
132
136
|
maskWith: "X",
|
|
133
137
|
unmaskedStartDigits: 4,
|
|
134
|
-
unmaskedEndDigits:
|
|
138
|
+
unmaskedEndDigits: 5
|
|
135
139
|
};
|
|
136
140
|
|
|
137
141
|
let cardNumber = "1234-5678-1234-5678";
|
|
138
142
|
console.log(`Unmasked cardNumber: ${cardNumber}`);
|
|
139
|
-
console.log(`cardNumber after masking: ${MaskData.maskCard(cardNumber,
|
|
143
|
+
console.log(`cardNumber after masking: ${MaskData.maskCard(cardNumber, cardMaskOptions)}`);
|
|
140
144
|
console.log("========================================");
|
|
141
145
|
|
|
142
146
|
const nestedJson = {
|
|
@@ -170,4 +174,14 @@ let afterReplacing = MaskData.replaceValue(input, 'age', 99);
|
|
|
170
174
|
afterReplacing = MaskData.replaceValue(input, 'married', false);
|
|
171
175
|
console.log("After replacing: " + JSON.stringify(afterReplacing));
|
|
172
176
|
console.log("Type of age: "+ typeof(afterReplacing.age));
|
|
173
|
-
console.log("Type of married: "+ typeof(afterReplacing.married));
|
|
177
|
+
console.log("Type of married: "+ typeof(afterReplacing.married));
|
|
178
|
+
|
|
179
|
+
const maskCardOptions = {
|
|
180
|
+
maskWith: "*",
|
|
181
|
+
unmaskedStartDigits: 4,
|
|
182
|
+
unmaskedEndDigits: 4
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const obj = { cardNumber: "123456789123" ,abc: "abceew" ,efg: "121212" } ;
|
|
186
|
+
const result = MaskData.replaceValue(obj, 'cardNumber', MaskData.maskCard(obj['cardNumber'], maskCardOptions));
|
|
187
|
+
console.log(result);
|