convert-csv-to-json 4.1.0 → 4.3.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/.eslintignore +3 -0
- package/.eslintrc.json +48 -0
- package/.github/workflows/ci-cd.yml +28 -2
- package/SECURITY.md +2 -0
- package/docs/api/BrowserApi.html +2435 -0
- package/docs/api/BrowserApiError.html +522 -0
- package/docs/api/ConfigurationError.html +594 -0
- package/docs/api/CsvFormatError.html +530 -0
- package/docs/api/CsvParsingError.html +384 -0
- package/docs/api/CsvToJson.html +3136 -0
- package/docs/api/CsvToJsonAsync.html +2672 -0
- package/docs/api/FileOperationError.html +270 -0
- package/docs/api/FileUtils.html +1012 -0
- package/docs/api/InputValidationError.html +293 -0
- package/docs/api/JsonUtil.html +340 -0
- package/docs/api/JsonValidationError.html +247 -0
- package/docs/api/fonts/OpenSans-Bold-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-Bold-webfont.svg +1830 -0
- package/docs/api/fonts/OpenSans-Bold-webfont.woff +0 -0
- package/docs/api/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-BoldItalic-webfont.svg +1830 -0
- package/docs/api/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
- package/docs/api/fonts/OpenSans-Italic-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-Italic-webfont.svg +1830 -0
- package/docs/api/fonts/OpenSans-Italic-webfont.woff +0 -0
- package/docs/api/fonts/OpenSans-Light-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-Light-webfont.svg +1831 -0
- package/docs/api/fonts/OpenSans-Light-webfont.woff +0 -0
- package/docs/api/fonts/OpenSans-LightItalic-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-LightItalic-webfont.svg +1835 -0
- package/docs/api/fonts/OpenSans-LightItalic-webfont.woff +0 -0
- package/docs/api/fonts/OpenSans-Regular-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-Regular-webfont.svg +1831 -0
- package/docs/api/fonts/OpenSans-Regular-webfont.woff +0 -0
- package/docs/api/global.html +3315 -0
- package/docs/api/index.html +326 -0
- package/docs/api/index.js.html +341 -0
- package/docs/api/scripts/linenumber.js +25 -0
- package/docs/api/scripts/prettify/Apache-License-2.0.txt +202 -0
- package/docs/api/scripts/prettify/lang-css.js +2 -0
- package/docs/api/scripts/prettify/prettify.js +28 -0
- package/docs/api/src_browserApi.js.html +271 -0
- package/docs/api/src_csvToJson.js.html +605 -0
- package/docs/api/src_csvToJsonAsync.js.html +244 -0
- package/docs/api/src_util_errors.js.html +374 -0
- package/docs/api/src_util_fileUtils.js.html +147 -0
- package/docs/api/src_util_jsonUtils.js.html +75 -0
- package/docs/api/src_util_stringUtils.js.html +212 -0
- package/docs/api/styles/jsdoc-default.css +358 -0
- package/docs/api/styles/prettify-jsdoc.css +111 -0
- package/docs/api/styles/prettify-tomorrow.css +132 -0
- package/index.js +109 -32
- package/jsdoc.json +17 -0
- package/package.json +10 -3
- package/src/browserApi.js +96 -4
- package/src/csvToJson.js +163 -2
- package/src/csvToJsonAsync.js +74 -14
- package/src/util/errors.js +96 -0
- package/src/util/fileUtils.js +34 -0
- package/src/util/jsonUtils.js +8 -0
- package/src/util/stringUtils.js +51 -0
package/src/util/fileUtils.js
CHANGED
|
@@ -3,8 +3,19 @@
|
|
|
3
3
|
const fs = require('fs');
|
|
4
4
|
const { FileOperationError } = require('./errors');
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* File I/O utilities for reading and writing CSV/JSON files
|
|
8
|
+
* Provides both synchronous and asynchronous file operations
|
|
9
|
+
*/
|
|
6
10
|
class FileUtils {
|
|
7
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Read a file synchronously with specified encoding
|
|
14
|
+
* @param {string} fileInputName - Path to file to read
|
|
15
|
+
* @param {string} encoding - File encoding (e.g., 'utf8', 'latin1')
|
|
16
|
+
* @returns {string} File contents as string
|
|
17
|
+
* @throws {FileOperationError} If file read fails
|
|
18
|
+
*/
|
|
8
19
|
readFile(fileInputName, encoding) {
|
|
9
20
|
try {
|
|
10
21
|
return fs.readFileSync(fileInputName, encoding).toString();
|
|
@@ -13,6 +24,14 @@ class FileUtils {
|
|
|
13
24
|
}
|
|
14
25
|
}
|
|
15
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Read a file asynchronously with specified encoding
|
|
29
|
+
* Uses fs.promises when available, falls back to callback-based API
|
|
30
|
+
* @param {string} fileInputName - Path to file to read
|
|
31
|
+
* @param {string} encoding - File encoding (default: 'utf8')
|
|
32
|
+
* @returns {Promise<string>} Promise resolving to file contents
|
|
33
|
+
* @throws {FileOperationError} If file read fails
|
|
34
|
+
*/
|
|
16
35
|
readFileAsync(fileInputName, encoding = 'utf8') {
|
|
17
36
|
// Use fs.promises when available for a Promise-based API
|
|
18
37
|
if (fs.promises && typeof fs.promises.readFile === 'function') {
|
|
@@ -33,6 +52,13 @@ class FileUtils {
|
|
|
33
52
|
});
|
|
34
53
|
}
|
|
35
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Write content to a file synchronously
|
|
57
|
+
* Logs confirmation message to console on success
|
|
58
|
+
* @param {string} json - Content to write to file
|
|
59
|
+
* @param {string} fileOutputName - Path to output file
|
|
60
|
+
* @throws {FileOperationError} If file write fails
|
|
61
|
+
*/
|
|
36
62
|
writeFile(json, fileOutputName) {
|
|
37
63
|
fs.writeFile(fileOutputName, json, function (err) {
|
|
38
64
|
if (err) {
|
|
@@ -43,6 +69,14 @@ class FileUtils {
|
|
|
43
69
|
});
|
|
44
70
|
}
|
|
45
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Write content to a file asynchronously
|
|
74
|
+
* Uses fs.promises when available, falls back to callback-based API
|
|
75
|
+
* @param {string} json - Content to write to file
|
|
76
|
+
* @param {string} fileOutputName - Path to output file
|
|
77
|
+
* @returns {Promise<void>} Promise that resolves when write completes
|
|
78
|
+
* @throws {FileOperationError} If file write fails
|
|
79
|
+
*/
|
|
46
80
|
writeFileAsync(json, fileOutputName) {
|
|
47
81
|
if (fs.promises && typeof fs.promises.writeFile === 'function') {
|
|
48
82
|
return fs.promises.writeFile(fileOutputName, json)
|
package/src/util/jsonUtils.js
CHANGED
|
@@ -2,8 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
const { JsonValidationError } = require('./errors');
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* JSON validation utilities
|
|
7
|
+
*/
|
|
5
8
|
class JsonUtil {
|
|
6
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Validate that a string is valid JSON
|
|
12
|
+
* @param {string} json - JSON string to validate
|
|
13
|
+
* @throws {JsonValidationError} If JSON is invalid
|
|
14
|
+
*/
|
|
7
15
|
validateJson(json) {
|
|
8
16
|
try {
|
|
9
17
|
JSON.parse(json);
|
package/src/util/stringUtils.js
CHANGED
|
@@ -64,34 +64,79 @@ class StringUtils {
|
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
// Private helper methods for type checking and conversion
|
|
67
|
+
/**
|
|
68
|
+
* Check if a value is empty (undefined or empty string)
|
|
69
|
+
* @param {*} value - Value to check
|
|
70
|
+
* @returns {boolean} True if value is undefined or empty string
|
|
71
|
+
* @private
|
|
72
|
+
*/
|
|
67
73
|
isEmpty(value) {
|
|
68
74
|
return value === undefined || value === '';
|
|
69
75
|
}
|
|
70
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Check if a value is a boolean string ('true' or 'false', case-insensitive)
|
|
79
|
+
* @param {string} value - Value to check
|
|
80
|
+
* @returns {boolean} True if value is 'true' or 'false'
|
|
81
|
+
* @private
|
|
82
|
+
*/
|
|
71
83
|
isBoolean(value) {
|
|
72
84
|
const normalizedValue = value.toLowerCase();
|
|
73
85
|
return normalizedValue === StringUtils.BOOLEAN_VALUES.TRUE ||
|
|
74
86
|
normalizedValue === StringUtils.BOOLEAN_VALUES.FALSE;
|
|
75
87
|
}
|
|
76
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Check if a value is an integer string (with optional leading minus sign)
|
|
91
|
+
* @param {string} value - Value to check
|
|
92
|
+
* @returns {boolean} True if value matches integer pattern
|
|
93
|
+
* @private
|
|
94
|
+
*/
|
|
77
95
|
isInteger(value) {
|
|
78
96
|
return StringUtils.PATTERNS.INTEGER.test(value);
|
|
79
97
|
}
|
|
80
98
|
|
|
99
|
+
/**
|
|
100
|
+
* Check if a value is a float string (decimal number with optional leading minus sign)
|
|
101
|
+
* @param {string} value - Value to check
|
|
102
|
+
* @returns {boolean} True if value matches float pattern
|
|
103
|
+
* @private
|
|
104
|
+
*/
|
|
81
105
|
isFloat(value) {
|
|
82
106
|
return StringUtils.PATTERNS.FLOAT.test(value);
|
|
83
107
|
}
|
|
84
108
|
|
|
109
|
+
/**
|
|
110
|
+
* Check if a numeric string has a leading zero (e.g., '01' or '-01')
|
|
111
|
+
* Leading zeros indicate the value should be kept as a string to preserve formatting
|
|
112
|
+
* @param {string} value - Numeric string value to check
|
|
113
|
+
* @returns {boolean} True if value has a leading zero
|
|
114
|
+
* @private
|
|
115
|
+
*/
|
|
85
116
|
hasLeadingZero(value) {
|
|
86
117
|
const isPositiveWithLeadingZero = value.length > 1 && value[0] === '0';
|
|
87
118
|
const isNegativeWithLeadingZero = value.length > 2 && value[0] === '-' && value[1] === '0';
|
|
88
119
|
return isPositiveWithLeadingZero || isNegativeWithLeadingZero;
|
|
89
120
|
}
|
|
90
121
|
|
|
122
|
+
/**
|
|
123
|
+
* Convert a boolean string to native boolean value
|
|
124
|
+
* Safely converts 'true' to true and 'false' to false
|
|
125
|
+
* @param {string} value - Boolean string ('true' or 'false')
|
|
126
|
+
* @returns {boolean} Native boolean value
|
|
127
|
+
* @private
|
|
128
|
+
*/
|
|
91
129
|
convertToBoolean(value) {
|
|
92
130
|
return JSON.parse(value.toLowerCase());
|
|
93
131
|
}
|
|
94
132
|
|
|
133
|
+
/**
|
|
134
|
+
* Convert an integer string to number or keep as string if it has leading zeros
|
|
135
|
+
* Preserves leading zeros in strings (e.g., '007' stays as string)
|
|
136
|
+
* @param {string} value - Integer string to convert
|
|
137
|
+
* @returns {number|string} Number if safe, otherwise string value
|
|
138
|
+
* @private
|
|
139
|
+
*/
|
|
95
140
|
convertInteger(value) {
|
|
96
141
|
if (this.hasLeadingZero(value)) {
|
|
97
142
|
return String(value);
|
|
@@ -101,6 +146,12 @@ class StringUtils {
|
|
|
101
146
|
return Number.isSafeInteger(num) ? num : String(value);
|
|
102
147
|
}
|
|
103
148
|
|
|
149
|
+
/**
|
|
150
|
+
* Convert a float string to number or keep as string if conversion is unsafe
|
|
151
|
+
* @param {string} value - Float string to convert
|
|
152
|
+
* @returns {number|string} Number if finite and valid, otherwise string value
|
|
153
|
+
* @private
|
|
154
|
+
*/
|
|
104
155
|
convertFloat(value) {
|
|
105
156
|
const num = Number(value);
|
|
106
157
|
return Number.isFinite(num) ? num : String(value);
|