Skip to content

Commit e6aa4a2

Browse files
authored
Flatten support for >200k row arrays (#166)
* flatten support for > 200k row arrays
1 parent 0320152 commit e6aa4a2

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

src/utils.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
let path = require('doc-path'),
44
constants = require('./constants.json');
55

6-
const dateStringRegex = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/;
6+
const dateStringRegex = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/,
7+
MAX_ARRAY_LENGTH = 100000;
78

89
module.exports = {
910
isStringRepresentation,
@@ -285,6 +286,13 @@ function unique(array) {
285286
}
286287

287288
function flatten(array) {
289+
if (array.length > MAX_ARRAY_LENGTH) {
290+
let safeArray = [];
291+
for (let a = 0; a < array.length; a += MAX_ARRAY_LENGTH) {
292+
safeArray = safeArray.concat(...array.slice(a, a + MAX_ARRAY_LENGTH));
293+
}
294+
return safeArray;
295+
}
288296
return [].concat(...array);
289297
}
290298

test/tests.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
let json2csvTests = require('./json2csv'),
22
csv2jsonTests = require('./csv2json'),
3+
utilsTests = require('./utilsTests'),
34
jsonTestData = require('./config/testJsonFilesList'),
45
csvTestData = require('./config/testCsvFilesList');
56

@@ -10,4 +11,7 @@ describe('json-2-csv Node Module', function() {
1011

1112
// Run CSV to JSON Tests
1213
csv2jsonTests.runTests(jsonTestData, csvTestData);
14+
15+
// Run Utils Tests
16+
utilsTests.runTests();
1317
});

test/utilsTests.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "should" }]*/
4+
5+
let should = require('should'),
6+
utils = require('../src/utils');
7+
8+
function runTests() {
9+
describe('utils', () => {
10+
describe('flatten()', () => {
11+
it('should flatten a nested array', (done) => {
12+
const nested = [[1], [2], [3], [4], [5]],
13+
expected = [1, 2, 3, 4, 5],
14+
result = utils.flatten(nested);
15+
16+
result.should.deepEqual(expected);
17+
done();
18+
});
19+
20+
it('should handle extremely large arrays', (done) => {
21+
const nested = [],
22+
expected = [];
23+
for (let a = 0; a < 234567; a++) {
24+
nested.push([a]);
25+
expected.push(a);
26+
}
27+
28+
const result = utils.flatten(nested);
29+
result.should.deepEqual(expected);
30+
done();
31+
});
32+
});
33+
});
34+
}
35+
36+
module.exports = { runTests };

0 commit comments

Comments
 (0)