Skip to content

Commit 8eae62b

Browse files
lerouxbaddaleax
andauthored
feat(import): Function to guess the import file type COMPASS-6421 (#3983)
* guessFileType * unnecessary * Update packages/compass-import-export/src/import/guess-filetype.ts Co-authored-by: Anna Henningsen <[email protected]> * Update packages/compass-import-export/src/import/guess-filetype.ts Co-authored-by: Anna Henningsen <[email protected]> * once * csvDelimiter required for csv * fixes for return type * lint * ugh Co-authored-by: Anna Henningsen <[email protected]>
1 parent 2c7ca44 commit 8eae62b

File tree

12 files changed

+346
-13
lines changed

12 files changed

+346
-13
lines changed

package-lock.json

Lines changed: 106 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-import-export/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@
8989
"@types/chai-dom": "^0.0.10",
9090
"@types/mime-types": "^2.1.1",
9191
"@types/mocha": "^9.0.0",
92+
"@types/papaparse": "^5.3.7",
9293
"@types/react": "^17.0.5",
9394
"@types/react-dom": "^17.0.10",
9495
"@types/sinon-chai": "^3.2.5",
95-
"JSONStream": "^1.3.5",
96+
"@types/stream-json": "^1.7.3",
9697
"ansi-to-html": "^0.6.11",
9798
"chai": "^4.3.6",
9899
"chai-as-promised": "^7.1.1",
@@ -105,6 +106,7 @@
105106
"fast-csv": "^3.4.0",
106107
"flat": "cipacda/flat",
107108
"hadron-app-registry": "^9.0.2",
109+
"JSONStream": "^1.3.5",
108110
"lodash": "^4.17.21",
109111
"lodash.throttle": "^4.1.1",
110112
"marky": "^1.2.1",
@@ -116,6 +118,7 @@
116118
"mongodb-query-parser": "^2.4.6",
117119
"nyc": "^15.1.0",
118120
"object-sizeof": "^1.5.1",
121+
"papaparse": "^5.3.2",
119122
"parse-json": "^5.0.0",
120123
"peek-stream": "^1.1.3",
121124
"prettier": "^2.7.1",
@@ -128,6 +131,7 @@
128131
"redux-thunk": "^2.3.0",
129132
"rimraf": "^3.0.2",
130133
"sinon": "^9.2.3",
134+
"stream-json": "^1.7.5",
131135
"strip-bom-stream": "^4.0.0",
132136
"xvfb-maybe": "^0.2.1"
133137
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import path from 'path';
2+
import { expect } from 'chai';
3+
import fs from 'fs';
4+
import { guessFileType } from './guess-filetype';
5+
import { fixtures } from '../../test/fixtures';
6+
7+
const expectedDelimiters = {
8+
'bad.csv': ',',
9+
'good-commas.csv': ',',
10+
'good-tabs.csv': '\t',
11+
'number-transform.csv': ',',
12+
'sparse.csv': ',',
13+
'semicolons.csv': ';',
14+
'spaces.csv': ' ',
15+
} as const;
16+
17+
describe('guessFileType', function () {
18+
for (const filepath of Object.values(fixtures.json)) {
19+
const basename = path.basename(filepath);
20+
it(`detects ${basename} as json`, async function () {
21+
const input = fs.createReadStream(filepath);
22+
const { type } = await guessFileType({ input });
23+
expect(type).to.equal('json');
24+
});
25+
}
26+
27+
for (const filepath of Object.values(fixtures.jsonl)) {
28+
const basename = path.basename(filepath);
29+
it(`detects ${basename} as jsonl`, async function () {
30+
const input = fs.createReadStream(filepath);
31+
const { type } = await guessFileType({ input });
32+
expect(type).to.equal('jsonl');
33+
});
34+
}
35+
36+
for (const filepath of Object.values(fixtures.csv)) {
37+
const basename = path.basename(filepath);
38+
it(`detects ${basename} as csv`, async function () {
39+
const input = fs.createReadStream(filepath);
40+
const result = await guessFileType({ input });
41+
expect(result.type).to.equal('csv');
42+
const expectedDelimiter =
43+
expectedDelimiters[basename as keyof typeof expectedDelimiters];
44+
if (expectedDelimiter) {
45+
expect(result.type === 'csv' && result.csvDelimiter).to.equal(
46+
expectedDelimiter
47+
);
48+
} else {
49+
expect(result.type === 'csv' && result.csvDelimiter).to.equal(
50+
`add an entry for ${basename} to expectedDelimiters`
51+
);
52+
}
53+
});
54+
}
55+
56+
// strip out known false positives
57+
const unknownFiles = Object.values(fixtures.other).filter(
58+
(filepath) => !filepath.endsWith('javascript')
59+
);
60+
61+
for (const filepath of unknownFiles) {
62+
const basename = path.basename(filepath);
63+
it(`detects ${basename} as unknown`, async function () {
64+
const input = fs.createReadStream(filepath);
65+
const { type } = await guessFileType({ input });
66+
expect(type).to.equal('unknown');
67+
});
68+
}
69+
70+
it('can treat javascript as a json false positive', async function () {
71+
const input = fs.createReadStream(fixtures.other.javascript);
72+
const { type } = await guessFileType({ input });
73+
expect(type).to.equal('json');
74+
});
75+
});

0 commit comments

Comments
 (0)