maskdata 1.1.1 → 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 CHANGED
@@ -1,4 +1,5 @@
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)
@@ -42,12 +43,14 @@ Removed the below deprecated methods
42
43
 
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
44
45
 
45
- All the above deprecated methods will be removed in the next version
46
+ ### Version: 1.1.2
47
+ - Bug fix in maskCard function. Issue: https://github.com/Sumukha1496/maskdata/issues/10
46
48
 
47
49
  # How to Use
48
50
  ```javascript
49
51
  const MaskData = require('./maskdata');
50
52
  ```
53
+
51
54
  ## Mask Phone Number
52
55
  ```javascript
53
56
  const MaskData = require('./maskdata');
@@ -93,6 +96,7 @@ const maskedPhoneNumber = MaskData.maskPhone(phoneNumber);
93
96
 
94
97
  ```
95
98
 
99
+
96
100
  ## Mask Password
97
101
  ```javascript
98
102
  const MaskData = require('./maskdata');
@@ -131,6 +135,7 @@ const maskedPassword = MaskData.maskPassword(password)
131
135
 
132
136
  ```
133
137
 
138
+
134
139
  ## Mask Email id
135
140
  Use this method instead of maskEmail(). To mask with the default options, dont pass the configurations.
136
141
  ```javascript
@@ -198,6 +203,7 @@ const maskedEmail = MaskData.maskEmail2(email);
198
203
 
199
204
  ```
200
205
 
206
+
201
207
  ## Get Nested Json Property
202
208
  This method returns the value of nested Json property if exists. Otherwise it returns ```undefined```
203
209
  ```javascript
@@ -225,6 +231,7 @@ const innerPropety = Maskdata.getInnerProperty(nestedObject, 'level1.level2.leve
225
231
 
226
232
  ```
227
233
 
234
+
228
235
  ## Replace the value of a json field
229
236
  To replace a value by keeping the type.
230
237
  ```javascript
@@ -235,24 +242,20 @@ const input = {
235
242
  married: true
236
243
  }
237
244
 
238
- console.log("Before replacing: " + JSON.stringify(input));
239
- console.log("========================================");
240
245
  let afterReplacing = MaskData.replaceValue(input, 'age', 99);
241
246
  afterReplacing = MaskData.replaceValue(input, 'married', false);
242
- console.log("After replacing: " + JSON.stringify(afterReplacing));
243
- console.log("Type of age: "+ typeof(afterReplacing.age));
244
- console.log("Type of married: "+ typeof(afterReplacing.married));
245
247
 
246
248
  //Output:
247
249
 
248
250
  Before replacing: {"name":"John","age":33,"married":true}
249
- ========================================
250
251
  After replacing: {"name":"John","age":99,"married":false}
252
+
251
253
  Type of age: number
252
254
  Type of married: boolean
253
255
 
254
256
  ```
255
257
 
258
+
256
259
  ## Mask fields in a JSON
257
260
  This method masks the field value if present in the given object
258
261
  ```javascript
@@ -277,6 +280,8 @@ const maskedObj = MaskData.maskJSONFields(obj, maskJSONOptions);
277
280
  //Output: { password: '************', firstName: '***', lastName: 'Snoww' }
278
281
 
279
282
  ```
283
+
284
+
280
285
  ## Mask fields of a nested Object
281
286
  This method masks the field value if present in the given object
282
287
 
@@ -357,6 +362,7 @@ const maskedObject = MaskData.maskJSONFields(nestedObject, maskAllFields);
357
362
 
358
363
  ```
359
364
 
365
+
360
366
  ## Mask the exact substring from throughout the string
361
367
  This will mask the field value if present in the given object
362
368
  ```javascript
@@ -383,8 +389,9 @@ const strAfterMasking = MaskData.maskString(str, maskStringOptions);
383
389
 
384
390
  ```
385
391
 
392
+
386
393
  ## Mask card number
387
- 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
388
395
  ```javascript
389
396
  const MaskData = require('./maskdata');
390
397
 
@@ -393,14 +400,13 @@ const maskCardOptions = {
393
400
  maskWith: "X",
394
401
 
395
402
  // Should be positive Integer
396
- // If the starting 'n' numbers needs to be unmasked
403
+ // If the starting 'n' digits needs to be unmasked
397
404
  // Default value is 4
398
405
  unmaskedStartDigits: 4,
399
406
 
400
407
  //Should be positive Integer
401
- //If the ending 'n' numbers needs to be unmasked
408
+ //If the ending 'n' digits needs to be unmasked
402
409
  // Default value is 1.
403
- // Max possible value is 4
404
410
  unmaskedEndDigits: 1
405
411
  };
406
412
 
@@ -412,6 +418,7 @@ const cardAfterMasking = MaskData.maskCard(cardNumber, maskCardOptions);
412
418
 
