maskdata 1.1.4 → 1.1.7
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 +114 -33
- package/index.js +22 -4
- package/lib/emailMask/EmailMask.js +1 -0
- package/lib/helpers/MaskHelper.js +6 -0
- package/package.json +11 -5
- package/test.js +26 -13
- package/tests/testCard.js +156 -0
- package/tests/testEmail.js +206 -0
- package/tests/testJsonFields.js +9 -0
- package/tests/testPassword.js +149 -0
- package/tests/testPhone.js +156 -0
- package/tests/testReplaceValue.js +35 -0
- package/tests/testString.js +258 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const maskData = require('../index');
|
|
3
|
+
const expect = require('chai').expect;
|
|
4
|
+
|
|
5
|
+
describe('Masking strings', function() {
|
|
6
|
+
|
|
7
|
+
// common strings without output - used as base for all tests below
|
|
8
|
+
let testData = [
|
|
9
|
+
{
|
|
10
|
+
title: 'test default string',
|
|
11
|
+
input: 'This is a testingAPI String',
|
|
12
|
+
output: ''
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
title: 'test string without space',
|
|
16
|
+
input: 'thisisastring',
|
|
17
|
+
output: ''
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
title: 'test string with newline',
|
|
21
|
+
input: 'this is a\nstring with newline',
|
|
22
|
+
output: ''
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
describe('Mask with default options', function() {
|
|
27
|
+
|
|
28
|
+
// default options are this - let tests fail when defaults change
|
|
29
|
+
// const defaultStringMaskOptions = {
|
|
30
|
+
// maskWith: "*",
|
|
31
|
+
// maskOnlyFirstOccurance: false,
|
|
32
|
+
// values: [],
|
|
33
|
+
// maskAll: false,
|
|
34
|
+
// maskSpace: true
|
|
35
|
+
// };
|
|
36
|
+
|
|
37
|
+
// no values -> no masking
|
|
38
|
+
const localTestData = JSON.parse(JSON.stringify(testData));
|
|
39
|
+
localTestData[0].output = localTestData[0].input;
|
|
40
|
+
localTestData[1].output = localTestData[1].input;
|
|
41
|
+
localTestData[2].output = localTestData[2].input;
|
|
42
|
+
|
|
43
|
+
localTestData.forEach(({title, input, output}) => {
|
|
44
|
+
it(`default mask - ${title}`, function() {
|
|
45
|
+
const masked = maskData.maskString(input);
|
|
46
|
+
expect(masked).to.equal(output, 'masked output does not match expected value');
|
|
47
|
+
});
|
|
48
|
+
})
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('Mask with custom options - maskWith, maskSpace', function() {
|
|
52
|
+
|
|
53
|
+
// only maskWith and maskSpace set
|
|
54
|
+
const maskOptions = {
|
|
55
|
+
maskWith: "?",
|
|
56
|
+
maskOnlyFirstOccurance: false,
|
|
57
|
+
values: [],
|
|
58
|
+
maskAll: false,
|
|
59
|
+
maskSpace: true
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// no values -> no masking
|
|
63
|
+
const localTestData = JSON.parse(JSON.stringify(testData));
|
|
64
|
+
localTestData[0].output = localTestData[0].input;
|
|
65
|
+
localTestData[1].output = localTestData[1].input;
|
|
66
|
+
localTestData[2].output = localTestData[2].input;
|
|
67
|
+
|
|
68
|
+
localTestData.forEach(({title, input, output}) => {
|
|
69
|
+
it(`custom mask - ${title}`, function() {
|
|
70
|
+
const masked = maskData.maskString(input, maskOptions);
|
|
71
|
+
expect(masked).to.equal(output, 'masked output does not match expected value');
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('Mask with custom options - maskWith, maskAll, no spaces', function() {
|
|
77
|
+
|
|
78
|
+
// only maskWith, maskAll set
|
|
79
|
+
const maskOptions = {
|
|
80
|
+
maskWith: "?",
|
|
81
|
+
maskOnlyFirstOccurance: false,
|
|
82
|
+
values: [],
|
|
83
|
+
maskAll: true,
|
|
84
|
+
maskSpace: false
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const localTestData = JSON.parse(JSON.stringify(testData));
|
|
88
|
+
localTestData[0].output = '???? ?? ? ?????????? ??????';
|
|
89
|
+
localTestData[1].output = '?'.repeat(localTestData[1].input.length); // no space here
|
|
90
|
+
localTestData[2].output = '???? ?? ???????? ???? ???????';
|
|
91
|
+
|
|
92
|
+
localTestData.forEach(({title, input, output}) => {
|
|
93
|
+
it(`custom mask - ${title}`, function() {
|
|
94
|
+
const masked = maskData.maskString(input, maskOptions);
|
|
95
|
+
expect(masked).to.equal(output, 'masked output does not match expected value');
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('Mask with custom options - maskWith multiple chars, maskAll, maskSpace', function() {
|
|
101
|
+
|
|
102
|
+
// only maskWith, maskAll set
|
|
103
|
+
const maskChar = "??"; // mask every char in input string with two chars in output
|
|
104
|
+
const maskOptions = {
|
|
105
|
+
maskWith: maskChar,
|
|
106
|
+
maskOnlyFirstOccurance: false,
|
|
107
|
+
values: [],
|
|
108
|
+
maskAll: true,
|
|
109
|
+
maskSpace: true
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const localTestData = JSON.parse(JSON.stringify(testData));
|
|
113
|
+
localTestData[0].output = maskChar.repeat(localTestData[0].input.length);
|
|
114
|
+
localTestData[1].output = maskChar.repeat(localTestData[1].input.length);
|
|
115
|
+
localTestData[2].output = maskChar.repeat(localTestData[2].input.length);
|
|
116
|
+
|
|
117
|
+
localTestData.forEach(({title, input, output}) => {
|
|
118
|
+
it(`custom mask - ${title}`, function() {
|
|
119
|
+
const masked = maskData.maskString(input, maskOptions);
|
|
120
|
+
expect(masked).to.equal(output, 'masked output does not match expected value');
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe('Mask with custom options - values and maskAll set, no spaces', function() {
|
|
126
|
+
|
|
127
|
+
const maskOptions = {
|
|
128
|
+
maskWith: "x",
|
|
129
|
+
maskOnlyFirstOccurance: false,
|
|
130
|
+
values: ['is', 'ing'],
|
|
131
|
+
maskAll: true,
|
|
132
|
+
maskSpace: false
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// expected: values ignored and all masked, space remains
|
|
136
|
+
const localTestData = JSON.parse(JSON.stringify(testData));
|
|
137
|
+
localTestData[0].output = 'xxxx xx x xxxxxxxxxx xxxxxx';
|
|
138
|
+
localTestData[1].output = 'x'.repeat(localTestData[1].input.length); // no spaces here
|
|
139
|
+
localTestData[2].output = 'xxxx xx xxxxxxxx xxxx xxxxxxx';
|
|
140
|
+
|
|
141
|
+
localTestData.forEach(({title, input, output}) => {
|
|
142
|
+
it(`custom mask - ${title}`, function() {
|
|
143
|
+
const masked = maskData.maskString(input, maskOptions);
|
|
144
|
+
expect(masked).to.equal(output, 'masked output does not match expected value');
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
describe('Mask with custom options - values and all occurrences', function() {
|
|
150
|
+
|
|
151
|
+
const maskOptions = {
|
|
152
|
+
maskWith: "*",
|
|
153
|
+
maskOnlyFirstOccurance: false,
|
|
154
|
+
values: ['is', 'ing'],
|
|
155
|
+
maskAll: false,
|
|
156
|
+
maskSpace: true
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const localTestData = JSON.parse(JSON.stringify(testData));
|
|
160
|
+
localTestData[0].output = 'Th** ** a test***API Str***';
|
|
161
|
+
localTestData[1].output = 'th****astr***';
|
|
162
|
+
localTestData[2].output = 'th** ** a\nstr*** with newline';
|
|
163
|
+
|
|
164
|
+
localTestData.forEach(({title, input, output}) => {
|
|
165
|
+
it(`custom mask - ${title}`, function() {
|
|
166
|
+
const masked = maskData.maskString(input, maskOptions);
|
|
167
|
+
expect(masked).to.equal(output, 'masked output does not match expected value');
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe('Mask with custom options - values and first occureances', function() {
|
|
173
|
+
|
|
174
|
+
const maskOptions = {
|
|
175
|
+
maskWith: "*",
|
|
176
|
+
maskOnlyFirstOccurance: true,
|
|
177
|
+
values: ['is', 'ing'],
|
|
178
|
+
maskAll: false,
|
|
179
|
+
maskSpace: true
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const localTestData = JSON.parse(JSON.stringify(testData));
|
|
183
|
+
localTestData[0].output = 'Th** is a test***API String';
|
|
184
|
+
localTestData[1].output = 'th**isastr***';
|
|
185
|
+
localTestData[2].output = 'th** is a\nstr*** with newline';
|
|
186
|
+
|
|
187
|
+
localTestData.forEach(({title, input, output}) => {
|
|
188
|
+
it(`custom mask - ${title}`, function() {
|
|
189
|
+
const masked = maskData.maskString(input, maskOptions);
|
|
190
|
+
expect(masked).to.equal(output, 'masked output does not match expected value');
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
describe('Mask with special input strings', function() {
|
|
196
|
+
|
|
197
|
+
const maskOptions = {
|
|
198
|
+
maskWith: "x",
|
|
199
|
+
maskOnlyFirstOccurance: true,
|
|
200
|
+
values: [],
|
|
201
|
+
maskAll: true,
|
|
202
|
+
maskSpace: true
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
describe('Mask with special input - input will generate an output', function() {
|
|
206
|
+
|
|
207
|
+
// first set with input generating an masked output
|
|
208
|
+
let testData = [
|
|
209
|
+
{
|
|
210
|
+
title: 'test with string length zero',
|
|
211
|
+
input: '',
|
|
212
|
+
output: ''
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
title: 'test input null',
|
|
216
|
+
input: null,
|
|
217
|
+
output: null
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
title: 'test input as number',
|
|
221
|
+
input: 12,
|
|
222
|
+
output: 'xx' // TODO - ok to convert number into string? Or should it fail?
|
|
223
|
+
},
|
|
224
|
+
]
|
|
225
|
+
|
|
226
|
+
testData.forEach(({title, input, output}) => {
|
|
227
|
+
it(`special input - ${title}`, function() {
|
|
228
|
+
const masked = maskData.maskString(input, maskOptions);
|
|
229
|
+
expect(masked).to.equal(output, 'masked output does not match expected value');
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
// describe('Mask with special input - input shall throw error', function() {
|
|
235
|
+
|
|
236
|
+
// // set with input generating an error / exception
|
|
237
|
+
// let testData = [
|
|
238
|
+
// {
|
|
239
|
+
// title: 'test input as array',
|
|
240
|
+
// input: ['12', 'ab'],
|
|
241
|
+
// output: 'xx' // mask method seems to join all strings in the array into on big string and masks it?
|
|
242
|
+
// },
|
|
243
|
+
// {
|
|
244
|
+
// title: 'test input as object',
|
|
245
|
+
// input: {a: 'b', x: 'y'},
|
|
246
|
+
// output: '12' // json stringified and masked?
|
|
247
|
+
// }
|
|
248
|
+
// ]
|
|
249
|
+
|
|
250
|
+
// testData.forEach(({title, input, output}) => {
|
|
251
|
+
// it(`special input - ${title}`, function() {
|
|
252
|
+
// const masked = maskData.maskString(input, maskOptions);
|
|
253
|
+
// expect(masked).to.equal(output, 'masked output does not match expected value');
|
|
254
|
+
// });
|
|
255
|
+
// });
|
|
256
|
+
// });
|
|
257
|
+
});
|
|
258
|
+
});
|