diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..95b96d2 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,15 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +### Added +- New `csvStringToJsonStringified()` method that converts CSV content directly to a validated JSON string without requiring file I/O +- Improved test coverage for string-based CSV parsing + +### Changed +- No breaking changes - all existing APIs remain unchanged \ No newline at end of file diff --git a/README.md b/README.md index 752190a..5383fdf 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ show your :heart: and support. - [Number](#number) - [Boolean](#boolean) + [Encoding](#encoding) + + [Working with CSV strings directly](#working-with-csv-strings-directly) * [Chaining Pattern](#chaining-pattern) - [Development](#development) - [CI CD github action](#ci-cd-github-action) @@ -306,6 +307,30 @@ You can read and decode files with the following encoding: .getJsonFromCsv(fileInputName); ``` +#### Working with CSV strings directly +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: + +```js + +// Parse CSV string to array of objects +let csvString = 'firstName;lastName\nJohn;Doe\nJane;Smith'; +let jsonArray = csvToJson.csvStringToJson(csvString); +// Output: [{"firstName":"John","lastName":"Doe"},{"firstName":"Jane","lastName":"Smith"}] + +// Parse CSV string to JSON string (validated) +let jsonString = csvToJson.csvStringToJsonStringified(csvString); +// Output: "[\n {\n \"firstName\": \"John\",\n \"lastName\": \"Doe\"\n },\n {\n \"firstName\": \"Jane\",\n \"lastName\": \"Smith\"\n }\n]" +``` + +Both methods support all configuration options through the chaining pattern: + +```js +let jsonArray = csvToJson + .fieldDelimiter(',') + .formatValueByType() + .csvStringToJson(csvString); +``` + ### Chaining Pattern 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.: diff --git a/index.d.ts b/index.d.ts index ce0c505..4f650cb 100644 --- a/index.d.ts +++ b/index.d.ts @@ -92,6 +92,14 @@ declare module 'convert-csv-to-json' { getJsonFromCsv(inputFileName: string): any[]; csvStringToJson(csvString: string): any[]; + + /** + * Parses a csv string and returns a JSON string (validated) + * @param {csvString} csvString CSV content as string + * @return {string} JSON stringified result + */ + csvStringToJsonStringified(csvString: string): string; + /** * Parses .csv file and put its content into a file in json format. * @param {inputFileName} path/filename diff --git a/index.js b/index.js index ac1bd60..ac9aec4 100644 --- a/index.js +++ b/index.js @@ -156,6 +156,18 @@ exports.csvStringToJson = function(csvString) { return csvToJson.csvStringToJson(csvString); }; +/** + * Parses a csv string and returns a JSON string (validated) + * @param {csvString} csvString CSV content as string + * @return {string} JSON stringified result + */ +exports.csvStringToJsonStringified = function(csvString) { + if (csvString === undefined || csvString === null) { + throw new Error("csvString is not defined!!!"); + } + return csvToJson.csvStringToJsonStringified(csvString); +}; + /** * Parses .csv file and put its content into a file in json format. * @param {inputFileName} path/filename diff --git a/src/csvToJson.js b/src/csvToJson.js index 661f657..cad7d0e 100644 --- a/src/csvToJson.js +++ b/src/csvToJson.js @@ -69,6 +69,13 @@ class CsvToJson { return this.csvToJson(csvString); } + csvStringToJsonStringified(csvString) { + let json = this.csvStringToJson(csvString); + let jsonStringified = JSON.stringify(json, undefined, 1); + jsonUtils.validateJson(jsonStringified); + return jsonStringified; + } + csvToJson(parsedCsv) { this.validateInputConfig(); let lines = parsedCsv.split(newLine); diff --git a/test/csvString.spec.js b/test/csvString.spec.js new file mode 100644 index 0000000..7f574eb --- /dev/null +++ b/test/csvString.spec.js @@ -0,0 +1,16 @@ +'use strict'; +let index = require('../index'); + +describe('csvStringToJsonStringified()', function () { + it('should parse csv content string and return validated JSON string', function () { + let csv = 'firstName;lastName;email;gender;age;birth;zip;registered\nConstantin;Langsdon;clangsdon0@hc360.com;Male;96;10.02.1965;123;true\nNorah;Raison;nraison1@wired.com;Female;32.5;10.05.2000;;false\n'; + let jsonString = index.csvStringToJsonStringified(csv); + expect(typeof jsonString).toBe('string'); + // should be valid JSON + let parsed = JSON.parse(jsonString); + expect(Array.isArray(parsed)).toBe(true); + expect(parsed.length).toBe(2); + expect(parsed[0].firstName).toBe('Constantin'); + expect(parsed[1].firstName).toBe('Norah'); + }); +});