413
419
  ```
414
420
 
421
+
415
422
  # Report Bugs
416
423
  Please raise an issue in github: https://github.com/Sumukha1496/maskdata/issues
417
424
 
package/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  const MaskHelper = require('./lib/helpers/MaskHelper');
4
4
  const MaskEmail = require('./lib/emailMask/EmailMask');
5
+ const MaskCard = require('./lib/cardMask/CardMask');
5
6
  const get = require('lodash.get');
6
7
  const set = require('lodash.set');
7
8
 
@@ -27,12 +28,6 @@ const defaultStringMaskOptions = {
27
28
  values: []
28
29
  };
29
30
 
30
- const defaultCardMaskOptions = {
31
- maskWith: "*",
32
- unmaskedStartDigits: 4,
33
- unmaskedEndDigits: 1
34
- };
35
-
36
31
  class MaskData {
37
32
 
38
33
  static maskPassword(password, options) {
@@ -50,7 +45,6 @@ class MaskData {
50
45
  }
51
46
 
52
47
  static maskPhone(phone, options) {
53
- let maskedPhone = null;
54
48
  if(options) {
55
49
  options = MaskHelper.mapWithDefaultValues(options, defaultPhoneMaskOptions);
56
50
  MaskHelper.validatePhoneOptions(options);
@@ -59,19 +53,19 @@ class MaskData {
59
53
  }
60
54
  let maskLength = phone.length - options.unmaskedStartDigits - options.unmaskedEndDigits;
61
55
  if((options.unmaskedStartDigits + options.unmaskedEndDigits) >= phone.length) {
62
- maskLength = 0;
63
- maskedPhone = phone;
64
- }
65
- if(!maskedPhone) {
66
- maskedPhone = phone.substr(0, options.unmaskedStartDigits) + `${options.maskWith}`.repeat(maskLength) + phone.substr(phone.length-options.unmaskedEndDigits);
56
+ return phone;
67
57
  }
68
- return maskedPhone;
58
+ return phone.substr(0, options.unmaskedStartDigits) + `${options.maskWith}`.repeat(maskLength) + phone.substr(phone.length-options.unmaskedEndDigits);
69
59
  }
70
60
 
71
61
  static maskEmail2(email, options) {
72
62
  return MaskEmail.maskEmail2(email, options);
73
63
  }
74
64
 
65
+ static maskCard(cardNumber, options) {
66
+ return MaskCard.maskCard(cardNumber, options);
67
+ }
68
+
75
69
  static maskJSONFields(obj, options) {
76
70
  if(options) {
77
71
  options = MaskHelper.mapWithDefaultValues(options, defaultJsonMaskOptions);
@@ -139,39 +133,6 @@ class MaskData {
139
133
  return str;
140
134
  }
141
135
 
142
- static maskCard(card, options) {
143
- card = card + '';
144
- card = card.trim();
145
- if(options) {
146
- options = MaskHelper.mapWithDefaultValues(options, defaultCardMaskOptions);
147
- MaskHelper.validateCardMaskOptions(options);
148
- } else {
149
- options = defaultCardMaskOptions;
150
- }
151
- let maskedCard;
152
- if((options.unmaskedStartDigits + options.unmaskedEndDigits) >= card.length) {
153
- return card;
154
- }
155
- if(!maskedCard) {
156
- maskedCard = '';
157
- for(let i = 0; i < options.unmaskedStartDigits; i++) {
158
- maskedCard += card[i];
159
- }
160
-
161
- for(let i = options.unmaskedStartDigits; i < (card.length-options.unmaskedEndDigits); i++) {
162
- if(isNaN(parseInt(card[i]))) {
163
- maskedCard += card[i];
164
- } else {
165
- maskedCard += options.maskWith;
166
- }
167
- }
168
- for(let i = (card.length-options.unmaskedEndDigits); i < card.length; i++) {
169
- maskedCard += card[i];
170
- }
171
- return maskedCard;
172
- }
173
- }
174
-
175
136
  static getInnerProperty(object, field) {
176
137
  return get(object, field);
177
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
@@ -163,7 +163,7 @@ class MaskHelper {
163
163
  }
164
164
  }
165
165
 
166
- static validateCardMaskOptions(options) {
166
+ static validateCardMaskOptions(options, cardLength) {
167
167
  let reasons = [];
168
168
  try{
169
169
  options.unmaskedStartDigits = parseInt(options.unmaskedStartDigits);
@@ -176,11 +176,13 @@ class MaskHelper {
176
176
  }
177
177
  if(options.unmaskedStartDigits < 0) {
178
178
  options.unmaskedStartDigits = 0;
179
+ } else if(options.unmaskedStartDigits > cardLength) {
180
+ options.unmaskedStartDigits = cardLength;
179
181
  }
180
182
  if(options.unmaskedEndDigits < 0) {
181
183
  options.unmaskedEndDigits = 0;
182
- } else if(options.unmaskedEndDigits > 4) {
183
- options.unmaskedEndDigits = 4;
184
+ } else if(options.unmaskedEndDigits > cardLength) {
185
+ options.unmaskedEndDigits = cardLength;
184
186
  }
185
187
  if((!options.maskWith) || options.maskWith.toString().length <= 0) {
186
188
  options.maskWith = '*';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maskdata",
3
- "version": "1.1.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": {
package/test.js CHANGED
@@ -135,7 +135,7 @@ console.log("========================================");
135
135
  const cardMaskOptions = {
136
136
  maskWith: "X",
137
137
  unmaskedStartDigits: 4,
138
- unmaskedEndDigits: 1
138
+ unmaskedEndDigits: 5
139
139
  };
140
140
 
141
141
  let cardNumber = "1234-5678-1234-5678";
@@ -174,4 +174,14 @@ let afterReplacing = MaskData.replaceValue(input, 'age', 99);
174
174
  afterReplacing = MaskData.replaceValue(input, 'married', false);
175
175
  console.log("After replacing: " + JSON.stringify(afterReplacing));
176
176
  console.log("Type of age: "+ typeof(afterReplacing.age));
177
- 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);