maskdata 1.1.2 → 1.1.6

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
@@ -4,7 +4,7 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
4
4
  # Table of Contents
5
5
  - [Features](#features)
6
6
  - [Install maskdata](#install-maskdata)
7
- - [Version 1.1.1 Features](#release-features)
7
+ - [Version 1.1.6 Features](#release-features)
8
8
  - [How to Use](#how-to-use)
9
9
  - [Mask Phone Number](#mask-phone-number)
10
10
  - [Mask Phone Number with the default configuration](#mask-phone-number-with-the-default-configuration)
@@ -16,9 +16,10 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
16
16
  - [Replace the value of a json field](#replace-the-value-of-a-json-field)
17
17
  - [Mask JSON fields](#mask-fields-in-a-json)
18
18
  - [Mask nested JSON fields](#mask-fields-of-a-nested-object)
19
- - [Mask a value from the string](#mask-the-exact-substring-from-throughout-the-string)
19
+ - [Mask words/characters in a string](#mask-the-characters-or-words-in-the-string)
20
20
  - [Mask Card number](#mask-card-number)
21
21
  - [Report Bugs](#report-bugs)
22
+ - [Give a Star](#give-a-star)
22
23
  - [LICENSE - "MIT"](#license---mit)
23
24
 
24
25
  # Features
@@ -26,25 +27,47 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
26
27
  * Mask Phone numbers
27
28
  * Mask Email
28
29
  * Mask desired fields in a JSON
29
- * Mask the given substring from throughout a String
30
+ * Mask the given words/substrings from throughout a String
30
31
  * Mask the card number
32
+ * Get nested field from JSON
33
+ * Set/Replace nested field values from JSON
31
34
 
32
35
  # Install maskdata
33
36
  > npm i maskdata
34
37
 
35
38
  # 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
+ ### Version: 1.1.6
40
+ - Feature to mask all the characters in the String along with mask/not mask spaces in the string.
41
+ - [Mask all characters in the String](#mask-all-characters-in-the-string)
42
+ ### Version: 1.1.4
43
+ - Password masking bug fix: https://github.com/Sumukha1496/maskdata/issues/12
39
44
 
40
- - **simplePhoneMask()** method -> To mask with default configs, simply dont pass the config argument to **maskPhone()** method.
45
+ ### Version: 1.1.3
46
+ - Unmasked start and end characters feature in password masking. It can be used in cases where the password/Secret key has some metadata info which needs to be printed or shown to users.
47
+ Ticket: https://github.com/Sumukha1496/maskdata/issues/11. Both the fields are opional and can find more info here: [Mask Password](#mask-password)
41
48
 
42
- - **simpleEmailMask()** method -> To mask with default configs, simply dont pass the config argument to **maskEmail()** method.
49
+ #### Example: Mask password OR secretKey with some meta info at the end
43
50
 
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
51
+ ```javascript
52
+ const MaskData = require('./maskdata');
53
+
54
+ const maskPasswordOptions = {
55
+ maskWith: "X",
56
+ maxMaskedCharacters: 20, // To limit the output String length to 20.
57
+ unmaskedStartCharacters: 4,
58
+ unmaskedEndCharacters: 9 // As last 9 characters of the secret key is a meta info which can be printed for debugging or other purpose
59
+ };
45
60
 
46
- ### Version: 1.1.2
47
- - Bug fix in maskCard function. Issue: https://github.com/Sumukha1496/maskdata/issues/10
61
+ const password = "TEST:U2VjcmV0S2V5MQ==:CLIENT-A";
62
+
63
+ const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
64
+ //Output: TESTXXXXXXX:CLIENT-A
65
+
66
+ maskPasswordOptions.unmaskedStartCharacters = 0;
67
+
68
+ const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
69
+ //Output: XXXXXXXXXXX:CLIENT-A
70
+ ```
48
71
 
49
72
  # How to Use
50
73
  ```javascript
@@ -108,7 +131,15 @@ const maskPasswordOptions = {
108
131
 
109
132
  // To limit the *s in the response when the password length is more
110
133
  // Default value is 16
111
- maxMaskedCharacters: 16
134
+ maxMaskedCharacters: 16,
135
+
136
+ // To show(not mask) first 'n' characters in the password/secret key.
137
+ // Default value is 0.
138
+ unmaskedStartCharacters: 0,
139
+
140
+ // To show(not mask) last 'n' characters in the password/secret key.
141
+ // Default value is 0.
142
+ unmaskedEndCharacters: 0
112
143
  };
113
144
 
114
145
  const password = "Password1$";
@@ -117,8 +148,6 @@ const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
117
148
 
118
149
  //Output: **********
119
150
 
120
- ```
121
-
122
151
  ## Mask Password with the default configuration
123
152
  To mask with the default options, dont pass the configurations.
124
153
  ```javascript
@@ -126,7 +155,9 @@ const MaskData = require('./maskdata');
126
155
 
127
156
  /** Default Options
128
157
  maskWith: "*"
129
- maxMaskedCharacters: 16
158
+ maxMaskedCharacters: 16,
159
+ unmaskedStartCharacters: 0,
160
+ unmaskedEndCharacters: 0
130
161
  **/
131
162
 
132
163
  const password = "Password1$";
@@ -321,7 +352,27 @@ const nestedObject = {
321
352
  const maskedObj = MaskData.maskJSONFields(nestedObject, defaultJSONMaskOptions2);
322
353
 
323
354
  //Output:
324
- {"level1":{"field1":"******","level2":{"field2":"******","level3":{"field3":"******","field4":[{"Hello":null},{"Hello":"Newworld"},"*************"]}}},"value1":"*****"}
355
+ {
356
+ "level1": {
357
+ "field1": "******",
358
+ "level2": {
359
+ "field2": "******",
360
+ "level3": {
361
+ "field3": "******",
362
+ "field4": [
363
+ {
364
+ "Hello": null
365
+ },
366
+ {
367
+ "Hello": "Newworld"
368
+ },
369
+ "*************"
370
+ ]
371
+ }
372
+ }
373
+ },
374
+ "value1": "*****"
375
+ }
325
376
  ```
326
377
 
327
378
 
@@ -357,14 +408,31 @@ const maskAllFields = {
357
408
  const maskedObject = MaskData.maskJSONFields(nestedObject, maskAllFields);
358
409
 
359
410
  // Output:
360
- {"level1":{"field1":"field1Value","level2":{"field2":"field2Value","field3":[{"Hello":"*****","Hi":"one"},{"Hello":"***********"}],"level3":{"field4":"***********","field5":"***********"}}},"value1":"value"}
361
-
362
-
411
+ {
412
+ "level1": {
413
+ "field1": "field1Value",
414
+ "level2": {
415
+ "field2": "field2Value",
416
+ "field3": [
417
+ {
418
+ "Hello": "*****",
419
+ "Hi": "one"
420
+ },
421
+ {
422
+ "Hello": "***********"
423
+ }
424
+ ],
425
+ "level3": {
426
+ "field4": "***********",
427
+ "field5": "***********"
428
+ }
429
+ }
430
+ },
431
+ "value1": "value"
432
+ }
363
433
  ```
364
-
365
-
366
- ## Mask the exact substring from throughout the string
367
- This will mask the field value if present in the given object
434
+ ## Mask the characters or words in the string
435
+ This will mask the characters or words value if present in the given string.
368
436
  ```javascript
369
437
  const MaskData = require('./maskdata');
370
438
 
@@ -372,13 +440,36 @@ const maskStringOptions = {
372
440
  // Character to mask the data. Default value is '*'
373
441
  maskWith: "*",
374
442
 
375
- // It should be an array of strings
376
- // Field names to mask. Can give multiple fields.
443
+ /**
444
+ * It is the words/substrings to mask.
445
+ * Should be an array of strings.
446
+ * Can give multiple words/substrings.
447
+ * values[] can be used only when maskAll is false. If maskAll is true, then this is of no use.
448
+ */
377
449
  values: ['is', 'test'],
378
450
 
379
- // Should be boolean
380
- // If to mask only the first occurance of each value in the given string
381
- maskOnlyFirstOccurance: false
451
+ /**
452
+ * If to mask only the first occurance of each word/substring in the given string
453
+ * Should be boolean
454
+ * Default value is false
455
+ */
456
+ maskOnlyFirstOccurance: false,
457
+
458
+ /**
459
+ * If to mask all the characters in a string make maskAll: true
460
+ * If maskAll is true, the words/substrings inside values[] will not be considered.
461
+ * Default value is false
462
+ */
463
+ maskAll: false,
464
+
465
+ /**
466
+ * This is to mask/not mask the spaces in a string when masking all the characters.
467
+ * Can be used ONLY when maskAll: true
468
+ * If maskSpace is true, the spaces in the string will be masked.
469
+ * This feature is to know the words and each word length but to hide the content
470
+ * Default value is true
471
+ */
472
+ maskSpace: true
382
473
  };
383
474
 
384
475
  const str = "This is a test String";
@@ -389,6 +480,36 @@ const strAfterMasking = MaskData.maskString(str, maskStringOptions);
389
480
 
390
481
  ```
391
482
 
483
+ ### Mask all characters in the String
484
+ ```
485
+ const str = "This is a test String";
486
+
487
+ const strAfterMasking = MaskData.maskString(str, maskStringOptions);
488
+
489
+ const stringMaskOptions = {
490
+ maskWith: "*",
491
+ values: [],
492
+ maskAll: true,
493
+ maskSpace: false // Do not mask space
494
+ };
495
+
496
+ // Output: **** ** * **** *****
497
+ -------------------------------------------------------------------
498
+
499
+ const str = "This is a test String";
500
+
501
+ const strAfterMasking = MaskData.maskString(str, maskStringOptions);
502
+
503
+ const stringMaskOptions = {
504
+ maskWith: "*",
505
+ values: [],
506
+ maskAll: true,
507
+ maskSpace: false // Mask spaces also
508
+ };
509
+
510
+ // Output: ********************
511
+
512
+ ```
392
513
 
393
514
  ## Mask card number
394
515
  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
@@ -418,11 +539,10 @@ const cardAfterMasking = MaskData.maskCard(cardNumber, maskCardOptions);
418
539
 
419
540
  ```
420
541
 
421
-
422
542
  # Report Bugs
423
543
  Please raise an issue in github: https://github.com/Sumukha1496/maskdata/issues
424
544
 
425
- # Give a Star:
545
+ # Give a Star
426
546
  You can give a start at: https://github.com/Sumukha1496/maskdata/stargazers
427
547
 
428
548
  # LICENSE - "MIT"
package/index.js CHANGED
@@ -19,18 +19,25 @@ const defaultJsonMaskOptions = {
19
19
 
20
20
  const defaultPasswordMaskOptions = {
21
21
  maskWith: "*",
22
- maxMaskedCharacters: 16
22
+ maxMaskedCharacters: 16,
23
+ unmaskedStartCharacters: 0,
24
+ unmaskedEndCharacters: 0
23
25
  };
24
26
 
25
27
  const defaultStringMaskOptions = {
26
28
  maskWith: "*",
27
29
  maskOnlyFirstOccurance: false,
28
- values: []
30
+ values: [],
31
+ maskAll: false,
32
+ maskSpace: true
29
33
  };
30
34
 
31
35
  class MaskData {
32
36
 
33
37
  static maskPassword(password, options) {
38
+ if(!password) {
39
+ return password;
40
+ }
34
41
  if(options) {
35
42
  options = MaskHelper.mapWithDefaultValues(options, defaultPasswordMaskOptions);
36
43
  MaskHelper.validatePasswordOptions(options);
@@ -41,7 +48,27 @@ class MaskData {
41
48
  if(password.length > options.maxMaskedCharacters) {
42
49
  maskPasswordLength = parseInt(options.maxMaskedCharacters);
43
50
  }
44
- return `${options.maskWith}`.repeat(maskPasswordLength);
51
+ const maskingCharacters = maskPasswordLength - options.unmaskedStartCharacters - options.unmaskedEndCharacters;
52
+
53
+ if(maskingCharacters < 0) {
54
+ if(maskPasswordLength <= options.unmaskedStartCharacters) {
55
+ return password.substr(0, maskPasswordLength);
56
+ } else {
57
+ let maskedPassword = password.substr(0, options.unmaskedStartCharacters);
58
+ const remainingChars = maskPasswordLength - options.unmaskedStartCharacters;
59
+ for(let i = password.length-remainingChars; i < password.length; i++) {
60
+ maskedPassword += password[i];
61
+ }
62
+ return maskedPassword;
63
+ }
64
+ }
65
+ let maskedPassword = "";
66
+ maskedPassword = password.substr(0, options.unmaskedStartCharacters);
67
+ maskedPassword += `${options.maskWith}`.repeat(maskingCharacters)
68
+ for(let i = password.length-options.unmaskedEndCharacters; i < password.length; i++) {
69
+ maskedPassword += password[i];
70
+ }
71
+ return maskedPassword;
45
72
  }
46
73
 
47
74
  static maskPhone(phone, options) {
@@ -121,7 +148,22 @@ class MaskData {
121
148
  options = defaultStringMaskOptions;
122
149
  }
123
150
  let values = options.values;
124
- if(options.maskOnlyFirstOccurance == true) {
151
+ if(options.maskAll === true) {
152
+ let result = '';
153
+ if(options.maskSpace === true) {
154
+ result = options.maskWith.repeat(str.length);
155
+ } else {
156
+ for(let eachChar of str) {
157
+ if(eachChar === ' ') {
158
+ result += ' ';
159
+ } else {
160
+ result += options.maskWith;
161
+ }
162
+ }
163
+ }
164
+ return result;
165
+ }
166
+ else if(options.maskOnlyFirstOccurance == true) {
125
167
  for(const value of Object.values(values)) {
126
168
  str = str.replace(value, `${options.maskWith}`.repeat(value.length));
127
169
  }
@@ -15,14 +15,23 @@ class MaskHelper {
15
15
  let reasons = [];
16
16
  try{
17
17
  options.maxMaskedCharacters = parseInt(options.maxMaskedCharacters);
18
- if(isNaN(options.maxMaskedCharacters)){
18
+ options.unmaskedStartCharacters = parseInt(options.unmaskedStartCharacters);
19
+ options.unmaskedEndCharacters = parseInt(options.unmaskedEndCharacters);
20
+ if(isNaN(options.maxMaskedCharacters) || isNaN(options.unmaskedStartCharacters) ||
21
+ isNaN(options.unmaskedEndCharacters)) {
19
22
  throw Error();
20
23
  };
21
- }catch(err) {
22
- reasons.push('maxMaskedCharacters must be positive integer');
24
+ } catch(err) {
25
+ reasons.push('maxMaskedCharacters, unmaskedStartCharacters and unmaskedEndCharacters must be positive integer');
23
26
  }
24
27
  if(options.maxMaskedCharacters < 0) {
25
- options.maxMaskedCharacters = 0;
28
+ options.maxMaskedCharacters = 16;
29
+ }
30
+ if(options.unmaskedStartCharacters < 0) {
31
+ options.unmaskedStartCharacters = 0;
32
+ }
33
+ if(options.unmaskedEndCharacters < 0) {
34
+ options.unmaskedEndCharacters = 0;
26
35
  }
27
36
  if((!options.maskWith) || options.maskWith.toString().length <= 0) {
28
37
  options.maskWith = '*';
@@ -154,6 +163,12 @@ class MaskHelper {
154
163
  if(typeof(options.maskOnlyFirstOccurance) !== 'boolean') {
155
164
  reasons.push('maskOnlyFirstOccurance should be a boolean field');
156
165
  }
166
+ if(typeof(options.maskAll) !== 'boolean') {
167
+ reasons.push('maskAll should be a boolean field');
168
+ }
169
+ if(typeof(options.maskSpace) !== 'boolean') {
170
+ reasons.push('maskSpace should be a boolean field');
171
+ }
157
172
  if((!options.maskWith) || options.maskWith.toString().length <= 0) {
158
173
  options.maskWith = '*';
159
174
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "maskdata",
3
- "version": "1.1.2",
4
- "description": "Node module to mask various kinds of data in required formats",
3
+ "version": "1.1.6",
4
+ "description": "Node module to mask various kinds of data in the required formats",
5
5
  "main": "index.js",
6
6
  "repository": {
7
7
  "type": "git",
package/test.js CHANGED
@@ -2,14 +2,18 @@ const MaskData = require('./index');
2
2
 
3
3
  const passwordMaskOptions = {
4
4
  maxMaskedCharacters: 10,
5
- maskWith: ""
5
+ maskWith: "X",
6
+ unmaskedStartCharacters: 7,
7
+ unmaskedEndCharacters: 3
6
8
  };
7
9
 
8
10
  const password = "Password1$";
9
- console.log(`Unmasked Password: ${password}`);
10
- console.log(`Password after masking: ${MaskData.maskPassword(password, passwordMaskOptions)}`);
11
+ console.log(`Unmasked Password: ${password}` + " Length: " + password.length);
12
+ const maskedP = MaskData.maskPassword(password, passwordMaskOptions);
13
+ console.log("Password after masking:" + maskedP + " Length: " + maskedP.length);
11
14
  console.log("========================================");
12
- console.log(`Password after masking with default options: ${MaskData.maskPassword(password)}`);
15
+ const maskedPD = MaskData.maskPassword(password);
16
+ console.log("Password after masking with default options:" + maskedPD + " Length: " + maskedPD.length);
13
17
  console.log("========================================");
14
18
 
15
19
 
@@ -121,17 +125,42 @@ const stringMaskOptions = {
121
125
  values: ['is', 'API']
122
126
  };
123
127
 
128
+ const fullStringMaskOptions = {
129
+ maskWith: "*",
130
+ values: ['is', 'API'],
131
+ maskAll: true
132
+ };
133
+
134
+ const fullStringMaskOptionsWithoutSpace = {
135
+ maskWith: "*",
136
+ values: ['is', 'API'],
137
+ maskAll: true,
138
+ maskSpace: false
139
+ };
140
+
124
141
  let str = "This is a testingAPI String";
125
142
  console.log(`Unmasked string: ${str}`);
126
143
  console.log(`String after masking: ${MaskData.maskString(str, stringMaskOptions)}`);
127
144
  console.log("========================================");
128
145
 
146
+ str = "This is a testingAPI String";
147
+ stringMaskOptions.maskOnlyFirstOccurance = true;
148
+ console.log(`Unmasked string: ${str}`);
149
+ console.log(`String after masking entire string: ${MaskData.maskString(str, fullStringMaskOptions)}`);
150
+ console.log("========================================");
151
+
129
152
  str = "This is a testingAPI String";
130
153
  stringMaskOptions.maskOnlyFirstOccurance = true;
131
154
  console.log(`Unmasked string: ${str}`);
132
155
  console.log(`String after masking only first occurances: ${MaskData.maskString(str, stringMaskOptions)}`);
133
156
  console.log("========================================");
134
157
 
158
+ str = "This is a testingAPI String";
159
+ stringMaskOptions.maskOnlyFirstOccurance = true;
160
+ console.log(`Unmasked string: ${str}`);
161
+ console.log(`String after masking entire string without spaces: ${MaskData.maskString(str, fullStringMaskOptionsWithoutSpace)}`);
162
+ console.log("========================================");
163
+
135
164
  const cardMaskOptions = {
136
165
  maskWith: "X",
137
166
  unmaskedStartDigits: 4,