diff --git a/package.json b/package.json index 1395666..3c8c650 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.16.0", + "version": "0.16.1", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/csv.ts b/src/csv.ts new file mode 100644 index 0000000..81aec4d --- /dev/null +++ b/src/csv.ts @@ -0,0 +1,26 @@ +import {dotNotation, flattenObj} from './objects.ts'; + +/** + * Convert an object to a CSV string + * + * @param {any[]} target Array of objects to create CSV from + * @param {boolean} flatten Should nested object be flattened or treated as values + * @return {string} CSV string + */ +export function csv(target: any[], flatten=true) { + const headers = target.reduce((acc, row) => { + Object.keys(flatten ? flattenObj(row) : row) + .forEach(key => { if(!acc.includes(key)) acc.push(key); }); + return acc; + }, []); + return [ + headers.join(','), + ...target.map(row => headers.map((h: string) => { + const value = dotNotation(row, h); + const type = typeof value; + if(type == 'string' && value.includes(',')) return `"${value}"`; + if(type == 'object') return `"${JSON.stringify(value)}"`; + return value; + }).join(',')) + ].join('\n'); +} diff --git a/src/files.ts b/src/files.ts index 72f058f..af5adff 100644 --- a/src/files.ts +++ b/src/files.ts @@ -1,33 +1,35 @@ +import {makeArray} from './array.ts'; import {JSONAttemptParse} from './objects.ts'; import {PromiseProgress} from './promise-progress'; -/** - * Download a file from a URL - * - * @param href URL that will be downloaded - * @param {string} name Override download name - */ -export function download(href: any, name?: string) { - const a = document.createElement('a'); - a.href = href; - a.download = name || href.split('/').pop(); - document.body.appendChild(a); - a.click(); - document.body.removeChild(a); -} - /** * Download blob as a file * * @param {Blob} blob File as a blob * @param {string} name Name blob will be downloaded as */ -export function downloadBlob(blob: Blob, name: string) { +export function downloadFile(blob: Blob | string | string[], name: string) { + if(!(blob instanceof Blob)) blob = new Blob(makeArray(blob)); const url = URL.createObjectURL(blob); - download(url, name); + downloadUrl(url, name); URL.revokeObjectURL(url); } +/** + * Download a file from a URL + * + * @param href URL that will be downloaded + * @param {string} name Override download name + */ +export function downloadUrl(href: any, name?: string) { + const a = document.createElement('a'); + a.href = href; + a.download = name || href.split('/').pop(); + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); +} + /** * Open filebrowser & return selected file * diff --git a/src/index.ts b/src/index.ts index 2d10954..30d4124 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ export * from './array'; export * from './aset'; +export * from './csv'; export * from './files'; export * from './emitter'; export * from './errors'; diff --git a/src/string.ts b/src/string.ts index 9069ee0..e3a5ebb 100644 --- a/src/string.ts +++ b/src/string.ts @@ -1,3 +1,5 @@ +import {dotNotation, flattenObj} from './objects.ts'; + /** * String of all letters */ @@ -18,16 +20,6 @@ const SYMBOL_LIST = '~`!@#$%^&*()_-+={[}]|\\:;"\'<,>.?/'; */ const CHAR_LIST = LETTER_LIST + NUMBER_LIST + SYMBOL_LIST; -/** - * Generate a random hexadecimal value - * - * @param {number} length Number of hexadecimal place values - * @return {string} Hexadecimal number as a string - */ -export function randomHex(length: number) { - return Array(length).fill(null).map(() => Math.round(Math.random() * 0xF).toString(16)).join(''); -} - /** * Convert number of bytes into a human-readable size * @@ -95,6 +87,16 @@ export function pad(text: any, length: number, char: string = ' ', start = true) return text.toString().padEnd(length, char); } +/** + * Generate a random hexadecimal value + * + * @param {number} length Number of hexadecimal place values + * @return {string} Hexadecimal number as a string + */ +export function randomHex(length: number) { + return Array(length).fill(null).map(() => Math.round(Math.random() * 0xF).toString(16)).join(''); +} + /** * Generate a string of random characters. *