maskdata 1.3.1 → 1.3.3

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
@@ -2,48 +2,48 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
2
2
  <br/>Also, it provides utility methods to get a field, or replace a field from any complex/nested JSON.
3
3
 
4
4
  # Table of Contents
5
- - [Features](#features)
6
5
  - [Install maskdata](#install-maskdata)
7
- - [Version 1.3.1 Features](#release-features)
8
- - [How to Use](#how-to-use)
6
+ - [Version 1.3.3 Features](#release-features)
9
7
  - [Maskdata for Typescript](#maskdata-for-typescript)
10
- - [Mask Card number](#mask-card-number)
11
- - [Mask Email](#mask-email-id)
12
- - [Mask Email id with the default configuration](#mask-email-id-with-the-default-configuration)
13
- - [Mask JSON fields](#mask-json)
14
- - [JSON mask examples with nested fields](#json-mask-examples)
15
- - [Mask multiple fields at once using .* and [*].](#mask-multiple-fields)
16
- - [Mask Password](#mask-password)
17
- - [Mask Password with the default configuration](#mask-password-with-the-default-configuration)
18
- - [Mask Phone Number](#mask-phone-number)
19
- - [Mask Phone Number with the default configuration](#mask-phone-number-with-the-default-configuration)
20
- - [Generic String masking](#generic-string-masking)
21
- - [Mask words/characters in a string](#mask-the-characters-or-words-in-the-string)
22
- - [Mask UUID](#mask-uuid)
23
- - [Mask JWT Token](#mask-jwt-token)
24
- - [Get Nested JSON Property](#get-nested-json-property)
25
- - [Replace the value of a JSON field](#replace-the-value-of-a-json-field)
8
+ - [How to Mask](#how-to-mask)
9
+ - [Card number](#mask-card-number)
10
+ - [Email](#mask-email-id)
11
+ - [Password](#mask-password)
12
+ - [Phone Number](#mask-phone-number)
13
+ - [Generic String](#generic-string-masking)
14
+ - [Words/characters in a string](#mask-the-characters-or-words-in-the-string)
15
+ - [UUID](#mask-uuid)
16
+ - [JWT Token](#mask-jwt-token)
17
+ - [Object / JSON fields](#mask-json)
18
+ - [JSON mask examples with nested fields](#json-mask-examples)
19
+ - [Mask multiple fields at once using .* and [*].](#mask-multiple-fields)
20
+ - [Recursive masking / Mask all the fields with a name in the json](#recursive-masking)
21
+ - [Get Nested JSON Property](#get-nested-json-property)
22
+ - [Replace the value of a JSON field](#replace-the-value-of-a-json-field)
26
23
  - [Report Bugs](#report-bugs)
27
24
  - [Give a Star](#give-a-star)
28
25
  - [LICENSE - "MIT"](#license---mit)
29
26
 
30
- # Features
31
- * Mask Card numbers
32
- * Mask Email ids
33
- * Mask Password
34
- * Mask Phone numbers
35
- * Mask the given words/substrings from throughout a String
36
- * Mask UUIDs
37
- * Mask JWT tokens
38
- * Mask JSON - JSON can contain cards, emails, passwords, phones, strings, and UUIDs. Mask all of them with a single call using - [Mask JSON fields](#mask-json)
39
- * Get nested field from JSON
40
- * Set/Replace nested field values from JSON
41
- * [Maskdata for typescript](#maskdata-for-typescript)
27
+ <br/>
42
28
 
43
29
  # Install maskdata
44
30
  > npm i maskdata
45
31
 
32
+ <br/>
33
+
46
34
  # Release Features
35
+ ### Version 1.3.3
36
+ - Better documentation
37
+ - Bug fix and enabling strings with regex characters in the string mask functionality
38
+ - https://github.com/Sumukha1496/maskdata/issues/49
39
+ - Deprecate stringmask v1
40
+
41
+ ### Version 1.3.2
42
+ - Recursive masking feature: [Details](#recursive-masking)
43
+ - https://github.com/Sumukha1496/maskdata/issues/45
44
+ - https://github.com/Sumukha1496/maskdata/issues/47
45
+ - Fixed output length masking: [Mask Password](#mask-password) and [Mask String](#generic-string-masking)
46
+ - https://github.com/Sumukha1496/maskdata/issues/46
47
47
  ### Version 1.3.1
48
48
  - Removal of maskJSONFields function: https://www.npmjs.com/package/maskdata/v/1.1.10#mask-fields-in-a-json
49
49
  - String mask version2 for generic string masking in response to the below issues: [Details](#generic-string-masking)
@@ -51,19 +51,6 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
51
51
  - https://github.com/Sumukha1496/maskdata/issues/40
52
52
  - https://github.com/Sumukha1496/maskdata/issues/42
53
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
55
- - JWT token masking: Mask JWT tokens with configs to mask as per your need. More details: [Mask JWT Token](#mask-jwt-token)
56
- - Mask Json now supports JWT token masking also. More details: [Mask JWT in a JSON](#mask-json)
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"`
58
- - Better test coverage to the module with the addition of new test cases
59
- ### Version 1.2.3
60
- - Bug fix for masking a list of elements in the nested json. More details: [Mask multiple fields](#mask-multiple-fields)
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
62
-
63
- # How to Use
64
- ```javascript
65
- const MaskData = require('./maskdata');
66
- ```
67
54
 
68
55
  <br/>
69
56
 
@@ -71,7 +58,12 @@ const MaskData = require('./maskdata');
71
58
  > Follow this document for more details:
72
59
  [Maskdata for typescript](./MASKDATA_FOR_TYPESCRIPT.md)
73
60
 
74
- <br/>
61
+ <br/><br/>
62
+
63
+ # How to Mask
64
+ ```javascript
65
+ const MaskData = require('./maskdata');
66
+ ```
75
67
 
76
68
  ## Mask card number
77
69
  This will mask the digits in a card number.<br/>This will mask only the numerical data and not any non-numeric delimiters, alphabets, or any other types of data
@@ -103,14 +95,20 @@ const cardAfterMasking = MaskData.maskCard(cardNumber, maskCardOptions);
103
95
 
104
96
 
105
97
  ## Mask Email id
106
- Use this method instead of maskEmail(). To mask with the default options, don't pass the configurations.
98
+ Function to mask email id. Input must have a '@' in it.
107
99
  ```javascript
108
100
  const MaskData = require('./maskdata');
109
101
 
110
102
  const emailMask2Options = {
103
+ // Character to mask the data. The default value is '*'
111
104
  maskWith: "*",
105
+ // If we want to keep the starting 'n' characters before the '@' unmasked.
106
+ // Default value is 3
112
107
  unmaskedStartCharactersBeforeAt: 3,
108
+ // If we want to keep the last 'n' characters AFTER the '@' unmasked.
109
+ // Default value is 2
113
110
  unmaskedEndCharactersAfterAt: 2,
111
+ // Flag to mask or show '@'. Default value is false means do not mask
114
112
  maskAtTheRate: false
115
113
  };
116
114
 
@@ -131,14 +129,14 @@ Here,
131
129
  2. **unmaskedEndCharactersAfterAt** --> number of characters not to be masked starting from the end till @.
132
130
  * If unmaskedEndCharactersAfterAt > number of characters after @, then it will not mask the characters after @
133
131
 
134
- ### Mask only the characters before '@'
132
+ ### Mask only the characters before '@' / Show only the domain
135
133
  ```javascript
136
134
  const MaskData = require('./maskdata');
137
135
 
138
136
  const emailMask2Options = {
139
137
  maskWith: "*",
140
- unmaskedStartCharactersBeforeAt: 0,
141
- unmaskedEndCharactersAfterAt: 257, // Give a number which is more than the characters after @
138
+ unmaskedStartCharactersBeforeAt: 0, // Set this to 0 to mask the first part
139
+ unmaskedEndCharactersAfterAt: 257, // Enter a large number which is always more than the characters after @
142
140
  maskAtTheRate: false
143
141
  };
144
142
 
@@ -150,516 +148,172 @@ const maskedEmail = MaskData.maskEmail2(email, emailMask2Options);
150
148
 
151
149
  ```
152
150
 
153
- ## Mask Email id with the default configuration
151
+ ## Mask Password
152
+ ```javascript
153
+ const MaskData = require('./maskdata');
154
+
155
+ const maskPasswordOptions = {
156
+ // Character to mask the data
157
+ // default value is '*'
158
+ maskWith: "*",
159
+
160
+ // To limit the *s in the response when the password length is more
161
+ // Default value is 16
162
+ maxMaskedCharacters: 16,
163
+
164
+ // To fix the length of output irrespective of the length of the input. This comes in handy when the input length < maxMaskedCharacters but we want a fixed output length.
165
+ // Default value is undefined. If this value is set, then maxMaskedCharacters will not be considered and the output length will always be equal to fixedOutputLength characters.
166
+ fixedOutputLength: undefined,
167
+
168
+ // To show(not mask) first 'n' characters in the password/secret key.
169
+ // Default value is 0.
170
+ unmaskedStartCharacters: 0,
171
+
172
+ // To show(not mask) last 'n' characters in the password/secret key.
173
+ // Default value is 0.
174
+ unmaskedEndCharacters: 0
175
+ };
176
+
177
+ const password = "Password1$";
178
+
179
+ const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
180
+
181
+ //Output: **********
182
+
183
+ ## Mask Password with the default configuration
154
184
  To mask with the default options, don't pass the configurations.
155
185
  ```javascript
156
186
  const MaskData = require('./maskdata');
157
187
 
158
- /** Default Options
159
- maskWith: "*",
160
- unmaskedStartCharactersBeforeAt: 3,
161
- unmaskedEndCharactersAfterAt: 2,
162
- maskAtTheRate: false
163
- **/
164
- const email = "my.test.email@testEmail.com";
188
+ /** Default Options
189
+ maskWith: "*"
190
+ maxMaskedCharacters: 16,
191
+ unmaskedStartCharacters: 0,
192
+ fixedOutputLength: undefined,
193
+ unmaskedEndCharacters: 0
194
+ **/
165
195
 
166
- const maskedEmail = MaskData.maskEmail2(email);
196
+ const password = "Password1$";
167
197
 
168
- //Output: my.********@**********om
198
+ const maskedPassword = MaskData.maskPassword(password)
169
199
 
170
200
  ```
171
201
 
172
- ## Mask JSON
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.
202
+ #### Example: Mask password OR secretKey with some meta info at the end
174
203
 
175
204
  ```javascript
176
205
  const MaskData = require('./maskdata');
177
206
 
178
- // Default configs are as below. If specific masking changes are needed, use the corresponding configs for each type of field.
179
- const defaultjsonMask2Configs = {
180
- cardMaskOptions: defaultCardMaskOptions, // Optional
181
- cardFields: [], // List of card fields to be masked
207
+ const maskPasswordOptions = {
208
+ maskWith: "X",
209
+ maxMaskedCharacters: 20, // To limit the output String length to 20.
210
+ unmaskedStartCharacters: 4,
211
+ unmaskedEndCharacters: 9, // As the last 9 characters of the secret key is meta info which can be printed for debugging or other purposes
212
+ fixedOutputLength: undefined
213
+ };
182
214
 
183
- emailMaskOptions: defaultEmailMask2Options, // Optional
184
- emailFields: [], // List of email fields to be masked
215
+ const password = "TEST:U2VjcmV0S2V5MQ==:CLIENT-A";
185
216
 
186
- // For passwords, tokens, etc...
187
- passwordMaskOptions: defaultPasswordMaskOptions, // Optional
188
- passwordFields: [], // List of password fields to be masked
217
+ const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
218
+ //Output: TESTXXXXXXX:CLIENT-A
189
219
 
190
- phoneMaskOptions: defaultPhoneMaskOptions, // Optional
191
- phoneFields: [], // List of phone fields to be masked
220
+ maskPasswordOptions.unmaskedStartCharacters = 0;
192
221
 
193
- stringMaskOptions: defaultStringMaskOptions, // Mandatory if stringFields are given. Otherwise, stringFields won't be masked
194
- stringFields: [], // List of String fields to be masked
222
+ const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
223
+ //Output: XXXXXXXXXXX:CLIENT-A
224
+ ```
195
225
 
196
- uuidMaskOptions: defaultUuidMaskOptions, // Optional
197
- uuidFields: [], // List of UUID fields to be masked
198
226
 
199
- jwtMaskOptions: defaultJwtMaskOptions, // Optional
200
- jwtFields: [], // List of JWT fields to be masked
227
+ ## Mask Phone Number
228
+ ```javascript
229
+ const MaskData = require('./maskdata');
201
230
 
202
- genericStrings: [
203
- {
204
- config: defaultStringMaskV2Options,
205
- fields: []
206
- }
207
- ]
231
+ const maskPhoneOptions = {
232
+ // Character to mask the data
233
+ // default value is '*'
234
+ maskWith: "*",
235
+
236
+ //Should be positive Integer
237
+ // If the starting 'n' digits need to be unmasked
238
+ // Default value is 4
239
+ unmaskedStartDigits: 5,
240
+
241
+ // Should be positive Integer
242
+ //If the ending 'n' digits need to be unmasked
243
+ // Default value is 1
244
+ unmaskedEndDigits: 1
208
245
  };
246
+
247
+ const phoneNumber = "+911234567890";
248
+
249
+ const maskedPhoneNumber = MaskData.maskPhone(phoneNumber, maskPhoneOptions);
250
+ //Output: +9112*******0
251
+
209
252
  ```
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)
211
253
 
254
+ ## Mask Phone Number with the default configuration
255
+ To mask with the default options, don't pass the configurations.
212
256
  ```javascript
213
- const defaultjsonMask2Configs = {
214
- cardMaskOptions: {
215
- maskWith: "*",
216
- unmaskedStartDigits: 4,
217
- unmaskedEndDigits: 1
218
- },
219
- cardFields: [],
257
+ const MaskData = require('./maskdata');
220
258
 
221
- emailMaskOptions: {
222
- maskWith: "*",
223
- unmaskedStartCharactersBeforeAt: 3,
224
- unmaskedEndCharactersAfterAt: 2,
225
- maskAtTheRate: false
226
- },
227
- emailFields: [],
259
+ /** Default Options
260
+ maskWith: "*"
261
+ unmaskedStartDigits: 4
262
+ unmaskedEndDigits: 1
263
+ **/
264
+ const phoneNumber = "+111234567890";
228
265
 
229
- passwordMaskOptions: {
230
- maskWith: "*",
231
- maxMaskedCharacters: 16,
232
- unmaskedStartCharacters: 0,
233
- unmaskedEndCharacters: 0
234
- },
235
- passwordFields: [],
266
+ const maskedPhoneNumber = MaskData.maskPhone(phoneNumber);
236
267
 
237
- phoneMaskOptions: {
238
- maskWith: "*",
239
- unmaskedStartDigits: 4,
240
- unmaskedEndDigits: 1
241
- },
242
- phoneFields: [],
268
+ //Output: +911********0
243
269
 
244
- stringMaskOptions: {
245
- maskWith: "*",
246
- maskOnlyFirstOccurance: false,
247
- values: [],
248
- maskAll: false,
249
- maskSpace: true
250
- },
251
- stringFields: [],
270
+ ```
271
+ ## Generic string masking
272
+ This functionality can be used to mask any string with the below configs.
273
+ #### Example1
274
+ ```javascript
275
+ const MaskData = require('./maskdata');
252
276
 
253
- uuidMaskOptions: {
254
- maskWith: "*",
255
- unmaskedStartCharacters: 0,
256
- unmaskedEndCharacters: 0
257
- },
258
- uuidFields: [],
277
+ const defaultStringMaskV2Options = {
278
+ // Character to mask the data
279
+ // default value is '*'
280
+ maskWith: "*",
259
281
 
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
- ]
282
+ // This is to limit the maximun characters in the output.
283
+ // Default value is 256
284
+ maxMaskedCharacters: 256,
285
+
286
+ // To fix the length of output irrespective of the length of the input. This comes in handy when the input length < maxMaskedCharacters but we want a fixed output length.
287
+ // Default value is undefined. If this value is set, then maxMaskedCharacters will not be considered and the output length will always be equal to fixedOutputLength characters.
288
+ fixedOutputLength: undefined,
289
+
290
+ // To show(not mask) first 'n' characters of the string.
291
+ // Default value is 0.
292
+ unmaskedStartCharacters: 0,
293
+
294
+ // To show(not mask) last 'n' characters of the string.
295
+ // Default value is 0.
296
+ unmaskedEndCharacters: 0
281
297
  };
282
298
 
283
- ```
299
+ const string1 = "Password1$";
300
+
301
+ const maskedString = MaskData.maskStringV2(string1, defaultStringMaskV2Options);
302
+
303
+ //Output: **********
304
+ ```
305
+
306
+ #### Example2
284
307
 
285
- ### JSON mask examples
286
- Example1:
287
308
  ```javascript
288
- const jsonInput = {
289
- 'credit': '1234-5678-8765-1234',
290
- 'debit': '0000-1111-2222-3333',
291
- 'primaryEmail': 'primary@Email.com',
292
- 'secondaryEmail': 'secondary@Email.com',
293
- 'password': 'dummyPassword',
294
- 'homePhone': "+1 1234567890",
295
- 'workPhone': "+1 9876543210",
296
- 'addressLine1': "This is my addressline 1. This is my home",
297
- 'addressLine2': "AddressLine 2",
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
- }
308
- };
309
-
310
- const jsonMaskConfig = {
311
- cardFields: ['credit', 'debit'],
312
- emailFields: ['primaryEmail', 'secondaryEmail'],
313
- passwordFields: ['password'],
314
- phoneFields: ['homePhone', 'workPhone'],
315
- stringMaskOptions: {
316
- maskWith: "*",
317
- maskOnlyFirstOccurance: false,
318
- values: ["This"]
319
- },
320
- stringFields: ['addressLine1', 'addressLine2'],
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
- ]
339
- };
340
-
341
- const maskedJsonOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
342
-
343
- Output:
344
- {
345
- credit: '1234-****-****-***4',
346
- debit: '0000-****-****-***3',
347
- primaryEmail: 'pri****@*******om',
348
- secondaryEmail: 'sec******@*******om',
349
- password: '*************',
350
- homePhone: '+1 1********0',
351
- workPhone: '+1 9********0',
352
- addressLine1: '**** is my addressline 1. **** is my home',
353
- addressLine2: 'AddressLine 2',
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
- }
364
- }
365
-
366
-
367
- Example2: Mask with custom configs for each/any type of fields
368
-
369
- const jsonInput2 = {
370
- 'credit': '1234-5678-8765-1234',
371
- 'debit': '0000-1111-2222-3333',
372
- 'primaryEmail': 'primary@Email.com',
373
- 'secondaryEmail': 'secondary@Email.com',
374
- 'password': 'dummyPasswordANDdummyPassword',
375
- 'homePhone': "+1 1234567890",
376
- 'workPhone': "+1 9876543210",
377
- 'addressLine1': "This is my addressline 1. This is my home",
378
- 'addressLine2': "AddressLine 2",
379
- 'uuid1': '123e4567-e89b-12d3-a456-426614174000'
380
- };
381
-
382
- const jsonMaskConfig2 = {
383
- // Card
384
- cardMaskOptions: { maskWith: "X", unmaskedStartDigits: 2,unmaskedEndDigits: 4 },
385
- cardFields: ['credit', 'debit'],
386
-
387
- // Email
388
- emailMaskOptions: { maskWith: "*", unmaskedStartCharactersBeforeAt: 2, unmaskedEndCharactersAfterAt: 2, maskAtTheRate: false },
389
- emailFields: ['primaryEmail', 'secondaryEmail'],
390
-
391
- // Password
392
- passwordMaskOptions: { maskWith: "*", maxMaskedCharacters: 10, unmaskedStartCharacters: 0, unmaskedEndCharacters: 0 },
393
- passwordFields: ['password'],
394
-
395
- // Phone
396
- phoneMaskOptions: { maskWith: "*", unmaskedStartDigits: 2, unmaskedEndDigits: 1 },
397
- phoneFields: ['homePhone', 'workPhone'],
398
-
399
- // String
400
- stringMaskOptions: { maskWith: "*", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
401
- stringFields: ['addressLine1', 'addressLine2'],
402
-
403
- // UUID
404
- uuidMaskOptions: { maskWith: "*", unmaskedStartCharacters: 4, unmaskedEndCharacters: 2 },
405
- uuidFields: ['uuid1']
406
- };
407
-
408
- const maskedJsonOutput2 = maskData.maskJSON2(jsonInput2, jsonMaskConfig2);
409
-
410
- Output:
411
-
412
- {
413
- credit: '12XX-XXXX-XXXX-1234',
414
- debit: '00XX-XXXX-XXXX-3333',
415
- primaryEmail: 'pr*****@*******om',
416
- secondaryEmail: 'se*******@*******om',
417
- password: '**********',
418
- homePhone: '+1**********0',
419
- workPhone: '+1**********0',
420
- addressLine1: '**** ** ** *********** ** **** ** ** ****',
421
- addressLine2: '*********** *',
422
- uuid1: '123e****-****-****-****-**********00'
423
- }
424
- ```
425
-
426
- Example3: Mask nested json fields -> Use dot(.) and Array([]) notation to specify the inner fields.
427
-
428
- ```javascript
429
-
430
- const jsonInput2 = {
431
- cards: {
432
- creditCards: ['1234-5678-8765-1234', '1111-2222-1111-2222'],
433
- debitCards: ['0000-1111-2222-3333', '2222-1111-3333-4444']
434
- },
435
- emails: {
436
- primaryEmail: 'primary@Email.com',
437
- secondaryEmail: 'secondary@Email.com'
438
- },
439
- passwords: [
440
- ['dummyPasswordANDdummyPassword', 'dummyPasswordANDdummyPassword']
441
- ],
442
- phones: {
443
- homePhone: "+1 1234567890",
444
- workPhone: "+1 9876543210",
445
- },
446
- address: {
447
- addressLine1: "This is my addressline 1. This is my home",
448
- addressLine2: "AddressLine 2"
449
- },
450
- uuids: {
451
- uuid1: '123e4567-e89b-12d3-a456-426614174000'
452
- },
453
- jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ'
454
- };
455
-
456
- const jsonMaskConfig2 = {
457
- // Card
458
- cardMaskOptions: { maskWith: "X", unmaskedStartDigits: 2,unmaskedEndDigits: 4 },
459
- cardFields: ['cards.creditCards[0]', 'cards.creditCards[1]', 'cards.debitCards[0]', 'cards.debitCards[1]'],
460
-
461
- // Email
462
- emailMaskOptions: { maskWith: "*", unmaskedStartCharactersBeforeAt: 2, unmaskedEndCharactersAfterAt: 2, maskAtTheRate: false },
463
- emailFields: ['emails.primaryEmail', 'emails.secondaryEmail'],
464
-
465
- // Password
466
- passwordMaskOptions: { maskWith: "*", maxMaskedCharacters: 10, unmaskedStartCharacters: 0, unmaskedEndCharacters: 0 },
467
- passwordFields: ['passwords[0][0]]', 'passwords[0][1]'],
468
-
469
- // Phone
470
- phoneMaskOptions: { maskWith: "*", unmaskedStartDigits: 2, unmaskedEndDigits: 1 },
471
- phoneFields: ['phones.homePhone', 'phones.workPhone'],
472
-
473
- // String
474
- stringMaskOptions: { maskWith: "*", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
475
- stringFields: ['address.addressLine1', 'address.addressLine2'],
476
-
477
- // UUID
478
- uuidMaskOptions: { maskWith: "*", unmaskedStartCharacters: 4, unmaskedEndCharacters: 2 },
479
- uuidFields: ['uuids.uuid1']
480
-
481
- // JWT
482
- jwtMaskOptions: { maskWith: '*', maxMaskedCharacters: 512, maskDot: true, maskHeader: true, maskPayload: true, maskSignature: true},
483
- jwtFields: ['jwt']
484
- };
485
-
486
- const maskedJsonOutput2 = MaskData.maskJSON2(jsonInput2, jsonMaskConfig2);
487
- // OUTPUT:
488
- {
489
- cards: {
490
- creditCards: [ '12XX-XXXX-XXXX-1234', '11XX-XXXX-XXXX-2222' ],
491
- debitCards: [ '00XX-XXXX-XXXX-3333', '22XX-XXXX-XXXX-4444' ]
492
- },
493
- emails: {
494
- primaryEmail: 'pr*****@*******om',
495
- secondaryEmail: 'se*******@*******om'
496
- },
497
- passwords: [ [ '**********', '**********' ] ],
498
- phones: { homePhone: '+1**********0', workPhone: '+1**********0' },
499
- address: {
500
- addressLine1: '**** ** ** *********** ** **** ** ** ****',
501
- addressLine2: '*********** *'
502
- },
503
- uuids: { uuid1: '123e****-****-****-****-**********00' },
504
- jwt: '*********************************************************************************************************'
505
- }
506
-
507
- ```
508
- ## Mask Password
509
- ```javascript
510
- const MaskData = require('./maskdata');
511
-
512
- const maskPasswordOptions = {
513
- // Character to mask the data
514
- // default value is '*'
515
- maskWith: "*",
516
-
517
- // To limit the *s in the response when the password length is more
518
- // Default value is 16
519
- maxMaskedCharacters: 16,
520
-
521
- // To show(not mask) first 'n' characters in the password/secret key.
522
- // Default value is 0.
523
- unmaskedStartCharacters: 0,
524
-
525
- // To show(not mask) last 'n' characters in the password/secret key.
526
- // Default value is 0.
527
- unmaskedEndCharacters: 0
528
- };
529
-
530
- const password = "Password1$";
531
-
532
- const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
533
-
534
- //Output: **********
535
-
536
- ## Mask Password with the default configuration
537
- To mask with the default options, don't pass the configurations.
538
- ```javascript
539
- const MaskData = require('./maskdata');
540
-
541
- /** Default Options
542
- maskWith: "*"
543
- maxMaskedCharacters: 16,
544
- unmaskedStartCharacters: 0,
545
- unmaskedEndCharacters: 0
546
- **/
547
-
548
- const password = "Password1$";
549
-
550
- const maskedPassword = MaskData.maskPassword(password)
551
-
552
- ```
553
-
554
- #### Example: Mask password OR secretKey with some meta info at the end
555
-
556
- ```javascript
557
- const MaskData = require('./maskdata');
558
-
559
- const maskPasswordOptions = {
560
- maskWith: "X",
561
- maxMaskedCharacters: 20, // To limit the output String length to 20.
562
- unmaskedStartCharacters: 4,
563
- unmaskedEndCharacters: 9 // As the last 9 characters of the secret key is meta info which can be printed for debugging or other purposes
564
- };
565
-
566
- const password = "TEST:U2VjcmV0S2V5MQ==:CLIENT-A";
567
-
568
- const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
569
- //Output: TESTXXXXXXX:CLIENT-A
570
-
571
- maskPasswordOptions.unmaskedStartCharacters = 0;
572
-
573
- const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
574
- //Output: XXXXXXXXXXX:CLIENT-A
575
- ```
576
-
577
-
578
- ## Mask Phone Number
579
- ```javascript
580
- const MaskData = require('./maskdata');
581
-
582
- const maskPhoneOptions = {
583
- // Character to mask the data
584
- // default value is '*'
585
- maskWith: "*",
586
-
587
- //Should be positive Integer
588
- // If the starting 'n' digits need to be unmasked
589
- // Default value is 4
590
- unmaskedStartDigits: 5,
591
-
592
- // Should be positive Integer
593
- //If the ending 'n' digits need to be unmasked
594
- // Default value is 1
595
- unmaskedEndDigits: 1
596
- };
597
-
598
- const phoneNumber = "+911234567890";
599
-
600
- const maskedPhoneNumber = MaskData.maskPhone(phoneNumber, maskPhoneOptions);
601
- //Output: +9112*******0
602
-
603
- ```
604
-
605
- ## Mask Phone Number with the default configuration
606
- To mask with the default options, don't pass the configurations.
607
- ```javascript
608
- const MaskData = require('./maskdata');
609
-
610
- /** Default Options
611
- maskWith: "*"
612
- unmaskedStartDigits: 4
613
- unmaskedEndDigits: 1
614
- **/
615
- const phoneNumber = "+111234567890";
616
-
617
- const maskedPhoneNumber = MaskData.maskPhone(phoneNumber);
618
-
619
- //Output: +911********0
620
-
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: "*",
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
309
+ const MaskData = require('./maskdata');
310
+
311
+ const stringMaskV2Options = {
312
+ maskWith: "X",
313
+ maxMaskedCharacters: 20, // To limit the output length to 20.
314
+ unmaskedStartCharacters: 4,
315
+ unmaskedEndCharacters: 9,
316
+ fixedOutputLength: undefined
663
317
  };
664
318
 
665
319
  const secret = "TEST:U2VjcmV0S2V5MQ==:CLIENT-A";
@@ -885,85 +539,373 @@ const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
885
539
  // Output:
886
540
  ************************************.eyJpYXQiOjE1MTYyMzkwMjJ9.*******************************************
887
541
 
888
- ```
542
+ ```
543
+
544
+ #### Example: Mask all parts except dots and limit the max masked characters in the output to lower value, say 16
545
+
546
+ ```javascript
547
+
548
+ const MaskData = require('./maskdata');
549
+
550
+ const jwtMaskOptions = {
551
+ maskWith: '*',
552
+ maxMaskedCharacters: 16,
553
+ maskDot: false,
554
+ maskHeader: true,
555
+ maskPayload: true,
556
+ maskSignature: true
557
+ };
558
+
559
+ const jwt =
560
+ 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ';
561
+
562
+ /** In this example,
563
+ * Header = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
564
+ * Payload = eyJpYXQiOjE1MTYyMzkwMjJ9
565
+ * Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
566
+ */
567
+ const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
568
+
569
+ // Output:
570
+ ******.*****.*****
571
+
572
+ ```
573
+
574
+ ## Mask JSON
575
+ This is the functionality in version 1.2.0+ to handle masking of multiple types of data in a JSON with a single function call.
576
+
577
+ ```javascript
578
+ const MaskData = require('./maskdata');
579
+
580
+ // Default configs are as below. If specific masking changes are needed, use the corresponding configs for each type of field.
581
+ const defaultjsonMask2Configs = {
582
+ cardMaskOptions: defaultCardMaskOptions, // Optional
583
+ cardFields: [], // List of card fields to be masked
584
+
585
+ emailMaskOptions: defaultEmailMask2Options, // Optional
586
+ emailFields: [], // List of email fields to be masked
587
+
588
+ passwordMaskOptions: defaultPasswordMaskOptions, // Optional
589
+ passwordFields: [], // List of password fields to be masked
590
+
591
+ phoneMaskOptions: defaultPhoneMaskOptions, // Optional
592
+ phoneFields: [], // List of phone fields to be masked
593
+
594
+ stringMaskOptions: defaultStringMaskOptions, // Mandatory if stringFields are given. Otherwise, stringFields won't be masked
595
+ stringFields: [], // List of String fields to be masked
596
+
597
+ uuidMaskOptions: defaultUuidMaskOptions, // Optional
598
+ uuidFields: [], // List of UUID fields to be masked
599
+
600
+ jwtMaskOptions: defaultJwtMaskOptions, // Optional
601
+ jwtFields: [], // List of JWT fields to be masked
602
+
603
+ genericStrings: [ // This list can have multiple entries of { config: defaultStringMaskV2Options, fields: [] }
604
+ {
605
+ config: defaultStringMaskV2Options,
606
+ fields: []
607
+ }
608
+ ]
609
+ };
610
+ ```
611
+ > 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)
612
+
613
+ ```javascript
614
+ const defaultjsonMask2Configs = {
615
+ cardMaskOptions: {
616
+ maskWith: "*",
617
+ unmaskedStartDigits: 4,
618
+ unmaskedEndDigits: 1
619
+ },
620
+ cardFields: [],
621
+
622
+ emailMaskOptions: {
623
+ maskWith: "*",
624
+ unmaskedStartCharactersBeforeAt: 3,
625
+ unmaskedEndCharactersAfterAt: 2,
626
+ maskAtTheRate: false
627
+ },
628
+ emailFields: [],
629
+
630
+ passwordMaskOptions: {
631
+ maskWith: "*",
632
+ maxMaskedCharacters: 16,
633
+ fixedOutputLength: undefined,
634
+ unmaskedStartCharacters: 0,
635
+ unmaskedEndCharacters: 0
636
+ },
637
+ passwordFields: [],
638
+
639
+ phoneMaskOptions: {
640
+ maskWith: "*",
641
+ unmaskedStartDigits: 4,
642
+ unmaskedEndDigits: 1
643
+ },
644
+ phoneFields: [],
645
+
646
+ stringMaskOptions: {
647
+ maskWith: "*",
648
+ maskOnlyFirstOccurance: false,
649
+ values: [],
650
+ maskAll: false,
651
+ maskSpace: true
652
+ },
653
+ stringFields: [],
654
+
655
+ uuidMaskOptions: {
656
+ maskWith: "*",
657
+ unmaskedStartCharacters: 0,
658
+ unmaskedEndCharacters: 0
659
+ },
660
+ uuidFields: [],
661
+
662
+ jwtMaskOptions: {
663
+ maskWith: '*',
664
+ maxMaskedCharacters: 512,
665
+ maskDot: true,
666
+ maskHeader: true,
667
+ maskPayload: true,
668
+ maskSignature: true
669
+ },
670
+ jwtFields: [],
671
+ // To extend the mask function to generic strings.
672
+ genericStrings: [
673
+ {
674
+ config: {
675
+ maskWith: "*",
676
+ maxMaskedCharacters: 256,
677
+ fixedOutputLength: undefined,
678
+ unmaskedStartDigits: 0,
679
+ unmaskedEndDigits: 0
680
+ },
681
+ fields: []
682
+ }
683
+ ]
684
+ };
685
+
686
+ ```
687
+
688
+ ### JSON mask examples
689
+ Example1:
690
+ ```javascript
691
+ const jsonInput = {
692
+ 'credit': '1234-5678-8765-1234',
693
+ 'debit': '0000-1111-2222-3333',
694
+ 'primaryEmail': 'primary@Email.com',
695
+ 'secondaryEmail': 'secondary@Email.com',
696
+ 'password': 'dummyPassword',
697
+ 'homePhone': "+1 1234567890",
698
+ 'workPhone': "+1 9876543210",
699
+ 'addressLine1': "This is my addressline 1. This is my home",
700
+ 'addressLine2': "AddressLine 2",
701
+ 'uuid1': '123e4567-e89b-12d3-a456-426614174000',
702
+ 'randomStrings': {
703
+ 'row1': 'This is row 1 random string',
704
+ 'row2': ['Entry1', 'Entry2', 'Entry3'],
705
+ 'row3': {
706
+ 'key1': 'Row3 Object 1',
707
+ 'key2': 'Row3 Object 2',
708
+ 'key3': ['Entry1', 'Entry2', 'Entry3']
709
+ }
710
+ }
711
+ };
712
+
713
+ const jsonMaskConfig = {
714
+ cardFields: ['credit', 'debit'],
715
+ emailFields: ['primaryEmail', 'secondaryEmail'],
716
+ passwordFields: ['password'],
717
+ phoneFields: ['homePhone', 'workPhone'],
718
+ stringMaskOptions: {
719
+ maskWith: "*",
720
+ maskOnlyFirstOccurance: false,
721
+ values: ["This"]
722
+ },
723
+ stringFields: ['addressLine1', 'addressLine2'],
724
+ uuidFields: ['uuid1'],
725
+ genericStrings: [
726
+ {
727
+ fields: ['randomStrings.row1'],
728
+ config: {
729
+ maskWith: '*',
730
+ unmaskedStartCharacters: 2,
731
+ unmaskedEndCharacters: 3,
732
+ maxMaskedCharacters: 8
733
+ }
734
+ },
735
+ { fields: ['randomStrings.row2.*'], config: { maskWith: 'X', unmaskedEndCharacters: 1 } },
736
+ { fields: ['randomStrings.row3.key1'] },
737
+ {
738
+ fields: ['randomStrings.row3.key3.*'],
739
+ config: { maskWith: '@', unmaskedEndCharacters: 1 }
740
+ }
741
+ ]
742
+ };
743
+
744
+ const maskedJsonOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
745
+
746
+ Output:
747
+ {
748
+ credit: '1234-****-****-***4',
749
+ debit: '0000-****-****-***3',
750
+ primaryEmail: 'pri****@*******om',
751
+ secondaryEmail: 'sec******@*******om',
752
+ password: '*************',
753
+ homePhone: '+1 1********0',
754
+ workPhone: '+1 9********0',
755
+ addressLine1: '**** is my addressline 1. **** is my home',
756
+ addressLine2: 'AddressLine 2',
757
+ uuid1: '********-****-****-****-************',
758
+ randomStrings: {
759
+ row1: 'Th***ing',
760
+ row2: ['XXXXX1', 'XXXXX2', 'XXXXX3'],
761
+ row3: {
762
+ key1: '*************',
763
+ key2: 'Row3 Object 2',
764
+ key3: ['@@@@@1', '@@@@@2', '@@@@@3']
765
+ }
766
+ }
767
+ }
768
+
769
+
770
+ Example2: Mask with custom configs for each/any type of fields
771
+
772
+ const jsonInput2 = {
773
+ 'credit': '1234-5678-8765-1234',
774
+ 'debit': '0000-1111-2222-3333',
775
+ 'primaryEmail': 'primary@Email.com',
776
+ 'secondaryEmail': 'secondary@Email.com',
777
+ 'password': 'dummyPasswordANDdummyPassword',
778
+ 'homePhone': "+1 1234567890",
779
+ 'workPhone': "+1 9876543210",
780
+ 'addressLine1': "This is my addressline 1. This is my home",
781
+ 'addressLine2': "AddressLine 2",
782
+ 'uuid1': '123e4567-e89b-12d3-a456-426614174000'
783
+ };
784
+
785
+ const jsonMaskConfig2 = {
786
+ // Card
787
+ cardMaskOptions: { maskWith: "X", unmaskedStartDigits: 2,unmaskedEndDigits: 4 },
788
+ cardFields: ['credit', 'debit'],
789
+
790
+ // Email
791
+ emailMaskOptions: { maskWith: "*", unmaskedStartCharactersBeforeAt: 2, unmaskedEndCharactersAfterAt: 2, maskAtTheRate: false },
792
+ emailFields: ['primaryEmail', 'secondaryEmail'],
889
793
 
890
- #### Example: Mask all parts except dots and limit the max masked characters in the output to lower value, say 16
794
+ // Password
795
+ passwordMaskOptions: { maskWith: "*", maxMaskedCharacters: 10, unmaskedStartCharacters: 0, unmaskedEndCharacters: 0 },
796
+ passwordFields: ['password'],
891
797
 
892
- ```javascript
798
+ // Phone
799
+ phoneMaskOptions: { maskWith: "*", unmaskedStartDigits: 2, unmaskedEndDigits: 1 },
800
+ phoneFields: ['homePhone', 'workPhone'],
893
801
 
894
- const MaskData = require('./maskdata');
802
+ // String
803
+ stringMaskOptions: { maskWith: "*", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
804
+ stringFields: ['addressLine1', 'addressLine2'],
895
805
 
896
- const jwtMaskOptions = {
897
- maskWith: '*',
898
- maxMaskedCharacters: 16,
899
- maskDot: false,
900
- maskHeader: true,
901
- maskPayload: true,
902
- maskSignature: true
806
+ // UUID
807
+ uuidMaskOptions: { maskWith: "*", unmaskedStartCharacters: 4, unmaskedEndCharacters: 2 },
808
+ uuidFields: ['uuid1']
903
809
  };
904
810
 
905
- const jwt =
906
- 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ';
907
-
908
- /** In this example,
909
- * Header = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
910
- * Payload = eyJpYXQiOjE1MTYyMzkwMjJ9
911
- * Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
912
- */
913
- const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
811
+ const maskedJsonOutput2 = maskData.maskJSON2(jsonInput2, jsonMaskConfig2);
914
812
 
915
- // Output:
916
- ******.*****.*****
813
+ Output:
917
814
 
815
+ {
816
+ credit: '12XX-XXXX-XXXX-1234',
817
+ debit: '00XX-XXXX-XXXX-3333',
818
+ primaryEmail: 'pr*****@*******om',
819
+ secondaryEmail: 'se*******@*******om',
820
+ password: '**********',
821
+ homePhone: '+1**********0',
822
+ workPhone: '+1**********0',
823
+ addressLine1: '**** ** ** *********** ** **** ** ** ****',
824
+ addressLine2: '*********** *',
825
+ uuid1: '123e****-****-****-****-**********00'
826
+ }
918
827
  ```
919
828
 
920
- ## Get Nested JSON Property
921
- This method returns the value of the nested JSON property if it exists. Otherwise, it returns ```undefined```
922
- ```javascript
923
- const MaskData = require('./maskdata');
924
-
925
- const innerPropety = Maskdata.getInnerProperty(object, field);
829
+ Example3: Mask nested json fields -> Use dot(.) and Array([]) notation to specify the inner fields.
926
830
 
927
- Example:
831
+ ```javascript
928
832
 
929
- const nestedObject = {
930
- level1: {
931
- field1: "field1",
932
- level2: {
933
- field2: "field2",
934
- level3: {
935
- field3: "field3",
936
- field4: [ { Hello: "world" }, { Hello: "Newworld" }, "Just a String" ]
937
- }
938
- }
833
+ const jsonInput2 = {
834
+ cards: {
835
+ creditCards: ['1234-5678-8765-1234', '1111-2222-1111-2222'],
836
+ debitCards: ['0000-1111-2222-3333', '2222-1111-3333-4444']
939
837
  },
940
- value1: "value"
838
+ emails: {
839
+ primaryEmail: 'primary@Email.com',
840
+ secondaryEmail: 'secondary@Email.com'
841
+ },
842
+ passwords: [
843
+ ['dummyPasswordANDdummyPassword', 'dummyPasswordANDdummyPassword']
844
+ ],
845
+ phones: {
846
+ homePhone: "+1 1234567890",
847
+ workPhone: "+1 9876543210",
848
+ },
849
+ address: {
850
+ addressLine1: "This is my addressline 1. This is my home",
851
+ addressLine2: "AddressLine 2"
852
+ },
853
+ uuids: {
854
+ uuid1: '123e4567-e89b-12d3-a456-426614174000'
855
+ },
856
+ jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ'
941
857
  };
942
858
 
943
- const innerPropety = Maskdata.getInnerProperty(nestedObject, 'level1.level2.level3.field4[0].Hello');
859
+ const jsonMaskConfig2 = {
860
+ // Card
861
+ cardMaskOptions: { maskWith: "X", unmaskedStartDigits: 2,unmaskedEndDigits: 4 },
862
+ cardFields: ['cards.creditCards[0]', 'cards.creditCards[1]', 'cards.debitCards[0]', 'cards.debitCards[1]'],
944
863
 
945
- ```
864
+ // Email
865
+ emailMaskOptions: { maskWith: "*", unmaskedStartCharactersBeforeAt: 2, unmaskedEndCharactersAfterAt: 2, maskAtTheRate: false },
866
+ emailFields: ['emails.primaryEmail', 'emails.secondaryEmail'],
946
867
 
868
+ // Password
869
+ passwordMaskOptions: { maskWith: "*", maxMaskedCharacters: 10, unmaskedStartCharacters: 0, unmaskedEndCharacters: 0 },
870
+ passwordFields: ['passwords[0][0]]', 'passwords[0][1]'],
947
871
 
948
- ## Replace the value of a JSON field
949
- To replace a value by keeping the type.
950
- ```javascript
872
+ // Phone
873
+ phoneMaskOptions: { maskWith: "*", unmaskedStartDigits: 2, unmaskedEndDigits: 1 },
874
+ phoneFields: ['phones.homePhone', 'phones.workPhone'],
951
875
 
952
- const input = {
953
- name: "John",
954
- age: 33,
955
- married: true
956
- }
876
+ // String
877
+ stringMaskOptions: { maskWith: "*", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
878
+ stringFields: ['address.addressLine1', 'address.addressLine2'],
957
879
 
958
- let afterReplacing = MaskData.replaceValue(input, 'age', 99);
959
- afterReplacing = MaskData.replaceValue(input, 'married', false);
880
+ // UUID
881
+ uuidMaskOptions: { maskWith: "*", unmaskedStartCharacters: 4, unmaskedEndCharacters: 2 },
882
+ uuidFields: ['uuids.uuid1']
960
883
 
961
- //Output:
962
- Before replacing: {"name":"John","age":33,"married":true}
963
- After replacing: {"name":"John","age":99,"married":false}
884
+ // JWT
885
+ jwtMaskOptions: { maskWith: '*', maxMaskedCharacters: 512, maskDot: true, maskHeader: true, maskPayload: true, maskSignature: true},
886
+ jwtFields: ['jwt']
887
+ };
964
888
 
965
- Type of age: number
966
- Type of married: boolean
889
+ const maskedJsonOutput2 = MaskData.maskJSON2(jsonInput2, jsonMaskConfig2);
890
+ // OUTPUT:
891
+ {
892
+ cards: {
893
+ creditCards: [ '12XX-XXXX-XXXX-1234', '11XX-XXXX-XXXX-2222' ],
894
+ debitCards: [ '00XX-XXXX-XXXX-3333', '22XX-XXXX-XXXX-4444' ]
895
+ },
896
+ emails: {
897
+ primaryEmail: 'pr*****@*******om',
898
+ secondaryEmail: 'se*******@*******om'
899
+ },
900
+ passwords: [ [ '**********', '**********' ] ],
901
+ phones: { homePhone: '+1**********0', workPhone: '+1**********0' },
902
+ address: {
903
+ addressLine1: '**** ** ** *********** ** **** ** ** ****',
904
+ addressLine2: '*********** *'
905
+ },
906
+ uuids: { uuid1: '123e****-****-****-****-**********00' },
907
+ jwt: '*********************************************************************************************************'
908
+ }
967
909
 
968
910
  ```
969
911
 
@@ -1047,6 +989,145 @@ const maskedOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
1047
989
 
1048
990
  ```
1049
991
 
992
+ ### Recursive Masking
993
+ If you want to mask all the fields with a name irrespective of its level in the json, use the `*{field_name}`.
994
+
995
+ Example:
996
+ ```javascript
997
+
998
+ const inputJson = {
999
+ car: "Mazda",
1000
+ addressLine1: "Mask me!",
1001
+ deeper: {
1002
+ addressLine1: "Mask me!",
1003
+ evenDeeper: { addressLine1: "Mask me!" },
1004
+ },
1005
+ };
1006
+
1007
+ const fieldsToMask = ["*addressLine1"]; // Specify the field with a '*' at the beginning and NO dot(.) anywhere else in that field.
1008
+
1009
+ const jsonMaskConfig = {
1010
+ genericStrings: [
1011
+ {
1012
+ config: {
1013
+ maskWith: "*",
1014
+ maxMaskedCharacters: 6
1015
+ },
1016
+ fields: fieldsToMask
1017
+ },
1018
+ ],
1019
+ };
1020
+
1021
+ const maskedOutput = maskData.maskJSON2(inputJson, jsonMaskConfig);
1022
+
1023
+ // Output:
1024
+ {
1025
+ car: 'Mazda',
1026
+ addressLine1: '******',
1027
+ deeper: { addressLine1: '******', evenDeeper: { addressLine1: '******' } }
1028
+ }
1029
+ ```
1030
+
1031
+ Example 2:
1032
+ Using Recursive masking to mask card numbers in a json using mask card config. Recursive masking can be used to mask all types of data mentioned [here](#mask-json)
1033
+
1034
+ ```javascript
1035
+
1036
+ const inputJson = {
1037
+ cards: [
1038
+ {
1039
+ number: "1234-5678-0123-0000"
1040
+ },
1041
+ {
1042
+ number: "1111-2222-3333-4444"
1043
+ },
1044
+ {
1045
+ number: "2222-4444-6666-8888"
1046
+ },
1047
+ {
1048
+ number: "1111-3333-5555-7777"
1049
+ },
1050
+ {
1051
+ number: "0000-0000-0000-0000"
1052
+ }
1053
+ ]
1054
+ };
1055
+
1056
+ const cardfieldsToMask = ["*number"]; // Specify the field with a '*' at the beginning and NO dot(.) anywhere else in that field.
1057
+
1058
+ const jsonMaskConfig = {
1059
+ cardMaskOptions: {
1060
+ maskWith: "*",
1061
+ unmaskedStartDigits: 4,
1062
+ unmaskedEndDigits: 1
1063
+ },
1064
+ cardFields: cardfieldsToMask,
1065
+ };
1066
+
1067
+ const maskedOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
1068
+
1069
+ // Output:
1070
+ {
1071
+ cards: [
1072
+ { number: '1234-****-****-***0' },
1073
+ { number: '1111-****-****-***4' },
1074
+ { number: '2222-****-****-***8' },
1075
+ { number: '1111-****-****-***7' },
1076
+ { number: '0000-****-****-***0' }
1077
+ ]
1078
+ }
1079
+ ```
1080
+
1081
+ ## Get Nested JSON Property
1082
+ This method returns the value of the nested JSON property if it exists. Otherwise, it returns ```undefined```
1083
+ ```javascript
1084
+ const MaskData = require('./maskdata');
1085
+
1086
+ const innerPropety = Maskdata.getInnerProperty(object, field);
1087
+
1088
+ Example:
1089
+
1090
+ const nestedObject = {
1091
+ level1: {
1092
+ field1: "field1",
1093
+ level2: {
1094
+ field2: "field2",
1095
+ level3: {
1096
+ field3: "field3",
1097
+ field4: [ { Hello: "world" }, { Hello: "Newworld" }, "Just a String" ]
1098
+ }
1099
+ }
1100
+ },
1101
+ value1: "value"
1102
+ };
1103
+
1104
+ const innerPropety = Maskdata.getInnerProperty(nestedObject, 'level1.level2.level3.field4[0].Hello');
1105
+
1106
+ ```
1107
+
1108
+
1109
+ ## Replace the value of a JSON field
1110
+ To replace a value by keeping the type.
1111
+ ```javascript
1112
+
1113
+ const input = {
1114
+ name: "John",
1115
+ age: 33,
1116
+ married: true
1117
+ }
1118
+
1119
+ let afterReplacing = MaskData.replaceValue(input, 'age', 99);
1120
+ afterReplacing = MaskData.replaceValue(input, 'married', false);
1121
+
1122
+ //Output:
1123
+ Before replacing: {"name":"John","age":33,"married":true}
1124
+ After replacing: {"name":"John","age":99,"married":false}
1125
+
1126
+ Type of age: number
1127
+ Type of married: boolean
1128
+
1129
+ ```
1130
+
1050
1131
 
1051
1132
  # Report Bugs
1052
1133
  If there is any help needed with the library functionalities or if there is any bug/issue, please raise an issue in GitHub: https://github.com/Sumukha1496/maskdata/issues