maskdata 1.2.5 → 1.3.0

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.
@@ -17,7 +17,6 @@ const options: PasswordMaskOptions = {
17
17
  };
18
18
 
19
19
  const maskedPassword = maskPassword(password, options);
20
- console.log(maskedPassword);
21
20
  ```
22
21
  <br/>
23
22
 
@@ -85,9 +84,25 @@ Usage: [UUID Masking](./README.md#mask-uuid)
85
84
  }
86
85
  ```
87
86
 
88
- ### 7. JsonMask2Configs
89
- Usage: [JSON fields masking](./README.md#mask-json)
87
+ ### 7. StringMaskV2Options
88
+ Usage: [Generic String Masking](./README.md#generic-string-masking)
90
89
  ```javascript
90
+ {
91
+ maskWith?: string;
92
+ maxMaskedCharacters?: number;
93
+ unmaskedStartCharacters?: number;
94
+ unmaskedEndCharacters?: number;
95
+ }
96
+ ```
97
+
98
+ ### 8. JsonMask2Configs
99
+ Usage: [JSON fields masking](./README.md#mask-json)
100
+ ```typescript
101
+ interface GenericStringMaskOptions {
102
+ config?: StringMaskV2Options;
103
+ fields: string[];
104
+ }
105
+
91
106
  {
92
107
  cardMaskOptions?: CardMaskOptions;
93
108
  cardFields?: string[];
@@ -103,22 +118,24 @@ Usage: [JSON fields masking](./README.md#mask-json)
103
118
  uuidFields?: string[];
104
119
  jwtMaskOptions?: JwtMaskOptions;
105
120
  jwtFields?: string[];
121
+ genericStrings?: GenericStringMaskOptions[]
106
122
  }
107
123
  ```
108
- ### 8. Mask JWT
124
+ ### 9. Mask JWT
109
125
  Usage: [JWT masking](./README.md#mask-jwt-token)
110
- const defaultJwtMaskOptions = {
111
- maskWith: '*',
112
- maxMaskedCharacters: 512,
113
- maskDot: true,
114
- maskHeader: true,
115
- maskPayload: true,
116
- maskSignature: true
117
- };
118
-
126
+ ```javascript
127
+ {
128
+ maskWith?: string;
129
+ maxMaskedCharacters?: number;
130
+ maskDot?: boolean;
131
+ maskHeader?: boolean;
132
+ maskPayload?: boolean;
133
+ maskSignature?: boolean;
134
+ }
135
+ ```
119
136
  <br/>
120
137
 
121
- ## Available functions to import along with their configs
138
+ ## Available imports
122
139
  ```javascript
123
- import { maskPassword, PasswordMaskOptions, maskJSON2, JsonMask2Configs, maskPhone, PhoneMaskOptions, maskEmail2, EmailMask2Options, maskCard, CardMaskOptions, maskString, StringMaskOptions, maskUuid, UuidMaskOptions, maskJwt, JwtMaskOptions, getInnerProperty, replaceValue } from 'maskdata';
140
+ import { maskPassword, PasswordMaskOptions, maskJSON2, JsonMask2Configs, maskPhone, PhoneMaskOptions, maskEmail2, EmailMask2Options, maskCard, CardMaskOptions, maskString, StringMaskOptions, maskUuid, UuidMaskOptions, maskJwt, JwtMaskOptions, maskStringV2, GenericStringMaskOptions, StringMaskV2Options, getInnerProperty, replaceValue } from 'maskdata';
124
141
  ```
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.2.5 Features](#release-features)
7
+ - [Version 1.3.0 Features](#release-features)
8
8
  - [How to Use](#how-to-use)
9
9
  - [Maskdata for Typescript](#maskdata-for-typescript)
10
10
  - [Mask Card number](#mask-card-number)
@@ -17,6 +17,7 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
17
17
  - [Mask Password with the default configuration](#mask-password-with-the-default-configuration)
18
18
  - [Mask Phone Number](#mask-phone-number)
19
19
  - [Mask Phone Number with the default configuration](#mask-phone-number-with-the-default-configuration)
20
+ - [Generic String masking](#generic-string-masking)
20
21
  - [Mask words/characters in a string](#mask-the-characters-or-words-in-the-string)
21
22
  - [Mask UUID](#mask-uuid)
22
23
  - [Mask JWT Token](#mask-jwt-token)
@@ -43,7 +44,14 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
43
44
  > npm i maskdata
44
45
 
45
46
  # Release Features
46
- ### Version 1.2.5
47
+ ### Version 1.3.0
48
+ - Removal of maskJSONFields function: https://www.npmjs.com/package/maskdata/v/1.1.10#mask-fields-in-a-json
49
+ - String mask version2 for generic string masking in response to the below issues: [Details](#generic-string-masking)
50
+ - https://github.com/Sumukha1496/maskdata/issues/17
51
+ - https://github.com/Sumukha1496/maskdata/issues/40
52
+ - https://github.com/Sumukha1496/maskdata/issues/42
53
+ - Supporting generic string masking(string mask V2) as part of maskJson2: [generic String masking in a JSON](#json-mask-examples)
54
+ ### Version 1.2.6
47
55
  - JWT token masking: Mask JWT tokens with configs to mask as per your need. More details: [Mask JWT Token](#mask-jwt-token)
48
56
  - Mask Json now supports JWT token masking also. More details: [Mask JWT in a JSON](#mask-json)
49
57
  - Bug fix in maskJson2 where it was not checking for the empty/null/undefined fields and was resulting in error `"TypeError: validatedConfig[typeToFunctionMap[key][1]] is not iterable"`
@@ -51,15 +59,6 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
51
59
  ### Version 1.2.3
52
60
  - Bug fix for masking a list of elements in the nested json. More details: [Mask multiple fields](#mask-multiple-fields)
53
61
  - Deprecated *maskJsonFields*(For documentation on the maskJsonFields, check previous version README.md files) function and will be removed in the subsequent versions. Use *maksJson2* instead: https://www.npmjs.com/package/maskdata#mask-json
54
- ### Version 1.2.2
55
- - Type definitions for the typescript developers. Yay!!!
56
- But there is a small change in how you import and use the library. Follow this to know how you can import and use the masking functions in a typescript project: [Mask data for Typescript](#maskdata-for-typescript)
57
- ### Version 1.2.0
58
- - Support for masking cards, emails, passwords, phones, strings, and UUIDs. Mask all of them with a single call using - [Mask JSON fields](#mask-json)
59
- - Support for masking UUID - [Mask UUID](#mask-uuid)
60
- - Minor bug fix in card masking.
61
- - Extensive testing with the help of mocha test cases for all features covering more than 110 test cases.
62
- - Improved Documentation
63
62
 
64
63
  # How to Use
65
64
  ```javascript
@@ -171,7 +170,7 @@ const maskedEmail = MaskData.maskEmail2(email);
171
170
  ```
172
171
 
173
172
  ## Mask JSON
174
- This is the new functionality in version 1.2.0 to handle the masking of multiple types of data in the JSON object with a single mask function call. Use this function instead of the previous function maskJsonFields(). <br><br>To mask with the default options, just pass the required fields(cardFields[] and/or emailFields[] and/or passwordFields[] and/or phoneFields[] and/or stringFields[] and/or uuidFields[]) in the second argument to the function.
173
+ This is the new functionality in version 1.2.0+ to handle masking of multiple types of data in the JSON object with a single mask function call.
175
174
 
176
175
  ```javascript
177
176
  const MaskData = require('./maskdata');
@@ -195,13 +194,20 @@ const defaultjsonMask2Configs = {
195
194
  stringFields: [], // List of String fields to be masked
196
195
 
197
196
  uuidMaskOptions: defaultUuidMaskOptions, // Optional
198
- uuidFields: [] // List of UUID fields to be masked
197
+ uuidFields: [], // List of UUID fields to be masked
199
198
 
200
199
  jwtMaskOptions: defaultJwtMaskOptions, // Optional
201
- jwtFields: [] // List of JWT fields to be masked
200
+ jwtFields: [], // List of JWT fields to be masked
201
+
202
+ genericStrings: [
203
+ {
204
+ config: defaultStringMaskV2Options,
205
+ fields: []
206
+ }
207
+ ]
202
208
  };
203
209
  ```
204
- <b>NOTE: For defaultCardMaskOptions, defaultEmailMask2Options, defaultPasswordMaskOptions, defaultPhoneMaskOptions, defaultStringMaskOptions and defaultUuidMaskOptions refer the corresponding masking features. </b>
210
+ > NOTE: For details on the configs mentioned above, refer: <br /> [defaultCardMaskOptions](#mask-card-number)<br/>[defaultEmailMask2Options](#mask-email-id)<br/> [defaultPasswordMaskOptions](#mask-password)<br/> [defaultPhoneMaskOptions](#mask-phone-number)<br/> [defaultStringMaskOptions](#mask-the-characters-or-words-in-the-string)<br/> [defaultUuidMaskOptions](#mask-uuid)<br /> [defaultJwtMaskOptions](#mask-jwt-token)<br /> [defaultStringMaskV2Options / generic String Maksing](#generic-string-masking)
205
211
 
206
212
  ```javascript
207
213
  const defaultjsonMask2Configs = {
@@ -249,7 +255,29 @@ const defaultjsonMask2Configs = {
249
255
  unmaskedStartCharacters: 0,
250
256
  unmaskedEndCharacters: 0
251
257
  },
252
- uuidFields: []
258
+ uuidFields: [],
259
+
260
+ jwtMaskOptions: {
261
+ maskWith: '*',
262
+ maxMaskedCharacters: 512,
263
+ maskDot: true,
264
+ maskHeader: true,
265
+ maskPayload: true,
266
+ maskSignature: true
267
+ },
268
+ jwtFields: [],
269
+ // To extend the mask function to other types of data.
270
+ genericStrings: [
271
+ {
272
+ config: {
273
+ maskWith: "*",
274
+ maxMaskedCharacters: 256,
275
+ unmaskedStartDigits: 0,
276
+ unmaskedEndDigits: 0
277
+ },
278
+ fields: []
279
+ }
280
+ ]
253
281
  };
254
282
 
255
283
  ```
@@ -267,7 +295,16 @@ const jsonInput = {
267
295
  'workPhone': "+1 9876543210",
268
296
  'addressLine1': "This is my addressline 1. This is my home",
269
297
  'addressLine2': "AddressLine 2",
270
- 'uuid1': '123e4567-e89b-12d3-a456-426614174000'
298
+ 'uuid1': '123e4567-e89b-12d3-a456-426614174000',
299
+ 'randomStrings': {
300
+ 'row1': 'This is row 1 random string',
301
+ 'row2': ['Entry1', 'Entry2', 'Entry3'],
302
+ 'row3': {
303
+ 'key1': 'Row3 Object 1',
304
+ 'key2': 'Row3 Object 2',
305
+ 'key3': ['Entry1', 'Entry2', 'Entry3']
306
+ }
307
+ }
271
308
  };
272
309
 
273
310
  const jsonMaskConfig = {
@@ -281,13 +318,29 @@ const jsonMaskConfig = {
281
318
  values: ["This"]
282
319
  },
283
320
  stringFields: ['addressLine1', 'addressLine2'],
284
- uuidFields: ['uuid1']
321
+ uuidFields: ['uuid1'],
322
+ genericStrings: [
323
+ {
324
+ fields: ['randomStrings.row1'],
325
+ config: {
326
+ maskWith: '*',
327
+ unmaskedStartCharacters: 2,
328
+ unmaskedEndCharacters: 3,
329
+ maxMaskedCharacters: 8
330
+ }
331
+ },
332
+ { fields: ['randomStrings.row2.*'], config: { maskWith: 'X', unmaskedEndCharacters: 1 } },
333
+ { fields: ['randomStrings.row3.key1'] },
334
+ {
335
+ fields: ['randomStrings.row3.key3.*'],
336
+ config: { maskWith: '@', unmaskedEndCharacters: 1 }
337
+ }
338
+ ]
285
339
  };
286
340
 
287
341
  const maskedJsonOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
288
342
 
289
343
  Output:
290
-
291
344
  {
292
345
  credit: '1234-****-****-***4',
293
346
  debit: '0000-****-****-***3',
@@ -298,7 +351,16 @@ Output:
298
351
  workPhone: '+1 9********0',
299
352
  addressLine1: '**** is my addressline 1. **** is my home',
300
353
  addressLine2: 'AddressLine 2',
301
- uuid1: '********-****-****-****-************'
354
+ uuid1: '********-****-****-****-************',
355
+ randomStrings: {
356
+ row1: 'Th***ing',
357
+ row2: ['XXXXX1', 'XXXXX2', 'XXXXX3'],
358
+ row3: {
359
+ key1: '*************',
360
+ key2: 'Row3 Object 2',
361
+ key3: ['@@@@@1', '@@@@@2', '@@@@@3']
362
+ }
363
+ }
302
364
  }
303
365
 
304
366
 
@@ -387,7 +449,8 @@ const jsonInput2 = {
387
449
  },
388
450
  uuids: {
389
451
  uuid1: '123e4567-e89b-12d3-a456-426614174000'
390
- }
452
+ },
453
+ jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ'
391
454
  };
392
455
 
393
456
  const jsonMaskConfig2 = {
@@ -414,6 +477,10 @@ const jsonMaskConfig2 = {
414
477
  // UUID
415
478
  uuidMaskOptions: { maskWith: "*", unmaskedStartCharacters: 4, unmaskedEndCharacters: 2 },
416
479
  uuidFields: ['uuids.uuid1']
480
+
481
+ // JWT
482
+ jwtMaskOptions: { maskWith: '*', maxMaskedCharacters: 512, maskDot: true, maskHeader: true, maskPayload: true, maskSignature: true},
483
+ jwtFields: ['jwt']
417
484
  };
418
485
 
419
486
  const maskedJsonOutput2 = MaskData.maskJSON2(jsonInput2, jsonMaskConfig2);
@@ -433,7 +500,8 @@ const maskedJsonOutput2 = MaskData.maskJSON2(jsonInput2, jsonMaskConfig2);
433
500
  addressLine1: '**** ** ** *********** ** **** ** ** ****',
434
501
  addressLine2: '*********** *'
435
502
  },
436
- uuids: { uuid1: '123e****-****-****-****-**********00' }
503
+ uuids: { uuid1: '123e****-****-****-****-**********00' },
504
+ jwt: '*********************************************************************************************************'
437
505
  }
438
506
 
439
507
  ```
@@ -551,7 +619,59 @@ const maskedPhoneNumber = MaskData.maskPhone(phoneNumber);
551
619
  //Output: +911********0
552
620
 
553
621
  ```
622
+ ## Generic string masking
623
+ This functionality can be used to mask any string with the below configs.
624
+ #### Example1
625
+ ```javascript
626
+ const MaskData = require('./maskdata');
627
+
628
+ const defaultStringMaskV2Options = {
629
+ // Character to mask the data
630
+ // default value is '*'
631
+ maskWith: "*",
554
632
 
633
+ // This is to limit the maximun characters in the output.
634
+ // Default value is 256
635
+ maxMaskedCharacters: 256,
636
+
637
+ // To show(not mask) first 'n' characters of the string.
638
+ // Default value is 0.
639
+ unmaskedStartCharacters: 0,
640
+
641
+ // To show(not mask) last 'n' characters of the string.
642
+ // Default value is 0.
643
+ unmaskedEndCharacters: 0
644
+ };
645
+
646
+ const string1 = "Password1$";
647
+
648
+ const maskedString = MaskData.maskStringV2(string1, defaultStringMaskV2Options);
649
+
650
+ //Output: **********
651
+ ```
652
+
653
+ #### Example2
654
+
655
+ ```javascript
656
+ const MaskData = require('./maskdata');
657
+
658
+ const stringMaskV2Options = {
659
+ maskWith: "X",
660
+ maxMaskedCharacters: 20, // To limit the output length to 20.
661
+ unmaskedStartCharacters: 4,
662
+ unmaskedEndCharacters: 9
663
+ };
664
+
665
+ const secret = "TEST:U2VjcmV0S2V5MQ==:CLIENT-A";
666
+
667
+ const maskedSecret = MaskData.maskStringV2(password, stringMaskV2Options);
668
+ //Output: TESTXXXXXXX:CLIENT-A
669
+
670
+ stringMaskV2Options.unmaskedStartCharacters = 0;
671
+
672
+ maskedSecret = MaskData.maskStringV2(password, stringMaskV2Options);
673
+ //Output: XXXXXXXXXXX:CLIENT-A
674
+ ```
555
675
 
556
676
  ## Mask the characters or words in the string
557
677
  This will mask the characters or words if present in the given string.
@@ -701,7 +821,6 @@ const jwt =
701
821
  * Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
702
822
  */
703
823
  const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
704
- console.log(maskedJwt);
705
824
 
706
825
  // Output:
707
826
  *********************************************************************************************************
@@ -732,7 +851,6 @@ const jwt =
732
851
  * Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
733
852
  */
734
853
  const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
735
- console.log(maskedJwt);
736
854
 
737
855
  // Output:
738
856
  eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.*******************************************
@@ -763,7 +881,6 @@ const jwt =
763
881
  * Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
764
882
  */
765
883
  const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
766
- console.log(maskedJwt);
767
884
 
768
885
  // Output:
769
886
  ************************************.eyJpYXQiOjE1MTYyMzkwMjJ9.*******************************************
@@ -794,7 +911,6 @@ const jwt =
794
911
  * Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
795
912
  */
796
913
  const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
797
- console.log(maskedJwt);
798
914
 
799
915
  // Output:
800
916
  ******.*****.*****
package/index.d.ts CHANGED
@@ -25,6 +25,18 @@ declare module 'maskdata' {
25
25
  unmaskedEndCharacters?: number;
26
26
  }
27
27
 
28
+ export interface StringMaskV2Options {
29
+ maskWith?: string;
30
+ maxMaskedCharacters?: number;
31
+ unmaskedStartCharacters?: number;
32
+ unmaskedEndCharacters?: number;
33
+ }
34
+
35
+ export interface GenericStringMaskOptions {
36
+ config?: StringMaskV2Options;
37
+ fields: string[];
38
+ }
39
+
28
40
  export interface PhoneMaskOptions {
29
41
  maskWith?: string;
30
42
  unmaskedStartDigits?: number;
@@ -69,19 +81,16 @@ declare module 'maskdata' {
69
81
  uuidFields?: string[];
70
82
  jwtMaskOptions?: JwtMaskOptions;
71
83
  jwtFields?: string[];
84
+ genericStrings?: GenericStringMaskOptions[];
72
85
  }
73
86
 
74
87
  export function maskPassword(password?: string, options?: PasswordMaskOptions): string;
75
- /**
76
- * @deprecated
77
- * Use maskJSON2 instead: https://www.npmjs.com/package/maskdata#mask-json
78
- */
79
- export function maskJSONFields<T extends object>(json?: T, options?: JsonMaskOptions): T;
80
88
  export function maskJSON2<T extends object>(json?: T, options?: JsonMask2Configs): T;
81
89
  export function maskPhone(phone?: string, options?: PhoneMaskOptions): string;
82
90
  export function maskEmail2(email?: string, options?: EmailMask2Options): string;
83
91
  export function maskCard(cardNumber?: string, options?: CardMaskOptions): string;
84
92
  export function maskString(str?: string, options?: StringMaskOptions): string;
93
+ export function maskStringV2(str?: string, options?: StringMaskV2Options): string;
85
94
  export function maskUuid(uuid?: string, options?: UuidMaskOptions): string;
86
95
  export function maskJwt(jwt?: string, options?: JwtMaskOptions): string;
87
96
  export function getInnerProperty(object: object, field: string): any;
package/index.js CHANGED
@@ -15,15 +15,6 @@ class MaskData {
15
15
  return MaskPassword.maskPassword(password, options);
16
16
  }
17
17
 
18
- /**
19
- *
20
- * @deprecated
21
- * Use maskJSON2 instead: https://www.npmjs.com/package/maskdata#mask-json
22
- */
23
- static maskJSONFields(json, options) {
24
- return JsonMask.maskJSONFields(json, options);
25
- }
26
-
27
18
  static maskJSON2(json, options) {
28
19
  return JsonMask.maskJSON2(json, options);
29
20
  }
@@ -44,6 +35,10 @@ class MaskData {
44
35
  return MaskString.maskString(str, options);
45
36
  }
46
37
 
38
+ static maskStringV2(str, options) {
39
+ return MaskString.maskStringV2(str, options);
40
+ }
41
+
47
42
  static maskUuid(uuid, options) {
48
43
  return MaskUuid.maskUuid(uuid, options);
49
44
  }
@@ -33,6 +33,13 @@ const defaultPasswordMaskOptions = {
33
33
  unmaskedEndCharacters: 0
34
34
  };
35
35
 
36
+ const defaultStringMaskV2Options = {
37
+ maskWith: '*',
38
+ maxMaskedCharacters: 256,
39
+ unmaskedStartCharacters: 0,
40
+ unmaskedEndCharacters: 0
41
+ };
42
+
36
43
  const defaultPhoneMaskOptions = {
37
44
  maskWith: '*',
38
45
  unmaskedStartDigits: 4,
@@ -65,7 +72,10 @@ const defaultjsonMask2Configs = {
65
72
  stringMaskOptions: defaultStringMaskOptions,
66
73
  stringFields: [],
67
74
  uuidMaskOptions: defaultUuidMaskOptions,
68
- uuidFields: []
75
+ uuidFields: [],
76
+ jwtMaskOptions: defaultJwtMaskOptions,
77
+ jwtFields: [],
78
+ genericStrings: []
69
79
  };
70
80
 
71
81
  module.exports = {
@@ -75,6 +85,7 @@ module.exports = {
75
85
  defaultJsonMaskOptions,
76
86
  defaultPasswordMaskOptions,
77
87
  defaultStringMaskOptions,
88
+ defaultStringMaskV2Options,
78
89
  defaultUuidMaskOptions,
79
90
  defaultjsonMask2Configs,
80
91
  defaultJwtMaskOptions
@@ -48,6 +48,44 @@ class MaskHelper {
48
48
  }
49
49
  }
50
50
 
51
+ static validateStringMask2Options(options) {
52
+ if (!options) return JSON.parse(JSON.stringify(Constants.defaultStringMaskV2Options));
53
+ let reasons = [];
54
+ try {
55
+ options.maxMaskedCharacters = parseInt(options.maxMaskedCharacters);
56
+ options.unmaskedStartCharacters = parseInt(options.unmaskedStartCharacters);
57
+ options.unmaskedEndCharacters = parseInt(options.unmaskedEndCharacters);
58
+ if (
59
+ isNaN(options.maxMaskedCharacters) ||
60
+ isNaN(options.unmaskedStartCharacters) ||
61
+ isNaN(options.unmaskedEndCharacters)
62
+ ) {
63
+ throw Error();
64
+ }
65
+ } catch (err) {
66
+ reasons.push(
67
+ 'maxMaskedCharacters, unmaskedStartCharacters and unmaskedEndCharacters must be positive integer'
68
+ );
69
+ }
70
+ if (options.maxMaskedCharacters < 0) {
71
+ options.maxMaskedCharacters = 256;
72
+ }
73
+ if (options.unmaskedStartCharacters < 0) {
74
+ options.unmaskedStartCharacters = 0;
75
+ }
76
+ if (options.unmaskedEndCharacters < 0) {
77
+ options.unmaskedEndCharacters = 0;
78
+ }
79
+ if (!options.maskWith || options.maskWith.toString().length <= 0) {
80
+ options.maskWith = '*';
81
+ }
82
+ if (reasons.length > 0) {
83
+ const badOptions = new BadOption('String mask2 configuration', reasons);
84
+ badOptions.throwBadOptionException();
85
+ }
86
+ return options;
87
+ }
88
+
51
89
  static validatePhoneOptions(options) {
52
90
  let reasons = [];
53
91
  try {
@@ -299,8 +337,6 @@ class MaskHelper {
299
337
  }
300
338
 
301
339
  static getValidatedJsonMask2Configs(configs) {
302
- if (!configs) return JSON.parse(JSON.stringify(Constants.defaultjsonMask2Configs));
303
-
304
340
  if (!configs.cardMaskOptions) {
305
341
  configs.cardMaskOptions = JSON.parse(JSON.stringify(Constants.defaultCardMaskOptions));
306
342
  }
@@ -351,6 +387,17 @@ class MaskHelper {
351
387
  configs.uuidMaskOptions = JSON.parse(JSON.stringify(Constants.defaultUuidMaskOptions));
352
388
  }
353
389
 
390
+ if (configs.genericStrings && configs.genericStrings.length > 0) {
391
+ for (let geneicString of configs.genericStrings) {
392
+ if (geneicString['fields'] && geneicString['fields'].length > 0) {
393
+ geneicString['config'] = {
394
+ ...Constants.defaultStringMaskV2Options,
395
+ ...geneicString['config']
396
+ };
397
+ }
398
+ }
399
+ }
400
+
354
401
  return configs;
355
402
  }
356
403
  }
@@ -19,107 +19,15 @@ const typeToFunctionMap = {
19
19
  phone: [PhoneMask.maskPhone, 'phoneFields', 'phoneMaskOptions'],
20
20
  string: [StringMask.maskString, 'stringFields', 'stringMaskOptions'],
21
21
  uuid: [UuidMask.maskUuid, 'uuidFields', 'uuidMaskOptions'],
22
- jwt: [JwtMask.maskJwt, 'jwtFields', 'jwtMaskOptions']
22
+ jwt: [JwtMask.maskJwt, 'jwtFields', 'jwtMaskOptions'],
23
+ generic: [StringMask.maskStringV2, 'fields', 'config']
23
24
  };
24
25
 
25
26
  class JsonMask {
26
- static maskJSONFields(obj, options) {
27
- if (options) {
28
- options = MaskHelper.mapWithDefaultValues(options, Constants.defaultJsonMaskOptions);
29
- MaskHelper.validateJSONOptions(options);
30
- } else {
31
- options = Constants.defaultJsonMaskOptions;
32
- }
33
- let maskedObj = {};
34
- try {
35
- maskedObj = JSON.parse(JSON.stringify(obj));
36
- } catch (ex) {
37
- return obj;
38
- }
39
- const fields = options.fields;
40
- for (const field of fields) {
41
- try {
42
- if (field.includes('[*].')) {
43
- let [arrayFieldName, subField] = field.split('[*].');
44
- const arrayValue = JsonGetSet.get(maskedObj, arrayFieldName);
45
- for (const arrayElement of arrayValue) {
46
- const value = arrayElement[subField];
47
- if (value === undefined || value === null) {
48
- continue;
49
- } else {
50
- if (
51
- typeof value == 'string' &&
52
- options.maxMaskedCharactersStr != -1 &&
53
- options.maxMaskedCharactersStr < value.length
54
- ) {
55
- JsonGetSet.set(
56
- arrayElement,
57
- subField,
58
- `${options.maskWith}`.repeat(options.maxMaskedCharactersStr)
59
- );
60
- } else {
61
- JsonGetSet.set(
62
- arrayElement,
63
- subField,
64
- `${options.maskWith}`.repeat(value.toString().length)
65
- );
66
- }
67
- }
68
- }
69
- } else if (field.includes('.*')) {
70
- let subField = field.split('.*')[0];
71
- const innerObject = JsonGetSet.get(maskedObj, subField);
72
- for (const innerObjectField of Object.keys(innerObject)) {
73
- const value = innerObject[innerObjectField];
74
- if (value === undefined || value === null) {
75
- continue;
76
- } else {
77
- if (
78
- typeof value == 'string' &&
79
- options.maxMaskedCharactersStr != -1 &&
80
- options.maxMaskedCharactersStr < value.length
81
- ) {
82
- JsonGetSet.set(
83
- innerObject,
84
- innerObjectField,
85
- `${options.maskWith}`.repeat(options.maxMaskedCharactersStr)
86
- );
87
- } else {
88
- JsonGetSet.set(
89
- innerObject,
90
- innerObjectField,
91
- `${options.maskWith}`.repeat(value.toString().length)
92
- );
93
- }
94
- }
95
- }
96
- } else {
97
- const value = JsonGetSet.get(maskedObj, field);
98
- if (value === undefined || value === null) {
99
- continue;
100
- }
101
- if (
102
- typeof value == 'string' &&
103
- options.maxMaskedCharactersStr != -1 &&
104
- options.maxMaskedCharactersStr < value.length
105
- ) {
106
- JsonGetSet.set(
107
- maskedObj,
108
- field,
109
- `${options.maskWith}`.repeat(options.maxMaskedCharactersStr)
110
- );
111
- } else {
112
- JsonGetSet.set(maskedObj, field, `${options.maskWith}`.repeat(value.toString().length));
113
- }
114
- }
115
- } catch (ex) {
116
- continue;
117
- }
118
- }
119
- return maskedObj;
120
- }
121
-
122
27
  static maskJSON2(obj, config) {
28
+ if (!config) {
29
+ config = JSON.parse(JSON.stringify(Constants.defaultjsonMask2Configs));
30
+ }
123
31
  const validatedConfig = MaskHelper.getValidatedJsonMask2Configs(config);
124
32
  let maskedObj = {};
125
33
  try {
@@ -134,12 +42,22 @@ class JsonMask {
134
42
  this.mask(maskedObj, field, key, validatedConfig[typeToFunctionMap[key][2]]);
135
43
  }
136
44
  }
45
+ if (validatedConfig['genericStrings'] && validatedConfig['genericStrings'].length > 0) {
46
+ for (let genericString of validatedConfig['genericStrings']) {
47
+ if (genericString['fields'] && genericString['fields'].length > 0) {
48
+ let validatedMaskConfig = MaskHelper.validateStringMask2Options(genericString['config']);
49
+ for (let field of genericString['fields']) {
50
+ this.mask(maskedObj, field, 'generic', validatedMaskConfig);
51
+ }
52
+ }
53
+ }
54
+ }
137
55
  return maskedObj;
138
56
  }
139
57
 
140
58
  static mask(json, field, type, maskConfig) {
141
59
  if (field.includes('.*') && !field.endsWith('.*')) {
142
- console.warn('Ignoring the invalid field: ', field);
60
+ console.warn('Ignoring the invalid field while masking: ', field);
143
61
  return;
144
62
  }
145
63
 
@@ -38,6 +38,43 @@ class StringMask {
38
38
  }
39
39
  return str;
40
40
  }
41
+
42
+ static maskStringV2(string, options) {
43
+ if (!string || typeof string === 'object') return string;
44
+ string = string + '';
45
+ if (options) {
46
+ options = MaskHelper.mapWithDefaultValues(options, Constants.defaultStringMaskV2Options);
47
+ MaskHelper.validateStringMask2Options(options);
48
+ } else {
49
+ options = Constants.defaultStringMaskV2Options;
50
+ }
51
+ let maskStringLength = string.length;
52
+ if (string.length > options.maxMaskedCharacters) {
53
+ maskStringLength = parseInt(options.maxMaskedCharacters);
54
+ }
55
+ const maskingCharacters =
56
+ maskStringLength - options.unmaskedStartCharacters - options.unmaskedEndCharacters;
57
+
58
+ if (maskingCharacters < 0) {
59
+ if (maskStringLength <= options.unmaskedStartCharacters) {
60
+ return string.substring(0, maskStringLength);
61
+ } else {
62
+ let maskedString = string.substring(0, options.unmaskedStartCharacters);
63
+ const remainingChars = maskStringLength - options.unmaskedStartCharacters;
64
+ for (let i = string.length - remainingChars; i < string.length; i++) {
65
+ maskedString += string[i];
66
+ }
67
+ return maskedString;
68
+ }
69
+ }
70
+ let maskedString = '';
71
+ maskedString = string.substring(0, options.unmaskedStartCharacters);
72
+ maskedString += `${options.maskWith}`.repeat(maskingCharacters);
73
+ for (let i = string.length - options.unmaskedEndCharacters; i < string.length; i++) {
74
+ maskedString += string[i];
75
+ }
76
+ return maskedString;
77
+ }
41
78
  }
42
79
 
43
80
  module.exports = StringMask;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maskdata",
3
- "version": "1.2.5",
3
+ "version": "1.3.0",
4
4
  "description": "Node module to mask various kinds of data in the required formats",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -1,6 +1,5 @@
1
1
  'use strict';
2
2
  const maskData = require('../index');
3
- const BadOption = require('../lib/ExceptionsHandler/BadOption');
4
3
  const expect = require('chai').expect;
5
4
 
6
5
  describe('Masking email addresses', function () {
@@ -15,104 +15,10 @@ const jsonInput = {
15
15
  workPhone: '+1 9876543210',
16
16
  addressLine1: 'This is my addressline 1. This is my home',
17
17
  addressLine2: 'AddressLine 2',
18
- uuid1: '123e4567-e89b-12d3-a456-426614174000'
18
+ uuid1: '123e4567-e89b-12d3-a456-426614174000',
19
+ randomString: 'This is a random string'
19
20
  };
20
21
 
21
- describe('JSON mask1', function () {
22
- describe('Mask json with default options', function () {
23
- let testData = {
24
- title: 'Mask json with default options',
25
- input: {
26
- level1: {
27
- field1: 'field1Value',
28
- level2: {
29
- field2: 'field2Value',
30
- field3: [{ Hello: 'Hello', Hi: 'one' }, { Hello: 'Hello again' }],
31
- level3: {
32
- field4: 'field4Value',
33
- field5: 'field5Value'
34
- }
35
- }
36
- },
37
- value1: 'value'
38
- }
39
- };
40
-
41
- it(`${testData.title}`, function () {
42
- const jsonMaskConfig = {
43
- fields: ['level1.level2.field3[*].Hello', 'level1.level2.level3.*', 'value1']
44
- };
45
- const masked = maskData.maskJSONFields(testData.input, jsonMaskConfig);
46
- expect(masked['level1']['level2']['field3'][0]['Hello']).to.equal('*****');
47
- expect(masked['level1']['level2']['field3'][1]['Hello']).to.equal('***********');
48
- expect(masked['level1']['level2']['level3']['field4']).to.equal('***********');
49
- expect(masked['level1']['level2']['level3']['field5']).to.equal('***********');
50
- expect(masked['value1']).to.equal('*****');
51
- });
52
-
53
- it('Mask json with maxMaskedCharactersStr', function () {
54
- const jsonMaskConfig = {
55
- fields: ['key1'],
56
- maxMaskedCharactersStr: 10
57
- };
58
- const masked = maskData.maskJSONFields(
59
- { key1: 'value1WithTooManyCharacters' },
60
- jsonMaskConfig
61
- );
62
- expect(masked['key1']).to.equal('**********');
63
- });
64
- });
65
-
66
- describe('Mask json without passing configs', function () {
67
- let testData = {
68
- title: 'Mask json with default options',
69
- input: {
70
- level1: {
71
- field1: 'field1Value',
72
- level2: {
73
- field2: 'field2Value',
74
- field3: [
75
- { Hello: 'Hello', Hi: 'one' },
76
- { Hello: 'Hello again' },
77
- { NoHello: 'No Hello' }
78
- ],
79
- level3: {
80
- field4: 'field4Value',
81
- field5: 'field5Value'
82
- }
83
- }
84
- },
85
- value1: 'value'
86
- }
87
- };
88
-
89
- it(`${testData.title}`, function () {
90
- const jsonMaskConfig = {
91
- fields: [
92
- 'level1.level2.field3[*].Hello',
93
- 'level1.level2.level3.*',
94
- 'value1',
95
- 'unknownField'
96
- ]
97
- };
98
- const masked = maskData.maskJSONFields(testData.input);
99
- expect(masked['level1']['level2']['field3'][0]['Hello']).to.equal(
100
- testData.input['level1']['level2']['field3'][0]['Hello']
101
- );
102
- expect(masked['level1']['level2']['field3'][1]['Hello']).to.equal(
103
- testData.input['level1']['level2']['field3'][1]['Hello']
104
- );
105
- expect(masked['level1']['level2']['level3']['field4']).to.equal(
106
- testData.input['level1']['level2']['level3']['field4']
107
- );
108
- expect(masked['level1']['level2']['level3']['field5']).to.equal(
109
- testData.input['level1']['level2']['level3']['field5']
110
- );
111
- expect(masked['value1']).to.equal(testData.input['value1']);
112
- });
113
- });
114
- });
115
-
116
22
  describe('JSON mask2', function () {
117
23
  describe('Mask with default options and no fields', function () {
118
24
  let testData = [
@@ -128,7 +34,8 @@ describe('JSON mask2', function () {
128
34
  outputWorkPhone: '+1 9876543210',
129
35
  outputAddressLine1: 'This is my addressline 1. This is my home',
130
36
  outputAddressLine2: 'AddressLine 2',
131
- outputUuid: '123e4567-e89b-12d3-a456-426614174000'
37
+ outputUuid: '123e4567-e89b-12d3-a456-426614174000',
38
+ outputRandomString: 'This is a random string'
132
39
  }
133
40
  ];
134
41
 
@@ -145,7 +52,8 @@ describe('JSON mask2', function () {
145
52
  outputWorkPhone,
146
53
  outputAddressLine1,
147
54
  outputAddressLine2,
148
- outputUuid
55
+ outputUuid,
56
+ outputRandomString
149
57
  }) => {
150
58
  it(`${title}`, function () {
151
59
  const masked = maskData.maskJSON2(input, Constants.defaultjsonMask2Configs);
@@ -159,11 +67,38 @@ describe('JSON mask2', function () {
159
67
  expect(masked['addressLine1']).to.equal(outputAddressLine1);
160
68
  expect(masked['addressLine2']).to.equal(outputAddressLine2);
161
69
  expect(masked['uuid1']).to.equal(outputUuid);
70
+ expect(masked['randomString']).to.equal(outputRandomString);
162
71
  });
163
72
  }
164
73
  );
165
74
  });
166
75
 
76
+ describe('Mask all fields with no config', function () {
77
+ let testData = [
78
+ {
79
+ title: 'test json masking with no config but all types of fields',
80
+ input: JSON.parse(JSON.stringify(jsonInput))
81
+ }
82
+ ];
83
+
84
+ testData.forEach(({ title, input }) => {
85
+ it(`${title}`, function () {
86
+ const masked = maskData.maskJSON2(input);
87
+ expect(masked['credit']).to.equal(input['credit']);
88
+ expect(masked['debit']).to.equal(input['debit']);
89
+ expect(masked['primaryEmail']).to.equal(input['primaryEmail']);
90
+ expect(masked['secondaryEmail']).to.equal(input['secondaryEmail']);
91
+ expect(masked['password']).to.equal(input['password']);
92
+ expect(masked['homePhone']).to.equal(input['homePhone']);
93
+ expect(masked['workPhone']).to.equal(input['workPhone']);
94
+ expect(masked['addressLine1']).to.equal(input['addressLine1']);
95
+ expect(masked['addressLine2']).to.equal(input['addressLine2']);
96
+ expect(masked['uuid1']).to.equal(input['uuid1']);
97
+ expect(masked['randomString']).to.equal(input['randomString']);
98
+ });
99
+ });
100
+ });
101
+
167
102
  describe('Mask all fields with default options', function () {
168
103
  let testData = [
169
104
  {
@@ -178,7 +113,8 @@ describe('JSON mask2', function () {
178
113
  outputWorkPhone: '+1 9********0',
179
114
  outputAddressLine1: 'This is my addressline 1. This is my home',
180
115
  outputAddressLine2: 'AddressLine 2',
181
- outputUuid: '********-****-****-****-************'
116
+ outputUuid: '********-****-****-****-************',
117
+ outputRandonString: '***********************'
182
118
  }
183
119
  ];
184
120
 
@@ -195,7 +131,8 @@ describe('JSON mask2', function () {
195
131
  outputWorkPhone,
196
132
  outputAddressLine1,
197
133
  outputAddressLine2,
198
- outputUuid
134
+ outputUuid,
135
+ outputRandonString
199
136
  }) => {
200
137
  const jsonMaskConfig = JSON.parse(JSON.stringify(Constants.defaultjsonMask2Configs));
201
138
  jsonMaskConfig['cardFields'] = ['credit', 'debit'];
@@ -204,6 +141,7 @@ describe('JSON mask2', function () {
204
141
  jsonMaskConfig['phoneFields'] = ['homePhone', 'workPhone'];
205
142
  jsonMaskConfig['stringFields'] = ['addressLine1', 'addressLine2'];
206
143
  jsonMaskConfig['uuidFields'] = ['uuid1'];
144
+ jsonMaskConfig['genericStrings'] = [{ fields: ['randomString'] }];
207
145
  it(`${title}`, function () {
208
146
  const masked = maskData.maskJSON2(input, jsonMaskConfig);
209
147
  expect(masked['credit']).to.equal(outputCredit);
@@ -216,6 +154,7 @@ describe('JSON mask2', function () {
216
154
  expect(masked['addressLine1']).to.equal(outputAddressLine1);
217
155
  expect(masked['addressLine2']).to.equal(outputAddressLine2);
218
156
  expect(masked['uuid1']).to.equal(outputUuid);
157
+ expect(masked['randomString']).to.equal(outputRandonString);
219
158
  });
220
159
  }
221
160
  );
@@ -253,6 +192,10 @@ describe('JSON mask2', function () {
253
192
  'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiVGVzdDIifQ.ekZkXoeOb4SE_YLlc0Iv8V3wm17xS1B7TzhtYHvIYl0',
254
193
  'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiVGVzdDMifQ.CNiJIr7TZgsVGS0dbFFwdPtG6v1wwSor9jFoVeUkrJ0'
255
194
  ]
195
+ },
196
+ drivingLicense: {
197
+ license1: 'XX1234 YY',
198
+ motorLicenses: ['ABCD1234', 'EFGH5678']
256
199
  }
257
200
  };
258
201
 
@@ -307,6 +250,9 @@ describe('JSON mask2', function () {
307
250
  jsonMaskConfig['stringFields'] = ['address.addressLine1', 'address.addressLine2'];
308
251
  jsonMaskConfig['uuidFields'] = ['uuids.uuid1'];
309
252
  jsonMaskConfig['jwtFields'] = ['jwts.token1', 'jwts.token2'];
253
+ jsonMaskConfig['genericStrings'] = [
254
+ { fields: ['drivingLicense.license1', 'drivingLicense.motorLicenses.*'] }
255
+ ];
310
256
  it(`${title}`, function () {
311
257
  const masked = maskData.maskJSON2(input, jsonMaskConfig);
312
258
  expect(masked['cards']['creditCards'][0]).to.equal(outputCredit);
@@ -329,6 +275,10 @@ describe('JSON mask2', function () {
329
275
 
330
276
  expect(masked['jwts']['token1']).to.equal(outputJwt1);
331
277
  expect(masked['jwts']['token2']).to.equal(outputJwt2);
278
+
279
+ expect(masked['drivingLicense']['license1']).to.equal('*********');
280
+ expect(masked['drivingLicense']['motorLicenses'][0]).to.equal('********');
281
+ expect(masked['drivingLicense']['motorLicenses'][1]).to.equal('********');
332
282
  });
333
283
  it('Test masking improper JSON', function () {
334
284
  const notAJson = 'Not a JSON. Just a String';
@@ -339,11 +289,20 @@ describe('JSON mask2', function () {
339
289
  );
340
290
  });
341
291
 
342
- describe('Mask card fields with custom options', function () {
292
+ describe('Mask fields with custom options', function () {
293
+ let input = JSON.parse(JSON.stringify(jsonInput));
294
+ input['randomStrings'] = {};
295
+ input['randomStrings']['row1'] = 'This is row 1 random string';
296
+ input['randomStrings']['row2'] = ['Entry1', 'Entry2', 'Entry3'];
297
+ input['randomStrings']['row3'] = {
298
+ key1: 'Row3 Object 1',
299
+ key2: 'Row3 Object 2',
300
+ key3: ['Entry1', 'Entry2', 'Entry3']
301
+ };
343
302
  let testData = [
344
303
  {
345
304
  title: 'Mask card fields with custom options',
346
- input: JSON.parse(JSON.stringify(jsonInput)),
305
+ input: input,
347
306
  outputCredit: 'XXXX-XXXX-XXXX-1234',
348
307
  outputDebit: 'XXXX-XXXX-XXXX-3333'
349
308
  }
@@ -352,16 +311,44 @@ describe('JSON mask2', function () {
352
311
  testData.forEach(({ title, input, outputCredit, outputDebit }) => {
353
312
  const jsonMaskConfig = JSON.parse(JSON.stringify(Constants.defaultjsonMask2Configs));
354
313
  const cardMaskOptions = JSON.parse(JSON.stringify(Constants.defaultCardMaskOptions));
355
- // const defaultCardMaskOptions = {
356
- // maskWith: "*",
357
- // unmaskedStartDigits: 4,
358
- // unmaskedEndDigits: 1
359
- // };
360
314
  cardMaskOptions['maskWith'] = 'X';
361
315
  cardMaskOptions['unmaskedStartDigits'] = 0;
362
316
  cardMaskOptions['unmaskedEndDigits'] = 4;
363
317
  jsonMaskConfig['cardFields'] = ['credit', 'debit'];
364
318
  jsonMaskConfig['cardMaskOptions'] = cardMaskOptions;
319
+ jsonMaskConfig['genericStrings'] = [
320
+ {
321
+ fields: ['randomString'],
322
+ config: {
323
+ maskWith: '?',
324
+ unmaskedStartCharacters: 2,
325
+ unmaskedEndCharacters: 3,
326
+ maxMaskedCharacters: 8
327
+ }
328
+ },
329
+ {
330
+ fields: ['randomStrings.row1'],
331
+ config: {
332
+ maskWith: '*',
333
+ unmaskedStartCharacters: 2,
334
+ unmaskedEndCharacters: 3,
335
+ maxMaskedCharacters: 8
336
+ }
337
+ },
338
+ { fields: ['randomStrings.row2.*'], config: { maskWith: 'X', unmaskedEndCharacters: 1 } },
339
+ { fields: ['randomStrings.row3.key1'] },
340
+ {
341
+ fields: ['randomStrings.row3.key3.*'],
342
+ config: { maskWith: '@', unmaskedEndCharacters: 1 }
343
+ },
344
+ {
345
+ fields: ['unknownField'],
346
+ config: { maskWith: '@', unmaskedEndCharacters: 1 }
347
+ },
348
+ {
349
+ config: { maskWith: '@', unmaskedEndCharacters: 1 }
350
+ }
351
+ ];
365
352
  it(`${title}`, function () {
366
353
  const masked = maskData.maskJSON2(input, jsonMaskConfig);
367
354
  expect(masked['credit']).to.equal(outputCredit);
@@ -373,6 +360,17 @@ describe('JSON mask2', function () {
373
360
  expect(masked['workPhone']).to.equal(input['workPhone']);
374
361
  expect(masked['addressLine1']).to.equal(input['addressLine1']);
375
362
  expect(masked['addressLine2']).to.equal(input['addressLine2']);
363
+ expect(masked['randomString']).to.equal('Th???ing');
364
+ expect(masked['randomStrings']['row1']).to.equal('Th***ing');
365
+ expect(masked['randomStrings']['row2'][0]).to.equal('XXXXX1');
366
+ expect(masked['randomStrings']['row2'][1]).to.equal('XXXXX2');
367
+ expect(masked['randomStrings']['row2'][2]).to.equal('XXXXX3');
368
+ expect(masked['randomStrings']['row3']['key1']).to.equal('*************');
369
+ expect(masked['randomStrings']['row3']['key2']).to.equal('Row3 Object 2');
370
+ expect(masked['randomStrings']['row3']['key3'][0]).to.equal('@@@@@1');
371
+ expect(masked['randomStrings']['row3']['key3'][1]).to.equal('@@@@@2');
372
+ expect(masked['randomStrings']['row3']['key3'][2]).to.equal('@@@@@3');
373
+ expect(masked['unknownField']).to.equal(undefined);
376
374
  });
377
375
  });
378
376
  });
package/tests/testJwt.js CHANGED
@@ -1,6 +1,5 @@
1
1
  'use strict';
2
2
  const maskData = require('../index');
3
- const BadOption = require('../lib/ExceptionsHandler/BadOption');
4
3
  const expect = require('chai').expect;
5
4
 
6
5
  describe('Masking JWT tokens', function () {
@@ -157,17 +157,15 @@ describe('Masking password', function () {
157
157
  let testData = [
158
158
  {
159
159
  title: 'test input as array',
160
- input: ['12'],
161
- output: '12'
160
+ input: ['12']
162
161
  },
163
162
  {
164
163
  title: 'test input as object',
165
- input: { a: 'b', x: 'y' },
166
- output: { a: 'b', x: 'y' }
164
+ input: { a: 'b', x: 'y' }
167
165
  }
168
166
  ];
169
167
 
170
- testData.forEach(({ title, input, output }) => {
168
+ testData.forEach(({ title, input }) => {
171
169
  it(`special input - ${title}`, function () {
172
170
  const masked = maskData.maskPassword(input, maskOptions);
173
171
  expect(masked).to.equal(input, "input won't be masked");
@@ -0,0 +1,180 @@
1
+ 'use strict';
2
+ const maskData = require('../index');
3
+ const expect = require('chai').expect;
4
+
5
+ describe('Masking String V2', function () {
6
+ describe('Mask with default options', function () {
7
+ // default options are this - let tests fail when defaults change
8
+ // const defaultStringMaskV2Options = {
9
+ // maskWith: "*",
10
+ // maxMaskedCharacters: 256,
11
+ // unmaskedStartCharacters: 0,
12
+ // unmaskedEndCharacters: 0
13
+ // };
14
+
15
+ let testData = [
16
+ {
17
+ title: 'test default string',
18
+ input:
19
+ '1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-123456',
20
+ output:
21
+ '****************************************************************************************************************************************************************************************************************************************************************' // 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:
31
+ 'x1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-1234-5678-123456',
32
+ output:
33
+ '****************************************************************************************************************************************************************************************************************************************************************' // equal maxMaskedChars
34
+ }
35
+ ];
36
+
37
+ testData.forEach(({ title, input, output }) => {
38
+ it(`default mask - ${title}`, function () {
39
+ const masked = maskData.maskStringV2(input);
40
+ expect(masked).to.equal(output, 'masked output does not match expected value');
41
+ });
42
+ });
43
+ });
44
+
45
+ describe('Mask with custom options - maskWith, maxMaskedChars', function () {
46
+ const maskOptions = {
47
+ maskWith: 'x',
48
+ maxMaskedCharacters: 6,
49
+ unmaskedStartCharacters: 1, // use different numbers for start / end to check correct masking
50
+ unmaskedEndCharacters: 2
51
+ };
52
+
53
+ let testData = [
54
+ {
55
+ title: 'test default string',
56
+ input: '1234-5678-1234-5678',
57
+ output: '1xxx78'
58
+ },
59
+ {
60
+ title: 'test string length equal to start + end',
61
+ input: '12-345',
62
+ output: '1xxx45'
63
+ },
64
+ {
65
+ title: 'test shorter string than start + end',
66
+ input: '12',
67
+ output: '12'
68
+ }
69
+ ];
70
+
71
+ testData.forEach(({ title, input, output }) => {
72
+ it(`custom mask - ${title}`, function () {
73
+ const masked = maskData.maskStringV2(input, maskOptions);
74
+ expect(masked).to.equal(output, 'masked output does not match expected value');
75
+ });
76
+ });
77
+
78
+ it('custom mask - test with negative unmaskedStartDigits', function () {
79
+ const negativeStartOption = {
80
+ maskWith: 'x',
81
+ maxMaskedCharacters: 6,
82
+ unmaskedStartCharacters: -2, // internal set to 0
83
+ unmaskedEndCharacters: 2
84
+ };
85
+ const input = '1234-5678-1234-5678';
86
+ const output = 'xxxx78';
87
+ const masked = maskData.maskStringV2(input, negativeStartOption);
88
+ expect(masked).to.equal(output, 'masked output does not match expected value');
89
+ });
90
+
91
+ it('custom mask - test with maxMasked shorter than start + end: No masking done', function () {
92
+ const shortMaxCharsOption = {
93
+ maskWith: 'x',
94
+ maxMaskedCharacters: 6,
95
+ unmaskedStartCharacters: 4, // 4+4=8 > 6
96
+ unmaskedEndCharacters: 4
97
+ };
98
+ const input = '1234-5678-1234-5678';
99
+ const output = '123478';
100
+ const masked = maskData.maskStringV2(input, shortMaxCharsOption);
101
+ expect(masked).to.equal(
102
+ output,
103
+ 'No masking done because maxMasked(max output characters) shorter than start + end'
104
+ );
105
+ });
106
+
107
+ it('custom mask - test with maxMasked equal to start + end: No masking done', function () {
108
+ const shortMaxCharsOption = {
109
+ maskWith: 'x',
110
+ maxMaskedCharacters: 8,
111
+ unmaskedStartCharacters: 4, // 4+4=8
112
+ unmaskedEndCharacters: 4
113
+ };
114
+ const input = '1234-abcd-efgh-5678';
115
+ const output = '12345678';
116
+ const masked = maskData.maskStringV2(input, shortMaxCharsOption);
117
+ expect(masked).to.equal(
118
+ output,
119
+ 'No masking done because maxMasked(max output characters) equal to start + end'
120
+ );
121
+ });
122
+ });
123
+
124
+ describe('Mask with special input strings', function () {
125
+ const maskOptions = {
126
+ maskWith: 'x',
127
+ maxMaskedCharacters: 6,
128
+ unmaskedStartCharacters: 2,
129
+ unmaskedEndCharacters: 2
130
+ };
131
+
132
+ describe('Mask with special input - input will generate an output', function () {
133
+ // first set with input generating an masked output
134
+ let testData = [
135
+ {
136
+ title: 'test with string length zero',
137
+ input: '',
138
+ output: ''
139
+ },
140
+ {
141
+ title: 'test input null',
142
+ input: null,
143
+ output: null
144
+ },
145
+ {
146
+ title: 'test input as number',
147
+ input: 12,
148
+ output: '12'
149
+ }
150
+ ];
151
+
152
+ testData.forEach(({ title, input, output }) => {
153
+ it(`special input - ${title}`, function () {
154
+ const masked = maskData.maskStringV2(input, maskOptions);
155
+ expect(masked).to.equal(output, 'masked output does not match expected value');
156
+ });
157
+ });
158
+ });
159
+
160
+ describe("Mask with object type input - input won't be masked", function () {
161
+ let testData = [
162
+ {
163
+ title: 'test input as array',
164
+ input: ['12']
165
+ },
166
+ {
167
+ title: 'test input as object',
168
+ input: { a: 'b', x: 'y' }
169
+ }
170
+ ];
171
+
172
+ testData.forEach(({ title, input }) => {
173
+ it(`special input - ${title}`, function () {
174
+ const masked = maskData.maskStringV2(input, maskOptions);
175
+ expect(masked).to.equal(input, "input won't be masked");
176
+ });
177
+ });
178
+ });
179
+ });
180
+ });