convert-csv-to-json 3.12.0 → 3.12.1

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 ADDED
@@ -0,0 +1,15 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ### Added
11
+ - New `csvStringToJsonStringified()` method that converts CSV content directly to a validated JSON string without requiring file I/O
12
+ - Improved test coverage for string-based CSV parsing
13
+
14
+ ### Changed
15
+ - No breaking changes - all existing APIs remain unchanged
package/README.md CHANGED
@@ -41,6 +41,7 @@ show your :heart: and support.
41
41
  - [Number](#number)
42
42
  - [Boolean](#boolean)
43
43
  + [Encoding](#encoding)
44
+ + [Working with CSV strings directly](#working-with-csv-strings-directly)
44
45
  * [Chaining Pattern](#chaining-pattern)
45
46
  - [Development](#development)
46
47
  - [CI CD github action](#ci-cd-github-action)
@@ -306,6 +307,30 @@ You can read and decode files with the following encoding:
306
307
  .getJsonFromCsv(fileInputName);
307
308
  ```
308
309
 
310
+ #### Working with CSV strings directly
311
+ If you have CSV content as a string (for example, from an API response or test data), you can parse it directly without writing to a file:
312
+
313
+ ```js
314
+
315
+ // Parse CSV string to array of objects
316
+ let csvString = 'firstName;lastName\nJohn;Doe\nJane;Smith';
317
+ let jsonArray = csvToJson.csvStringToJson(csvString);
318
+ // Output: [{"firstName":"John","lastName":"Doe"},{"firstName":"Jane","lastName":"Smith"}]
319
+
320
+ // Parse CSV string to JSON string (validated)
321
+ let jsonString = csvToJson.csvStringToJsonStringified(csvString);
322
+ // Output: "[\n {\n \"firstName\": \"John\",\n \"lastName\": \"Doe\"\n },\n {\n \"firstName\": \"Jane\",\n \"lastName\": \"Smith\"\n }\n]"
323
+ ```
324
+
325
+ Both methods support all configuration options through the chaining pattern:
326
+
327
+ ```js
328
+ let jsonArray = csvToJson
329
+ .fieldDelimiter(',')
330
+ .formatValueByType()
331
+ .csvStringToJson(csvString);
332
+ ```
333
+
309
334
  ### Chaining Pattern
310
335
 
311
336
  The exposed API is implemented with the [Method Chaining Pattern](https://en.wikipedia.org/wiki/Method_chaining), which means that multiple methods can be chained, e.g.:
package/index.d.ts CHANGED
@@ -92,6 +92,14 @@ declare module 'convert-csv-to-json' {
92
92
  getJsonFromCsv(inputFileName: string): any[];
93
93
 
94
94
  csvStringToJson(csvString: string): any[];
95
+
96
+ /**
97
+ * Parses a csv string and returns a JSON string (validated)
98
+ * @param {csvString} csvString CSV content as string
99
+ * @return {string} JSON stringified result
100
+ */
101
+ csvStringToJsonStringified(csvString: string): string;
102
+
95
103
  /**
96
104
  * Parses .csv file and put its content into a file in json format.
97
105
  * @param {inputFileName} path/filename
package/index.js CHANGED
@@ -156,6 +156,18 @@ exports.csvStringToJson = function(csvString) {
156
156
  return csvToJson.csvStringToJson(csvString);
157
157
  };
158
158
 
159
+ /**
160
+ * Parses a csv string and returns a JSON string (validated)
161
+ * @param {csvString} csvString CSV content as string
162
+ * @return {string} JSON stringified result
163
+ */
164
+ exports.csvStringToJsonStringified = function(csvString) {
165
+ if (csvString === undefined || csvString === null) {
166
+ throw new Error("csvString is not defined!!!");
167
+ }
168
+ return csvToJson.csvStringToJsonStringified(csvString);
169
+ };
170
+
159
171
  /**
160
172
  * Parses .csv file and put its content into a file in json format.
161
173
  * @param {inputFileName} path/filename
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "convert-csv-to-json",
3
- "version": "3.12.0",
3
+ "version": "3.12.1",
4
4
  "description": "Convert CSV to JSON",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/src/csvToJson.js CHANGED
@@ -69,6 +69,13 @@ class CsvToJson {
69
69
  return this.csvToJson(csvString);
70
70
  }
71
71
 
72
+ csvStringToJsonStringified(csvString) {
73
+ let json = this.csvStringToJson(csvString);
74
+ let jsonStringified = JSON.stringify(json, undefined, 1);
75
+ jsonUtils.validateJson(jsonStringified);
76
+ return jsonStringified;
77
+ }
78
+
72
79
  csvToJson(parsedCsv) {
73
80
  this.validateInputConfig();
74
81
  let lines = parsedCsv.split(newLine);