maskdata 1.1.4 → 1.1.7

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.4 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,18 +27,23 @@ 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
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)
36
42
  ### Version: 1.1.4
37
43
  - Password masking bug fix: https://github.com/Sumukha1496/maskdata/issues/12
38
44
 
39
45
  ### Version: 1.1.3
40
- - Unmasked start and end characters feature in password masking. It is an can be used in cases where the password/Secret key has some metadata info which needs to be printed or shown to users.
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.
41
47
  Ticket: https://github.com/Sumukha1496/maskdata/issues/11. Both the fields are opional and can find more info here: [Mask Password](#mask-password)
42
48
 
43
49
  #### Example: Mask password OR secretKey with some meta info at the end
@@ -63,20 +69,6 @@ const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
63
69
  //Output: XXXXXXXXXXX:CLIENT-A
64
70
  ```
65
71
 
66
-
67
- ### Version: 1.1.2
68
- - Bug fix in maskCard function. Issue: https://github.com/Sumukha1496/maskdata/issues/10
69
-
70
- ### Version: 1.1.1
71
- Removed the below deprecated methods
72
- - **simplePasswordMask()** method -> To mask with default configs, simply dont pass the config argument to **maskPassword()** method.
73
-
74
- - **simplePhoneMask()** method -> To mask with default configs, simply dont pass the config argument to **maskPhone()** method.
75
-
76
- - **simpleEmailMask()** method -> To mask with default configs, simply dont pass the config argument to **maskEmail()** method.
77
-
78
- - **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
79
-
80
72
  # How to Use
81
73
  ```javascript
82
74
  const MaskData = require('./maskdata');
