maskdata 1.1.10 → 1.2.1

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.
@@ -0,0 +1,43 @@
1
+ 'use strict';
2
+ const MaskHelper = require('../helpers/MaskHelper');
3
+ const Constants = require('../helpers/Constants');
4
+
5
+ class PasswordMask {
6
+ static maskPassword(password, options) {
7
+ if(!password || typeof(password) === 'object') return password;
8
+ password = password + ''
9
+ if(options) {
10
+ options = MaskHelper.mapWithDefaultValues(options, Constants.defaultPasswordMaskOptions);
11
+ MaskHelper.validatePasswordOptions(options);
12
+ } else {
13
+ options = Constants.defaultPasswordMaskOptions;
14
+ }
15
+ let maskPasswordLength = password.length;
16
+ if(password.length > options.maxMaskedCharacters) {
17
+ maskPasswordLength = parseInt(options.maxMaskedCharacters);
18
+ }
19
+ const maskingCharacters = maskPasswordLength - options.unmaskedStartCharacters - options.unmaskedEndCharacters;
20
+
21
+ if(maskingCharacters < 0) {
22
+ if(maskPasswordLength <= options.unmaskedStartCharacters) {
23
+ return password.substring(0, maskPasswordLength);
24
+ } else {
25
+ let maskedPassword = password.substring(0, options.unmaskedStartCharacters);
26
+ const remainingChars = maskPasswordLength - options.unmaskedStartCharacters;
27
+ for(let i = password.length-remainingChars; i < password.length; i++) {
28
+ maskedPassword += password[i];
29
+ }
30
+ return maskedPassword;
31
+ }
32
+ }
33
+ let maskedPassword = "";
34
+ maskedPassword = password.substring(0, options.unmaskedStartCharacters);
35
+ maskedPassword += `${options.maskWith}`.repeat(maskingCharacters)
36
+ for(let i = password.length-options.unmaskedEndCharacters; i < password.length; i++) {
37
+ maskedPassword += password[i];
38
+ }
39
+ return maskedPassword;
40
+ }
41
+ }
42
+
43
+ module.exports = PasswordMask
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+ const MaskHelper = require('../helpers/MaskHelper');
3
+ const Constants = require('../helpers/Constants');
4
+
5
+ class PhoneMask {
6
+ static maskPhone(phone, options) {
7
+ if(!phone || typeof(phone) === 'object') return phone;
8
+ phone += '';
9
+ if(options) {
10
+ options = MaskHelper.mapWithDefaultValues(options, Constants.defaultPhoneMaskOptions);
11
+ MaskHelper.validatePhoneOptions(options);
12
+ } else {
13
+ options = Constants.defaultPhoneMaskOptions;
14
+ }
15
+ let maskLength = phone.length - options.unmaskedStartDigits - options.unmaskedEndDigits;
16
+ if((options.unmaskedStartDigits + options.unmaskedEndDigits) >= phone.length) {
17
+ return phone;
18
+ }
19
+ return phone.substring(0, options.unmaskedStartDigits) + `${options.maskWith}`.repeat(maskLength) + phone.substring(phone.length-options.unmaskedEndDigits);
20
+ }
21
+ }
22
+
23
+ module.exports = PhoneMask
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+ const MaskHelper = require('../helpers/MaskHelper');
3
+ const Constants = require('../helpers/Constants');
4
+
5
+ class StringMask {
6
+ static maskString(str, options) {
7
+ if(!str || typeof(str) === 'object') return str;
8
+ str = str + '';
9
+ if(options) {
10
+ options = MaskHelper.mapWithDefaultValues(options, Constants.defaultStringMaskOptions);
11
+ MaskHelper.validateStringOptions(options);
12
+ } else {
13
+ options = Constants.defaultStringMaskOptions;
14
+ }
15
+ let values = options.values;
16
+ if(options.maskAll === true) {
17
+ let result = '';
18
+ if(options.maskSpace === true) {
19
+ result = options.maskWith.repeat(str.length);
20
+ } else {
21
+ for(let eachChar of str) {
22
+ if(eachChar === ' ') {
23
+ result += ' ';
24
+ } else {
25
+ result += options.maskWith;
26
+ }
27
+ }
28
+ }
29
+ return result;
30
+ }
31
+ else if(options.maskOnlyFirstOccurance == true) {
32
+ for(const value of Object.values(values)) {
33
+ str = str.replace(value, `${options.maskWith}`.repeat(value.length));
34
+ }
35
+ } else {
36
+ for(const value of Object.values(values)) {
37
+ str = str.replace(new RegExp(value, 'g'), `${options.maskWith}`.repeat(value.length));
38
+ }
39
+ }
40
+ return str;
41
+ }
42
+ }
43
+
44
+ module.exports = StringMask
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ const MaskHelper = require('../helpers/MaskHelper');
4
+ const Constants = require('../helpers/Constants');
5
+ const uuidFormat = /[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}/g;
6
+
7
+ class UuidMask {
8
+ static maskUuid(uuid, options) {
9
+ if(!uuid || typeof(uuid) !== 'string' || !uuid.match(uuidFormat) || uuid.length !== 36) return uuid;
10
+ if(!options) {
11
+ options = Constants.defaultUuidMaskOptions;
12
+ } else {
13
+ options = MaskHelper.mapWithDefaultValues(options, Constants.defaultUuidMaskOptions);
14
+ MaskHelper.validateUuidMaskOptions(options);
15
+ }
16
+
17
+ const toBeProcessedUuid = uuid.replaceAll("-", "");
18
+
19
+ const start = toBeProcessedUuid.slice(0, options.unmaskedStartCharacters);
20
+ const end = toBeProcessedUuid.slice(32-options.unmaskedEndCharacters);
21
+ const maskLength = toBeProcessedUuid.length - options.unmaskedStartCharacters - options.unmaskedEndCharacters;
22
+ const tempMaskedUuid = start + (options.maskWith.repeat(maskLength)) + end;
23
+
24
+ let maskedUuid = tempMaskedUuid.slice(0, 8) + "-"
25
+ + tempMaskedUuid.slice(8, 12) + "-"
26
+ + tempMaskedUuid.slice(12, 16) + "-"
27
+ + tempMaskedUuid.slice(16, 20) + "-"
28
+ + tempMaskedUuid.slice(20);
29
+
30
+ return maskedUuid;
31
+ }
32
+ }
33
+
34
+ module.exports = UuidMask
package/package.json CHANGED
@@ -1,15 +1,16 @@
1
1
  {
2
2
  "name": "maskdata",
3
- "version": "1.1.10",
3
+ "version": "1.2.1",
4
4
  "description": "Node module to mask various kinds of data in the required formats",
5
5
  "main": "index.js",
6
+ "typings": "index.d.ts",
6
7
  "repository": {
7
8
  "type": "git",
8
9
  "url": "https://github.com/Sumukha1496/maskdata"
9
10
  },
10
11
  "scripts": {
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
+ "test": "mocha tests/test*.js --timeout 5000 --exit",
13
+ "coverage": "nyc --reporter=html --reporter=text npm run test"
13
14
  },
14
15
  "keywords": [
15
16
  "mask",
@@ -28,15 +29,17 @@
28
29
  "Set json fields",
29
30
  "Replcae json fields"
30
31
  ],
31
- "author": "sumukha.s<sumukha.hs369@gmail.com>",
32
+ "author": "sumukha.hs<sumukha.hs369@gmail.com>",
33
+ "homepage": "https://github.com/Sumukha1496/maskdata#readme",
32
34
  "license": "MIT",
33
35
  "dependencies": {
34
36
  "lodash": "^4.17.21"
35
37
  },
36
38
  "devDependencies": {
37
- "chai": "^4.3.6",
38
- "mocha": "^9.2.2",
39
- "mocha-junit-reporter": "^2.0.2",
40
- "nyc": "^15.1.0"
39
+ "chai": "^4.3.7",
40
+ "mocha": "^10.2.0",
41
+ "mocha-junit-reporter": "^2.2.0",
42
+ "nyc": "^15.1.0",
43
+ "typescript": "^4.9.5"
41
44
  }
42
45
  }
