@alwatr/json2csv 1.0.4 โ 1.0.5
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 +6 -0
- package/dist/main.cjs +1 -2
- package/dist/main.cjs.map +1 -1
- package/dist/main.mjs +1 -2
- package/dist/main.mjs.map +1 -1
- package/package.json +13 -13
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [1.0.5](https://github.com/Alwatr/nanolib/compare/@alwatr/json2csv@1.0.4...@alwatr/json2csv@1.0.5) (2026-03-16)
|
|
7
|
+
|
|
8
|
+
### ๐จ Code Refactoring
|
|
9
|
+
|
|
10
|
+
* migrate build scripts from yarn to bun across multiple packages ([d90e962](https://github.com/Alwatr/nanolib/commit/d90e962f15e5c951e191d5f02341279b6472abc3))
|
|
11
|
+
|
|
6
12
|
## [1.0.4](https://github.com/Alwatr/nanolib/compare/@alwatr/json2csv@1.0.3...@alwatr/json2csv@1.0.4) (2026-02-18)
|
|
7
13
|
|
|
8
14
|
### ๐งน Miscellaneous Chores
|
package/dist/main.cjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/** ๐ฆ @alwatr/json2csv v1.0.
|
|
2
|
-
__dev_mode__: console.debug("๐ฆ @alwatr/json2csv v1.0.4");
|
|
1
|
+
/** ๐ฆ @alwatr/json2csv v1.0.5 */
|
|
3
2
|
"use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from==="object"||typeof from==="function"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:true}),mod);var main_exports={};__export(main_exports,{jsonToCsv:()=>jsonToCsv});module.exports=__toCommonJS(main_exports);function jsonToCsv(jsonData,delimiter=",",includeHeaders=true,replacer){if(!Array.isArray(jsonData)||jsonData.length===0||typeof jsonData[0]!=="object"||jsonData[0]===null){return""}const delimiterRegex=new RegExp(`[${delimiter}\\n"]`);const doubleQuoteRegex=/"/g;const headers=Object.keys(jsonData[0]);const headersLen=headers.length;const csvRows=[];if(includeHeaders){let headerRow="";for(let i=0;i<headersLen;i++){if(i>0)headerRow+=delimiter;headerRow+=escapeCsvValue(headers[i],delimiterRegex,doubleQuoteRegex)}csvRows.push(headerRow)}for(const row of jsonData){if(typeof row!=="object"||row===null||Array.isArray(row)){continue}let rowStr="";for(let i=0;i<headersLen;i++){if(i>0)rowStr+=delimiter;const header=headers[i];let cellValue=row[header];if(replacer&&cellValue!==void 0){cellValue=replacer(header,cellValue)}if(cellValue===null||cellValue===void 0){}else if(typeof cellValue==="object"){rowStr+=escapeCsvValue(JSON.stringify(cellValue,replacer),delimiterRegex,doubleQuoteRegex)}else{rowStr+=escapeCsvValue(String(cellValue),delimiterRegex,doubleQuoteRegex)}}csvRows.push(rowStr)}return csvRows.join("\n")}function escapeCsvValue(value,delimiterRegex,doubleQuoteRegex){if(value==="")return value;if(delimiterRegex.test(value)){return`"${value.replace(doubleQuoteRegex,'""')}"`}return value}0&&(module.exports={jsonToCsv});
|
|
4
3
|
//# sourceMappingURL=main.cjs.map
|
package/dist/main.cjs.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/main.ts"],
|
|
4
4
|
"sourcesContent": ["import type {} from '@alwatr/type-helper';\n\n/**\n * Converts a JSON array of objects to a CSV string.\n *\n * @param jsonData - The Array of JSON objects to convert.\n * @param delimiter - The delimiter character to use (default: ',').\n * @param includeHeaders - Whether to include the header row (default: true).\n * @param replacer - A function that handles value replacement, similar to `JSON.stringify`.\n * @returns The CSV string.\n *\n * @example\n * ```ts\n * const data = [{name: 'Ali', age: 30}, {name: 'John', age: 25}];\n * const csv = jsonToCsv(data);\n * // \"name,age\\nAli,30\\nJohn,25\"\n * ```\n */\nexport function jsonToCsv(\n jsonData?: DictionaryOpt<unknown>[],\n delimiter = ',',\n includeHeaders = true,\n replacer?: (key: string, value: unknown) => JsonValue,\n): string {\n if (!Array.isArray(jsonData) || jsonData.length === 0 || typeof jsonData[0] !== 'object' || jsonData[0] === null) {\n return '';\n }\n\n const delimiterRegex = new RegExp(`[${delimiter}\\\\n\"]`);\n const doubleQuoteRegex = /\"/g;\n\n // 1. Extract Headers (Keys)\n const headers = Object.keys(jsonData[0]);\n const headersLen = headers.length;\n\n // 2. Create CSV\n const csvRows: string[] = [];\n\n if (includeHeaders) {\n let headerRow = '';\n for (let i = 0; i < headersLen; i++) {\n if (i > 0) headerRow += delimiter;\n headerRow += escapeCsvValue(headers[i], delimiterRegex, doubleQuoteRegex);\n }\n csvRows.push(headerRow);\n }\n\n // 3. Iterate through Data and Create CSV Rows\n for (const row of jsonData) {\n if (typeof row !== 'object' || row === null || Array.isArray(row)) {\n // Skip non-object rows\n continue;\n }\n\n let rowStr = '';\n for (let i = 0; i < headersLen; i++) {\n if (i > 0) rowStr += delimiter;\n\n const header = headers[i];\n let cellValue = (row as DictionaryOpt<JsonValue>)[header];\n\n if (replacer && cellValue !== undefined) {\n cellValue = replacer(header, cellValue);\n }\n\n if (cellValue === null || cellValue === undefined) {\n // skip empty value\n }\n else if (typeof cellValue === 'object') {\n rowStr += escapeCsvValue(JSON.stringify(cellValue, replacer), delimiterRegex, doubleQuoteRegex);\n }\n else {\n rowStr += escapeCsvValue(String(cellValue), delimiterRegex, doubleQuoteRegex);\n }\n }\n csvRows.push(rowStr);\n }\n\n return csvRows.join('\\n');\n}\n\n// Optimized helper function\nfunction escapeCsvValue(value: string, delimiterRegex: RegExp, doubleQuoteRegex: RegExp): string {\n if (value === '') return value;\n\n // Only replace if necessary\n if (delimiterRegex.test(value)) {\n return `\"${value.replace(doubleQuoteRegex, '\"\"')}\"`;\n }\n\n return value;\n}\n"],
|
|
5
|
-
"mappings": "
|
|
5
|
+
"mappings": ";qqBAAA,+GAkBO,SAAS,UACd,SACA,UAAY,IACZ,eAAiB,KACjB,SACQ,CACR,GAAI,CAAC,MAAM,QAAQ,QAAQ,GAAK,SAAS,SAAW,GAAK,OAAO,SAAS,CAAC,IAAM,UAAY,SAAS,CAAC,IAAM,KAAM,CAChH,MAAO,EACT,CAEA,MAAM,eAAiB,IAAI,OAAO,IAAI,SAAS,OAAO,EACtD,MAAM,iBAAmB,KAGzB,MAAM,QAAU,OAAO,KAAK,SAAS,CAAC,CAAC,EACvC,MAAM,WAAa,QAAQ,OAG3B,MAAM,QAAoB,CAAC,EAE3B,GAAI,eAAgB,CAClB,IAAI,UAAY,GAChB,QAAS,EAAI,EAAG,EAAI,WAAY,IAAK,CACnC,GAAI,EAAI,EAAG,WAAa,UACxB,WAAa,eAAe,QAAQ,CAAC,EAAG,eAAgB,gBAAgB,CAC1E,CACA,QAAQ,KAAK,SAAS,CACxB,CAGA,UAAW,OAAO,SAAU,CAC1B,GAAI,OAAO,MAAQ,UAAY,MAAQ,MAAQ,MAAM,QAAQ,GAAG,EAAG,CAEjE,QACF,CAEA,IAAI,OAAS,GACb,QAAS,EAAI,EAAG,EAAI,WAAY,IAAK,CACnC,GAAI,EAAI,EAAG,QAAU,UAErB,MAAM,OAAS,QAAQ,CAAC,EACxB,IAAI,UAAa,IAAiC,MAAM,EAExD,GAAI,UAAY,YAAc,OAAW,CACvC,UAAY,SAAS,OAAQ,SAAS,CACxC,CAEA,GAAI,YAAc,MAAQ,YAAc,OAAW,CAEnD,SACS,OAAO,YAAc,SAAU,CACtC,QAAU,eAAe,KAAK,UAAU,UAAW,QAAQ,EAAG,eAAgB,gBAAgB,CAChG,KACK,CACH,QAAU,eAAe,OAAO,SAAS,EAAG,eAAgB,gBAAgB,CAC9E,CACF,CACA,QAAQ,KAAK,MAAM,CACrB,CAEA,OAAO,QAAQ,KAAK,IAAI,CAC1B,CAGA,SAAS,eAAe,MAAe,eAAwB,iBAAkC,CAC/F,GAAI,QAAU,GAAI,OAAO,MAGzB,GAAI,eAAe,KAAK,KAAK,EAAG,CAC9B,MAAO,IAAI,MAAM,QAAQ,iBAAkB,IAAI,CAAC,GAClD,CAEA,OAAO,KACT",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/main.mjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/** ๐ฆ @alwatr/json2csv v1.0.
|
|
2
|
-
__dev_mode__: console.debug("๐ฆ @alwatr/json2csv v1.0.4");
|
|
1
|
+
/** ๐ฆ @alwatr/json2csv v1.0.5 */
|
|
3
2
|
function jsonToCsv(jsonData,delimiter=",",includeHeaders=true,replacer){if(!Array.isArray(jsonData)||jsonData.length===0||typeof jsonData[0]!=="object"||jsonData[0]===null){return""}const delimiterRegex=new RegExp(`[${delimiter}\\n"]`);const doubleQuoteRegex=/"/g;const headers=Object.keys(jsonData[0]);const headersLen=headers.length;const csvRows=[];if(includeHeaders){let headerRow="";for(let i=0;i<headersLen;i++){if(i>0)headerRow+=delimiter;headerRow+=escapeCsvValue(headers[i],delimiterRegex,doubleQuoteRegex)}csvRows.push(headerRow)}for(const row of jsonData){if(typeof row!=="object"||row===null||Array.isArray(row)){continue}let rowStr="";for(let i=0;i<headersLen;i++){if(i>0)rowStr+=delimiter;const header=headers[i];let cellValue=row[header];if(replacer&&cellValue!==void 0){cellValue=replacer(header,cellValue)}if(cellValue===null||cellValue===void 0){}else if(typeof cellValue==="object"){rowStr+=escapeCsvValue(JSON.stringify(cellValue,replacer),delimiterRegex,doubleQuoteRegex)}else{rowStr+=escapeCsvValue(String(cellValue),delimiterRegex,doubleQuoteRegex)}}csvRows.push(rowStr)}return csvRows.join("\n")}function escapeCsvValue(value,delimiterRegex,doubleQuoteRegex){if(value==="")return value;if(delimiterRegex.test(value)){return`"${value.replace(doubleQuoteRegex,'""')}"`}return value}export{jsonToCsv};
|
|
4
3
|
//# sourceMappingURL=main.mjs.map
|
package/dist/main.mjs.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/main.ts"],
|
|
4
4
|
"sourcesContent": ["import type {} from '@alwatr/type-helper';\n\n/**\n * Converts a JSON array of objects to a CSV string.\n *\n * @param jsonData - The Array of JSON objects to convert.\n * @param delimiter - The delimiter character to use (default: ',').\n * @param includeHeaders - Whether to include the header row (default: true).\n * @param replacer - A function that handles value replacement, similar to `JSON.stringify`.\n * @returns The CSV string.\n *\n * @example\n * ```ts\n * const data = [{name: 'Ali', age: 30}, {name: 'John', age: 25}];\n * const csv = jsonToCsv(data);\n * // \"name,age\\nAli,30\\nJohn,25\"\n * ```\n */\nexport function jsonToCsv(\n jsonData?: DictionaryOpt<unknown>[],\n delimiter = ',',\n includeHeaders = true,\n replacer?: (key: string, value: unknown) => JsonValue,\n): string {\n if (!Array.isArray(jsonData) || jsonData.length === 0 || typeof jsonData[0] !== 'object' || jsonData[0] === null) {\n return '';\n }\n\n const delimiterRegex = new RegExp(`[${delimiter}\\\\n\"]`);\n const doubleQuoteRegex = /\"/g;\n\n // 1. Extract Headers (Keys)\n const headers = Object.keys(jsonData[0]);\n const headersLen = headers.length;\n\n // 2. Create CSV\n const csvRows: string[] = [];\n\n if (includeHeaders) {\n let headerRow = '';\n for (let i = 0; i < headersLen; i++) {\n if (i > 0) headerRow += delimiter;\n headerRow += escapeCsvValue(headers[i], delimiterRegex, doubleQuoteRegex);\n }\n csvRows.push(headerRow);\n }\n\n // 3. Iterate through Data and Create CSV Rows\n for (const row of jsonData) {\n if (typeof row !== 'object' || row === null || Array.isArray(row)) {\n // Skip non-object rows\n continue;\n }\n\n let rowStr = '';\n for (let i = 0; i < headersLen; i++) {\n if (i > 0) rowStr += delimiter;\n\n const header = headers[i];\n let cellValue = (row as DictionaryOpt<JsonValue>)[header];\n\n if (replacer && cellValue !== undefined) {\n cellValue = replacer(header, cellValue);\n }\n\n if (cellValue === null || cellValue === undefined) {\n // skip empty value\n }\n else if (typeof cellValue === 'object') {\n rowStr += escapeCsvValue(JSON.stringify(cellValue, replacer), delimiterRegex, doubleQuoteRegex);\n }\n else {\n rowStr += escapeCsvValue(String(cellValue), delimiterRegex, doubleQuoteRegex);\n }\n }\n csvRows.push(rowStr);\n }\n\n return csvRows.join('\\n');\n}\n\n// Optimized helper function\nfunction escapeCsvValue(value: string, delimiterRegex: RegExp, doubleQuoteRegex: RegExp): string {\n if (value === '') return value;\n\n // Only replace if necessary\n if (delimiterRegex.test(value)) {\n return `\"${value.replace(doubleQuoteRegex, '\"\"')}\"`;\n }\n\n return value;\n}\n"],
|
|
5
|
-
"mappings": "
|
|
5
|
+
"mappings": ";AAkBO,SAAS,UACd,SACA,UAAY,IACZ,eAAiB,KACjB,SACQ,CACR,GAAI,CAAC,MAAM,QAAQ,QAAQ,GAAK,SAAS,SAAW,GAAK,OAAO,SAAS,CAAC,IAAM,UAAY,SAAS,CAAC,IAAM,KAAM,CAChH,MAAO,EACT,CAEA,MAAM,eAAiB,IAAI,OAAO,IAAI,SAAS,OAAO,EACtD,MAAM,iBAAmB,KAGzB,MAAM,QAAU,OAAO,KAAK,SAAS,CAAC,CAAC,EACvC,MAAM,WAAa,QAAQ,OAG3B,MAAM,QAAoB,CAAC,EAE3B,GAAI,eAAgB,CAClB,IAAI,UAAY,GAChB,QAAS,EAAI,EAAG,EAAI,WAAY,IAAK,CACnC,GAAI,EAAI,EAAG,WAAa,UACxB,WAAa,eAAe,QAAQ,CAAC,EAAG,eAAgB,gBAAgB,CAC1E,CACA,QAAQ,KAAK,SAAS,CACxB,CAGA,UAAW,OAAO,SAAU,CAC1B,GAAI,OAAO,MAAQ,UAAY,MAAQ,MAAQ,MAAM,QAAQ,GAAG,EAAG,CAEjE,QACF,CAEA,IAAI,OAAS,GACb,QAAS,EAAI,EAAG,EAAI,WAAY,IAAK,CACnC,GAAI,EAAI,EAAG,QAAU,UAErB,MAAM,OAAS,QAAQ,CAAC,EACxB,IAAI,UAAa,IAAiC,MAAM,EAExD,GAAI,UAAY,YAAc,OAAW,CACvC,UAAY,SAAS,OAAQ,SAAS,CACxC,CAEA,GAAI,YAAc,MAAQ,YAAc,OAAW,CAEnD,SACS,OAAO,YAAc,SAAU,CACtC,QAAU,eAAe,KAAK,UAAU,UAAW,QAAQ,EAAG,eAAgB,gBAAgB,CAChG,KACK,CACH,QAAU,eAAe,OAAO,SAAS,EAAG,eAAgB,gBAAgB,CAC9E,CACF,CACA,QAAQ,KAAK,MAAM,CACrB,CAEA,OAAO,QAAQ,KAAK,IAAI,CAC1B,CAGA,SAAS,eAAe,MAAe,eAAwB,iBAAkC,CAC/F,GAAI,QAAU,GAAI,OAAO,MAGzB,GAAI,eAAe,KAAK,KAAK,EAAG,CAC9B,MAAO,IAAI,MAAM,QAAQ,iBAAkB,IAAI,CAAC,GAClD,CAEA,OAAO,KACT",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/json2csv",
|
|
3
3
|
"description": "A tiny, efficient, and cross-platform JSON to CSV converter.",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.5",
|
|
5
5
|
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
|
|
6
6
|
"bugs": "https://github.com/Alwatr/nanolib/issues",
|
|
7
7
|
"devDependencies": {
|
|
8
|
-
"@alwatr/nano-build": "6.4.
|
|
8
|
+
"@alwatr/nano-build": "6.4.2",
|
|
9
9
|
"@alwatr/prettier-config": "6.0.2",
|
|
10
10
|
"@alwatr/tsconfig-base": "6.0.4",
|
|
11
|
-
"@alwatr/type-helper": "7.0.
|
|
11
|
+
"@alwatr/type-helper": "7.0.2",
|
|
12
12
|
"typescript": "^5.9.3"
|
|
13
13
|
},
|
|
14
14
|
"exports": {
|
|
@@ -56,21 +56,21 @@
|
|
|
56
56
|
"directory": "packages/json2csv"
|
|
57
57
|
},
|
|
58
58
|
"scripts": {
|
|
59
|
-
"b": "
|
|
60
|
-
"build": "
|
|
59
|
+
"b": "bun run build",
|
|
60
|
+
"build": "bun run build:ts && bun run build:es",
|
|
61
61
|
"build:es": "nano-build --preset=module",
|
|
62
62
|
"build:ts": "tsc --build",
|
|
63
|
-
"c": "
|
|
64
|
-
"cb": "
|
|
63
|
+
"c": "bun run clean",
|
|
64
|
+
"cb": "bun run clean && bun run build",
|
|
65
65
|
"clean": "rm -rfv dist *.tsbuildinfo",
|
|
66
|
-
"d": "
|
|
67
|
-
"w": "
|
|
68
|
-
"watch": "
|
|
69
|
-
"watch:es": "
|
|
70
|
-
"watch:ts": "
|
|
66
|
+
"d": "bun run build:es && bun --enable-source-maps --trace-warnings",
|
|
67
|
+
"w": "bun run watch",
|
|
68
|
+
"watch": "bun run watch:ts & bun run watch:es",
|
|
69
|
+
"watch:es": "bun run build:es --watch",
|
|
70
|
+
"watch:ts": "bun run build:ts --watch --preserveWatchOutput"
|
|
71
71
|
},
|
|
72
72
|
"sideEffects": false,
|
|
73
73
|
"type": "module",
|
|
74
74
|
"types": "./dist/main.d.ts",
|
|
75
|
-
"gitHead": "
|
|
75
|
+
"gitHead": "c3889e3756b0a0f9b935a1b702a1373ac52cb379"
|
|
76
76
|
}
|