@@ -360,7 +352,27 @@ const nestedObject = {
360
352
  const maskedObj = MaskData.maskJSONFields(nestedObject, defaultJSONMaskOptions2);
361
353
 
362
354
  //Output:
363
- {"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
+ }
364
376
  ```
365
377
 
366
378
 
@@ -396,14 +408,31 @@ const maskAllFields = {
396
408
  const maskedObject = MaskData.maskJSONFields(nestedObject, maskAllFields);
397
409
 
398
410
  // Output:
399
- {"level1":{"field1":"field1Value","level2":{"field2":"field2Value","field3":[{"Hello":"*****","Hi":"one"},{"Hello":"***********"}],"level3":{"field4":"***********","field5":"***********"}}},"value1":"value"}
400
-
401
-
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
+ }
402
433
  ```
403
-
404
-
405
- ## Mask the exact substring from throughout the string
406
- 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.
407
436
  ```javascript
408
437
  const MaskData = require('./maskdata');
409
438
 
@@ -411,13 +440,36 @@ const maskStringOptions = {
411
440
  // Character to mask the data. Default value is '*'
412
441
  maskWith: "*",
413
442
 
414
- // It should be an array of strings
415
- // 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
+ */
416
449
  values: ['is', 'test'],
417
450
 
418
- // Should be boolean
419
- // If to mask only the first occurance of each value in the given string
420
- 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
421
473
  };
422
474
 
423
475
  const str = "This is a test String";
@@ -428,6 +480,36 @@ const strAfterMasking = MaskData.maskString(str, maskStringOptions);
428
480
 
429
481
  ```
430
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
+ ```
431
513
 
432
514
  ## Mask card number
433
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
@@ -457,11 +539,10 @@ const cardAfterMasking = MaskData.maskCard(cardNumber, maskCardOptions);
457
539
 
458
540
  ```
459
541
 
460
-
461
542
  # Report Bugs
462
543
  Please raise an issue in github: https://github.com/Sumukha1496/maskdata/issues
463
544
 
464
- # Give a Star:
545
+ # Give a Star
465
546
  You can give a start at: https://github.com/Sumukha1496/maskdata/stargazers
466
547
 
467
548
  # LICENSE - "MIT"
package/index.js CHANGED
@@ -3,8 +3,7 @@
3
3
  const MaskHelper = require('./lib/helpers/MaskHelper');
4
4
  const MaskEmail = require('./lib/emailMask/EmailMask');
5
5
  const MaskCard = require('./lib/cardMask/CardMask');
6
- const get = require('lodash.get');
7
- const set = require('lodash.set');
6
+ const {get, set} = require('lodash');
8
7
 
9
8
  const defaultPhoneMaskOptions = {
10
9
  maskWith: "*",
@@ -27,7 +26,9 @@ const defaultPasswordMaskOptions = {
27
26
  const defaultStringMaskOptions = {
28
27
  maskWith: "*",
29
28
  maskOnlyFirstOccurance: false,
30
- values: []
29
+ values: [],
30
+ maskAll: false,
31
+ maskSpace: true
31
32
  };
32
33
 
33
34
  class MaskData {
@@ -70,6 +71,7 @@ class MaskData {
70
71
  }
71
72
 
72
73
  static maskPhone(phone, options) {
74
+ if(!phone) return phone;
73
75
  if(options) {
74
76
  options = MaskHelper.mapWithDefaultValues(options, defaultPhoneMaskOptions);
75
77
  MaskHelper.validatePhoneOptions(options);
@@ -138,6 +140,7 @@ class MaskData {
138
140
  }
139
141
 
140
142
  static maskString(str, options) {
143
+ if(!str) return str;
141
144
  str = str + '';
142
145
  if(options) {
143
146
  options = MaskHelper.mapWithDefaultValues(options, defaultStringMaskOptions);
@@ -146,7 +149,22 @@ class MaskData {
146
149
  options = defaultStringMaskOptions;
147
150
  }
148
151
  let values = options.values;
149
- if(options.maskOnlyFirstOccurance == true) {
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) {
150
168
  for(const value of Object.values(values)) {
151
169
  str = str.replace(value, `${options.maskWith}`.repeat(value.length));
152
170
  }
@@ -10,6 +10,7 @@ const defaultEmailMask2Options = {
10
10
  class EmailMask {
11
11
 
12
12
  static maskEmail2(email, options) {
13
+ if(!email) return email;
13
14
  let maskedEmail = '';
14
15
  options = options ? MaskHelper.mapWithDefaultValues(options, defaultEmailMask2Options) : defaultEmailMask2Options;
15
16
  MaskHelper.validateEmailOptions2(email, options);
@@ -163,6 +163,12 @@ class MaskHelper {
163
163
  if(typeof(options.maskOnlyFirstOccurance) !== 'boolean') {
164
164
  reasons.push('maskOnlyFirstOccurance should be a boolean field');
165
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
+ }
166
172
  if((!options.maskWith) || options.maskWith.toString().length <= 0) {
167
173
  options.maskWith = '*';
168
174
  }
package/package.json CHANGED
@@ -1,14 +1,15 @@
1
1
  {
2
2
  "name": "maskdata",
3
- "version": "1.1.4",
4
- "description": "Node module to mask various kinds of data in required formats",
3
+ "version": "1.1.7",
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",
8
8
  "url": "https://github.com/Sumukha1496/maskdata"
9
9
  },
10
10
  "scripts": {
11
- "test": "node test.js"
11
+ "test": "node test.js && mocha tests/test*.js --timeout 5000 --exit",
12
+ "coverage": "nyc --reporter=lcov --report-dir coverage/ mocha --timeout 5000 --reporter mocha-junit-reporter --reporter-options mochaFile=coverage/junit.xml --exit"
12
13
  },
13
14
  "keywords": [
14
15
  "mask",
@@ -30,7 +31,12 @@
30
31
  "author": "sumukha.s<sumukha.hs369@gmail.com>",
31
32
  "license": "MIT",
32
33
  "dependencies": {
33
- "lodash.get": "^4.4.2",
34
- "lodash.set": "^4.3.2"
34
+ "lodash": "^4.17.21"
35
+ },
36
+ "devDependencies": {
37
+ "chai": "^4.3.6",
38
+ "mocha": "^9.2.2",
39
+ "mocha-junit-reporter": "^2.0.2",
40
+ "nyc": "^15.1.0"
35
41
  }
36
42
  }
package/test.js CHANGED
@@ -55,13 +55,11 @@ console.log("========================================");
55
55
  const jsonMaskOptions = {
56
56
  fields: ['password', 'firstName']
57
57
  };
58
-
59
58
  const json = {
60
59
  password: "PasswordValue",
61
60
  firstName: "FIRST_NAME",
62
61
  lastName: "LAST_NAME"
63
62
  };
64
-
65
63
  console.log(`Unmasked object: `);
66
64
  console.log(JSON.stringify(json));
67
65
  console.log(`Object after masking:`);
@@ -69,15 +67,12 @@ console.log(JSON.stringify(MaskData.maskJSONFields(json, jsonMaskOptions)));
69
67
  console.log("========================================");
70
68
  console.log(JSON.stringify(json));
71
69
  console.log("========================================");
72
-
73
70
  const jsonMaskOptions2 = {
74
71
  fields : ['level1.level2.level3.field3', 'level1.level2.field2', 'level1.field1', 'value1', 'level1.level2.level3.field4[0].Hello', 'level1.level2.level3.field4[2]']
75
72
  };
76
-
77
73
  const maskAllFieldsOptions = {
78
74
  fields : ['level1.level2.field3[*].Hello', 'level1.level2.level3.*']
79
75
  };
80
-
81
76
  const nestedAllObject = {
82
77
  level1: {
83
78
  field1: "field1Value",
@@ -92,13 +87,11 @@ const nestedAllObject = {
92
87
  },
93
88
  value1: "value"
94
89
  };
95
-
96
90
  console.log(`Unmasked nested object: `);
97
91
  console.log(JSON.stringify(nestedAllObject));
98
92
  console.log(`Nested Object after masking all:`);
99
93
  console.log(JSON.stringify(MaskData.maskJSONFields(nestedAllObject, maskAllFieldsOptions)));
100
94
  console.log("========================================");
101
-
102
95
  const nestedObject = {
103
96
  level1: {
104
97
  field1: "field1",
@@ -112,7 +105,6 @@ const nestedObject = {
112
105
  },
113
106
  value1: "value"
114
107
  };
115
-
116
108
  console.log(`Unmasked nested object: `);
117
109
  console.log(JSON.stringify(nestedObject));
118
110
  console.log(`Nested Object after masking:`);
@@ -125,17 +117,42 @@ const stringMaskOptions = {
125
117
  values: ['is', 'API']
126
118
  };
127
119
 
120
+ const fullStringMaskOptions = {
121
+ maskWith: "*",
122
+ values: ['is', 'API'],
123
+ maskAll: true
124
+ };
125
+
126
+ const fullStringMaskOptionsWithoutSpace = {
127
+ maskWith: "*",
128
+ values: ['is', 'API'],
129
+ maskAll: true,
130
+ maskSpace: false
131
+ };
132
+
128
133
  let str = "This is a testingAPI String";
129
134
  console.log(`Unmasked string: ${str}`);
130
135
  console.log(`String after masking: ${MaskData.maskString(str, stringMaskOptions)}`);
131
136
  console.log("========================================");
132
137
 
138
+ str = "This is a testingAPI String";
139
+ stringMaskOptions.maskOnlyFirstOccurance = true;
140
+ console.log(`Unmasked string: ${str}`);
141
+ console.log(`String after masking entire string: ${MaskData.maskString(str, fullStringMaskOptions)}`);
142
+ console.log("========================================");
143
+
133
144
  str = "This is a testingAPI String";
134
145
  stringMaskOptions.maskOnlyFirstOccurance = true;
135
146
  console.log(`Unmasked string: ${str}`);
136
147
  console.log(`String after masking only first occurances: ${MaskData.maskString(str, stringMaskOptions)}`);
137
148
  console.log("========================================");
138
149
 
150
+ str = "This is a testingAPI String";
151
+ stringMaskOptions.maskOnlyFirstOccurance = true;
152
+ console.log(`Unmasked string: ${str}`);
153
+ console.log(`String after masking entire string without spaces: ${MaskData.maskString(str, fullStringMaskOptionsWithoutSpace)}`);
154
+ console.log("========================================");
155
+
139
156
  const cardMaskOptions = {
140
157
  maskWith: "X",
141
158
  unmaskedStartDigits: 4,
@@ -160,18 +177,14 @@ const nestedJson = {
160
177
  },
161
178
  value1: "value"
162
179
  };
163
-
164
180
  const field = 'level1.level2.level3.field4[0].Hello';
165
-
166
181
  console.log("========================================");
167
182
  console.log(`${field}: ${MaskData.getInnerProperty(nestedJson, field)}`);
168
-
169
183
  const input = {
170
184
  name: "John",
171
185
  age: 33,
172
186
  married: true
173
187
  }
174
-
175
188
  console.log("Before replacing: " + JSON.stringify(input));
176
189
  console.log("========================================");
177
190
  let afterReplacing = MaskData.replaceValue(input, 'age', 99);
@@ -188,4 +201,4 @@ const maskCardOptions = {
188
201
 
189
202
  const obj = { cardNumber: "123456789123" ,abc: "abceew" ,efg: "121212" } ;
190
203
  const result = MaskData.replaceValue(obj, 'cardNumber', MaskData.maskCard(obj['cardNumber'], maskCardOptions));
191
- console.log(result);
204
+ console.log(result);
@@ -0,0 +1,156 @@
1
+ 'use strict';
2
+ const maskData = require('../index');
3
+ const expect = require('chai').expect;
4
+
5
+ describe('Masking card numbers', function() {
6
+
7
+ describe('Mask with default options', function() {
8
+
9
+ // default options are this - let tests fail when defaults change
10
+ // const defaultCardMaskOptions = {
11
+ // maskWith: '*',
12
+ // unmaskedStartDigits: 4,
13
+ // unmaskedEndDigits: 1
14
+ // };
15
+
16
+ let testData = [
17
+ {
18
+ title: 'test default string',
19
+ input: '1234-5678-1234-5678',
20
+ output: '1234-****-****-***8'
21
+ },
22
+ {
23
+ title: 'test string length equal than start + end',
24
+ input: '123-4',
25
+ output: '123-4'
26
+ },
27
+ {
28
+ title: 'test shorter string than start + end',
29
+ input: '1234',
30
+ output: '1234'
31
+ }
32
+ ]
33
+
34
+ testData.forEach(({title, input, output}) => {
35
+ it(`default mask - ${title}`, function() {
36
+ const masked = maskData.maskCard(input);
37
+ expect(masked).to.equal(output, 'masked output does not match expected value');
38
+ });
39
+ })
40
+ });
41
+
42
+ describe('Mask with custom options', function() {
43
+
44
+ const maskOptions = {
45
+ maskWith: 'x',
46
+ unmaskedStartDigits: 2, // use different numbers for start / end to check correct masking
47
+ unmaskedEndDigits: 3
48
+ };
49
+
50
+ let testData = [
51
+ {
52
+ title: 'test default string',
53
+ input: '1234-5678-1234-5678',
54
+ output: '12xx-xxxx-xxxx-x678'
55
+ },
56
+ {
57
+ title: 'test string length equal than start + end',
58
+ input: '12-345',
59
+ output: '12-345'
60
+ },
61
+ {
62
+ title: 'test shorter string than start + end',
63
+ input: '1234',
64
+ output: '1234'
65
+ }
66
+ ]
67
+
68
+ testData.forEach(({title, input, output}) => {
69
+ it(`custom mask - ${title}`, function() {
70
+ const masked = maskData.maskCard(input, maskOptions);
71
+ expect(masked).to.equal(output, 'masked output does not match expected value');
72
+ });
73
+ });
74
+
75
+ it('test with negative unmaskedStartDigits', function() {
76
+ const negativeStartOption = {
77
+ maskWith: 'x',
78
+ unmaskedStartDigits: -2, // internal set to 0
79
+ unmaskedEndDigits: 3
80
+ }
81
+ const input = '1234-5678-1234-5678'
82
+ const output = 'xxxx-xxxx-xxxx-x678'
83
+ const masked = maskData.maskCard(input, negativeStartOption);
84
+ expect(masked).to.equal(output, 'masked output does not match expected value');
85
+ });
86
+
87
+ it('test with negative unmaskedEndDigits', function() {
88
+ const negativeEndOption = {
89
+ maskWith: 'x',
90
+ unmaskedStartDigits: 2,
91
+ unmaskedEndDigits: -3 // internal set to 0
92
+ }
93
+ const input = '1234-5678-1234-5678'
94
+ const output = '12xx-xxxx-xxxx-xxxx'
95
+ const masked = maskData.maskCard(input, negativeEndOption);
96
+ expect(masked).to.equal(output, 'masked output does not match expected value');
97
+ });
98
+ });
99
+
100
+ describe('Mask with special input strings', function() {
101
+
102
+ const maskOptions = {
103
+ maskWith: "x",
104
+ unmaskedStartDigits: 2,
105
+ unmaskedEndDigits: 3
106
+ };
107
+
108
+ describe('Mask with special input - input will generate an output', function() {
109
+
110
+ // first set with input generating an masked output
111
+ let testData = [
112
+ {
113
+ title: 'test with string length zero',
114
+ input: '',
115
+ output: ''
116
+ },
117
+ {
118
+ title: 'test input null',
119
+ input: null,
120
+ output: null
121
+ }
122
+ ]
123
+
124
+ testData.forEach(({title, input, output}) => {
125
+ it(`special input - ${title}`, function() {
126
+ const masked = maskData.maskCard(input, maskOptions);
127
+ expect(masked).to.equal(output, 'masked output does not match expected value');
128
+ });
129
+ });
130
+ });
131
+
132
+ describe('Mask with special input - input shall throw error', function() {
133
+
134
+ // set with input generating an error / exception
135
+ let testData = [
136
+ {
137
+ title: 'test input as number',
138
+ input: 12,
139
+ output: '12'
140
+ },
141
+ {
142
+ title: 'test input as array',
143
+ input: ['12'],
144
+ output: '12'
145
+ }
146
+ ]
147
+
148
+ testData.forEach(({title, input, output}) => {
149
+ it(`special input - ${title}`, function() {
150
+ const masked = maskData.maskCard(input, maskOptions);
151
+ expect(masked).to.equal(output, 'masked output does not match expected value');
152
+ });
153
+ });
154
+ });
155
+ });
156
+ });