@@ -1,4 +1,4 @@
1
- const MaskData = require('./index');
1
+ const MaskData = require('../index');
2
2
 
3
3
  const passwordMaskOptions = {
4
4
  maxMaskedCharacters: 10,
@@ -203,3 +203,86 @@ const maskCardOptions = {
203
203
  const obj = { cardNumber: "123456789123" ,abc: "abceew" ,efg: "121212" } ;
204
204
  const result = MaskData.replaceValue(obj, 'cardNumber', MaskData.maskCard(obj['cardNumber'], maskCardOptions));
205
205
  console.log(result);
206
+
207
+ const jsonInput = {
208
+ 'credit': '1234-5678-8765-1234',
209
+ 'debit': '0000-1111-2222-3333',
210
+ 'primaryEmail': 'primary@Email.com',
211
+ 'secondaryEmail': 'secondary@Email.com',
212
+ 'password': 'dummyPassword',
213
+ 'homePhone': "+1 1234567890",
214
+ 'workPhone': "+1 9876543210",
215
+ 'addressLine1': "This is my addressline 1. This is my home",
216
+ 'addressLine2': "AddressLine 2",
217
+ 'uuid1': '123e4567-e89b-12d3-a456-426614174000'
218
+ };
219
+
220
+ const jsonMaskConfig = {
221
+ cardFields: ['credit', 'debit'],
222
+ emailFields: ['primaryEmail', 'secondaryEmail'],
223
+ passwordFields: ['password'],
224
+ phoneFields: ['homePhone', 'workPhone'],
225
+ stringMaskOptions: {
226
+ maskWith: "*",
227
+ maskOnlyFirstOccurance: false,
228
+ values: ["This"]
229
+ },
230
+ stringFields: ['addressLine1', 'addressLine2'],
231
+ uuidFields: ['uuid1']
232
+ };
233
+
234
+ const maskedJsonOutput = MaskData.maskJSON2(jsonInput, jsonMaskConfig);
235
+ console.log(maskedJsonOutput);
236
+
237
+
238
+ const jsonInput2 = {
239
+ cards: {
240
+ creditCards: ['1234-5678-8765-1234', '1111-2222-1111-2222'],
241
+ debitCards: ['0000-1111-2222-3333', '2222-1111-3333-4444']
242
+ },
243
+ emails: {
244
+ primaryEmail: 'primary@Email.com',
245
+ secondaryEmail: 'secondary@Email.com'
246
+ },
247
+ passwords: [['dummyPasswordANDdummyPassword', 'dummyPasswordANDdummyPassword']],
248
+ phones: {
249
+ homePhone: "+1 1234567890",
250
+ workPhone: "+1 9876543210",
251
+ },
252
+ address: {
253
+ addressLine1: "This is my addressline 1. This is my home",
254
+ addressLine2: "AddressLine 2"
255
+ },
256
+ uuids: {
257
+ uuid1: '123e4567-e89b-12d3-a456-426614174000'
258
+ }
259
+ };
260
+
261
+ const jsonMaskConfig2 = {
262
+ // Card
263
+ cardMaskOptions: { maskWith: "X", unmaskedStartDigits: 2,unmaskedEndDigits: 4 },
264
+ cardFields: ['cards.creditCards[0]', 'cards.creditCards[1]', 'cards.debitCards[0]', 'cards.debitCards[1]'],
265
+
266
+ // Email
267
+ emailMaskOptions: { maskWith: "*", unmaskedStartCharactersBeforeAt: 2, unmaskedEndCharactersAfterAt: 2, maskAtTheRate: false },
268
+ emailFields: ['emails.primaryEmail', 'emails.secondaryEmail'],
269
+
270
+ // Password
271
+ passwordMaskOptions: { maskWith: "*", maxMaskedCharacters: 10, unmaskedStartCharacters: 0, unmaskedEndCharacters: 0 },
272
+ passwordFields: ['passwords[0][0]]', 'passwords[0][1]'],
273
+
274
+ // Phone
275
+ phoneMaskOptions: { maskWith: "*", unmaskedStartDigits: 2, unmaskedEndDigits: 1 },
276
+ phoneFields: ['phones.homePhone', 'phones.workPhone'],
277
+
278
+ // String
279
+ stringMaskOptions: { maskWith: "*", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
280
+ stringFields: ['address.addressLine1', 'address.addressLine2'],
281
+
282
+ // UUID
283
+ uuidMaskOptions: { maskWith: "*", unmaskedStartCharacters: 4, unmaskedEndCharacters: 2 },
284
+ uuidFields: ['uuids.uuid1']
285
+ };
286
+
287
+ const maskedJsonOutput2 = MaskData.maskJSON2(jsonInput2, jsonMaskConfig2);
288
+ console.log(maskedJsonOutput2);
package/tests/testCard.js CHANGED
@@ -95,6 +95,30 @@ describe('Masking card numbers', function() {
95
95
  const masked = maskData.maskCard(input, negativeEndOption);
96
96
  expect(masked).to.equal(output, 'masked output does not match expected value');
97
97
  });
98
+
99
+ it('test with 0 unmasked start and end digits', function() {
100
+ const _0UnmaskedOption = {
101
+ maskWith: 'x',
102
+ unmaskedStartDigits: 0,
103
+ unmaskedEndDigits: 0
104
+ }
105
+ const input = '1234-5678-1234-5678'
106
+ const output = 'xxxx-xxxx-xxxx-xxxx'
107
+ const masked = maskData.maskCard(input, _0UnmaskedOption);
108
+ expect(masked).to.equal(output);
109
+ });
110
+
111
+ it('test with non number card data', function() {
112
+ const cardMaskOptions = {
113
+ maskWith: 'x',
114
+ unmaskedStartDigits: 1,
115
+ unmaskedEndDigits: 1
116
+ }
117
+ const input = 'm1234-5678-1234-5678'
118
+ const output = 'm1xxx-xxxx-xxxx-xxx8'
119
+ const masked = maskData.maskCard(input, cardMaskOptions);
120
+ expect(masked).to.equal(output);
121
+ });
98
122
  });
99
123
 
100
124
  describe('Mask with special input strings', function() {
@@ -123,36 +147,7 @@ describe('Masking card numbers', function() {
123
147
 
124
148
  testData.forEach(({title, input, output}) => {
125
149
  it(`special input - ${title}`, function() {
126
- const masked = maskData.maskPassword(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
- 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.maskPassword(input, maskOptions);
150
+ const masked = maskData.maskCard(input, maskOptions);
156
151
  expect(masked).to.equal(output, 'masked output does not match expected value');
157
152
  });
158
153
  });
@@ -197,37 +197,33 @@ 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.maskPassword(input, maskOptions);
200
+ const masked = maskData.maskEmail2(input, maskOptions);
201
201
  expect(masked).to.equal(output, 'masked output does not match expected value');
202
202
  });
203
203
  });
204
204
  });
205
205
 
206
- describe('Mask with special input - input shall throw error', function() {
206
+ describe('Mask with non string input - input will not be masked', function() {
207
207
 
208
- // set with input generating an error / exception
209
208
  let testData = [
210
209
  {
211
210
  title: 'test input as number',
212
211
  input: 12,
213
- output: '12'
214
212
  },
215
213
  {
216
214
  title: 'test input as array',
217
215
  input: ['12'],
218
- output: '12'
219
216
  },
220
217
  {
221
218
  title: 'test input as object',
222
219
  input: {a: 'b', x: 'y'},
223
- output: '12'
224
220
  }
225
221
  ]
226
222
 
227
223
  testData.forEach(({title, input, output}) => {
228
224
  it(`special input - ${title}`, function() {
229
- const masked = maskData.maskPassword(input, maskOptions);
230
- expect(masked).to.equal(output, 'masked output does not match expected value');
225
+ const masked = maskData.maskEmail2(input, maskOptions);
226
+ expect(masked).to.equal(input, 'Improper input');
231
227
  });
232
228
  });
233
229
  });