quival 0.2.5 → 0.2.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/test/test.js CHANGED
@@ -1,4 +1,20 @@
1
1
  import { strict as assert } from 'assert';
2
+ import {
3
+ escapeRegExp,
4
+ flattenObject,
5
+ getByPath,
6
+ isDigits,
7
+ isEmpty,
8
+ isNumeric,
9
+ isPlainObject,
10
+ isValidDate,
11
+ parseCsvString,
12
+ parseDate,
13
+ parseDateByFormat,
14
+ setByPath,
15
+ toCamelCase,
16
+ toSnakeCase,
17
+ } from '../src/helpers.js';
2
18
  import Validator from '../src/Validator.js';
3
19
 
4
20
  globalThis.File = class {
@@ -13,6 +29,163 @@ globalThis.File = class {
13
29
  }
14
30
  };
15
31
 
32
+ describe('Helpers', () => {
33
+ it('toCamelCase', () => {
34
+ assert.equal(toCamelCase('a_test_name'), 'aTestName');
35
+ assert.equal(toCamelCase('_test_name'), 'testName');
36
+ assert.equal(toCamelCase('a-test-name'), 'aTestName');
37
+ assert.equal(toCamelCase('-test-name'), 'testName');
38
+ assert.equal(toCamelCase('a test name'), 'aTestName');
39
+ assert.equal(toCamelCase(' test name'), 'testName');
40
+ });
41
+
42
+ it('toSnakeCase', () => {
43
+ assert.equal(toSnakeCase('aTestName'), 'a_test_name');
44
+ assert.equal(toSnakeCase('a_testName'), 'a_test_name');
45
+ assert.equal(toSnakeCase('ATESTNAME'), 'a_t_e_s_t_n_a_m_e');
46
+ });
47
+
48
+ it('escapeRegExp', () => {
49
+ assert.equal(escapeRegExp('.*+?^${}()|[]\\'), '\\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\\\');
50
+ });
51
+
52
+ it('getByPath', () => {
53
+ assert.equal(getByPath({ a: { b: 1 } }, 'a.b'), 1);
54
+ assert.equal(getByPath({ a: [1, { c: 2 }] }, 'a.1.c'), 2);
55
+ });
56
+
57
+ it('setByPath', () => {
58
+ let obj = { a: { b: 1 } };
59
+ setByPath(obj, 'a.b', 3);
60
+
61
+ assert.deepEqual(obj, { a: { b: 3 } });
62
+
63
+ obj = { a: [1, { c: 2 }] };
64
+ setByPath(obj, 'a.1.c', 5);
65
+
66
+ assert.deepEqual(obj, { a: [1, { c: 5 }] });
67
+ });
68
+
69
+ it('flattenObject', () => {
70
+ assert.deepEqual(flattenObject({ a: 1, b: { x: 41, y: 42 } }), { a: 1, 'b.x': 41, 'b.y': 42 });
71
+ assert.deepEqual(flattenObject({ a: 1, b: [71, 73, 75] }), { a: 1, 'b.0': 71, 'b.1': 73, 'b.2': 75 });
72
+ });
73
+
74
+ it('parseCsvString', () => {
75
+ assert.deepEqual(parseCsvString('a,b,c'), ['a', 'b', 'c']);
76
+ assert.deepEqual(parseCsvString('a, b, c'), ['a', ' b', ' c']);
77
+ assert.deepEqual(parseCsvString('"a","b","c"'), ['a', 'b', 'c']);
78
+ assert.deepEqual(parseCsvString('"a", "b", "c"'), ['a', 'b', 'c']);
79
+ assert.deepEqual(parseCsvString('"a", "b", ""'), ['a', 'b', '']);
80
+
81
+ assert.deepEqual(parseCsvString('"r2""c1","r2c2","r2c3"'), ['r2"c1', 'r2c2', 'r2c3']);
82
+ assert.deepEqual(parseCsvString('"r2""c1",r2c2,r2c3'), ['r2"c1', 'r2c2', 'r2c3']);
83
+ assert.deepEqual(parseCsvString('r1c1,"r1,c2",r1c3'), ['r1c1', 'r1,c2', 'r1c3']);
84
+ assert.deepEqual(parseCsvString('"r2""c1",r2c2,"r2""""c3"'), ['r2"c1', 'r2c2', 'r2""c3']);
85
+ assert.deepEqual(parseCsvString('r1c1,"r1,c2","r1"",""c3"'), ['r1c1', 'r1,c2', 'r1","c3']);
86
+ });
87
+
88
+ it('parseDate', () => {
89
+ const date1 = new Date('2023/08/11 00:00:00');
90
+ const date2 = new Date('2023/08/11 08:40:50 am');
91
+ const date3 = new Date();
92
+ date3.setHours(8);
93
+ date3.setMinutes(40);
94
+ date3.setSeconds(50);
95
+ date3.setMilliseconds(0);
96
+
97
+ assert.deepEqual(parseDate('11-08-2023'), date1);
98
+ assert.deepEqual(parseDate('11-08-2023 08:40:50'), date2);
99
+ assert.deepEqual(parseDate('11-08-2023 08:40:50 am'), date2);
100
+ assert.deepEqual(parseDate('11/08/2023'), date1);
101
+ assert.deepEqual(parseDate('11/08/2023 08:40:50'), date2);
102
+ assert.deepEqual(parseDate('11/08/2023 08:40:50 am'), date2);
103
+ assert.deepEqual(parseDate('11.08.2023'), date1);
104
+ assert.deepEqual(parseDate('11.08.2023 08:40:50'), date2);
105
+ assert.deepEqual(parseDate('11.08.2023 08:40:50 am'), date2);
106
+
107
+ assert.deepEqual(parseDate('2023-08-11'), date1);
108
+ assert.deepEqual(parseDate('2023-08-11 08:40:50'), date2);
109
+ assert.deepEqual(parseDate('2023-08-11 08:40:50 am'), date2);
110
+ assert.deepEqual(parseDate('2023/08/11'), date1);
111
+ assert.deepEqual(parseDate('2023/08/11 08:40:50'), date2);
112
+ assert.deepEqual(parseDate('2023/08/11 08:40:50 am'), date2);
113
+ assert.deepEqual(parseDate('2023.08.11'), date1);
114
+ assert.deepEqual(parseDate('2023.08.11 08:40:50'), date2);
115
+ assert.deepEqual(parseDate('2023.08.11 08:40:50 am'), date2);
116
+
117
+ assert.deepEqual(parseDate('08:40:50 2023-08-11'), date2);
118
+ assert.deepEqual(parseDate('08:40:50 am 2023-08-11'), date2);
119
+ assert.deepEqual(parseDate('08:40:50 2023/08/11'), date2);
120
+ assert.deepEqual(parseDate('08:40:50 am 2023/08/11'), date2);
121
+ assert.deepEqual(parseDate('08:40:50 2023.08.11'), date2);
122
+ assert.deepEqual(parseDate('08:40:50 am 2023.08.11'), date2);
123
+
124
+ assert.deepEqual(parseDate('08:40:50'), date3);
125
+ assert.deepEqual(parseDate('08:40:50 am'), date3);
126
+ assert.deepEqual(parseDate('08:40:50'), date3);
127
+ assert.deepEqual(parseDate('08:40:50 am'), date3);
128
+ assert.deepEqual(parseDate('08:40:50'), date3);
129
+ assert.deepEqual(parseDate('08:40:50 am'), date3);
130
+ });
131
+
132
+ it('parseDateByFormat', () => {
133
+ const date1 = new Date('2023/08/11 00:00:00');
134
+ const date2 = new Date('2023/08/11 08:40:50 am');
135
+ const date3 = new Date();
136
+ date3.setHours(8);
137
+ date3.setMinutes(40);
138
+ date3.setSeconds(50);
139
+ date3.setMilliseconds(0);
140
+
141
+ assert.deepEqual(parseDateByFormat('08-11-2023', 'm-d-Y'), date1);
142
+ assert.deepEqual(parseDateByFormat('08-11-23', 'm-d-y'), date1);
143
+ assert.deepEqual(parseDateByFormat('08-11-2023 08:40:50', 'm-d-Y H:i:s'), date2);
144
+ assert.deepEqual(parseDateByFormat('08-11-23 08:40:50', 'm-d-y H:i:s'), date2);
145
+ assert.deepEqual(parseDateByFormat('08-11-2023 08:40:50 am', 'm-d-Y H:i:s a'), date2);
146
+ assert.deepEqual(parseDateByFormat('08-11-23 08:40:50 am', 'm-d-y H:i:s a'), date2);
147
+ });
148
+
149
+ it('isDigits', () => {
150
+ assert(isDigits(123));
151
+ assert(isDigits('123'));
152
+ assert(!isDigits('abc'));
153
+ assert(!isDigits('abc123'));
154
+ });
155
+
156
+ it('isEmpty', () => {
157
+ assert(isEmpty(''));
158
+ assert(isEmpty(null));
159
+ assert(isEmpty(undefined));
160
+ assert(!isEmpty(123));
161
+ assert(!isEmpty('abc'));
162
+ assert(!isEmpty({}));
163
+ assert(!isEmpty([]));
164
+ });
165
+
166
+ it('isNumeric', () => {
167
+ assert(isNumeric(123));
168
+ assert(isNumeric('123'));
169
+ assert(!isNumeric('abc'));
170
+ assert(!isNumeric(true));
171
+ });
172
+
173
+ it('isPlainObject', () => {
174
+ assert(isPlainObject({}));
175
+ assert(!isPlainObject([]));
176
+ assert(!isPlainObject(new Date()));
177
+ });
178
+
179
+ it('isValidDate', () => {
180
+ assert(isValidDate(new Date()));
181
+ assert(isValidDate(new Date('2023/08/11 00:00:00')));
182
+ assert(isValidDate(new Date('2023/08/11 08:40:50 am')));
183
+ assert(isValidDate(new Date('08:40:50 am 2023/08/11')));
184
+ assert(!isValidDate(new Date('')));
185
+ assert(!isValidDate(new Date('08:40:50 am')));
186
+ });
187
+ });
188
+
16
189
  describe('Validation', () => {
17
190
  describe(`Empty rule`, () => {
18
191
  it(`Passes when the rule is empty`, async () => {
@@ -772,6 +945,26 @@ describe('Validation', () => {
772
945
  });
773
946
  });
774
947
 
948
+ describe(`Rule 'extensions'`, () => {
949
+ const rules = { field: 'extensions:jpg,png' };
950
+
951
+ it(`Passes when the field has a valid extension`, async () => {
952
+ const validator = new Validator({ field: new File('hello.jpg', 5 * 1024, 'image/jpg') }, rules);
953
+ assert(await validator.passes());
954
+
955
+ validator.setData({ field: new File('hello.png', 5 * 1024, 'image/png') });
956
+ assert(await validator.passes());
957
+ });
958
+
959
+ it(`Fails when the field has an invalid extension`, async () => {
960
+ const validator = new Validator({ field: new File('hello.doc', 5 * 1024, 'application/msword') }, rules);
961
+ assert(await validator.fails());
962
+
963
+ validator.setData({ field: new File('hello.txt', 5 * 1024, 'text/plain') });
964
+ assert(await validator.fails());
965
+ });
966
+ });
967
+
775
968
  describe(`Rule 'file'`, () => {
776
969
  const rules = { field: 'file' };
777
970
 
@@ -1064,6 +1257,63 @@ describe('Validation', () => {
1064
1257
  });
1065
1258
  });
1066
1259
 
1260
+ describe(`Rule 'hex_color'`, () => {
1261
+ it(`Passes when the field is a valid color`, async () => {
1262
+ const validator = new Validator({
1263
+ field_1: '#abc',
1264
+ field_2: '#abcd',
1265
+ field_3: '#abcabc',
1266
+ field_4: '#abcdabcd',
1267
+ }, {
1268
+ field_1: 'hex_color',
1269
+ field_2: 'hex_color',
1270
+ field_3: 'hex_color',
1271
+ field_4: 'hex_color',
1272
+ });
1273
+
1274
+ assert(await validator.passes());
1275
+ });
1276
+
1277
+ it(`Fails when the field is not a valid color`, async () => {
1278
+ const validator = new Validator({
1279
+ field_1: '#ghi',
1280
+ field_2: '#ghij',
1281
+ field_3: '#ghighi',
1282
+ field_4: '#ghijghij',
1283
+ field_5: 'abc',
1284
+ field_6: 123,
1285
+ field_7: [1, 2, 3],
1286
+ }, {
1287
+ field_1: 'hex_color',
1288
+ field_2: 'hex_color',
1289
+ field_3: 'hex_color',
1290
+ field_4: 'hex_color',
1291
+ field_5: 'hex_color',
1292
+ field_6: 'hex_color',
1293
+ field_7: 'hex_color',
1294
+ });
1295
+
1296
+ assert(await validator.fails());
1297
+ });
1298
+
1299
+ it(`Fails when the color's value is not 3, 4, 6 or 8 digits.`, async () => {
1300
+ const validator = new Validator({
1301
+ field_1: '#1',
1302
+ field_2: '#12',
1303
+ field_3: '#12345',
1304
+ field_4: '#1234567',
1305
+ field_4: '#123456789',
1306
+ }, {
1307
+ field_1: 'hex_color',
1308
+ field_2: 'hex_color',
1309
+ field_3: 'hex_color',
1310
+ field_4: 'hex_color',
1311
+ });
1312
+
1313
+ assert(await validator.fails());
1314
+ });
1315
+ });
1316
+
1067
1317
  describe(`Rule 'image'`, () => {
1068
1318
  const rules = { field: 'image' };
1069
1319