maskdata 1.1.10 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,133 +1,438 @@
1
- maskdata is a Node.js module to mask various kinds of data. With the help of maskdata, you can mask email, phone number, card number, JSON fields, password etc..
2
- <br/>Also, it provides utility methods to get a field, or replace a field from any complex/nested json.
1
+ maskdata is a Node.js module to mask various kinds of data. With the help of maskdata, you can mask email, phone number, card number, JSON fields, password, etc...
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
5
  - [Features](#features)
6
6
  - [Install maskdata](#install-maskdata)
7
- - [Version 1.1.10 Features](#release-features)
7
+ - [Version 1.2.1 Features](#release-features)
8
8
  - [How to Use](#how-to-use)
9
- - [Mask Phone Number](#mask-phone-number)
10
- - [Mask Phone Number with the default configuration](#mask-phone-number-with-the-default-configuration)
11
- - [Mask Password](#mask-password)
12
- - [Mask Password with the default configuration](#mask-password-with-the-default-configuration)
9
+ - [Maskdata for Typescript](#maskdata-for-typescript)
10
+ - [Mask Card number](#mask-card-number)
13
11
  - [Mask Email](#mask-email-id)
14
- - [Mask Email id with the default configuration](#mask-email-id-with-the-default-configuration)
15
- - [Get Nested Json Property](#get-nested-json-property)
16
- - [Replace the value of a json field](#replace-the-value-of-a-json-field)
17
- - [Mask JSON fields](#mask-fields-in-a-json)
18
- - [Mask nested JSON fields](#mask-fields-of-a-nested-object)
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 Password](#mask-password)
16
+ - [Mask Password with the default configuration](#mask-password-with-the-default-configuration)
17
+ - [Mask Phone Number](#mask-phone-number)
18
+ - [Mask Phone Number with the default configuration](#mask-phone-number-with-the-default-configuration)
19
19
  - [Mask words/characters in a string](#mask-the-characters-or-words-in-the-string)
20
- - [Mask Card number](#mask-card-number)
20
+ - [Mask UUID](#mask-uuid)
21
+ - [Get Nested JSON Property](#get-nested-json-property)
22
+ - [Replace the value of a JSON field](#replace-the-value-of-a-json-field)
23
+ - [Mask JSON fields](#mask-fields-in-a-json)
24
+ - [Mask nested JSON fields](#mask-fields-of-a-nested-object)
21
25
  - [Report Bugs](#report-bugs)
22
- - [How to run test cases](#run-tests)
23
26
  - [Give a Star](#give-a-star)
24
27
  - [LICENSE - "MIT"](#license---mit)
25
28
 
26
29
  # Features
27
- * Mask password
30
+ * Mask Card numbers
31
+ * Mask Email ids
32
+ * Mask Password
28
33
  * Mask Phone numbers
29
- * Mask Email
30
- * Mask desired fields in a JSON
31
34
  * Mask the given words/substrings from throughout a String
32
- * Mask the card number
35
+ * Mask UUIDs
36
+ * 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)
33
37
  * Get nested field from JSON
34
38
  * Set/Replace nested field values from JSON
39
+ * [Maskdata for typescript](#maskdata-for-typescript)
35
40
 
36
41
  # Install maskdata
37
42
  > npm i maskdata
38
43
 
39
44
  # Release Features
45
+ ### Version 1.2.1
46
+ - Type definitions for the typescript developers. Yay!!!
47
+ 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](#mask-data-for-typescript)
48
+ ### Version 1.2.0
49
+ - Support for masking cards, emails, passwords, phones, strings, and UUIDs. Mask all of them with a single call using - [Mask JSON fields](#mask-json)
50
+ - Support for masking UUID - [Mask UUID](#mask-uuid)
51
+ - Minor bug fix in card masking.
52
+ - Extensive testing with the help of mocha test cases for all features covering more than 110 test cases.
53
+ - Improved Documentation
40
54
  ### Version: 1.1.10
41
- - Support of maxMaskedCharacters in masking json fields: https://github.com/Sumukha1496/maskdata/issues/21
42
- - This is applicable only if the type of value is string. Details: [Mask JSON fields](#mask-fields-in-a-json)
55
+ - Support of maxMaskedCharacters in masking JSON fields: https://github.com/Sumukha1496/maskdata/issues/21
56
+ - This is applicable only if the type of value is a string. Details: [Mask JSON fields](#mask-fields-in-a-json)
43
57
  ### Version: 1.1.8
44
- - Mocha test cases for most of the usecases.
58
+ - Mocha test cases for most of the use cases.
45
59
  https://github.com/Sumukha1496/maskdata/pull/23
46
60
  - Handling null/Empty inputs --> Mask function will return the input itself.
47
61
  ### Version: 1.1.6
48
- - Feature to mask all the characters in the String along with mask/not mask spaces in the string.
49
- - [Mask all characters in the String](#mask-all-characters-in-the-string)
50
- ### Version: 1.1.4
51
- - Password masking bug fix: https://github.com/Sumukha1496/maskdata/issues/12
62
+ - Feature to mask all the characters in the String along with mask/visible spaces in the string.
52
63
 
53
- ### Version: 1.1.3
54
- - Unmasked start and end characters feature in password masking. It can be used in cases where the password/Secret key has some metadata info which needs to be printed or shown to users.
55
- Ticket: https://github.com/Sumukha1496/maskdata/issues/11. Both the fields are opional and can find more info here: [Mask Password](#mask-password)
64
+ # How to Use
65
+ ```javascript
66
+ const MaskData = require('./maskdata');
67
+ ```
56
68
 
57
- #### Example: Mask password OR secretKey with some meta info at the end
69
+ <br/>
70
+
71
+ ## Maskdata for Typescript
72
+ > Follow this document for more details:
73
+ [Maskdata for typescript](./MASKDATA_FOR_TYPESCRIPT.md) <br/><br/>
58
74
 
75
+
76
+ ## Mask card number
77
+ 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
59
78
  ```javascript
60
79
  const MaskData = require('./maskdata');
61
80
 
62
- const maskPasswordOptions = {
63
- maskWith: "X",
64
- maxMaskedCharacters: 20, // To limit the output String length to 20.
65
- unmaskedStartCharacters: 4,
66
- unmaskedEndCharacters: 9 // As last 9 characters of the secret key is a meta info which can be printed for debugging or other purpose
81
+ const maskCardOptions = {
82
+ // Character to mask the data. The default value is '*'
83
+ maskWith: "*",
84
+
85
+ // Should be positive Integer
86
+ // If the starting 'n' digits need to be visible/unmasked
87
+ // Default value is 4
88
+ unmaskedStartDigits: 4,
89
+
90
+ // Should be positive Integer
91
+ // If the ending 'n' digits need to be visible/unmasked
92
+ // Default value is 1.
93
+ unmaskedEndDigits: 1
67
94
  };
68
95
 
69
- const password = "TEST:U2VjcmV0S2V5MQ==:CLIENT-A";
96
+ const cardNumber = "1234-5678-1234-5678";
70
97
 
71
- const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
72
- //Output: TESTXXXXXXX:CLIENT-A
98
+ const cardAfterMasking = MaskData.maskCard(cardNumber, maskCardOptions);
73
99
 
74
- maskPasswordOptions.unmaskedStartCharacters = 0;
100
+ //Output: 1234-****-****-***8
75
101
 
76
- const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
77
- //Output: XXXXXXXXXXX:CLIENT-A
78
102
  ```
79
103
 
80
- # How to Use
104
+
105
+ ## Mask Email id
106
+ Use this method instead of maskEmail(). To mask with the default options, don't pass the configurations.
81
107
  ```javascript
82
108
  const MaskData = require('./maskdata');
109
+
110
+ const emailMask2Options = {
111
+ maskWith: "*",
112
+ unmaskedStartCharactersBeforeAt: 3,
113
+ unmaskedEndCharactersAfterAt: 2,
114
+ maskAtTheRate: false
115
+ };
116
+
117
+ const email = "my.test.email@testEmail.com";
118
+
119
+ const maskedEmail = MaskData.maskEmail2(email, emailMask2Options);
120
+
121
+ //Output: my.********@**********om
122
+
83
123
  ```
124
+ ### Consider an email: abcd@email.com
125
+ Here,
126
+ - number of characters before @ -> 4
127
+ - number of characters after @ --> 9
84
128
 
85
- ## Mask Phone Number
129
+ 1. **unmaskedStartCharactersBeforeAt** --> number of starting characters (before @)not to be masked.
130
+ * If unmaskedStartCharactersBeforeAt > number of characters before @, then it will not mask the characters before @
131
+ 2. **unmaskedEndCharactersAfterAt** --> number of characters not to be masked starting from the end till @.
132
+ * If unmaskedEndCharactersAfterAt > number of characters after @, then it will not mask the characters after @
133
+
134
+ ### Mask only the characters before '@'
86
135
  ```javascript
87
136
  const MaskData = require('./maskdata');
88
137
 
89
- const maskPhoneOptions = {
90
- // Character to mask the data
91
- // default value is '*'
92
- maskWith: "*",
93
-
94
- //Should be positive Integer
95
- // If the starting 'n' digits needs to be unmasked
96
- // Default value is 4
97
- unmaskedStartDigits: 5,
98
-
99
- // Should be positive Integer
100
- //If the ending 'n' digits needs to be unmasked
101
- // Default value is 1
102
- unmaskedEndDigits: 1
138
+ const emailMask2Options = {
139
+ maskWith: "*",
140
+ unmaskedStartCharactersBeforeAt: 0,
141
+ unmaskedEndCharactersAfterAt: 257, // Give a number which is more than the characters after @
142
+ maskAtTheRate: false
103
143
  };
104
144
 
105
- const phoneNumber = "+911234567890";
145
+ const email = "abcd@email.com";
106
146
 
107
- const maskedPhoneNumber = MaskData.maskPhone(phoneNumber, maskPhoneOptions);
108
- //Output: +9112*******0
147
+ const maskedEmail = MaskData.maskEmail2(email, emailMask2Options);
148
+
149
+ //Output: ****@email.com
109
150
 
110
151
  ```
111
152
 
112
- ## Mask Phone Number with the default configuration
113
- To mask with the default options, dont pass the configurations.
153
+ ## Mask Email id with the default configuration
154
+ To mask with the default options, don't pass the configurations.
114
155
  ```javascript
115
156
  const MaskData = require('./maskdata');
116
157
 
117
158
  /** Default Options
118
- maskWith: "*"
119
- unmaskedStartDigits: 4
120
- unmaskedEndDigits: 1
159
+ maskWith: "*",
160
+ unmaskedStartCharactersBeforeAt: 3,
161
+ unmaskedEndCharactersAfterAt: 2,
162
+ maskAtTheRate: false
121
163
  **/
122
- const phoneNumber = "+111234567890";
164
+ const email = "my.test.email@testEmail.com";
123
165
 
124
- const maskedPhoneNumber = MaskData.maskPhone(phoneNumber);
166
+ const maskedEmail = MaskData.maskEmail2(email);
125
167
 
126
- //Output: +911********0
168
+ //Output: my.********@**********om
127
169
 
128
170
  ```
129
171
 
172
+ ## Mask JSON
173
+ 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.
130
174
 
175
+ ```javascript
176
+ const MaskData = require('./maskdata');
177
+
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
182
+
183
+ emailMaskOptions: defaultEmailMask2Options, // Optional
184
+ emailFields: [], // List of email fields to be masked
185
+
186
+ // For passwords, tokens, etc...
187
+ passwordMaskOptions: defaultPasswordMaskOptions, // Optional
188
+ passwordFields: [], // List of password fields to be masked
189
+
190
+ phoneMaskOptions: defaultPhoneMaskOptions, // Optional
191
+ phoneFields: [], // List of phone fields to be masked
192
+
193
+ stringMaskOptions: defaultStringMaskOptions, // Mandatory if stringFields are given. Otherwise, stringFields won't be masked
194
+ stringFields: [], // List of String fields to be masked
195
+
196
+ uuidMaskOptions: defaultUuidMaskOptions, // Optional
197
+ uuidFields: [] // List of UUID fields to be masked
198
+ };
199
+ ```
200
+ <b>NOTE: For defaultCardMaskOptions, defaultEmailMask2Options, defaultPasswordMaskOptions, defaultPhoneMaskOptions, defaultStringMaskOptions and defaultUuidMaskOptions refer the corresponding masking features. </b>
201
+
202
+ ```javascript
203
+ const defaultjsonMask2Configs = {
204
+ cardMaskOptions: {
205
+ maskWith: "*",
206
+ unmaskedStartDigits: 4,
207
+ unmaskedEndDigits: 1
208
+ },
209
+ cardFields: [],
210
+
211
+ emailMaskOptions: {
212
+ maskWith: "*",
213
+ unmaskedStartCharactersBeforeAt: 3,
214
+ unmaskedEndCharactersAfterAt: 2,
215
+ maskAtTheRate: false
216
+ },
217
+ emailFields: [],
218
+
219
+ passwordMaskOptions: {
220
+ maskWith: "*",
221
+ maxMaskedCharacters: 16,
222
+ unmaskedStartCharacters: 0,
223
+ unmaskedEndCharacters: 0
224
+ },
225
+ passwordFields: [],
226
+
227
+ phoneMaskOptions: {
228
+ maskWith: "*",
229
+ unmaskedStartDigits: 4,
230
+ unmaskedEndDigits: 1
231
+ },
232
+ phoneFields: [],
233
+
234
+ stringMaskOptions: {
235
+ maskWith: "*",
236
+ maskOnlyFirstOccurance: false,
237
+ values: [],
238
+ maskAll: false,
239
+ maskSpace: true
240
+ },
241
+ stringFields: [],
242
+
243
+ uuidMaskOptions: {
244
+ maskWith: "*",
245
+ unmaskedStartCharacters: 0,
246
+ unmaskedEndCharacters: 0
247
+ },
248
+ uuidFields: []
249
+ };
250
+
251
+ ```
252
+
253
+ ### JSON mask examples
254
+ Example1:
255
+ ```javascript
256
+ const jsonInput = {
257
+ 'credit': '1234-5678-8765-1234',
258
+ 'debit': '0000-1111-2222-3333',
259
+ 'primaryEmail': 'primary@Email.com',
260
+ 'secondaryEmail': 'secondary@Email.com',
261
+ 'password': 'dummyPassword',
262
+ 'homePhone': "+1 1234567890",
263
+ 'workPhone': "+1 9876543210",
264
+ 'addressLine1': "This is my addressline 1. This is my home",
265
+ 'addressLine2': "AddressLine 2",
266
+ 'uuid1': '123e4567-e89b-12d3-a456-426614174000'
267
+ };
268
+
269
+ const jsonMaskConfig = {
270
+ cardFields: ['credit', 'debit'],
271
+ emailFields: ['primaryEmail', 'secondaryEmail'],
272
+ passwordFields: ['password'],
273
+ phoneFields: ['homePhone', 'workPhone'],
274
+ stringMaskOptions: {
275
+ maskWith: "*",
276
+ maskOnlyFirstOccurance: false,
277
+ values: ["This"]
278
+ },
279
+ stringFields: ['addressLine1', 'addressLine2'],
280
+ uuidFields: ['uuid1']
281
+ };
282
+
283
+ const maskedJsonOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
284
+
285
+ Output:
286
+
287
+ {
288
+ credit: '1234-****-****-***4',
289
+ debit: '0000-****-****-***3',
290
+ primaryEmail: 'pri****@*******om',
291
+ secondaryEmail: 'sec******@*******om',
292
+ password: '*************',
293
+ homePhone: '+1 1********0',
294
+ workPhone: '+1 9********0',
295
+ addressLine1: '**** is my addressline 1. **** is my home',
296
+ addressLine2: 'AddressLine 2',
297
+ uuid1: '********-****-****-****-************'
298
+ }
299
+
300
+
301
+ Example2: Mask with custom configs for each/any type of fields
302
+
303
+ const jsonInput2 = {
304
+ 'credit': '1234-5678-8765-1234',
305
+ 'debit': '0000-1111-2222-3333',
306
+ 'primaryEmail': 'primary@Email.com',
307
+ 'secondaryEmail': 'secondary@Email.com',
308
+ 'password': 'dummyPasswordANDdummyPassword',
309
+ 'homePhone': "+1 1234567890",
310
+ 'workPhone': "+1 9876543210",
311
+ 'addressLine1': "This is my addressline 1. This is my home",
312
+ 'addressLine2': "AddressLine 2",
313
+ 'uuid1': '123e4567-e89b-12d3-a456-426614174000'
314
+ };
315
+
316
+ const jsonMaskConfig2 = {
317
+ // Card
318
+ cardMaskOptions: { maskWith: "X", unmaskedStartDigits: 2,unmaskedEndDigits: 4 },
319
+ cardFields: ['credit', 'debit'],
320
+
321
+ // Email
322
+ emailMaskOptions: { maskWith: "*", unmaskedStartCharactersBeforeAt: 2, unmaskedEndCharactersAfterAt: 2, maskAtTheRate: false },
323
+ emailFields: ['primaryEmail', 'secondaryEmail'],
324
+
325
+ // Password
326
+ passwordMaskOptions: { maskWith: "*", maxMaskedCharacters: 10, unmaskedStartCharacters: 0, unmaskedEndCharacters: 0 },
327
+ passwordFields: ['password'],
328
+
329
+ // Phone
330
+ phoneMaskOptions: { maskWith: "*", unmaskedStartDigits: 2, unmaskedEndDigits: 1 },
331
+ phoneFields: ['homePhone', 'workPhone'],
332
+
333
+ // String
334
+ stringMaskOptions: { maskWith: "*", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
335
+ stringFields: ['addressLine1', 'addressLine2'],
336
+
337
+ // UUID
338
+ uuidMaskOptions: { maskWith: "*", unmaskedStartCharacters: 4, unmaskedEndCharacters: 2 },
339
+ uuidFields: ['uuid1']
340
+ };
341
+
342
+ const maskedJsonOutput2 = maskData.maskJSON2(jsonInput2, jsonMaskConfig2);
343
+
344
+ Output:
345
+
346
+ {
347
+ credit: '12XX-XXXX-XXXX-1234',
348
+ debit: '00XX-XXXX-XXXX-3333',
349
+ primaryEmail: 'pr*****@*******om',
350
+ secondaryEmail: 'se*******@*******om',
351
+ password: '**********',
352
+ homePhone: '+1**********0',
353
+ workPhone: '+1**********0',
354
+ addressLine1: '**** ** ** *********** ** **** ** ** ****',
355
+ addressLine2: '*********** *',
356
+ uuid1: '123e****-****-****-****-**********00'
357
+ }
358
+ ```
359
+
360
+ Example3: Mask nested json fields -> Use dot(.) and Array([]) notation to specify the inner fields.
361
+
362
+ ```javascript
363
+
364
+ const jsonInput2 = {
365
+ cards: {
366
+ creditCards: ['1234-5678-8765-1234', '1111-2222-1111-2222'],
367
+ debitCards: ['0000-1111-2222-3333', '2222-1111-3333-4444']
368
+ },
369
+ emails: {
370
+ primaryEmail: 'primary@Email.com',
371
+ secondaryEmail: 'secondary@Email.com'
372
+ },
373
+ passwords: [
374
+ ['dummyPasswordANDdummyPassword', 'dummyPasswordANDdummyPassword']
375
+ ],
376
+ phones: {
377
+ homePhone: "+1 1234567890",
378
+ workPhone: "+1 9876543210",
379
+ },
380
+ address: {
381
+ addressLine1: "This is my addressline 1. This is my home",
382
+ addressLine2: "AddressLine 2"
383
+ },
384
+ uuids: {
385
+ uuid1: '123e4567-e89b-12d3-a456-426614174000'
386
+ }
387
+ };
388
+
389
+ const jsonMaskConfig2 = {
390
+ // Card
391
+ cardMaskOptions: { maskWith: "X", unmaskedStartDigits: 2,unmaskedEndDigits: 4 },
392
+ cardFields: ['cards.creditCards[0]', 'cards.creditCards[1]', 'cards.debitCards[0]', 'cards.debitCards[1]'],
393
+
394
+ // Email
395
+ emailMaskOptions: { maskWith: "*", unmaskedStartCharactersBeforeAt: 2, unmaskedEndCharactersAfterAt: 2, maskAtTheRate: false },
396
+ emailFields: ['emails.primaryEmail', 'emails.secondaryEmail'],
397
+
398
+ // Password
399
+ passwordMaskOptions: { maskWith: "*", maxMaskedCharacters: 10, unmaskedStartCharacters: 0, unmaskedEndCharacters: 0 },
400
+ passwordFields: ['passwords[0][0]]', 'passwords[0][1]'],
401
+
402
+ // Phone
403
+ phoneMaskOptions: { maskWith: "*", unmaskedStartDigits: 2, unmaskedEndDigits: 1 },
404
+ phoneFields: ['phones.homePhone', 'phones.workPhone'],
405
+
406
+ // String
407
+ stringMaskOptions: { maskWith: "*", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
408
+ stringFields: ['address.addressLine1', 'address.addressLine2'],
409
+
410
+ // UUID
411
+ uuidMaskOptions: { maskWith: "*", unmaskedStartCharacters: 4, unmaskedEndCharacters: 2 },
412
+ uuidFields: ['uuids.uuid1']
413
+ };
414
+
415
+ const maskedJsonOutput2 = MaskData.maskJSON2(jsonInput2, jsonMaskConfig2);
416
+ // OUTPUT:
417
+ {
418
+ cards: {
419
+ creditCards: [ '12XX-XXXX-XXXX-1234', '11XX-XXXX-XXXX-2222' ],
420
+ debitCards: [ '00XX-XXXX-XXXX-3333', '22XX-XXXX-XXXX-4444' ]
421
+ },
422
+ emails: {
423
+ primaryEmail: 'pr*****@*******om',
424
+ secondaryEmail: 'se*******@*******om'
425
+ },
426
+ passwords: [ [ '**********', '**********' ] ],
427
+ phones: { homePhone: '+1**********0', workPhone: '+1**********0' },
428
+ address: {
429
+ addressLine1: '**** ** ** *********** ** **** ** ** ****',
430
+ addressLine2: '*********** *'
431
+ },
432
+ uuids: { uuid1: '123e****-****-****-****-**********00' }
433
+ }
434
+
435
+ ```
131
436
  ## Mask Password
132
437
  ```javascript
133
438
  const MaskData = require('./maskdata');
@@ -157,7 +462,7 @@ const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
157
462
  //Output: **********
158
463
 
159
464
  ## Mask Password with the default configuration
160
- To mask with the default options, dont pass the configurations.
465
+ To mask with the default options, don't pass the configurations.
161
466
  ```javascript
162
467
  const MaskData = require('./maskdata');
163
468
 
@@ -174,77 +479,189 @@ const maskedPassword = MaskData.maskPassword(password)
174
479
 
175
480
  ```
176
481
 
482
+ #### Example: Mask password OR secretKey with some meta info at the end
177
483
 
178
- ## Mask Email id
179
- Use this method instead of maskEmail(). To mask with the default options, dont pass the configurations.
180
484
  ```javascript
181
485
  const MaskData = require('./maskdata');
182
486
 
183
- const emailMask2Options = {
184
- maskWith: "*",
185
- unmaskedStartCharactersBeforeAt: 3,
186
- unmaskedEndCharactersAfterAt: 2,
187
- maskAtTheRate: false
487
+ const maskPasswordOptions = {
488
+ maskWith: "X",
489
+ maxMaskedCharacters: 20, // To limit the output String length to 20.
490
+ unmaskedStartCharacters: 4,
491
+ unmaskedEndCharacters: 9 // As the last 9 characters of the secret key is meta info which can be printed for debugging or other purposes
188
492
  };
189
493
 
190
- const email = "my.test.email@testEmail.com";
494
+ const password = "TEST:U2VjcmV0S2V5MQ==:CLIENT-A";
191
495
 
192
- const maskedEmail = MaskData.maskEmail2(email, emailMask2Options);
496
+ const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
497
+ //Output: TESTXXXXXXX:CLIENT-A
193
498
 
194
- //Output: my.********@**********om
499
+ maskPasswordOptions.unmaskedStartCharacters = 0;
195
500
 
501
+ const maskedPassword = MaskData.maskPassword(password, maskPasswordOptions);
502
+ //Output: XXXXXXXXXXX:CLIENT-A
196
503
  ```
197
- ### Consider an email: abcd@email.com
198
- Here,
199
- - number of characters before @ -> 4
200
- - number of characters after @ --> 9
201
504
 
202
- 1. **unmaskedStartCharactersBeforeAt** --> number of starting characters (before @)not to be masked.
203
- * If unmaskedStartCharactersBeforeAt > number of characters before @, then it will not mask the characters before @
204
- 2. **unmaskedEndCharactersAfterAt** --> number of characters not to be masked starting from the end till @.
205
- * If unmaskedEndCharactersAfterAt > number of characters after @, then it will not mask the characters after @
206
505
 
207
- ### Mask only the characters before '@'
506
+ ## Mask Phone Number
208
507
  ```javascript
209
508
  const MaskData = require('./maskdata');
210
509
 
211
- const emailMask2Options = {
212
- maskWith: "*",
213
- unmaskedStartCharactersBeforeAt: 0,
214
- unmaskedEndCharactersAfterAt: 257, // Give a number which is more than the characters after @
215
- maskAtTheRate: false
216
- };
510
+ const maskPhoneOptions = {
511
+ // Character to mask the data
512
+ // default value is '*'
513
+ maskWith: "*",
217
514
 
218
- const email = "abcd@email.com";
515
+ //Should be positive Integer
516
+ // If the starting 'n' digits need to be unmasked
517
+ // Default value is 4
518
+ unmaskedStartDigits: 5,
219
519
 
220
- const maskedEmail = MaskData.maskEmail2(email, emailMask2Options);
520
+ // Should be positive Integer
521
+ //If the ending 'n' digits need to be unmasked
522
+ // Default value is 1
523
+ unmaskedEndDigits: 1
524
+ };
221
525
 
222
- //Output: ****@email.com
526
+ const phoneNumber = "+911234567890";
527
+
528
+ const maskedPhoneNumber = MaskData.maskPhone(phoneNumber, maskPhoneOptions);
529
+ //Output: +9112*******0
223
530
 
224
531
  ```
225
532
 
226
- ## Mask Email id with the default configuration
227
- To mask with the default options, dont pass the configurations.
533
+ ## Mask Phone Number with the default configuration
534
+ To mask with the default options, don't pass the configurations.
228
535
  ```javascript
229
536
  const MaskData = require('./maskdata');
230
537
 
231
538
  /** Default Options
232
- maskWith: "*",
233
- unmaskedStartCharactersBeforeAt: 3,
234
- unmaskedEndCharactersAfterAt: 2,
235
- maskAtTheRate: false
539
+ maskWith: "*"
540
+ unmaskedStartDigits: 4
541
+ unmaskedEndDigits: 1
236
542
  **/
237
- const email = "my.test.email@testEmail.com";
543
+ const phoneNumber = "+111234567890";
238
544
 
239
- const maskedEmail = MaskData.maskEmail2(email);
545
+ const maskedPhoneNumber = MaskData.maskPhone(phoneNumber);
240
546
 
241
- //Output: my.********@**********om
547
+ //Output: +911********0
548
+
549
+ ```
550
+
551
+
552
+ ## Mask the characters or words in the string
553
+ This will mask the characters or words if present in the given string.
554
+ ```javascript
555
+ const MaskData = require('./maskdata');
556
+
557
+ const maskStringOptions = {
558
+ // Character to mask the data. The default value is '*'
559
+ maskWith: "*",
560
+
561
+ /**
562
+ * It is the words/substrings to mask.
563
+ * Should be an array of strings.
564
+ * Can give multiple words/substrings.
565
+ * values[] can be used only when maskAll is false. If maskAll is true, then this is of no use.
566
+ */
567
+ values: ['is', 'test'],
568
+
569
+ /**
570
+ * If to mask only the first occurrence of each word/substring in the given string
571
+ * Should be boolean
572
+ * Default value is false
573
+ */
574
+ maskOnlyFirstOccurance: false,
575
+
576
+ /**
577
+ * If to mask all the characters in a string make maskAll: true
578
+ * If maskAll is true, the words/substrings inside values[] will not be considered.
579
+ * Default value is false
580
+ */
581
+ maskAll: false,
582
+
583
+ /**
584
+ * This is to mask/not mask the spaces in a string when masking all the characters.
585
+ * Can be used ONLY when maskAll: true
586
+ * If maskSpace is false, the spaces in the string will not be masked.
587
+ * This feature is to know the words and each word's length but to hide the content
588
+ * Default value is true
589
+ */
590
+ maskSpace: true
591
+ };
592
+
593
+ const str = "This is a test String";
594
+
595
+ const strAfterMasking = MaskData.maskString(str, maskStringOptions);
596
+
597
+ //Output: Th** ** a **** String
242
598
 
243
599
  ```
244
600
 
601
+ ### Mask all characters in the String
602
+ ```javascript
603
+ const str = "This is a test String";
604
+
605
+ const strAfterMasking = MaskData.maskString(str, maskStringOptions);
245
606
 
246
- ## Get Nested Json Property
247
- This method returns the value of nested Json property if exists. Otherwise it returns ```undefined```
607
+ const stringMaskOptions = {
608
+ maskWith: "*",
609
+ values: [],
610
+ maskAll: true,
611
+ maskSpace: false // Maks all characters except spaces
612
+ };
613
+
614
+ // Output: **** ** * **** *****
615
+
616
+ const str = "This is a test String";
617
+
618
+ const strAfterMasking = MaskData.maskString(str, maskStringOptions);
619
+
620
+ const stringMaskOptions = {
621
+ maskWith: "*",
622
+ values: [],
623
+ maskAll: true,
624
+ maskSpace: true // Mask all characters including spaces
625
+ };
626
+
627
+ // Output: ********************
628
+
629
+ ```
630
+
631
+
632
+ ## Mask UUID
633
+ This will mask the alphanumeric characters in a UUID.<br/>This will not mask the hyphen present in the UUID. Masking is done, only when the input is a valid UUID with only a-f, A-F, and 0-9 and has 36 characters (32 alphanumerics + 4 hyphens, and should work with the <b>regex:</b>
634
+ > /[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}/g;).
635
+
636
+ If the input is not a valid UUID, it will return the input itself without masking.
637
+ ```javascript
638
+ const MaskData = require('./maskdata');
639
+
640
+ const maskUuidOptions = {
641
+ // Character to mask the data. The default value is '*'
642
+ maskWith: "*",
643
+
644
+ // Should be a positive Integer <= 32
645
+ // If the starting 'n' alphanumeric characters need to be visible/unmasked
646
+ // Default value is 0
647
+ unmaskedStartCharacters: 0,
648
+
649
+ //Should be a positive Integer <= 32
650
+ //If the ending 'n' alphanumeric characters need to be visible/unmasked
651
+ // Default value is 0
652
+ unmaskedEndCharacters: 0
653
+ };
654
+
655
+ const uuidInput = "123e4567-e89b-12d3-a456-426614174000";
656
+
657
+ const uuidAfterMasking = MaskData.maskUuid(uuidInput, maskUuidOptions);
658
+
659
+ // Output: ********-****-****-****-************
660
+
661
+ ```
662
+
663
+ ## Get Nested JSON Property
664
+ This method returns the value of the nested JSON property if it exists. Otherwise, it returns ```undefined```
248
665
  ```javascript
249
666
  const MaskData = require('./maskdata');
250
667
 
@@ -271,7 +688,7 @@ const innerPropety = Maskdata.getInnerProperty(nestedObject, 'level1.level2.leve
271
688
  ```
272
689
 
273
690
 
274
- ## Replace the value of a json field
691
+ ## Replace the value of a JSON field
275
692
  To replace a value by keeping the type.
276
693
  ```javascript
277
694
 
@@ -285,7 +702,6 @@ let afterReplacing = MaskData.replaceValue(input, 'age', 99);
285
702
  afterReplacing = MaskData.replaceValue(input, 'married', false);
286
703
 
287
704
  //Output:
288
-
289
705
  Before replacing: {"name":"John","age":33,"married":true}
290
706
  After replacing: {"name":"John","age":99,"married":false}
291
707
 
@@ -294,21 +710,22 @@ Type of married: boolean
294
710
 
295
711
  ```
296
712
 
713
+ ## Mask fields in a JSON
714
+ <b> NOTE: This is an old functionality. For more flexibility and single masking call to mask different types of data in the json object, start using the new maskJson2() function - [Mask JSON fields](#mask-json)</b>
297
715
 
298
- ## Mask fields in a JSON
299
- This method masks the field value if present in the given object
716
+ This method masks the field value if present in the given object.
300
717
  ```javascript
301
718
  const MaskData = require('./maskdata');
302
719
 
303
720
  const maskJSONOptions = {
304
- // Character to mask the data. Default value is '*'
721
+ // Character to mask the data. The default value is '*'
305
722
  maskWith: "*",
306
723
 
307
724
  // It should be an array
308
725
  // Field names to mask. Can give multiple fields.
309
726
  fields: ['password', 'firstName'],
310
727
 
311
- // If the type of value is string and if we want to limit the number of characters in the output, then we can use this. Default value is -1 means, by default this feature is disabled. It is enabled if we provide positive integer value. This is not a mandatory field in the config and if not provided, it will take the default value -1 and disable the feature.
728
+ // If the type of value is a string and if we want to limit the number of characters in the output, then we can use this. The default value is -1 means, by default this feature is disabled. It is enabled if we provide a positive integer value. This is not a mandatory field in the config and if not provided, it will take the default value -1 and disable the feature.
312
729
  maxMaskedCharactersStr: 4 // Default value is -1 (Disable feature)
313
730
  };
314
731
 
@@ -326,16 +743,17 @@ const maskedObj = MaskData.maskJSONFields(obj, maskJSONOptions);
326
743
 
327
744
 
328
745
  ## Mask fields of a nested Object
329
- This method masks the field value if present in the given object
746
+ <b>NOTE: USE [Mask JSON fields](#mask-json) for better functionalities.</b>
330
747
 
331
- The masked value type will always be string. Won't mask if the value is ```null```.
748
+ This method masks the field value if present in the given object.
749
+ The masked value type will always be a string. Won't mask if the value is ```null```.
332
750
 
333
- If the field doesn't exist or if there is any syntax error, then it will ignore without throwing any error.
751
+ If the field doesn't exist or if there is any syntax error, then it will ignore it without throwing any error.
334
752
  ```javascript
335
753
  const MaskData = require('./maskdata');
336
754
 
337
755
  const maskJSONOptions = {
338
- // Character to mask the data. Default value is '*'
756
+ // Character to mask the data. The default value is '*'
339
757
  maskWith: "*",
340
758
 
341
759
  // It should be an array
@@ -391,9 +809,9 @@ const maskedObj = MaskData.maskJSONFields(nestedObject, defaultJSONMaskOptions2)
391
809
  ### Example2: To mask all the keys of an object or to mask a field from all the elements of an array.
392
810
 
393
811
  ```javascript
394
- Limititions:
812
+ Limitations:
395
813
  1. Only one * is allowed per field. Either ARRAY[*].FIELD or JSON.*
396
- 2. It will not work for the nested fields like ARRAY[*].FIELD1.FIELD2
814
+ 2. It will not work for nested fields like ARRAY[*].FIELD1.FIELD2
397
815
  3. It will not mask all array elements if given ARRAY[*] or ARRAY[*].
398
816
  4. Will not mask null values.
399
817
  5. If ARRAY[*].FIELD is an object, then it will mask that entire object including the key.
@@ -442,127 +860,16 @@ const maskedObject = MaskData.maskJSONFields(nestedObject, maskAllFields);
442
860
  },
443
861
  "value1": "value"
444
862
  }
445
- ```
446
- ## Mask the characters or words in the string
447
- This will mask the characters or words value if present in the given string.
448
- ```javascript
449
- const MaskData = require('./maskdata');
450
-
451
- const maskStringOptions = {
452
- // Character to mask the data. Default value is '*'
453
- maskWith: "*",
454
-
455
- /**
456
- * It is the words/substrings to mask.
457
- * Should be an array of strings.
458
- * Can give multiple words/substrings.
459
- * values[] can be used only when maskAll is false. If maskAll is true, then this is of no use.
460
- */
461
- values: ['is', 'test'],
462
-
463
- /**
464
- * If to mask only the first occurance of each word/substring in the given string
465
- * Should be boolean
466
- * Default value is false
467
- */
468
- maskOnlyFirstOccurance: false,
469
-
470
- /**
471
- * If to mask all the characters in a string make maskAll: true
472
- * If maskAll is true, the words/substrings inside values[] will not be considered.
473
- * Default value is false
474
- */
475
- maskAll: false,
476
-
477
- /**
478
- * This is to mask/not mask the spaces in a string when masking all the characters.
479
- * Can be used ONLY when maskAll: true
480
- * If maskSpace is true, the spaces in the string will be masked.
481
- * This feature is to know the words and each word length but to hide the content
482
- * Default value is true
483
- */
484
- maskSpace: true
485
- };
486
-
487
- const str = "This is a test String";
488
-
489
- const strAfterMasking = MaskData.maskString(str, maskStringOptions);
490
-
491
- //Output: Th** ** a **** String
492
-
493
- ```
494
-
495
- ### Mask all characters in the String
496
- ```
497
- const str = "This is a test String";
498
-
499
- const strAfterMasking = MaskData.maskString(str, maskStringOptions);
500
-
501
- const stringMaskOptions = {
502
- maskWith: "*",
503
- values: [],
504
- maskAll: true,
505
- maskSpace: false // Do not mask space
506
- };
507
-
508
- // Output: **** ** * **** *****
509
- -------------------------------------------------------------------
510
-
511
- const str = "This is a test String";
512
-
513
- const strAfterMasking = MaskData.maskString(str, maskStringOptions);
514
-
515
- const stringMaskOptions = {
516
- maskWith: "*",
517
- values: [],
518
- maskAll: true,
519
- maskSpace: false // Mask spaces also
520
- };
521
-
522
- // Output: ********************
523
-
524
- ```
525
-
526
- ## Mask card number
527
- This will mask the digits in a card numbers.<br/>This will mask only the numerical data and not any non numeric delimeters, alphabets or any other types of data
528
- ```javascript
529
- const MaskData = require('./maskdata');
530
-
531
- const maskCardOptions = {
532
- // Character to mask the data. Default value is 'X'
533
- maskWith: "X",
534
-
535
- // Should be positive Integer
536
- // If the starting 'n' digits needs to be unmasked
537
- // Default value is 4
538
- unmaskedStartDigits: 4,
539
-
540
- //Should be positive Integer
541
- //If the ending 'n' digits needs to be unmasked
542
- // Default value is 1.
543
- unmaskedEndDigits: 1
544
- };
545
-
546
- const cardNumber = "1234-5678-1234-5678";
547
-
548
- const cardAfterMasking = MaskData.maskCard(cardNumber, maskCardOptions);
549
-
550
- //Output: 1234-XXXX-XXXX-XXX8
551
-
552
863
  ```
553
864
 
554
865
  # Report Bugs
555
- Please raise an issue in github: https://github.com/Sumukha1496/maskdata/issues
556
-
557
- # Run Tests
558
- Below command will run mocha tests and also the test cases present in test.js file. Install all the dependencies using npm i before runnning the below command.
559
- > npm run test
866
+ 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
560
867
 
561
868
  # Give a Star
562
- You can give a start at: https://github.com/Sumukha1496/maskdata/stargazers
869
+ You can give a star at: https://github.com/Sumukha1496/maskdata/stargazers
563
870
 
564
871
  # LICENSE - "MIT"
565
- Licenced under MIT Licence
872
+ Licensed under MIT Licence
566
873
 
567
874
  Copyright (c) 2019 Sumukha H S
568
875
 
@@ -578,8 +885,8 @@ copies or substantial portions of the Software.
578
885
 
579
886
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
580
887
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
581
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
582
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
888
+ FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
889
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
583
890
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
584
891
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
585
892
  SOFTWARE.