maskdata 1.1.8 → 1.1.10

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.8 Features](#release-features)
7
+ - [Version 1.1.10 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)
@@ -37,6 +37,9 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
37
37
  > npm i maskdata
38
38
 
39
39
  # Release Features
40
+ ### Version: 1.1.10
41
+ - Support of maxMaskedCharacters in masking json fields: https://github.com/Sumukha1496/maskdata/issues/21
42
+ - This is applicable only if the type of value is string. Details: [Mask JSON fields](#mask-fields-in-a-json)
40
43
  ### Version: 1.1.8
41
44
  - Mocha test cases for most of the usecases.
42
45
  https://github.com/Sumukha1496/maskdata/pull/23
@@ -303,7 +306,10 @@ const maskJSONOptions = {
303
306
 
304
307
  // It should be an array
305
308
  // Field names to mask. Can give multiple fields.
306
- fields: ['password', 'firstName']
309
+ fields: ['password', 'firstName'],
310
+
311
+ // If the type of value is string and if we want to limit the number of characters in the output, then we can use this. Default value is -1 means, by default this feature is disabled. It is enabled if we provide positive integer value. This is not a mandatory field in the config and if not provided, it will take the default value -1 and disable the feature.
312
+ maxMaskedCharactersStr: 4 // Default value is -1 (Disable feature)
307
313
  };
308
314
 
309
315
  const obj = {
@@ -311,9 +317,10 @@ const obj = {
311
317
  firstName: "Jon",
312
318
  lastName: "Snoww"
313
319
  };
320
+
314
321
  const maskedObj = MaskData.maskJSONFields(obj, maskJSONOptions);
315
322
 
316
- //Output: { password: '************', firstName: '***', lastName: 'Snoww' }
323
+ //Output: { password: '****', firstName: '***', lastName: 'Snoww' }
317
324
 
318
325
  ```
319
326
 
@@ -389,7 +396,7 @@ Limititions:
389
396
  2. It will not work for the nested fields like ARRAY[*].FIELD1.FIELD2
390
397
  3. It will not mask all array elements if given ARRAY[*] or ARRAY[*].
391
398
  4. Will not mask null values.
392
- 5. If ARRAY[*].FIELD is an object, then it will mask that entire object include key.
399
+ 5. If ARRAY[*].FIELD is an object, then it will mask that entire object including the key.
393
400
 
394
401
  const nestedObject = {
395
402
  level1: {
package/index.js CHANGED
@@ -13,7 +13,8 @@ const defaultPhoneMaskOptions = {
13
13
 
14
14
  const defaultJsonMaskOptions = {
15
15
  maskWith: "*",
16
- fields: []
16
+ fields: [],
17
+ maxMaskedCharactersStr: -1
17
18
  };
18
19
 
19
20
  const defaultPasswordMaskOptions = {
@@ -115,8 +116,14 @@ class MaskData {
115
116
  const arrayValue = get(maskedObj, arrayFieldName);
116
117
  for(const arrayElement of arrayValue) {
117
118
  const value = arrayElement[subField];
118
- if(value !== undefined && value !== null) {
119
- set(arrayElement, subField, (`${options.maskWith}`.repeat(value.toString().length)))
119
+ if(value === undefined || value === null) {
120
+ continue;
121
+ } else {
122
+ if(typeof(value) == 'string' && options.maxMaskedCharactersStr != -1 && options.maxMaskedCharactersStr < value.length) {
123
+ set(arrayElement, subField, (`${options.maskWith}`.repeat(options.maxMaskedCharactersStr)))
124
+ } else {
125
+ set(arrayElement, subField, (`${options.maskWith}`.repeat(value.toString().length)));
126
+ }
120
127
  }
121
128
  }
122
129
  } else if(field.includes('.*')) {
@@ -124,15 +131,26 @@ class MaskData {
124
131
  const innerObject = get(maskedObj, subField);
125
132
  for(const innerObjectField of Object.keys(innerObject)) {
126
133
  const value = innerObject[innerObjectField];
127
- if(value !== undefined && value !== null) {
128
- set(innerObject, innerObjectField, (`${options.maskWith}`.repeat(value.toString().length)))
134
+ if(value === undefined || value === null) {
135
+ continue;
136
+ } else {
137
+ if(typeof(value) == 'string' && options.maxMaskedCharactersStr != -1 && options.maxMaskedCharactersStr < value.length) {
138
+ set(innerObject, innerObjectField, (`${options.maskWith}`.repeat(options.maxMaskedCharactersStr)))
139
+ } else {
140
+ set(innerObject, innerObjectField, (`${options.maskWith}`.repeat(value.toString().length)));
141
+ }
129
142
  }
130
143
  }
131
144
  } else {
132
- const value = get(maskedObj, field);
133
- if(value !== undefined && value !== null) {
134
- set(maskedObj, field, (`${options.maskWith}`.repeat(value.toString().length)))
135
- }
145
+ const value = get(maskedObj, field);
146
+ if(value === undefined || value === null) {
147
+ continue;
148
+ }
149
+ if(typeof(value) == 'string' && options.maxMaskedCharactersStr != -1 && options.maxMaskedCharactersStr < value.length) {
150
+ set(maskedObj, field, (`${options.maskWith}`.repeat(options.maxMaskedCharactersStr)))
151
+ } else {
152
+ set(maskedObj, field, (`${options.maskWith}`.repeat(value.toString().length)))
153
+ }
136
154
  }
137
155
  } catch(ex) { continue; }
138
156
  }
@@ -10,7 +10,6 @@ const defaultEmailMask2Options = {
10
10
  class EmailMask {
11
11
 
12
12
  static maskEmail2(email, options) {
13
- if(!email) return email;
14
13
  let maskedEmail = '';
15
14
  options = options ? MaskHelper.mapWithDefaultValues(options, defaultEmailMask2Options) : defaultEmailMask2Options;
16
15
  MaskHelper.validateEmailOptions2(email, options);
@@ -149,6 +149,9 @@ class MaskHelper {
149
149
  if((!options.maskWith) || options.maskWith.toString().length <= 0) {
150
150
  options.maskWith = '*';
151
151
  }
152
+ if(!options.maxMaskedCharactersStr || typeof(options.maxMaskedCharactersStr) !== 'number' || options.maxMaskedCharactersStr <= 0) {
153
+ options.maxMaskedCharactersStr = -1;
154
+ }
152
155
  if(reasons.length > 0) {
153
156
  const badOptions = new BadOption('JSON mask configuration', reasons);
154
157
  badOptions.throwBadOptionException();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maskdata",
3
- "version": "1.1.8",
3
+ "version": "1.1.10",
4
4
  "description": "Node module to mask various kinds of data in the required formats",
5
5
  "main": "index.js",
6
6
  "repository": {
package/test.js CHANGED
@@ -53,7 +53,8 @@ console.log(`shortEmail after masking: ${MaskData.maskEmail2(shortEmail, emailMa
53
53
  console.log("========================================");
54
54
 
55
55
  const jsonMaskOptions = {
56
- fields: ['password', 'firstName']
56
+ fields: ['password', 'firstName'],
57
+ maxMaskedCharactersStr: 3
57
58
  };
58
59
  const json = {
59
60
  password: "PasswordValue",
@@ -78,7 +79,7 @@ const nestedAllObject = {
78
79
  field1: "field1Value",
79
80
  level2: {
80
81
  field2: "field2Value",
81
- field3: [ { Hello: "Hello", Hi: "one" }, { Hello: "Hello again" } ],
82
+ field3: [ { Hello: "HelloValue", Hi: "one" }, { Hello: "Hello again" } ],
82
83
  level3: {
83
84
  field4: "field4Value",
84
85
  field5: "field5Value"
package/tests/testCard.js CHANGED
@@ -123,7 +123,7 @@ describe('Masking card numbers', function() {
123
123
 
124
124
  testData.forEach(({title, input, output}) => {
125
125
  it(`special input - ${title}`, function() {
126
- const masked = maskData.maskCard(input, maskOptions);
126
+ const masked = maskData.maskPassword(input, maskOptions);
127
127
  expect(masked).to.equal(output, 'masked output does not match expected value');
128
128
  });
129
129
  });
@@ -142,12 +142,17 @@ describe('Masking card numbers', function() {
142
142
  title: 'test input as array',
143
143
  input: ['12'],
144
144
  output: '12'
145
+ },
146
+ {
147
+ title: 'test input as object',
148
+ input: {a: 'b', x: 'y'},
149
+ output: '12'
145
150
  }
146
151
  ]
147
152
 
148
153
  testData.forEach(({title, input, output}) => {
149
154
  it(`special input - ${title}`, function() {
150
- const masked = maskData.maskCard(input, maskOptions);
155
+ const masked = maskData.maskPassword(input, maskOptions);
151
156
  expect(masked).to.equal(output, 'masked output does not match expected value');
152
157
  });
153
158
  });
@@ -197,7 +197,36 @@ describe('Masking email addresses', function() {
197
197
 
198
198
  testData.forEach(({title, input, output}) => {
199
199
  it(`special input - ${title}`, function() {
200
- const masked = maskData.maskEmail2(input, maskOptions);
200
+ const masked = maskData.maskPassword(input, maskOptions);
201
+ expect(masked).to.equal(output, 'masked output does not match expected value');
202
+ });
203
+ });
204
+ });
205
+
206
+ describe('Mask with special input - input shall throw error', function() {
207
+
208
+ // set with input generating an error / exception
209
+ let testData = [
210
+ {
211
+ title: 'test input as number',
212
+ input: 12,
213
+ output: '12'
214
+ },
215
+ {
216
+ title: 'test input as array',
217
+ input: ['12'],
218
+ output: '12'
219
+ },
220
+ {
221
+ title: 'test input as object',
222
+ input: {a: 'b', x: 'y'},
223
+ output: '12'
224
+ }
225
+ ]
226
+
227
+ testData.forEach(({title, input, output}) => {
228
+ it(`special input - ${title}`, function() {
229
+ const masked = maskData.maskPassword(input, maskOptions);
201
230
  expect(masked).to.equal(output, 'masked output does not match expected value');
202
231
  });
203
232
  });
@@ -56,7 +56,7 @@ describe('Masking password', function() {
56
56
  output: '1xxx78'
57
57
  },
58
58
  {
59
- title: 'test string length equal to start + end',
59
+ title: 'test string length equal than start + end',
60
60
  input: '12-345',
61
61
  output: '1xxx45'
62
62
  },
@@ -88,19 +88,22 @@ describe('Masking password', function() {
88
88
  });
89
89
 
90
90
  it('custom mask - test with maxMasked shorter than start + end', function() {
91
+ // TODO - should this throw an error? there is no masking at all
91
92
  const shortMaxCharsOption = {
92
93
  maskWith: 'x',
93
94
  maxMaskedCharacters: 6,
94
- unmaskedStartCharacters: 4, // 4+4=8 > 6 --> But this will give more preference to maxMaskedCharacters
95
+ unmaskedStartCharacters: 4, // 4+4=8 > 6
95
96
  unmaskedEndCharacters: 4
96
97
  }
97
98
  const input = '1234-5678-1234-5678'
98
99
  const output = '123478'
99
100
  const masked = maskData.maskPassword(input, shortMaxCharsOption);
100
101
  expect(masked).to.equal(output, 'masked output does not match expected value');
102
+ expect.fail('Shall this test fail because options prevent full masking?')
101
103
  });
102
104
 
103
- it('custom mask - test with maxMasked equal to start + end', function() {
105
+ it('custom mask - test with maxMasked equal than start + end', function() {
106
+ // TODO - should this throw an error? there is no masking at all
104
107
  const shortMaxCharsOption = {
105
108
  maskWith: 'x',
106
109
  maxMaskedCharacters: 8,
@@ -111,6 +114,7 @@ describe('Masking password', function() {
111
114
  const output = '12345678'
112
115
  const masked = maskData.maskPassword(input, shortMaxCharsOption);
113
116
  expect(masked).to.equal(output, 'masked output does not match expected value');
117
+ expect.fail('Shall this test fail because options prevent full masking?')
114
118
  });
115
119
  });
116
120
 
@@ -138,6 +142,36 @@ describe('Masking password', function() {
138
142
  output: null
139
143
  }
140
144
  ]
145
+
146
+ testData.forEach(({title, input, output}) => {
147
+ it(`special input - ${title}`, function() {
148
+ const masked = maskData.maskPassword(input, maskOptions);
149
+ expect(masked).to.equal(output, 'masked output does not match expected value');
150
+ });
151
+ });
152
+ });
153
+
154
+ describe('Mask with special input - input shall throw error', function() {
155
+
156
+ // set with input generating an error / exception
157
+ let testData = [
158
+ {
159
+ title: 'test input as number',
160
+ input: 12,
161
+ output: '12'
162
+ },
163
+ {
164
+ title: 'test input as array',
165
+ input: ['12'],
166
+ output: '12'
167
+ },
168
+ {
169
+ title: 'test input as object',
170
+ input: {a: 'b', x: 'y'},
171
+ output: '12'
172
+ }
173
+ ]
174
+
141
175
  testData.forEach(({title, input, output}) => {
142
176
  it(`special input - ${title}`, function() {
143
177
  const masked = maskData.maskPassword(input, maskOptions);
@@ -118,7 +118,12 @@ describe('Masking phone numbers', function() {
118
118
  title: 'test input null',
119
119
  input: null,
120
120
  output: null
121
- }
121
+ },
122
+ {
123
+ title: 'test input as number',
124
+ input: 1234567,
125
+ output: 'xx' // TODO - ok to convert number into string? Or should it fail?
126
+ },
122
127
  ]
123
128
 
124
129
  testData.forEach(({title, input, output}) => {
@@ -129,28 +134,28 @@ describe('Masking phone numbers', function() {
129
134
  });
130
135
  });
131
136
 
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
- // });
137
+ describe('Mask with special input - input shall throw error', function() {
138
+
139
+ // set with input generating an error / exception
140
+ let testData = [
141
+ {
142
+ title: 'test input as array',
143
+ input: ['12', '34'],
144
+ output: 'xx'
145
+ },
146
+ {
147
+ title: 'test input as object',
148
+ input: {a: 'b', x: 'y'},
149
+ output: '12'
150
+ }
151
+ ]
152
+
153
+ testData.forEach(({title, input, output}) => {
154
+ it(`special input - ${title}`, function() {
155
+ const masked = maskData.maskPhone(input, maskOptions);
156
+ expect(masked).to.equal(output, 'masked output does not match expected value');
157
+ });
158
+ });
159
+ });
155
160
  });
156
161
  });
@@ -2,34 +2,8 @@
2
2
  const maskData = require('../index');
3
3
  const expect = require('chai').expect;
4
4
 
5
- describe('Test Replace value', function() {
5
+ describe('Test replaceValue', function() {
6
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
- });
7
+ // TODO - migrate all tests from test.js into mocha tests to fail on error
8
+ // TODO - extend tests
35
9
  });
@@ -231,28 +231,28 @@ describe('Masking strings', function() {
231
231
  });
232
232
  });
233
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
- // });
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
257
  });
258
258
  });