qs 6.14.0 → 6.14.2
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/.editorconfig +1 -1
- package/.github/SECURITY.md +11 -0
- package/.github/THREAT_MODEL.md +78 -0
- package/CHANGELOG.md +22 -0
- package/README.md +11 -4
- package/dist/qs.js +24 -24
- package/eslint.config.mjs +56 -0
- package/lib/parse.js +90 -47
- package/lib/utils.js +83 -11
- package/package.json +9 -8
- package/test/parse.js +245 -9
- package/test/stringify.js +7 -3
- package/test/utils.js +135 -0
- package/.eslintrc +0 -39
package/test/parse.js
CHANGED
|
@@ -235,11 +235,11 @@ test('parse()', function (t) {
|
|
|
235
235
|
st.deepEqual(qs.parse('a=b&a[0]=c'), { a: ['b', 'c'] });
|
|
236
236
|
|
|
237
237
|
st.deepEqual(qs.parse('a[1]=b&a=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
|
|
238
|
-
st.deepEqual(qs.parse('a[]=b&a=c', { arrayLimit: 0 }), { a:
|
|
238
|
+
st.deepEqual(qs.parse('a[]=b&a=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
|
|
239
239
|
st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
|
|
240
240
|
|
|
241
241
|
st.deepEqual(qs.parse('a=b&a[1]=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
|
|
242
|
-
st.deepEqual(qs.parse('a=b&a[]=c', { arrayLimit: 0 }), { a:
|
|
242
|
+
st.deepEqual(qs.parse('a=b&a[]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
|
|
243
243
|
st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
|
|
244
244
|
|
|
245
245
|
st.end();
|
|
@@ -261,11 +261,11 @@ test('parse()', function (t) {
|
|
|
261
261
|
});
|
|
262
262
|
|
|
263
263
|
t.test('limits specific array indices to arrayLimit', function (st) {
|
|
264
|
-
st.deepEqual(qs.parse('a[
|
|
265
|
-
st.deepEqual(qs.parse('a[
|
|
264
|
+
st.deepEqual(qs.parse('a[19]=a', { arrayLimit: 20 }), { a: ['a'] });
|
|
265
|
+
st.deepEqual(qs.parse('a[20]=a', { arrayLimit: 20 }), { a: { 20: 'a' } });
|
|
266
266
|
|
|
267
|
-
st.deepEqual(qs.parse('a[
|
|
268
|
-
st.deepEqual(qs.parse('a[
|
|
267
|
+
st.deepEqual(qs.parse('a[19]=a'), { a: ['a'] });
|
|
268
|
+
st.deepEqual(qs.parse('a[20]=a'), { a: { 20: 'a' } });
|
|
269
269
|
st.end();
|
|
270
270
|
});
|
|
271
271
|
|
|
@@ -364,7 +364,7 @@ test('parse()', function (t) {
|
|
|
364
364
|
);
|
|
365
365
|
st.deepEqual(
|
|
366
366
|
qs.parse('a[]=b&a[]&a[]=c&a[]=', { strictNullHandling: true, arrayLimit: 0 }),
|
|
367
|
-
{ a:
|
|
367
|
+
{ a: { 0: 'b', 1: null, 2: 'c', 3: '' } },
|
|
368
368
|
'with arrayLimit 0 + array brackets: null then empty string works'
|
|
369
369
|
);
|
|
370
370
|
|
|
@@ -375,7 +375,7 @@ test('parse()', function (t) {
|
|
|
375
375
|
);
|
|
376
376
|
st.deepEqual(
|
|
377
377
|
qs.parse('a[]=b&a[]=&a[]=c&a[]', { strictNullHandling: true, arrayLimit: 0 }),
|
|
378
|
-
{ a:
|
|
378
|
+
{ a: { 0: 'b', 1: '', 2: 'c', 3: null } },
|
|
379
379
|
'with arrayLimit 0 + array brackets: empty string then null works'
|
|
380
380
|
);
|
|
381
381
|
|
|
@@ -483,7 +483,7 @@ test('parse()', function (t) {
|
|
|
483
483
|
|
|
484
484
|
t.test('allows overriding array limit', function (st) {
|
|
485
485
|
st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { 0: 'b' } });
|
|
486
|
-
st.deepEqual(qs.parse('a[0]=b', { arrayLimit: 0 }), { a:
|
|
486
|
+
st.deepEqual(qs.parse('a[0]=b', { arrayLimit: 0 }), { a: { 0: 'b' } });
|
|
487
487
|
|
|
488
488
|
st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
|
|
489
489
|
st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: 0 }), { a: { '-1': 'b' } });
|
|
@@ -996,6 +996,20 @@ test('parse()', function (t) {
|
|
|
996
996
|
st.end();
|
|
997
997
|
});
|
|
998
998
|
|
|
999
|
+
t.test('handles a custom decoder returning `null`, with a string key of `null`', function (st) {
|
|
1000
|
+
st.deepEqual(
|
|
1001
|
+
qs.parse('null=1&ToNull=2', {
|
|
1002
|
+
decoder: function (str, defaultDecoder, charset) {
|
|
1003
|
+
return str === 'ToNull' ? null : defaultDecoder(str, defaultDecoder, charset);
|
|
1004
|
+
}
|
|
1005
|
+
}),
|
|
1006
|
+
{ 'null': '1' },
|
|
1007
|
+
'"null" key is not overridden by `null` decoder result'
|
|
1008
|
+
);
|
|
1009
|
+
|
|
1010
|
+
st.end();
|
|
1011
|
+
});
|
|
1012
|
+
|
|
999
1013
|
t.test('does not interpret numeric entities in iso-8859-1 when `interpretNumericEntities` is absent', function (st) {
|
|
1000
1014
|
st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1' }), { foo: '☺' });
|
|
1001
1015
|
st.end();
|
|
@@ -1104,6 +1118,7 @@ test('parse()', function (t) {
|
|
|
1104
1118
|
});
|
|
1105
1119
|
|
|
1106
1120
|
st.test('throws error when array limit exceeded', function (sst) {
|
|
1121
|
+
// 4 elements exceeds limit of 3
|
|
1107
1122
|
sst['throws'](
|
|
1108
1123
|
function () {
|
|
1109
1124
|
qs.parse('a[]=1&a[]=2&a[]=3&a[]=4', { arrayLimit: 3, throwOnLimitExceeded: true });
|
|
@@ -1114,6 +1129,14 @@ test('parse()', function (t) {
|
|
|
1114
1129
|
sst.end();
|
|
1115
1130
|
});
|
|
1116
1131
|
|
|
1132
|
+
st.test('does not throw when at limit', function (sst) {
|
|
1133
|
+
// 3 elements = limit of 3, should not throw
|
|
1134
|
+
var result = qs.parse('a[]=1&a[]=2&a[]=3', { arrayLimit: 3, throwOnLimitExceeded: true });
|
|
1135
|
+
sst.ok(Array.isArray(result.a), 'result is an array');
|
|
1136
|
+
sst.deepEqual(result.a, ['1', '2', '3'], 'all values present');
|
|
1137
|
+
sst.end();
|
|
1138
|
+
});
|
|
1139
|
+
|
|
1117
1140
|
st.test('converts array to object if length is greater than limit', function (sst) {
|
|
1118
1141
|
var result = qs.parse('a[1]=1&a[2]=2&a[3]=3&a[4]=4&a[5]=5&a[6]=6', { arrayLimit: 5 });
|
|
1119
1142
|
|
|
@@ -1121,6 +1144,40 @@ test('parse()', function (t) {
|
|
|
1121
1144
|
sst.end();
|
|
1122
1145
|
});
|
|
1123
1146
|
|
|
1147
|
+
st.test('throws error when indexed notation exceeds arrayLimit with throwOnLimitExceeded', function (sst) {
|
|
1148
|
+
sst['throws'](
|
|
1149
|
+
function () {
|
|
1150
|
+
qs.parse('a[1001]=b', { arrayLimit: 1000, throwOnLimitExceeded: true });
|
|
1151
|
+
},
|
|
1152
|
+
new RangeError('Array limit exceeded. Only 1000 elements allowed in an array.'),
|
|
1153
|
+
'throws error for a single index exceeding arrayLimit'
|
|
1154
|
+
);
|
|
1155
|
+
|
|
1156
|
+
sst['throws'](
|
|
1157
|
+
function () {
|
|
1158
|
+
qs.parse('a[0]=1&a[1]=2&a[2]=3&a[10]=4', { arrayLimit: 6, throwOnLimitExceeded: true, allowSparse: true });
|
|
1159
|
+
},
|
|
1160
|
+
new RangeError('Array limit exceeded. Only 6 elements allowed in an array.'),
|
|
1161
|
+
'throws error when a sparse index exceeds arrayLimit'
|
|
1162
|
+
);
|
|
1163
|
+
|
|
1164
|
+
sst.end();
|
|
1165
|
+
});
|
|
1166
|
+
|
|
1167
|
+
st.test('does not throw for indexed notation within arrayLimit with throwOnLimitExceeded', function (sst) {
|
|
1168
|
+
var result = qs.parse('a[4]=b', { arrayLimit: 5, throwOnLimitExceeded: true, allowSparse: true });
|
|
1169
|
+
sst.ok(Array.isArray(result.a), 'result is an array');
|
|
1170
|
+
sst.equal(result.a.length, 5, 'array has correct length');
|
|
1171
|
+
sst.equal(result.a[4], 'b', 'value at index 4 is correct');
|
|
1172
|
+
sst.end();
|
|
1173
|
+
});
|
|
1174
|
+
|
|
1175
|
+
st.test('silently converts to object for indexed notation exceeding arrayLimit without throwOnLimitExceeded', function (sst) {
|
|
1176
|
+
var result = qs.parse('a[1001]=b', { arrayLimit: 1000 });
|
|
1177
|
+
sst.deepEqual(result, { a: { 1001: 'b' } }, 'converts to object without throwing');
|
|
1178
|
+
sst.end();
|
|
1179
|
+
});
|
|
1180
|
+
|
|
1124
1181
|
st.end();
|
|
1125
1182
|
});
|
|
1126
1183
|
|
|
@@ -1274,3 +1331,182 @@ test('qs strictDepth option - non-throw cases', function (t) {
|
|
|
1274
1331
|
st.end();
|
|
1275
1332
|
});
|
|
1276
1333
|
});
|
|
1334
|
+
|
|
1335
|
+
test('DOS', function (t) {
|
|
1336
|
+
var arr = [];
|
|
1337
|
+
for (var i = 0; i < 105; i++) {
|
|
1338
|
+
arr[arr.length] = 'x';
|
|
1339
|
+
}
|
|
1340
|
+
var attack = 'a[]=' + arr.join('&a[]=');
|
|
1341
|
+
var result = qs.parse(attack, { arrayLimit: 100 });
|
|
1342
|
+
|
|
1343
|
+
t.notOk(Array.isArray(result.a), 'arrayLimit is respected: result is an object, not an array');
|
|
1344
|
+
t.equal(Object.keys(result.a).length, 105, 'all values are preserved');
|
|
1345
|
+
|
|
1346
|
+
t.end();
|
|
1347
|
+
});
|
|
1348
|
+
|
|
1349
|
+
test('arrayLimit boundary conditions', function (t) {
|
|
1350
|
+
// arrayLimit is the max number of elements allowed in an array
|
|
1351
|
+
t.test('exactly at the limit stays as array', function (st) {
|
|
1352
|
+
// 3 elements = limit of 3
|
|
1353
|
+
var result = qs.parse('a[]=1&a[]=2&a[]=3', { arrayLimit: 3 });
|
|
1354
|
+
st.ok(Array.isArray(result.a), 'result is an array when count equals limit');
|
|
1355
|
+
st.deepEqual(result.a, ['1', '2', '3'], 'all values present');
|
|
1356
|
+
st.end();
|
|
1357
|
+
});
|
|
1358
|
+
|
|
1359
|
+
t.test('one over the limit converts to object', function (st) {
|
|
1360
|
+
// 4 elements exceeds limit of 3
|
|
1361
|
+
var result = qs.parse('a[]=1&a[]=2&a[]=3&a[]=4', { arrayLimit: 3 });
|
|
1362
|
+
st.notOk(Array.isArray(result.a), 'result is not an array when over limit');
|
|
1363
|
+
st.deepEqual(result.a, { 0: '1', 1: '2', 2: '3', 3: '4' }, 'all values preserved as object');
|
|
1364
|
+
st.end();
|
|
1365
|
+
});
|
|
1366
|
+
|
|
1367
|
+
t.test('arrayLimit 1 with one value', function (st) {
|
|
1368
|
+
// 1 element = limit of 1
|
|
1369
|
+
var result = qs.parse('a[]=1', { arrayLimit: 1 });
|
|
1370
|
+
st.ok(Array.isArray(result.a), 'result is an array when count equals limit');
|
|
1371
|
+
st.deepEqual(result.a, ['1'], 'value preserved as array');
|
|
1372
|
+
st.end();
|
|
1373
|
+
});
|
|
1374
|
+
|
|
1375
|
+
t.test('arrayLimit 1 with two values converts to object', function (st) {
|
|
1376
|
+
// 2 elements exceeds limit of 1
|
|
1377
|
+
var result = qs.parse('a[]=1&a[]=2', { arrayLimit: 1 });
|
|
1378
|
+
st.notOk(Array.isArray(result.a), 'result is not an array');
|
|
1379
|
+
st.deepEqual(result.a, { 0: '1', 1: '2' }, 'all values preserved as object');
|
|
1380
|
+
st.end();
|
|
1381
|
+
});
|
|
1382
|
+
|
|
1383
|
+
t.end();
|
|
1384
|
+
});
|
|
1385
|
+
|
|
1386
|
+
test('comma + arrayLimit', function (t) {
|
|
1387
|
+
t.test('comma-separated values within arrayLimit stay as array', function (st) {
|
|
1388
|
+
var result = qs.parse('a=1,2,3', { comma: true, arrayLimit: 5 });
|
|
1389
|
+
st.ok(Array.isArray(result.a), 'result is an array');
|
|
1390
|
+
st.deepEqual(result.a, ['1', '2', '3'], 'all values present');
|
|
1391
|
+
st.end();
|
|
1392
|
+
});
|
|
1393
|
+
|
|
1394
|
+
t.test('comma-separated values exceeding arrayLimit convert to object', function (st) {
|
|
1395
|
+
var result = qs.parse('a=1,2,3,4', { comma: true, arrayLimit: 3 });
|
|
1396
|
+
st.notOk(Array.isArray(result.a), 'result is not an array when over limit');
|
|
1397
|
+
st.deepEqual(result.a, { 0: '1', 1: '2', 2: '3', 3: '4' }, 'all values preserved as object');
|
|
1398
|
+
st.end();
|
|
1399
|
+
});
|
|
1400
|
+
|
|
1401
|
+
t.test('comma-separated values exceeding arrayLimit with throwOnLimitExceeded throws', function (st) {
|
|
1402
|
+
st['throws'](
|
|
1403
|
+
function () {
|
|
1404
|
+
qs.parse('a=1,2,3,4', { comma: true, arrayLimit: 3, throwOnLimitExceeded: true });
|
|
1405
|
+
},
|
|
1406
|
+
new RangeError('Array limit exceeded. Only 3 elements allowed in an array.'),
|
|
1407
|
+
'throws error when comma-split exceeds array limit'
|
|
1408
|
+
);
|
|
1409
|
+
st.end();
|
|
1410
|
+
});
|
|
1411
|
+
|
|
1412
|
+
t.test('comma-separated values at exactly arrayLimit stay as array', function (st) {
|
|
1413
|
+
var result = qs.parse('a=1,2,3', { comma: true, arrayLimit: 3 });
|
|
1414
|
+
st.ok(Array.isArray(result.a), 'result is an array when exactly at limit');
|
|
1415
|
+
st.deepEqual(result.a, ['1', '2', '3'], 'all values present');
|
|
1416
|
+
st.end();
|
|
1417
|
+
});
|
|
1418
|
+
|
|
1419
|
+
t.end();
|
|
1420
|
+
});
|
|
1421
|
+
|
|
1422
|
+
test('mixed array and object notation', function (t) {
|
|
1423
|
+
t.test('array brackets with object key - under limit', function (st) {
|
|
1424
|
+
st.deepEqual(
|
|
1425
|
+
qs.parse('a[]=b&a[c]=d'),
|
|
1426
|
+
{ a: { 0: 'b', c: 'd' } },
|
|
1427
|
+
'mixing [] and [key] converts to object'
|
|
1428
|
+
);
|
|
1429
|
+
st.end();
|
|
1430
|
+
});
|
|
1431
|
+
|
|
1432
|
+
t.test('array index with object key - under limit', function (st) {
|
|
1433
|
+
st.deepEqual(
|
|
1434
|
+
qs.parse('a[0]=b&a[c]=d'),
|
|
1435
|
+
{ a: { 0: 'b', c: 'd' } },
|
|
1436
|
+
'mixing [0] and [key] produces object'
|
|
1437
|
+
);
|
|
1438
|
+
st.end();
|
|
1439
|
+
});
|
|
1440
|
+
|
|
1441
|
+
t.test('plain value with array brackets - under limit', function (st) {
|
|
1442
|
+
st.deepEqual(
|
|
1443
|
+
qs.parse('a=b&a[]=c', { arrayLimit: 20 }),
|
|
1444
|
+
{ a: ['b', 'c'] },
|
|
1445
|
+
'plain value combined with [] stays as array under limit'
|
|
1446
|
+
);
|
|
1447
|
+
st.end();
|
|
1448
|
+
});
|
|
1449
|
+
|
|
1450
|
+
t.test('array brackets with plain value - under limit', function (st) {
|
|
1451
|
+
st.deepEqual(
|
|
1452
|
+
qs.parse('a[]=b&a=c', { arrayLimit: 20 }),
|
|
1453
|
+
{ a: ['b', 'c'] },
|
|
1454
|
+
'[] combined with plain value stays as array under limit'
|
|
1455
|
+
);
|
|
1456
|
+
st.end();
|
|
1457
|
+
});
|
|
1458
|
+
|
|
1459
|
+
t.test('plain value with array index - under limit', function (st) {
|
|
1460
|
+
st.deepEqual(
|
|
1461
|
+
qs.parse('a=b&a[0]=c', { arrayLimit: 20 }),
|
|
1462
|
+
{ a: ['b', 'c'] },
|
|
1463
|
+
'plain value combined with [0] stays as array under limit'
|
|
1464
|
+
);
|
|
1465
|
+
st.end();
|
|
1466
|
+
});
|
|
1467
|
+
|
|
1468
|
+
t.test('multiple plain values with duplicates combine', function (st) {
|
|
1469
|
+
st.deepEqual(
|
|
1470
|
+
qs.parse('a=b&a=c&a=d', { arrayLimit: 20 }),
|
|
1471
|
+
{ a: ['b', 'c', 'd'] },
|
|
1472
|
+
'duplicate plain keys combine into array'
|
|
1473
|
+
);
|
|
1474
|
+
st.end();
|
|
1475
|
+
});
|
|
1476
|
+
|
|
1477
|
+
t.test('multiple plain values exceeding limit', function (st) {
|
|
1478
|
+
// 3 elements (indices 0-2), max index 2 > limit 1
|
|
1479
|
+
st.deepEqual(
|
|
1480
|
+
qs.parse('a=b&a=c&a=d', { arrayLimit: 1 }),
|
|
1481
|
+
{ a: { 0: 'b', 1: 'c', 2: 'd' } },
|
|
1482
|
+
'duplicate plain keys convert to object when exceeding limit'
|
|
1483
|
+
);
|
|
1484
|
+
st.end();
|
|
1485
|
+
});
|
|
1486
|
+
|
|
1487
|
+
t.test('mixed notation produces consistent results when arrayLimit is exceeded', function (st) {
|
|
1488
|
+
var expected = { a: { 0: 'b', 1: 'c', 2: 'd' } };
|
|
1489
|
+
|
|
1490
|
+
st.deepEqual(
|
|
1491
|
+
qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: -1 }),
|
|
1492
|
+
expected,
|
|
1493
|
+
'arrayLimit -1'
|
|
1494
|
+
);
|
|
1495
|
+
|
|
1496
|
+
st.deepEqual(
|
|
1497
|
+
qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: 0 }),
|
|
1498
|
+
expected,
|
|
1499
|
+
'arrayLimit 0'
|
|
1500
|
+
);
|
|
1501
|
+
|
|
1502
|
+
st.deepEqual(
|
|
1503
|
+
qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: 1 }),
|
|
1504
|
+
expected,
|
|
1505
|
+
'arrayLimit 1'
|
|
1506
|
+
);
|
|
1507
|
+
|
|
1508
|
+
st.end();
|
|
1509
|
+
});
|
|
1510
|
+
|
|
1511
|
+
t.end();
|
|
1512
|
+
});
|
package/test/stringify.js
CHANGED
|
@@ -1293,13 +1293,17 @@ test('stringifies empty keys', function (t) {
|
|
|
1293
1293
|
});
|
|
1294
1294
|
|
|
1295
1295
|
t.test('stringifies non-string keys', function (st) {
|
|
1296
|
-
var
|
|
1297
|
-
|
|
1296
|
+
var S = Object('abc');
|
|
1297
|
+
S.toString = function () {
|
|
1298
|
+
return 'd';
|
|
1299
|
+
};
|
|
1300
|
+
var actual = qs.stringify({ a: 'b', 'false': {}, 1e+22: 'c', d: 'e' }, {
|
|
1301
|
+
filter: ['a', false, null, 10000000000000000000000, S],
|
|
1298
1302
|
allowDots: true,
|
|
1299
1303
|
encodeDotInKeys: true
|
|
1300
1304
|
});
|
|
1301
1305
|
|
|
1302
|
-
st.equal(actual, 'a=b', 'stringifies correctly');
|
|
1306
|
+
st.equal(actual, 'a=b&1e%2B22=c&d=e', 'stringifies correctly');
|
|
1303
1307
|
|
|
1304
1308
|
st.end();
|
|
1305
1309
|
});
|
package/test/utils.js
CHANGED
|
@@ -68,6 +68,62 @@ test('merge()', function (t) {
|
|
|
68
68
|
}
|
|
69
69
|
);
|
|
70
70
|
|
|
71
|
+
t.test('with overflow objects (from arrayLimit)', function (st) {
|
|
72
|
+
// arrayLimit is max index, so with limit 0, max index 0 is allowed (1 element)
|
|
73
|
+
// To create overflow, need 2+ elements with limit 0, or 3+ with limit 1, etc.
|
|
74
|
+
st.test('merges primitive into overflow object at next index', function (s2t) {
|
|
75
|
+
// Create an overflow object via combine: 3 elements (indices 0-2) with limit 0
|
|
76
|
+
var overflow = utils.combine(['a', 'b'], 'c', 0, false);
|
|
77
|
+
s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');
|
|
78
|
+
var merged = utils.merge(overflow, 'd');
|
|
79
|
+
s2t.deepEqual(merged, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'adds primitive at next numeric index');
|
|
80
|
+
s2t.end();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
st.test('merges primitive into regular object with numeric keys normally', function (s2t) {
|
|
84
|
+
var obj = { 0: 'a', 1: 'b' };
|
|
85
|
+
s2t.notOk(utils.isOverflow(obj), 'plain object is not marked as overflow');
|
|
86
|
+
var merged = utils.merge(obj, 'c');
|
|
87
|
+
s2t.deepEqual(merged, { 0: 'a', 1: 'b', c: true }, 'adds primitive as key (not at next index)');
|
|
88
|
+
s2t.end();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
st.test('merges primitive into object with non-numeric keys normally', function (s2t) {
|
|
92
|
+
var obj = { foo: 'bar' };
|
|
93
|
+
var merged = utils.merge(obj, 'baz');
|
|
94
|
+
s2t.deepEqual(merged, { foo: 'bar', baz: true }, 'adds primitive as key with value true');
|
|
95
|
+
s2t.end();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
st.test('merges overflow object into primitive', function (s2t) {
|
|
99
|
+
// Create an overflow object via combine: 2 elements (indices 0-1) with limit 0
|
|
100
|
+
var overflow = utils.combine(['a'], 'b', 0, false);
|
|
101
|
+
s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');
|
|
102
|
+
var merged = utils.merge('c', overflow);
|
|
103
|
+
s2t.ok(utils.isOverflow(merged), 'result is also marked as overflow');
|
|
104
|
+
s2t.deepEqual(merged, { 0: 'c', 1: 'a', 2: 'b' }, 'creates object with primitive at 0, source values shifted');
|
|
105
|
+
s2t.end();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
st.test('merges overflow object with multiple values into primitive', function (s2t) {
|
|
109
|
+
// Create an overflow object via combine: 3 elements (indices 0-2) with limit 0
|
|
110
|
+
var overflow = utils.combine(['b', 'c'], 'd', 0, false);
|
|
111
|
+
s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');
|
|
112
|
+
var merged = utils.merge('a', overflow);
|
|
113
|
+
s2t.deepEqual(merged, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'shifts all source indices by 1');
|
|
114
|
+
s2t.end();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
st.test('merges regular object into primitive as array', function (s2t) {
|
|
118
|
+
var obj = { foo: 'bar' };
|
|
119
|
+
var merged = utils.merge('a', obj);
|
|
120
|
+
s2t.deepEqual(merged, ['a', { foo: 'bar' }], 'creates array with primitive and object');
|
|
121
|
+
s2t.end();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
st.end();
|
|
125
|
+
});
|
|
126
|
+
|
|
71
127
|
t.end();
|
|
72
128
|
});
|
|
73
129
|
|
|
@@ -132,6 +188,85 @@ test('combine()', function (t) {
|
|
|
132
188
|
st.end();
|
|
133
189
|
});
|
|
134
190
|
|
|
191
|
+
t.test('with arrayLimit', function (st) {
|
|
192
|
+
st.test('under the limit', function (s2t) {
|
|
193
|
+
var combined = utils.combine(['a', 'b'], 'c', 10, false);
|
|
194
|
+
s2t.deepEqual(combined, ['a', 'b', 'c'], 'returns array when under limit');
|
|
195
|
+
s2t.ok(Array.isArray(combined), 'result is an array');
|
|
196
|
+
s2t.end();
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
st.test('exactly at the limit stays as array', function (s2t) {
|
|
200
|
+
var combined = utils.combine(['a', 'b'], 'c', 3, false);
|
|
201
|
+
s2t.deepEqual(combined, ['a', 'b', 'c'], 'stays as array when count equals limit');
|
|
202
|
+
s2t.ok(Array.isArray(combined), 'result is an array');
|
|
203
|
+
s2t.end();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
st.test('over the limit', function (s2t) {
|
|
207
|
+
var combined = utils.combine(['a', 'b', 'c'], 'd', 3, false);
|
|
208
|
+
s2t.deepEqual(combined, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'converts to object when over limit');
|
|
209
|
+
s2t.notOk(Array.isArray(combined), 'result is not an array');
|
|
210
|
+
s2t.end();
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
st.test('with arrayLimit 1', function (s2t) {
|
|
214
|
+
var combined = utils.combine([], 'a', 1, false);
|
|
215
|
+
s2t.deepEqual(combined, ['a'], 'stays as array when count equals limit');
|
|
216
|
+
s2t.ok(Array.isArray(combined), 'result is an array');
|
|
217
|
+
s2t.end();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
st.test('with arrayLimit 0 converts single element to object', function (s2t) {
|
|
221
|
+
var combined = utils.combine([], 'a', 0, false);
|
|
222
|
+
s2t.deepEqual(combined, { 0: 'a' }, 'converts to object when count exceeds limit');
|
|
223
|
+
s2t.notOk(Array.isArray(combined), 'result is not an array');
|
|
224
|
+
s2t.end();
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
st.test('with arrayLimit 0 and two elements converts to object', function (s2t) {
|
|
228
|
+
var combined = utils.combine(['a'], 'b', 0, false);
|
|
229
|
+
s2t.deepEqual(combined, { 0: 'a', 1: 'b' }, 'converts to object when count exceeds limit');
|
|
230
|
+
s2t.notOk(Array.isArray(combined), 'result is not an array');
|
|
231
|
+
s2t.end();
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
st.test('with plainObjects option', function (s2t) {
|
|
235
|
+
var combined = utils.combine(['a', 'b'], 'c', 1, true);
|
|
236
|
+
var expected = { __proto__: null, 0: 'a', 1: 'b', 2: 'c' };
|
|
237
|
+
s2t.deepEqual(combined, expected, 'converts to object with null prototype');
|
|
238
|
+
s2t.equal(Object.getPrototypeOf(combined), null, 'result has null prototype when plainObjects is true');
|
|
239
|
+
s2t.end();
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
st.end();
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
t.test('with existing overflow object', function (st) {
|
|
246
|
+
st.test('adds to existing overflow object at next index', function (s2t) {
|
|
247
|
+
// Create overflow object first via combine: 3 elements (indices 0-2) with limit 0
|
|
248
|
+
var overflow = utils.combine(['a', 'b'], 'c', 0, false);
|
|
249
|
+
s2t.ok(utils.isOverflow(overflow), 'initial object is marked as overflow');
|
|
250
|
+
|
|
251
|
+
var combined = utils.combine(overflow, 'd', 10, false);
|
|
252
|
+
s2t.equal(combined, overflow, 'returns the same object (mutated)');
|
|
253
|
+
s2t.deepEqual(combined, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'adds value at next numeric index');
|
|
254
|
+
s2t.end();
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
st.test('does not treat plain object with numeric keys as overflow', function (s2t) {
|
|
258
|
+
var plainObj = { 0: 'a', 1: 'b' };
|
|
259
|
+
s2t.notOk(utils.isOverflow(plainObj), 'plain object is not marked as overflow');
|
|
260
|
+
|
|
261
|
+
// combine treats this as a regular value, not an overflow object to append to
|
|
262
|
+
var combined = utils.combine(plainObj, 'c', 10, false);
|
|
263
|
+
s2t.deepEqual(combined, [{ 0: 'a', 1: 'b' }, 'c'], 'concatenates as regular values');
|
|
264
|
+
s2t.end();
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
st.end();
|
|
268
|
+
});
|
|
269
|
+
|
|
135
270
|
t.end();
|
|
136
271
|
});
|
|
137
272
|
|
package/.eslintrc
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"root": true,
|
|
3
|
-
|
|
4
|
-
"extends": "@ljharb",
|
|
5
|
-
|
|
6
|
-
"ignorePatterns": [
|
|
7
|
-
"dist/",
|
|
8
|
-
],
|
|
9
|
-
|
|
10
|
-
"rules": {
|
|
11
|
-
"complexity": 0,
|
|
12
|
-
"consistent-return": 1,
|
|
13
|
-
"func-name-matching": 0,
|
|
14
|
-
"id-length": [2, { "min": 1, "max": 25, "properties": "never" }],
|
|
15
|
-
"indent": [2, 4],
|
|
16
|
-
"max-lines": 0,
|
|
17
|
-
"max-lines-per-function": [2, { "max": 150 }],
|
|
18
|
-
"max-params": [2, 18],
|
|
19
|
-
"max-statements": [2, 100],
|
|
20
|
-
"multiline-comment-style": 0,
|
|
21
|
-
"no-continue": 1,
|
|
22
|
-
"no-magic-numbers": 0,
|
|
23
|
-
"no-restricted-syntax": [2, "BreakStatement", "DebuggerStatement", "ForInStatement", "LabeledStatement", "WithStatement"],
|
|
24
|
-
},
|
|
25
|
-
|
|
26
|
-
"overrides": [
|
|
27
|
-
{
|
|
28
|
-
"files": "test/**",
|
|
29
|
-
"rules": {
|
|
30
|
-
"function-paren-newline": 0,
|
|
31
|
-
"max-lines-per-function": 0,
|
|
32
|
-
"max-statements": 0,
|
|
33
|
-
"no-buffer-constructor": 0,
|
|
34
|
-
"no-extend-native": 0,
|
|
35
|
-
"no-throw-literal": 0,
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
],
|
|
39
|
-
}
|