export-to-csv
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -1,4 +0,4 @@ | ||
var y={fieldSeparator:",",decimalSeparator:".",quoteStrings:!0,quoteCharacter:'"',showTitle:!1,title:"My Generated Report",filename:"generated",showColumnHeaders:!0,useTextFile:!1,useBom:!0,columnHeaders:[],useKeysAsHeaders:!1,boolDisplay:{true:"TRUE",false:"FALSE"},replaceUndefinedWith:""},L="\r\n",F="\uFEFF",q=($)=>Object.assign({},y,$);class Q extends Error{constructor($){super($);this.name="CsvGenerationError"}}class T extends Error{constructor($){super($);this.name="EmptyHeadersError"}}class U extends Error{constructor($){super($);this.name="CsvDownloadEnvironmentError"}}class V extends Error{constructor($){super($);this.name="UnsupportedDataFormatError"}}var X=($)=>$,z=($)=>$,Z=X,_=X,Y=X,K=X,N=X;var r=function($,x){if(x=='"'&&$.indexOf('"')>-1)return $.replace(/"/g,'""');return $},D=($)=>typeof $==="object"?K($.key):K($),h=($)=>typeof $==="object"?N($.displayLabel):N($),P=($,...x)=>x.reduce((W,j)=>j(W),$),S=($)=>(x)=>$.useBom?_(z(x)+F):x,R=($)=>(x)=>$.showTitle?B(_(z(x)+$.title))(Y("")):x,B=($)=>(x)=>_(z($)+z(x)+L),E=($)=>(x,W)=>w($)(Y(z(x)+z(W))),w=($)=>(x)=>X(z(x)+$.fieldSeparator),O=($,x)=>(W)=>{if(!$.showColumnHeaders)return W;if(x.length<1)throw new T("Option to show headers but none supplied. Make sure there are keys in your collection or that you've supplied headers through the config options.");let j=Y("");for(let A=0;A<x.length;A++){const J=h(x[A]);j=E($)(j,b($,z(J)))}return j=Y(z(j).slice(0,-1)),B(W)(j)},C=($,x,W)=>(j)=>{let A=j;for(var J=0;J<W.length;J++){let I=Y("");for(let M=0;M<x.length;M++){const H=D(x[M]),v=W[J][z(H)];I=E($)(I,b($,v))}I=Y(z(I).slice(0,-1)),A=B(A)(I)}return A},l=z,k=($)=>+$===$&&(!isFinite($)||Boolean($%1)),m=($,x)=>{if(k(x)){if($.decimalSeparator==="locale")return Z(x.toLocaleString());if($.decimalSeparator)return Z(x.toString().replace(".",$.decimalSeparator))}return Z(x.toString())},G=($,x)=>{let W=x;if($.quoteStrings||$.fieldSeparator&&x.indexOf($.fieldSeparator)>-1||$.quoteCharacter&&x.indexOf($.quoteCharacter)>-1||x.indexOf("\n")>-1||x.indexOf("\r")>-1)W=$.quoteCharacter+r(x,$.quoteCharacter)+$.quoteCharacter;return Z(W)},p=($,x)=>{const W=x?"true":"false";return Z($.boolDisplay[W])},g=($,x)=>{if(typeof x==="undefined"&&$.replaceUndefinedWith!==void 0)return G($,$.replaceUndefinedWith+"");if(x===null)return G($,"null");return G($,"")},b=($,x)=>{if(typeof x==="number")return m($,x);if(typeof x==="string")return G($,x);if(typeof x==="boolean"&&$.boolDisplay)return p($,x);if(x===null||typeof x==="undefined")return g($,x);throw new V(` | ||
typeof ${typeof x} isn't supported. Only number, string, boolean, null and undefined are supported. | ||
var q;(function(x){x["csv"]="text/csv";x["tsv"]="text/tab-separated-values";x["plain"]="text/plain"})(q||(q={}));var X=(W)=>W,z=(W)=>W,Z=X,_=X,Y=X,N=X,V=X;var h={fieldSeparator:",",decimalSeparator:".",quoteStrings:!0,quoteCharacter:'"',showTitle:!1,title:"My Generated Report",filename:"generated",showColumnHeaders:!0,useTextFile:!1,fileExtension:"csv",mediaType:q.csv,useBom:!0,columnHeaders:[],useKeysAsHeaders:!1,boolDisplay:{true:"TRUE",false:"FALSE"},replaceUndefinedWith:""},F="\r\n",R="\uFEFF",G=(W)=>Object.assign({},h,W);class K extends Error{constructor(W){super(W);this.name="CsvGenerationError"}}class B extends Error{constructor(W){super(W);this.name="EmptyHeadersError"}}class L extends Error{constructor(W){super(W);this.name="CsvDownloadEnvironmentError"}}class P extends Error{constructor(W){super(W);this.name="UnsupportedDataFormatError"}}var s=function(W,$){if($=='"'&&W.indexOf('"')>-1)return W.replace(/"/g,'""');return W},w=(W)=>typeof W==="object"?N(W.key):N(W),y=(W)=>typeof W==="object"?V(W.displayLabel):V(W),T=(W,...$)=>$.reduce((j,x)=>x(j),W),E=(W)=>($)=>W.useBom?_(z($)+R):$,M=(W)=>($)=>W.showTitle?S(_(z($)+W.title))(Y("")):$,S=(W)=>($)=>_(z(W)+z($)+F),O=(W)=>($,j)=>l(W)(Y(z($)+z(j))),l=(W)=>($)=>X(z($)+W.fieldSeparator),b=(W,$)=>(j)=>{if(!W.showColumnHeaders)return j;if($.length<1)throw new B("Option to show headers but none supplied. Make sure there are keys in your collection or that you've supplied headers through the config options.");let x=Y("");for(let A=0;A<$.length;A++){const J=y($[A]);x=O(W)(x,v(W,z(J)))}return x=Y(z(x).slice(0,-1)),S(j)(x)},C=(W,$,j)=>(x)=>{let A=x;for(var J=0;J<j.length;J++){let I=Y("");for(let U=0;U<$.length;U++){const H=w($[U]),D=j[J][z(H)];I=O(W)(I,v(W,D))}I=Y(z(I).slice(0,-1)),A=S(A)(I)}return A},k=z,m=(W)=>+W===W&&(!isFinite(W)||Boolean(W%1)),p=(W,$)=>{if(m($)){if(W.decimalSeparator==="locale")return Z($.toLocaleString());if(W.decimalSeparator)return Z($.toString().replace(".",W.decimalSeparator))}return Z($.toString())},Q=(W,$)=>{let j=$;if(W.quoteStrings||W.fieldSeparator&&$.indexOf(W.fieldSeparator)>-1||W.quoteCharacter&&$.indexOf(W.quoteCharacter)>-1||$.indexOf("\n")>-1||$.indexOf("\r")>-1)j=W.quoteCharacter+s($,W.quoteCharacter)+W.quoteCharacter;return Z(j)},g=(W,$)=>{const j=$?"true":"false";return Z(W.boolDisplay[j])},r=(W,$)=>{if(typeof $==="undefined"&&W.replaceUndefinedWith!==void 0)return Q(W,W.replaceUndefinedWith+"");if($===null)return Q(W,"null");return Q(W,"")},v=(W,$)=>{if(typeof $==="number")return p(W,$);if(typeof $==="string")return Q(W,$);if(typeof $==="boolean"&&W.boolDisplay)return g(W,$);if($===null||typeof $==="undefined")return r(W,$);throw new P(` | ||
typeof ${typeof $} isn't supported. Only number, string, boolean, null and undefined are supported. | ||
Please convert the data in your object to one of those before generating the CSV. | ||
`)};var Q$=($)=>(x)=>{const W=q($),j=W.useKeysAsHeaders?Object.keys(x[0]):W.columnHeaders;let A=P(_(""),S(W),R(W),O(W,j),C(W,j,x));if(z(A).length<1)throw new Q("Output is empty. Is your data formatted correctly?");return A},s=($)=>(x)=>{const W=q($),j=z(x),A=W.useTextFile?"plain":"csv";return new Blob([j],{type:`text/${A};charset=utf8;`})},T$=($)=>(x)=>{if(!window)throw new U("Downloading only supported in a browser environment.");const W=s($)(x),j=q($),A=j.useTextFile?"txt":"csv",J=`${j.filename}.${A}`,I=document.createElement("a");I.download=J,I.href=URL.createObjectURL(W),I.setAttribute("visibility","hidden"),document.body.appendChild(I),I.click(),document.body.removeChild(I)};export{q as mkConfig,Q$ as generateCsv,T$ as download,l as asString,s as asBlob}; | ||
`)};var BW=(W)=>($)=>{const j=G(W),x=j.useKeysAsHeaders?Object.keys($[0]):j.columnHeaders;let A=T(_(""),E(j),M(j),b(j,x),C(j,x,$));if(z(A).length<1)throw new K("Output is empty. Is your data formatted correctly?");return A},f=(W)=>($)=>{const j=G(W),x=z($),A=j.useTextFile?"text/plain":j.mediaType;return new Blob([x],{type:`${A};charset=utf8;`})},LW=(W)=>($)=>{if(!window)throw new L("Downloading only supported in a browser environment.");const j=f(W)($),x=G(W),A=x.useTextFile?"txt":x.fileExtension,J=`${x.filename}.${A}`,I=document.createElement("a");I.download=J,I.href=URL.createObjectURL(j),I.setAttribute("visibility","hidden"),document.body.appendChild(I),I.click(),document.body.removeChild(I)};export{G as mkConfig,BW as generateCsv,LW as download,k as asString,f as asBlob,q as MediaType}; |
export * from "./generator.ts"; | ||
export { mkConfig } from "./config.ts"; | ||
export { CsvOutput, ConfigOptions, ColumnHeader } from "./types.ts"; | ||
export { CsvOutput, ConfigOptions, ColumnHeader, MediaType } from "./types.ts"; | ||
export { asString } from "./helpers.ts"; |
@@ -10,2 +10,7 @@ export type Newtype<URI, A> = { | ||
}; | ||
export declare enum MediaType { | ||
csv = "text/csv", | ||
tsv = "text/tab-separated-values", | ||
plain = "text/plain" | ||
} | ||
export type ConfigOptions = { | ||
@@ -20,3 +25,12 @@ filename?: string; | ||
title?: string; | ||
/** | ||
* Use `fileExtension` instead. | ||
* | ||
* Will be removed in the next major version (`2.x.x`) | ||
* | ||
* @deprecated | ||
*/ | ||
useTextFile?: boolean; | ||
fileExtension?: string; | ||
mediaType?: MediaType; | ||
useBom?: boolean; | ||
@@ -23,0 +37,0 @@ columnHeaders?: Array<ColumnHeader>; |
{ | ||
"name": "export-to-csv", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Easily create CSV data from json collection", | ||
@@ -12,2 +12,3 @@ "type": "module", | ||
"build": "rm -rf output && bun build index.ts --outdir ./output --minify && tsc", | ||
"watch": "rm -rf output && bun build index.ts --outdir ./output --minify --watch", | ||
"e2e": "rm -rf integration/export-to-csv.js && bun run build && cp output/index.js integration/export-to-csv.js && playwright test", | ||
@@ -18,3 +19,4 @@ "e2e-ci": "rm -rf integration/export-to-csv.js && bun run build && cp output/index.js integration/export-to-csv.js && bunx playwright install --with-deps && playwright test", | ||
"format": "bunx prettier --write '.'", | ||
"prepublishOnly": "bun run build" | ||
"prepublishOnly": "bun run build", | ||
"prepare": "bun run build" | ||
}, | ||
@@ -51,2 +53,2 @@ "keywords": [ | ||
} | ||
} | ||
} |
# export-to-csv | Export to CSV Mini Library | ||
Like this library and want to support active development? | ||
<a href='https://ko-fi.com/T6T8XO5J5' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://storage.ko-fi.com/cdn/kofi4.png?v=3' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a> | ||
Small, simple, and single purpose. Zero dependencies, functionally inspired, and fairly well-typed. | ||
@@ -202,3 +206,4 @@ | ||
| `useBom` | `true` | `boolean` | Adds a [byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark) which is required by Excel to display CSVs, despite it not being necessary with UTF-8 🤷♂️ | | ||
| `useTextFile` | `false` | `boolean` | Will download the file as `text/plain` instead of `text/csv` and use a `.txt` vs `.csv` file extension. | | ||
| `useTextFile` | `false` | `boolean` | _Deprecation warning. This will be removed in the next major version._ Will download the file as `text/plain` instead of `text/csv` and use a `.txt` vs `.csv` file extension. | | ||
| `fileExtension` | `csv` | `string` | File extension to use. Currently, this only applies if `useTextFile` is `false`. | | ||
@@ -205,0 +210,0 @@ # Alternatives |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27947
156
225
13