convert-csv-to-json 3.28.0 → 4.1.0
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/CHANGELOG.md +41 -1
- package/README.md +173 -833
- package/docs/ASYNC.md +485 -0
- package/docs/BROWSER.md +425 -0
- package/docs/ERROR_HANDLING.md +367 -0
- package/docs/MAPROWS.md +196 -0
- package/docs/SYNC.md +239 -0
- package/index.d.ts +10 -8
- package/index.js +13 -10
- package/migration/RFC4180_MIGRATION_GUIDE.md +737 -0
- package/package.json +1 -1
- package/release-notes/RELEASE_NOTES_v4.0.0.md +320 -0
- package/src/browserApi.js +29 -6
- package/src/csvToJson.js +233 -82
- package/src/csvToJsonAsync.js +15 -1
- package/src/util/errors.js +227 -0
- package/src/util/fileUtils.js +18 -7
- package/src/util/jsonUtils.js +3 -1
- /package/{MIGRATION.md → migration/MIGRATION_TO_ASYNC.md} +0 -0
package/src/util/fileUtils.js
CHANGED
|
@@ -1,23 +1,31 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const { FileOperationError } = require('./errors');
|
|
4
5
|
|
|
5
6
|
class FileUtils {
|
|
6
7
|
|
|
7
8
|
readFile(fileInputName, encoding) {
|
|
8
|
-
|
|
9
|
+
try {
|
|
10
|
+
return fs.readFileSync(fileInputName, encoding).toString();
|
|
11
|
+
} catch (error) {
|
|
12
|
+
throw new FileOperationError('read', fileInputName, error);
|
|
13
|
+
}
|
|
9
14
|
}
|
|
10
15
|
|
|
11
16
|
readFileAsync(fileInputName, encoding = 'utf8') {
|
|
12
17
|
// Use fs.promises when available for a Promise-based API
|
|
13
18
|
if (fs.promises && typeof fs.promises.readFile === 'function') {
|
|
14
19
|
return fs.promises.readFile(fileInputName, encoding)
|
|
15
|
-
.then(buf => buf.toString())
|
|
20
|
+
.then(buf => buf.toString())
|
|
21
|
+
.catch(error => {
|
|
22
|
+
throw new FileOperationError('read', fileInputName, error);
|
|
23
|
+
});
|
|
16
24
|
}
|
|
17
25
|
return new Promise((resolve, reject) => {
|
|
18
26
|
fs.readFile(fileInputName, encoding, (err, data) => {
|
|
19
27
|
if (err) {
|
|
20
|
-
reject(err);
|
|
28
|
+
reject(new FileOperationError('read', fileInputName, err));
|
|
21
29
|
return;
|
|
22
30
|
}
|
|
23
31
|
resolve(data.toString());
|
|
@@ -28,7 +36,7 @@ class FileUtils {
|
|
|
28
36
|
writeFile(json, fileOutputName) {
|
|
29
37
|
fs.writeFile(fileOutputName, json, function (err) {
|
|
30
38
|
if (err) {
|
|
31
|
-
throw err;
|
|
39
|
+
throw new FileOperationError('write', fileOutputName, err);
|
|
32
40
|
} else {
|
|
33
41
|
console.log('File saved: ' + fileOutputName);
|
|
34
42
|
}
|
|
@@ -37,11 +45,14 @@ class FileUtils {
|
|
|
37
45
|
|
|
38
46
|
writeFileAsync(json, fileOutputName) {
|
|
39
47
|
if (fs.promises && typeof fs.promises.writeFile === 'function') {
|
|
40
|
-
return fs.promises.writeFile(fileOutputName, json)
|
|
48
|
+
return fs.promises.writeFile(fileOutputName, json)
|
|
49
|
+
.catch(error => {
|
|
50
|
+
throw new FileOperationError('write', fileOutputName, error);
|
|
51
|
+
});
|
|
41
52
|
}
|
|
42
53
|
return new Promise((resolve, reject) => {
|
|
43
54
|
fs.writeFile(fileOutputName, json, (err) => {
|
|
44
|
-
if (err) return reject(err);
|
|
55
|
+
if (err) return reject(new FileOperationError('write', fileOutputName, err));
|
|
45
56
|
resolve();
|
|
46
57
|
});
|
|
47
58
|
});
|
package/src/util/jsonUtils.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const { JsonValidationError } = require('./errors');
|
|
4
|
+
|
|
3
5
|
class JsonUtil {
|
|
4
6
|
|
|
5
7
|
validateJson(json) {
|
|
6
8
|
try {
|
|
7
9
|
JSON.parse(json);
|
|
8
10
|
} catch (err) {
|
|
9
|
-
throw
|
|
11
|
+
throw new JsonValidationError(json, err);
|
|
10
12
|
}
|
|
11
13
|
}
|
|
12
14
|
|
|
File without changes
|