maskdata 1.1.5 → 1.1.8

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.5 Features](#release-features)
7
+ - [Version 1.1.8 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)
@@ -19,7 +19,8 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
19
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
+ - [How to run test cases](#run-tests)
23
+ - [Give a Star](#give-a-star)
23
24
  - [LICENSE - "MIT"](#license---mit)
24
25
 
25
26
  # Features
@@ -36,7 +37,11 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
36
37
  > npm i maskdata
37
38
 
38
39
  # Release Features
39
- ### Version: 1.1.5
40
+ ### Version: 1.1.8
41
+ - Mocha test cases for most of the usecases.
42
+ https://github.com/Sumukha1496/maskdata/pull/23
43
+ - Handling null/Empty inputs --> Mask function will return the input itself.
44
+ ### Version: 1.1.6
40
45
  - Feature to mask all the characters in the String along with mask/not mask spaces in the string.
41
46
  - [Mask all characters in the String](#mask-all-characters-in-the-string)
42
47
  ### Version: 1.1.4
@@ -542,7 +547,11 @@ const cardAfterMasking = MaskData.maskCard(cardNumber, maskCardOptions);
542
547
  # Report Bugs
543
548
  Please raise an issue in github: https://github.com/Sumukha1496/maskdata/issues
544
549
 
545
- # Give a Star:
550
+ # Run Tests
551
+ Below command will run mocha tests and also the test cases present in test.js file. Install all the dependencies using npm i before runnning the below command.
552
+ > npm run test
553
+
554
+ # Give a Star
546
555
  You can give a start at: https://github.com/Sumukha1496/maskdata/stargazers
547
556
 
548
557
  # 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: "*",
@@ -72,6 +71,7 @@ class MaskData {
72
71
  }
73
72
 
74
73
  static maskPhone(phone, options) {
74
+ if(!phone) return phone;
75
75
  if(options) {
76
76
  options = MaskHelper.mapWithDefaultValues(options, defaultPhoneMaskOptions);
77
77
  MaskHelper.validatePhoneOptions(options);
@@ -140,6 +140,7 @@ class MaskData {
140
140
  }
141
141
 
142
142
  static maskString(str, options) {
143
+ if(!str) return str;
143
144
  str = str + '';
144
145
  if(options) {
145
146
  options = MaskHelper.mapWithDefaultValues(options, defaultStringMaskOptions);
@@ -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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maskdata",
3
- "version": "1.1.5",
3
+ "version": "1.1.8",
4
4
  "description": "Node module to mask various kinds of data in the required formats",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -8,7 +8,8 @@
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:`);
@@ -185,18 +177,14 @@ const nestedJson = {
185
177
  },
186
178
  value1: "value"
187
179
  };
188
-
189
180
  const field = 'level1.level2.level3.field4[0].Hello';
190
-
191
181
  console.log("========================================");
192
182
  console.log(`${field}: ${MaskData.getInnerProperty(nestedJson, field)}`);
193
-
194
183
  const input = {
195
184
  name: "John",
196
185
  age: 33,
197
186
  married: true
198
187
  }
199
-
200
188
  console.log("Before replacing: " + JSON.stringify(input));
201
189
  console.log("========================================");
202
190
  let afterReplacing = MaskData.replaceValue(input, 'age', 99);
@@ -213,4 +201,4 @@ const maskCardOptions = {
213
201
 
214
202
  const obj = { cardNumber: "123456789123" ,abc: "abceew" ,efg: "121212" } ;
215
203
  const result = MaskData.replaceValue(obj, 'cardNumber', MaskData.maskCard(obj['cardNumber'], maskCardOptions));
216
- 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
+ });
@@ -0,0 +1,206 @@
1
+ 'use strict';
2
+ const maskData = require('../index');
3
+ const BadOption = require('../lib/ExceptionsHandler/BadOption');
4
+ const expect = require('chai').expect;
5
+
6
+ describe('Masking email addresses', function() {
7
+
8
+ describe('Mask with default options', function() {
9
+
10
+ // default options are this - let tests fail when defaults change
11
+ // const defaultCardMaskOptions = {
12
+ // maskWith: '*',
13
+ // unmaskedStartCharactersBeforeAt: 3,
14
+ // unmaskedEndCharactersAfterAt: 2
15
+ // maskAtTheRate: false
16
+ // };
17
+
18
+ let testData = [
19
+ {
20
+ title: 'test standard address',
21
+ input: 'testuser@dummy.org',
22
+ output: 'tes*****@*******rg'
23
+ },
24
+ {
25
+ title: 'test user part shorter than chars before',
26
+ input: 'we@dummy.org',
27
+ output: 'we@*******rg'
28
+ },
29
+ {
30
+ title: 'test user part equal to chars before',
31
+ input: 'you@dummy.org',
32
+ output: 'you@*******rg'
33
+ },
34
+ {
35
+ title: 'test no user part and domain only',
36
+ input: '@dummy.org',
37
+ output: '@*******rg'
38
+ },
39
+ {
40
+ title: 'test no domain part and user only',
41
+ input: 'user@',
42
+ output: 'use*@'
43
+ },
44
+ {
45
+ title: 'test domain part shorter than chars after',
46
+ input: 'user@x',
47
+ output: 'use*@x'
48
+ }
49
+ ]
50
+
51
+ testData.forEach(({title, input, output}) => {
52
+ it(`default mask - ${title}`, function() {
53
+ const masked = maskData.maskEmail2(input);
54
+ expect(masked).to.equal(output, 'masked output does not match expected value');
55
+ });
56
+ })
57
+ });
58
+
59
+ describe('Mask with custom options (char, before, after)', function() {
60
+
61
+ const maskOptions = {
62
+ maskWith: 'x',
63
+ unmaskedStartCharactersBeforeAt: 4,
64
+ unmaskedEndCharactersAfterAt: 6, // full tld and part of sld
65
+ maskAtTheRate: false
66
+ };
67
+
68
+ let testData = [
69
+ {
70
+ title: 'test default string',
71
+ input: 'testuser@dummy.org',
72
+ output: 'testxxxx@xxxmy.org'
73
+ }
74
+ ]
75
+
76
+ testData.forEach(({title, input, output}) => {
77
+ it(`default mask - ${title}`, function() {
78
+ const masked = maskData.maskEmail2(input, maskOptions);
79
+ expect(masked).to.equal(output, 'masked output does not match expected value');
80
+ });
81
+ })
82
+ });
83
+
84
+ describe('Mask with custom options (maskAtTheRate)', function() {
85
+
86
+ const maskOptions = {
87
+ maskWith: '*',
88
+ unmaskedStartCharactersBeforeAt: 4,
89
+ unmaskedEndCharactersAfterAt: 6, // full tld and part of sld
90
+ maskAtTheRate: true
91
+ };
92
+
93
+ let testData = [
94
+ {
95
+ title: 'test default string',
96
+ input: 'testuser@dummy.org',
97
+ output: 'test********my.org'
98
+ },
99
+ {
100
+ title: 'test user and domain part to short',
101
+ input: 'we@a.de',
102
+ output: 'we*a.de' // before and after are shorter than visible, only @ replaced
103
+ },
104
+ {
105
+ title: 'test user part to short',
106
+ input: 'we@dummy.org',
107
+ output: 'we****my.org' // before and after are shorter than visible, only @ replaced
108
+ },
109
+ {
110
+ title: 'test domain part to short',
111
+ input: 'testuser@a.de',
112
+ output: 'test*****a.de' // before and after are shorter than visible, only @ replaced
113
+ }
114
+ ]
115
+
116
+ testData.forEach(({title, input, output}) => {
117
+ it(`custom mask - ${title}`, function() {
118
+ const masked = maskData.maskEmail2(input, maskOptions);
119
+ expect(masked).to.equal(output, 'masked output does not match expected value');
120
+ });
121
+ });
122
+
123
+ it('custom mask - negative start digits', function() {
124
+ const negativeStartOptions = {
125
+ maskWith: '*',
126
+ unmaskedStartCharactersBeforeAt: -1, // replaced with 0 internally
127
+ unmaskedEndCharactersAfterAt: 1,
128
+ maskAtTheRate: false
129
+ };
130
+ const input = 'test@dummy.de';
131
+ const output = '****@*******e';
132
+ const masked = maskData.maskEmail2(input, negativeStartOptions);
133
+ expect(masked).to.equal(output, 'masked output does not match expected value');
134
+ });
135
+
136
+ it('custom mask - negative end digits', function() {
137
+ const negativeEndOptions = {
138
+ maskWith: '*',
139
+ unmaskedStartCharactersBeforeAt: 1, // replaced with 0 internally
140
+ unmaskedEndCharactersAfterAt: -1,
141
+ maskAtTheRate: false
142
+ };
143
+ const input = 'test@dummy.de';
144
+ const output = 't***@********';
145
+ const masked = maskData.maskEmail2(input, negativeEndOptions);
146
+ expect(masked).to.equal(output, 'masked output does not match expected value');
147
+ });
148
+ });
149
+
150
+ describe('test error cases for email addresses', function() {
151
+
152
+ let testData = [
153
+ {
154
+ title: 'test no email address at all',
155
+ input: 'user-dummy.org',
156
+ output: 'use*@x'
157
+ }
158
+ ]
159
+
160
+ testData.forEach(({title, input, output}) => {
161
+ it(`default mask - ${title}`, function() {
162
+ try {
163
+ const masked = maskData.maskEmail2(input);
164
+ expect.fail('maskEmail2 should throw error');
165
+ }
166
+ catch(e) {
167
+ expect(e).to.be.a('string').and.match(/Bad config.*Email must.*@/);
168
+ }
169
+ });
170
+ })
171
+ });
172
+
173
+ describe('Mask with special input strings', function() {
174
+
175
+ const maskOptions = {
176
+ maskWith: "x",
177
+ unmaskedStartCharactersBeforeAt: 1,
178
+ unmaskedEndCharactersAfterAt: 1,
179
+ maskAtTheRate: false
180
+ };
181
+
182
+ describe('Mask with special input - input will generate an output', function() {
183
+
184
+ // first set with input generating an masked output
185
+ let testData = [
186
+ {
187
+ title: 'test with string length zero',
188
+ input: '',
189
+ output: ''
190
+ },
191
+ {
192
+ title: 'test input null',
193
+ input: null,
194
+ output: null
195
+ }
196
+ ]
197
+
198
+ testData.forEach(({title, input, output}) => {
199
+ it(`special input - ${title}`, function() {
200
+ const masked = maskData.maskEmail2(input, maskOptions);
201
+ expect(masked).to.equal(output, 'masked output does not match expected value');
202
+ });
203
+ });
204
+ });
205
+ });
206
+ });
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+ const maskData = require('../index');
3
+ const expect = require('chai').expect;
4
+
5
+ describe('Masking JSON fields', function() {
6
+
7
+ // TODO - migrate all tests from test.js into mocha tests to fail on error
8
+ // TODO - extend tests
9
+ });
@@ -0,0 +1,149 @@
1
+ 'use strict';
2
+ const maskData = require('../index');
3
+ const expect = require('chai').expect;
4
+
5
+ describe('Masking password', function() {
6
+
7
+ describe('Mask with default options', function() {
8
+
9
+ // default options are this - let tests fail when defaults change
10
+ // const defaultPasswordMaskOptions = {
11
+ // maskWith: "*",
12
+ // maxMaskedCharacters: 16,
13
+ // unmaskedStartCharacters: 0,
14
+ // unmaskedEndCharacters: 0
15
+ // };
16
+
17
+ let testData = [
18
+ {
19
+ title: 'test default string',
20
+ input: '1234-5678-1234-5678',
21
+ output: '****************' // equal maxMaskedChars
22
+ },
23
+ {
24
+ title: 'test string shorter than maxMasked',
25
+ input: '123-4',
26
+ output: '*****'
27
+ },
28
+ {
29
+ title: 'test longer than maxMasked',
30
+ input: '1234567890abcdefghijkl',
31
+ output: '****************' // equal maxMaskedChars
32
+ }
33
+ ]
34
+
35
+ testData.forEach(({title, input, output}) => {
36
+ it(`default mask - ${title}`, function() {
37
+ const masked = maskData.maskPassword(input);
38
+ expect(masked).to.equal(output, 'masked output does not match expected value');
39
+ });
40
+ })
41
+ });
42
+
43
+ describe('Mask with custom options - maskWith, maxMaskedChars', function() {
44
+
45
+ const maskOptions = {
46
+ maskWith: "x",
47
+ maxMaskedCharacters: 6,
48
+ unmaskedStartCharacters: 1, // use different numbers for start / end to check correct masking
49
+ unmaskedEndCharacters: 2
50
+ };
51
+
52
+ let testData = [
53
+ {
54
+ title: 'test default string',
55
+ input: '1234-5678-1234-5678',
56
+ output: '1xxx78'
57
+ },
58
+ {
59
+ title: 'test string length equal to start + end',
60
+ input: '12-345',
61
+ output: '1xxx45'
62
+ },
63
+ {
64
+ title: 'test shorter string than start + end',
65
+ input: '12',
66
+ output: '12'
67
+ }
68
+ ]
69
+
70
+ testData.forEach(({title, input, output}) => {
71
+ it(`custom mask - ${title}`, function() {
72
+ const masked = maskData.maskPassword(input, maskOptions);
73
+ expect(masked).to.equal(output, 'masked output does not match expected value');
74
+ });
75
+ });
76
+
77
+ it('custom mask - test with negative unmaskedStartDigits', function() {
78
+ const negativeStartOption = {
79
+ maskWith: 'x',
80
+ maxMaskedCharacters: 6,
81
+ unmaskedStartCharacters: -2, // internal set to 0
82
+ unmaskedEndCharacters: 2
83
+ }
84
+ const input = '1234-5678-1234-5678'
85
+ const output = 'xxxx78'
86
+ const masked = maskData.maskPassword(input, negativeStartOption);
87
+ expect(masked).to.equal(output, 'masked output does not match expected value');
88
+ });
89
+
90
+ it('custom mask - test with maxMasked shorter than start + end', function() {
91
+ const shortMaxCharsOption = {
92
+ maskWith: 'x',
93
+ maxMaskedCharacters: 6,
94
+ unmaskedStartCharacters: 4, // 4+4=8 > 6 --> But this will give more preference to maxMaskedCharacters
95
+ unmaskedEndCharacters: 4
96
+ }
97
+ const input = '1234-5678-1234-5678'
98
+ const output = '123478'
99
+ const masked = maskData.maskPassword(input, shortMaxCharsOption);
100
+ expect(masked).to.equal(output, 'masked output does not match expected value');
101
+ });
102
+
103
+ it('custom mask - test with maxMasked equal to start + end', function() {
104
+ const shortMaxCharsOption = {
105
+ maskWith: 'x',
106
+ maxMaskedCharacters: 8,
107
+ unmaskedStartCharacters: 4, // 4+4=8
108
+ unmaskedEndCharacters: 4
109
+ }
110
+ const input = '1234-abcd-efgh-5678'
111
+ const output = '12345678'
112
+ const masked = maskData.maskPassword(input, shortMaxCharsOption);
113
+ expect(masked).to.equal(output, 'masked output does not match expected value');
114
+ });
115
+ });
116
+
117
+ describe('Mask with special input strings', function() {
118
+
119
+ const maskOptions = {
120
+ maskWith: "x",
121
+ maxMaskedCharacters: 6,
122
+ unmaskedStartCharacters: 2,
123
+ unmaskedEndCharacters: 2
124
+ };
125
+
126
+ describe('Mask with special input - input will generate an output', function() {
127
+
128
+ // first set with input generating an masked output
129
+ let testData = [
130
+ {
131
+ title: 'test with string length zero',
132
+ input: '',
133
+ output: ''
134
+ },
135
+ {
136
+ title: 'test input null',
137
+ input: null,
138
+ output: null
139
+ }
140
+ ]
141
+ testData.forEach(({title, input, output}) => {
142
+ it(`special input - ${title}`, function() {
143
+ const masked = maskData.maskPassword(input, maskOptions);
144
+ expect(masked).to.equal(output, 'masked output does not match expected value');
145
+ });
146
+ });
147
+ });
148
+ });
149
+ });
@@ -0,0 +1,156 @@
1
+ 'use strict';
2
+ const maskData = require('../index');
3
+ const expect = require('chai').expect;
4
+
5
+ describe('Masking phone numbers', function() {
6
+
7
+ describe('Mask with default options', function() {
8
+
9
+ // default options are this - let tests fail when defaults change
10
+ // const phoneMaskOptions = {
11
+ // maskWith: "*",
12
+ // unmaskedStartDigits: 4,
13
+ // unmaskedEndDigits: 1
14
+ // };
15
+
16
+ let testData = [
17
+ {
18
+ title: 'test default number',
19
+ input: '1234-56781234',
20
+ output: '1234********4'
21
+ },
22
+ {
23
+ title: 'test number length equal than start + end',
24
+ input: '12345',
25
+ output: '12345'
26
+ },
27
+ {
28
+ title: 'test shorter number 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.maskPhone(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: '12xxxxxxxxxxxxxx678'
55
+ },
56
+ {
57
+ title: 'test number length equal than start + end',
58
+ input: '12-34',
59
+ output: '12-34'
60
+ },
61
+ {
62
+ title: 'test shorter number 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.maskPhone(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'
82
+ const output = 'xxxxxx678'
83
+ const masked = maskData.maskPhone(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'
94
+ const output = '12xxxxxxx'
95
+ const masked = maskData.maskPhone(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: 2
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.maskPhone(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 array',
138
+ // input: ['12', '34'],
139
+ // output: 'xx'
140
+ // },
141
+ // {
142
+ // title: 'test input as object',
143
+ // input: {a: 'b', x: 'y'},
144
+ // output: '12'
145
+ // }
146
+ // ]
147
+
148
+ // testData.forEach(({title, input, output}) => {
149
+ // it(`special input - ${title}`, function() {
150
+ // const masked = maskData.maskPhone(input, maskOptions);
151
+ // expect(masked).to.equal(output, 'masked output does not match expected value');
152
+ // });
153
+ // });
154
+ // });
155
+ });
156
+ });
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+ const maskData = require('../index');
3
+ const expect = require('chai').expect;
4
+
5
+ describe('Test Replace value', function() {
6
+
7
+ const input = {
8
+ name: "John",
9
+ age: 33,
10
+ married: true
11
+ };
12
+
13
+ const testData = [
14
+ {
15
+ title: 'replace integer inside json',
16
+ input: input,
17
+ inputField: 'age',
18
+ output: 65
19
+ },
20
+ {
21
+ title: 'replace string inside json',
22
+ input: input,
23
+ inputField: 'name',
24
+ output: "Sherlock"
25
+ }
26
+ ];
27
+ describe('Test replaceValue positive cases', function() {
28
+ testData.forEach(({title, input, inputField, output}) => {
29
+ it(`custom mask - ${title}`, function() {
30
+ const afterReplacing = maskData.replaceValue(input, inputField, output);
31
+ expect(afterReplacing[inputField]).to.equal(output, 'Replaced value same as expected value');
32
+ });
33
+ });
34
+ });
35
+ });
@@ -0,0 +1,258 @@
1
+ 'use strict';
2
+ const maskData = require('../index');
3
+ const expect = require('chai').expect;
4
+
5
+ describe('Masking strings', function() {
6
+
7
+ // common strings without output - used as base for all tests below
8
+ let testData = [
9
+ {
10
+ title: 'test default string',
11
+ input: 'This is a testingAPI String',
12
+ output: ''
13
+ },
14
+ {
15
+ title: 'test string without space',
16
+ input: 'thisisastring',
17
+ output: ''
18
+ },
19
+ {
20
+ title: 'test string with newline',
21
+ input: 'this is a\nstring with newline',
22
+ output: ''
23
+ }
24
+ ]
25
+
26
+ describe('Mask with default options', function() {
27
+
28
+ // default options are this - let tests fail when defaults change
29
+ // const defaultStringMaskOptions = {
30
+ // maskWith: "*",
31
+ // maskOnlyFirstOccurance: false,
32
+ // values: [],
33
+ // maskAll: false,
34
+ // maskSpace: true
35
+ // };
36
+
37
+ // no values -> no masking
38
+ const localTestData = JSON.parse(JSON.stringify(testData));
39
+ localTestData[0].output = localTestData[0].input;
40
+ localTestData[1].output = localTestData[1].input;
41
+ localTestData[2].output = localTestData[2].input;
42
+
43
+ localTestData.forEach(({title, input, output}) => {
44
+ it(`default mask - ${title}`, function() {
45
+ const masked = maskData.maskString(input);
46
+ expect(masked).to.equal(output, 'masked output does not match expected value');
47
+ });
48
+ })
49
+ });
50
+
51
+ describe('Mask with custom options - maskWith, maskSpace', function() {
52
+
53
+ // only maskWith and maskSpace set
54
+ const maskOptions = {
55
+ maskWith: "?",
56
+ maskOnlyFirstOccurance: false,
57
+ values: [],
58
+ maskAll: false,
59
+ maskSpace: true
60
+ };
61
+
62
+ // no values -> no masking
63
+ const localTestData = JSON.parse(JSON.stringify(testData));
64
+ localTestData[0].output = localTestData[0].input;
65
+ localTestData[1].output = localTestData[1].input;
66
+ localTestData[2].output = localTestData[2].input;
67
+
68
+ localTestData.forEach(({title, input, output}) => {
69
+ it(`custom mask - ${title}`, function() {
70
+ const masked = maskData.maskString(input, maskOptions);
71
+ expect(masked).to.equal(output, 'masked output does not match expected value');
72
+ });
73
+ });
74
+ });
75
+
76
+ describe('Mask with custom options - maskWith, maskAll, no spaces', function() {
77
+
78
+ // only maskWith, maskAll set
79
+ const maskOptions = {
80
+ maskWith: "?",
81
+ maskOnlyFirstOccurance: false,
82
+ values: [],
83
+ maskAll: true,
84
+ maskSpace: false
85
+ };
86
+
87
+ const localTestData = JSON.parse(JSON.stringify(testData));
88
+ localTestData[0].output = '???? ?? ? ?????????? ??????';
89
+ localTestData[1].output = '?'.repeat(localTestData[1].input.length); // no space here
90
+ localTestData[2].output = '???? ?? ???????? ???? ???????';
91
+
92
+ localTestData.forEach(({title, input, output}) => {
93
+ it(`custom mask - ${title}`, function() {
94
+ const masked = maskData.maskString(input, maskOptions);
95
+ expect(masked).to.equal(output, 'masked output does not match expected value');
96
+ });
97
+ });
98
+ });
99
+
100
+ describe('Mask with custom options - maskWith multiple chars, maskAll, maskSpace', function() {
101
+
102
+ // only maskWith, maskAll set
103
+ const maskChar = "??"; // mask every char in input string with two chars in output
104
+ const maskOptions = {
105
+ maskWith: maskChar,
106
+ maskOnlyFirstOccurance: false,
107
+ values: [],
108
+ maskAll: true,
109
+ maskSpace: true
110
+ };
111
+
112
+ const localTestData = JSON.parse(JSON.stringify(testData));
113
+ localTestData[0].output = maskChar.repeat(localTestData[0].input.length);
114
+ localTestData[1].output = maskChar.repeat(localTestData[1].input.length);
115
+ localTestData[2].output = maskChar.repeat(localTestData[2].input.length);
116
+
117
+ localTestData.forEach(({title, input, output}) => {
118
+ it(`custom mask - ${title}`, function() {
119
+ const masked = maskData.maskString(input, maskOptions);
120
+ expect(masked).to.equal(output, 'masked output does not match expected value');
121
+ });
122
+ });
123
+ });
124
+
125
+ describe('Mask with custom options - values and maskAll set, no spaces', function() {
126
+
127
+ const maskOptions = {
128
+ maskWith: "x",
129
+ maskOnlyFirstOccurance: false,
130
+ values: ['is', 'ing'],
131
+ maskAll: true,
132
+ maskSpace: false
133
+ };
134
+
135
+ // expected: values ignored and all masked, space remains
136
+ const localTestData = JSON.parse(JSON.stringify(testData));
137
+ localTestData[0].output = 'xxxx xx x xxxxxxxxxx xxxxxx';
138
+ localTestData[1].output = 'x'.repeat(localTestData[1].input.length); // no spaces here
139
+ localTestData[2].output = 'xxxx xx xxxxxxxx xxxx xxxxxxx';
140
+
141
+ localTestData.forEach(({title, input, output}) => {
142
+ it(`custom mask - ${title}`, function() {
143
+ const masked = maskData.maskString(input, maskOptions);
144
+ expect(masked).to.equal(output, 'masked output does not match expected value');
145
+ });
146
+ });
147
+ });
148
+
149
+ describe('Mask with custom options - values and all occurrences', function() {
150
+
151
+ const maskOptions = {
152
+ maskWith: "*",
153
+ maskOnlyFirstOccurance: false,
154
+ values: ['is', 'ing'],
155
+ maskAll: false,
156
+ maskSpace: true
157
+ };
158
+
159
+ const localTestData = JSON.parse(JSON.stringify(testData));
160
+ localTestData[0].output = 'Th** ** a test***API Str***';
161
+ localTestData[1].output = 'th****astr***';
162
+ localTestData[2].output = 'th** ** a\nstr*** with newline';
163
+
164
+ localTestData.forEach(({title, input, output}) => {
165
+ it(`custom mask - ${title}`, function() {
166
+ const masked = maskData.maskString(input, maskOptions);
167
+ expect(masked).to.equal(output, 'masked output does not match expected value');
168
+ });
169
+ });
170
+ });
171
+
172
+ describe('Mask with custom options - values and first occureances', function() {
173
+
174
+ const maskOptions = {
175
+ maskWith: "*",
176
+ maskOnlyFirstOccurance: true,
177
+ values: ['is', 'ing'],
178
+ maskAll: false,
179
+ maskSpace: true
180
+ };
181
+
182
+ const localTestData = JSON.parse(JSON.stringify(testData));
183
+ localTestData[0].output = 'Th** is a test***API String';
184
+ localTestData[1].output = 'th**isastr***';
185
+ localTestData[2].output = 'th** is a\nstr*** with newline';
186
+
187
+ localTestData.forEach(({title, input, output}) => {
188
+ it(`custom mask - ${title}`, function() {
189
+ const masked = maskData.maskString(input, maskOptions);
190
+ expect(masked).to.equal(output, 'masked output does not match expected value');
191
+ });
192
+ });
193
+ });
194
+
195
+ describe('Mask with special input strings', function() {
196
+
197
+ const maskOptions = {
198
+ maskWith: "x",
199
+ maskOnlyFirstOccurance: true,
200
+ values: [],
201
+ maskAll: true,
202
+ maskSpace: true
203
+ };
204
+
205
+ describe('Mask with special input - input will generate an output', function() {
206
+
207
+ // first set with input generating an masked output
208
+ let testData = [
209
+ {
210
+ title: 'test with string length zero',
211
+ input: '',
212
+ output: ''
213
+ },
214
+ {
215
+ title: 'test input null',
216
+ input: null,
217
+ output: null
218
+ },
219
+ {
220
+ title: 'test input as number',
221
+ input: 12,
222
+ output: 'xx' // TODO - ok to convert number into string? Or should it fail?
223
+ },
224
+ ]
225
+
226
+ testData.forEach(({title, input, output}) => {
227
+ it(`special input - ${title}`, function() {
228
+ const masked = maskData.maskString(input, maskOptions);
229
+ expect(masked).to.equal(output, 'masked output does not match expected value');
230
+ });
231
+ });
232
+ });
233
+
234
+ // describe('Mask with special input - input shall throw error', function() {
235
+
236
+ // // set with input generating an error / exception
237
+ // let testData = [
238
+ // {
239
+ // title: 'test input as array',
240
+ // input: ['12', 'ab'],
241
+ // output: 'xx' // mask method seems to join all strings in the array into on big string and masks it?
242
+ // },
243
+ // {
244
+ // title: 'test input as object',
245
+ // input: {a: 'b', x: 'y'},
246
+ // output: '12' // json stringified and masked?
247
+ // }
248
+ // ]
249
+
250
+ // testData.forEach(({title, input, output}) => {
251
+ // it(`special input - ${title}`, function() {
252
+ // const masked = maskData.maskString(input, maskOptions);
253
+ // expect(masked).to.equal(output, 'masked output does not match expected value');
254
+ // });
255
+ // });
256
+ // });
257
+ });
258
+ });