@alwatr/json2csv 9.1.1 → 9.4.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/dist/main.js +2 -2
- package/dist/main.js.map +1 -1
- package/package.json +4 -4
package/dist/main.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/* 📦 @alwatr/json2csv v9.
|
|
1
|
+
/* 📦 @alwatr/json2csv v9.4.0 */
|
|
2
2
|
function Y(q,F=",",J=!0,K){if(!Array.isArray(q)||q.length===0||typeof q[0]!=="object"||q[0]===null)return"";let M=new RegExp(`[${F}\\n"]`),N=/"/g,P=Object.keys(q[0]),W=P.length,T=[];if(J){let B="";for(let A=0;A<W;A++){if(A>0)B+=F;B+=U(P[A],M,N)}T.push(B)}for(let B of q){if(typeof B!=="object"||B===null||Array.isArray(B))continue;let A="";for(let G=0;G<W;G++){if(G>0)A+=F;let X=P[G],E=B[X];if(K&&E!==void 0)E=K(X,E);if(E===null||E===void 0);else if(typeof E==="object")A+=U(JSON.stringify(E,K),M,N);else A+=U(String(E),M,N)}T.push(A)}return T.join(`
|
|
3
3
|
`)}function U(q,F,J){if(q==="")return q;if(F.test(q))return`"${q.replace(J,'""')}"`;return q}export{Y as jsonToCsv};
|
|
4
4
|
|
|
5
|
-
//# debugId=
|
|
5
|
+
//# debugId=17992D73E1B5DE0B64756E2164756E21
|
|
6
6
|
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
CHANGED
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
"\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"
|
|
6
6
|
],
|
|
7
7
|
"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"debugId": "17992D73E1B5DE0B64756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/json2csv",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.4.0",
|
|
4
4
|
"description": "A tiny, efficient, and cross-platform JSON to CSV converter.",
|
|
5
5
|
"license": "MPL-2.0",
|
|
6
6
|
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com> (https://ali.mihandoost.com)",
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
},
|
|
22
22
|
"sideEffects": false,
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@alwatr/nano-build": "9.
|
|
25
|
-
"@alwatr/
|
|
24
|
+
"@alwatr/nano-build": "9.3.0",
|
|
25
|
+
"@alwatr/standard": "9.4.0",
|
|
26
26
|
"@alwatr/type-helper": "9.1.1",
|
|
27
27
|
"typescript": "^6.0.2"
|
|
28
28
|
},
|
|
@@ -70,5 +70,5 @@
|
|
|
70
70
|
"utility",
|
|
71
71
|
"zero-dependency"
|
|
72
72
|
],
|
|
73
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "662f1047fc2a1b53a00902028d6cebfd04b52dd5"
|
|
74
74
|
}
|