improved toCSV
This commit is contained in:
parent
ce2f32dd45
commit
2f59a9d02e
@ -6,9 +6,9 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import {PathEvent} from './dist/index.mjs';
|
import {toCsv} from './dist/index.mjs';
|
||||||
|
|
||||||
console.log(PathEvent.has(['data/Inventory:n', 'logs:c'], `logs/client:n`));
|
console.log(toCsv([{test:123, test2: {test: [1, 2, 3, 4], test3: true, test4: 'okay'}}]));
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.22.2",
|
"version": "0.22.3",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
16
src/csv.ts
16
src/csv.ts
@ -1,4 +1,5 @@
|
|||||||
import {dotNotation, flattenObj} from './objects.ts';
|
import {ASet} from './aset.ts';
|
||||||
|
import {dotNotation, flattenObj, JSONAttemptParse, JSONSanitize} from './objects.ts';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert an object to a CSV string
|
* Convert an object to a CSV string
|
||||||
@ -7,20 +8,13 @@ import {dotNotation, flattenObj} from './objects.ts';
|
|||||||
* @param {boolean} flatten Should nested object be flattened or treated as values
|
* @param {boolean} flatten Should nested object be flattened or treated as values
|
||||||
* @return {string} CSV string
|
* @return {string} CSV string
|
||||||
*/
|
*/
|
||||||
export function csv(target: any[], flatten=true) {
|
export function toCsv(target: any[], flatten=true) {
|
||||||
const headers = target.reduce((acc, row) => {
|
const headers = new ASet(target.reduce((acc, row) => [...acc, ...Object.keys(flatten ? flattenObj(row) : row)], []));
|
||||||
Object.keys(flatten ? flattenObj(row) : row)
|
|
||||||
.forEach(key => { if(!acc.includes(key)) acc.push(key); });
|
|
||||||
return acc;
|
|
||||||
}, []);
|
|
||||||
return [
|
return [
|
||||||
headers.join(','),
|
headers.join(','),
|
||||||
...target.map(row => headers.map((h: string) => {
|
...target.map(row => headers.map((h: string) => {
|
||||||
const value = dotNotation<any>(row, h);
|
const value = dotNotation<any>(row, h);
|
||||||
const type = typeof value;
|
return (typeof value == 'object' && value != null) ? '"' + JSONSanitize(value).replaceAll('"', '""') + '"' : value;
|
||||||
if(type == 'string' && value.includes(',')) return `"${value}"`;
|
|
||||||
if(type == 'object') return `"${JSON.stringify(value)}"`;
|
|
||||||
return value;
|
|
||||||
}).join(','))
|
}).join(','))
|
||||||
].join('\n');
|
].join('\n');
|
||||||
}
|
}
|
||||||
|
@ -91,7 +91,6 @@ export function dotNotation<T>(obj: any, prop: string, set?: T): T | undefined {
|
|||||||
}, obj);
|
}, obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert object into URL encoded query string
|
* Convert object into URL encoded query string
|
||||||
*
|
*
|
||||||
@ -128,8 +127,9 @@ export function encodeQuery(data: any): string {
|
|||||||
export function flattenObj(obj: any, parent?: any, result: any = {}) {
|
export function flattenObj(obj: any, parent?: any, result: any = {}) {
|
||||||
if(typeof obj === "object" && !Array.isArray(obj)) {
|
if(typeof obj === "object" && !Array.isArray(obj)) {
|
||||||
for(const key of Object.keys(obj)) {
|
for(const key of Object.keys(obj)) {
|
||||||
const propName = parent ? parent + '.' + key : key;
|
const propName = parent ? `${parent}.${key}` : key;
|
||||||
if(typeof obj[key] === 'object') {
|
if(typeof obj[key] === 'object' && obj[key] != null && !Array.isArray(obj[key])) {
|
||||||
|
console.log(propName, );
|
||||||
flattenObj(obj[key], propName, result);
|
flattenObj(obj[key], propName, result);
|
||||||
} else {
|
} else {
|
||||||
result[propName] = obj[key];
|
result[propName] = obj[key];
|
||||||
@ -216,11 +216,11 @@ export function mixin(target: any, constructors: any[]) {
|
|||||||
/**
|
/**
|
||||||
* Parse JSON but return the original string if it fails
|
* Parse JSON but return the original string if it fails
|
||||||
*
|
*
|
||||||
* @param {string} json JSON string to parse
|
* @param {any} json JSON string to parse
|
||||||
* @return {string | T} Object if successful, original string otherwise
|
* @return {string | T} Object if successful, original string otherwise
|
||||||
*/
|
*/
|
||||||
export function JSONAttemptParse<T>(json: string): T | string {
|
export function JSONAttemptParse<T1, T2>(json: T2): T1 | T2 {
|
||||||
try { return JSON.parse(json); }
|
try { return JSON.parse(<any>json); }
|
||||||
catch { return json; }
|
catch { return json; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user