diff --git a/README.md b/README.md index 0f14e69..c8f392e 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,9 @@ http.createServer(function (request, response){ case 'csv': response.setHeader('Content-Type', 'text/csv') break + case 'tsv': + response.setHeader('Content-Type', 'text/tsv') + break case 'xls': response.setHeader('Content-Type', 'application/vnd.ms-excel') break @@ -119,18 +122,18 @@ http.createServer(function (request, response){ **Note:** `JSON` refers to a parsable JSON string or a serializable JavaScript object. -| Option name | Required | Type | Description -| ----------- | -------- | ---- | ---- -| data | true | `Array`, `JSON` or `string` | If the exportType is 'json', data can be any parsable JSON. If the exportType is 'csv' or 'xls', data can only be an array of parsable JSON. If the exportType is 'txt', 'css', 'html', the data must be a string type. -| fileName | false | string | filename without extension, default to `'download'` -| extension | false | string | filename extension, by default it takes the exportType -| fileNameFormatter | false | `(name: string) => string` | filename formatter, by default the file name will be formatted to snake case -| fields | false | `string[]` or field name mapper type `Record` | fields filter, also supports mapper field name by passing an name mapper, e.g. { 'bar': 'baz' }, default to `undefined` -| exportType | false | Enum ExportType | 'txt'(default), 'css', 'html', 'json', 'csv', 'xls', 'xml' -| processor | false | `(content: string, type: ExportType, fileName: string) => any` | default to a front-end downloader -| withBOM | false | boolean | Add BOM(byte order mark) meta to CSV file. BOM is expected by `Excel` when reading UTF8 CSV file. It is default to `false`. -| beforeTableEncode | false | `(entries: { fieldName: string, fieldValues: string[] }[]) => { fieldName: string, fieldValues: string[] }[]` | Given a chance to altering table entries, only works for `CSV` and `XLS` file, by default no altering. -| delimiter | false | `',' \| ';'` | Specify CSV raw data's delimiter between values. It is default to `,` +| Option name | Required | Type | Description +| ----------- | -------- |---------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +| data | true | `Array`, `JSON` or `string` | If the exportType is 'json', data can be any parsable JSON. If the exportType is 'csv' or 'xls', data can only be an array of parsable JSON. If the exportType is 'txt', 'css', 'html', the data must be a string type. +| fileName | false | string | filename without extension, default to `'download'` +| extension | false | string | filename extension, by default it takes the exportType +| fileNameFormatter | false | `(name: string) => string` | filename formatter, by default the file name will be formatted to snake case +| fields | false | `string[]` or field name mapper type `Record` | fields filter, also supports mapper field name by passing an name mapper, e.g. { 'bar': 'baz' }, default to `undefined` +| exportType | false | Enum ExportType | 'txt'(default), 'css', 'html', 'json', 'csv', 'xls', 'xml', 'tsv' +| processor | false | `(content: string, type: ExportType, fileName: string) => any` | default to a front-end downloader +| withBOM | false | boolean | Add BOM(byte order mark) meta to CSV file. BOM is expected by `Excel` when reading UTF8 CSV file. It is default to `false`. +| beforeTableEncode | false | `(entries: { fieldName: string, fieldValues: string[] }[]) => { fieldName: string, fieldValues: string[] }[]` | Given a chance to altering table entries, only works for `CSV` and `XLS` file, by default no altering. +| delimiter | false | `',' \| ';' \| '\t'` | Specify CSV/TSV raw data's delimiter between values. It is default to `,` ### Tips diff --git a/src/converters.ts b/src/converters.ts index 78e82f2..75a1fab 100644 --- a/src/converters.ts +++ b/src/converters.ts @@ -110,8 +110,8 @@ export function _createTableEntries ( // (not all programs support values with line breaks). // Rule: All other fields do not require double quotes. // Rule: Double quotes within values are represented by two contiguous double quotes. -function encloser (value: string, delimiter: ',' | ';') { - const enclosingTester = new RegExp(`${delimiter}|"|\n`) +function encloser (value: string, delimiter: ',' | ';' | '\t') { + const enclosingTester = new RegExp(`${delimiter === "\t" ? "\\t" : delimiter}|"|\n`) const enclosingCharacter = enclosingTester.test(value) ? '"' : '' const escaped = value.replace(/"/g, '""') @@ -120,7 +120,7 @@ function encloser (value: string, delimiter: ',' | ';') { interface CreateCSVDataOptions { beforeTableEncode?: (entries: ITableEntries) => ITableEntries, - delimiter?: ',' | ';', + delimiter?: ',' | ';' | "\t", } const defaultCreateCSVDataOption: Required = { beforeTableEncode: i => i, delimiter: ',' } diff --git a/src/exportFromJSON.ts b/src/exportFromJSON.ts index b30db82..c9138c1 100644 --- a/src/exportFromJSON.ts +++ b/src/exportFromJSON.ts @@ -16,7 +16,7 @@ export interface IOption { beforeTableEncode?: ( tableRow: Array<{ fieldName: string, fieldValues: string[] }>, ) => Array<{ fieldName: string, fieldValues: string[]}> - delimiter?: ',' | ';' + delimiter?: ',' | ';' | '\t' } function exportFromJSON ({ @@ -31,7 +31,7 @@ function exportFromJSON ({ processor = downloadFile as never, withBOM = false, beforeTableEncode = (i) => i, - delimiter = ',', + delimiter, }: IOption): R { const MESSAGE_IS_ARRAY_FAIL = 'Invalid export data. Please provide an array of objects' const MESSAGE_UNKNOWN_EXPORT_TYPE = `Can't export unknown data type ${exportType}.` @@ -64,13 +64,19 @@ function exportFromJSON ({ case 'json': { return processor(JSONData, exportType, normalizeFileName(fileName, extension ?? 'json', fileNameFormatter)) } + case 'tsv': case 'csv': { assert(isArray(safeData), MESSAGE_IS_ARRAY_FAIL) const BOM = '\ufeff' + + if(delimiter === undefined) { + delimiter = exportType === 'tsv' ? '\t' : ','; + } + const CSVData = createCSVData(safeData, { beforeTableEncode, delimiter }) const content = withBOM ? BOM + CSVData : CSVData - return processor(content, exportType, normalizeFileName(fileName, extension ?? 'csv', fileNameFormatter)) + return processor(content, exportType, normalizeFileName(fileName, extension ?? exportType, fileNameFormatter)) } case 'xls': { assert(isArray(safeData), MESSAGE_IS_ARRAY_FAIL) diff --git a/src/processors.ts b/src/processors.ts index 02174e6..ad7e690 100644 --- a/src/processors.ts +++ b/src/processors.ts @@ -30,8 +30,9 @@ export function generateDataURI (content: string, type: ExportType, byBlob: bool return `data:,${blobType}` + encodeURIComponent(content) } + case "tsv": case 'csv': { - const blobType = 'text/csv;charset=utf-8' + const blobType = `text/${type};charset=utf-8` if (byBlob) return URL.createObjectURL(new Blob([content], { type: blobType })) diff --git a/src/types.ts b/src/types.ts index 42972a6..19b84fe 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -export type ExportType = 'txt' | 'json' | 'csv' | 'xls' | 'xml' | 'css' | 'html' +export type ExportType = 'txt' | 'json' | 'csv' | 'xls' | 'xml' | 'css' | 'html' | 'tsv' export const exportTypes: { [ET in ExportType]: ET } = { txt : 'txt', @@ -8,4 +8,5 @@ export const exportTypes: { [ET in ExportType]: ET } = { csv : 'csv', xls : 'xls', xml : 'xml', + tsv : 'tsv' }