Socket
Socket
Sign inDemoInstall

export-to-csv

Package Overview
Dependencies
0
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.0 to 1.2.0

2

output/index.js

@@ -1,1 +0,1 @@

var O={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:""},_="\r\n",G="\uFEFF",S=(x)=>Object.assign({},O,x);class W extends Error{constructor(x){super(x);this.name="CsvGenerationError"}}class X extends Error{constructor(x){super(x);this.name="EmptyHeadersError"}}class Y extends Error{constructor(x){super(x);this.name="CsvDownloadEnvironmentError"}}var U=(x)=>x,A=(x)=>x,N=U,Q=U;var H=(x,...$)=>$.reduce((j,q)=>q(j),x),M=(x)=>($)=>x.useBom?N(A($)+G):$,T=(x)=>($)=>x.showTitle?N(A($)+x.title):$,V=(x)=>($)=>N(A(x)+A($)+_),F=(x)=>($,j)=>b(x)(Q($+j)),b=(x)=>($)=>U(A($)+x.fieldSeparator),L=(x,$)=>(j)=>{if(!x.showColumnHeaders)return j;if($.length<1)throw new X("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 q=Q("");for(let z=0;z<$.length;z++)q=F(x)(q,P(x,$[z]));return q=Q(A(q).slice(0,-1)),V(j)(q)},R=(x,$,j)=>(q)=>{let z=q;for(var K=0;K<j.length;K++){let J=Q("");for(let I=0;I<$.length;I++){const Z=$[I],E=typeof j[K][Z]==="undefined"?x.replaceUndefinedWith:j[K][Z];J=F(x)(J,P(x,E))}J=Q(A(J).slice(0,-1)),z=V(z)(J)}return z},m=A,B=(x)=>+x===x&&(!isFinite(x)||Boolean(x%1)),P=(x,$)=>{if(x.decimalSeparator==="locale"&&B($))return $.toLocaleString();if(x.decimalSeparator!=="."&&B($))return $.toString().replace(".",x.decimalSeparator);if(typeof $==="string"){let j=$;if(x.quoteStrings||x.fieldSeparator&&$.indexOf(x.fieldSeparator)>-1||$.indexOf("\n")>-1||$.indexOf("\r")>-1)j=x.quoteCharacter+$+x.quoteCharacter;return j}if(typeof $==="boolean"&&x.boolDisplay){const j=$?"true":"false";return x.boolDisplay[j]}return $};var o=(x)=>($)=>{const j=S(x),q=j.useKeysAsHeaders?Object.keys($[0]):j.columnHeaders;let z=H(N(""),M(j),T(j),L(j,q),R(j,q,$));if(A(z).length<1)throw new W("Output is empty. Is your data formatted correctly?");return z},t=(x)=>($)=>{if(!window)throw new Y("Downloading only supported in a browser environment.");const j=S(x),q=A($),z=j.useTextFile?"plain":"csv",K=j.useTextFile?"txt":"csv";let J=new Blob([q],{type:`text/${z};charset=utf8;`}),I=document.createElement("a");I.download=`${j.filename}.${K}`,I.href=URL.createObjectURL(J),I.setAttribute("visibility","hidden"),document.body.appendChild(I),I.click(),document.body.removeChild(I)};export{S as mkConfig,o as generateCsv,t as download,m as asString};
var v={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:""},M="\r\n",T="\uFEFF",X=(x)=>Object.assign({},v,x);class Y extends Error{constructor(x){super(x);this.name="CsvGenerationError"}}class Z extends Error{constructor(x){super(x);this.name="EmptyHeadersError"}}class _ extends Error{constructor(x){super(x);this.name="CsvDownloadEnvironmentError"}}var Q=(x)=>x,q=(x)=>x,U=Q,W=Q,G=Q,S=Q;var D=(x)=>typeof x==="object"?G(x.key):G(x),H=(x)=>typeof x==="object"?S(x.displayLabel):S(x),F=(x,...A)=>A.reduce(($,j)=>j($),x),K=(x)=>(A)=>x.useBom?U(q(A)+T):A,L=(x)=>(A)=>x.showTitle?U(q(A)+x.title):A,O=(x)=>(A)=>U(q(x)+q(A)+M),P=(x)=>(A,$)=>h(x)(W(A+$)),h=(x)=>(A)=>Q(q(A)+x.fieldSeparator),E=(x,A)=>($)=>{if(!x.showColumnHeaders)return $;if(A.length<1)throw new Z("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=W("");for(let z=0;z<A.length;z++){const J=H(A[z]);j=P(x)(j,C(x,J))}return j=W(q(j).slice(0,-1)),O($)(j)},R=(x,A,$)=>(j)=>{let z=j;for(var J=0;J<$.length;J++){let N=W("");for(let I=0;I<A.length;I++){const B=D(A[I]),b=typeof $[J][q(B)]==="undefined"?x.replaceUndefinedWith:$[J][q(B)];N=P(x)(N,C(x,b))}N=W(q(N).slice(0,-1)),z=O(z)(N)}return z},y=q,V=(x)=>+x===x&&(!isFinite(x)||Boolean(x%1)),C=(x,A)=>{if(x.decimalSeparator==="locale"&&V(A))return A.toLocaleString();if(x.decimalSeparator!=="."&&V(A))return A.toString().replace(".",x.decimalSeparator);if(typeof A==="string"){let $=A;if(x.quoteStrings||x.fieldSeparator&&A.indexOf(x.fieldSeparator)>-1||A.indexOf("\n")>-1||A.indexOf("\r")>-1)$=x.quoteCharacter+A+x.quoteCharacter;return $}if(typeof A==="boolean"&&x.boolDisplay){const $=A?"true":"false";return x.boolDisplay[$]}return A};var qx=(x)=>(A)=>{const $=X(x),j=$.useKeysAsHeaders?Object.keys(A[0]):$.columnHeaders;let z=F(U(""),K($),L($),E($,j),R($,j,A));if(q(z).length<1)throw new Y("Output is empty. Is your data formatted correctly?");return z},zx=(x)=>(A)=>{if(!window)throw new _("Downloading only supported in a browser environment.");const $=X(x),j=q(A),z=$.useTextFile?"plain":"csv",J=$.useTextFile?"txt":"csv";let N=new Blob([j],{type:`text/${z};charset=utf8;`}),I=document.createElement("a");I.download=`${$.filename}.${J}`,I.href=URL.createObjectURL(N),I.setAttribute("visibility","hidden"),document.body.appendChild(I),I.click(),document.body.removeChild(I)};export{X as mkConfig,qx as generateCsv,zx as download,y as asString};

@@ -1,2 +0,2 @@

import { ConfigOptions, CsvOutput, CsvRow, Newtype, WithDefaults } from "./types";
import { ColumnHeader, ConfigOptions, CsvOutput, CsvRow, Newtype, WithDefaults } from "./types";
export declare const thread: <T>(initialValue: T, ...fns: Array<Function>) => T;

@@ -8,6 +8,6 @@ export declare const addBOM: (config: WithDefaults<ConfigOptions>) => (output: CsvOutput) => CsvOutput;

export declare const addFieldSeparator: (config: WithDefaults<ConfigOptions>) => <T extends Newtype<any, string>>(output: T) => T;
export declare const addHeaders: (config: WithDefaults<ConfigOptions>, headers: Array<string>) => (output: CsvOutput) => CsvOutput;
export declare const addHeaders: (config: WithDefaults<ConfigOptions>, headers: Array<ColumnHeader>) => (output: CsvOutput) => CsvOutput;
export declare const addBody: <T extends {
[k: string]: unknown;
}[]>(config: WithDefaults<ConfigOptions>, headers: Array<string>, bodyData: T) => (output: CsvOutput) => CsvOutput;
}[]>(config: WithDefaults<ConfigOptions>, headers: Array<ColumnHeader>, bodyData: T) => (output: CsvOutput) => CsvOutput;
/**

@@ -14,0 +14,0 @@ *

@@ -6,2 +6,6 @@ export type Newtype<URI, A> = {

export type WithDefaults<T> = Required<T>;
export type ColumnHeader = string | {
key: string;
displayLabel: string;
};
export type ConfigOptions = {

@@ -18,3 +22,3 @@ filename?: string;

useBom?: boolean;
columnHeaders?: Array<string>;
columnHeaders?: Array<ColumnHeader>;
useKeysAsHeaders?: boolean;

@@ -27,6 +31,11 @@ boolDisplay?: {

};
export interface CsvOutput extends Newtype<{
export type HeaderKey = Newtype<{
readonly HeaderKey: unique symbol;
}, string>;
export type HeaderDisplayLabel = Newtype<{
readonly HeaderDisplayLabel: unique symbol;
}, string>;
export type CsvOutput = Newtype<{
readonly CsvOutput: unique symbol;
}, string> {
}
}, string>;
export type CsvRow = Newtype<{

@@ -40,1 +49,3 @@ readonly CsvRow: unique symbol;

export declare const mkCsvRow: (value: string) => CsvRow;
export declare const mkHeaderKey: (value: string) => HeaderKey;
export declare const mkHeaderDisplayLabel: (value: string) => HeaderDisplayLabel;
{
"name": "export-to-csv",
"version": "1.1.0",
"version": "1.2.0",
"description": "Easily create CSV data from json collection",

@@ -5,0 +5,0 @@ "type": "module",

@@ -99,17 +99,17 @@ # export-to-csv | Export to CSV Mini Library

| Option | Default | Type | Description |
| ------------------- | -------------------------------- | ------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `fieldSeparator` | `","` | `string` | Defines the field separator character |
| `filename` | `"generated"` | `string` | Sets the name of the file creates from the `download` function |
| `quoteStrings` | `false` | `boolean` | Determines whether or not to quote strings (using `quoteCharacter`'s value). Whether or not this is set, `\r`, `\n`, and `fieldSeparator` will be quoted. |
| `quoteCharacter` | `'"'` | `string` | Sets the quote character to use. |
| `decimalSeparator` | `"."` | `string` | Defines the decimal separator character (default is .). If set to "locale", it uses the [language sensitive representation of the number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString). |
| `showTitle` | `false` | `boolean` | Sets whether or not to add the value of `title` to the start of the CSV. (This is not supported by all CSV readers) |
| `title` | `"My Generated Report"` | `string` | The title to display as the first line of the CSV file. (This **is not** the name of the file [see `filename`]) |
| `showColumnHeaders` | `true` | `boolean` | Determines if columns should have headers. When set to `false`, the first row of the CSV will be data. |
| `columnHeaders` | `[]` | `Array<string>` | **Use this option if column/header order is important!** Determines the headers to use as the first line of the CSV data. Items in the array _must match key names of objects in your input collection._ |
| `useKeysAsHeaders` | `false` | `boolean` | If set, the CSV will use the key names in your collection as headers. **Warning: `headers` recommended for large collections. If set, it'll override the `headers` option. Column/header order also not guaranteed. Use `headers` only if order is important!** |
| `boolDisplay` | `{true: "TRUE", false: "FALSE"}` | `{true: string, false: string}` | Determines how to display boolean values in the CSV. **This only works for `true` and `false`. `1` and `0` will not be coerced and will display as `1` and `0`.** |
| `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 is 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. |
| Option | Default | Type | Description |
| ------------------- | -------------------------------- | ------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `fieldSeparator` | `","` | `string` | Defines the field separator character |
| `filename` | `"generated"` | `string` | Sets the name of the file creates from the `download` function |
| `quoteStrings` | `false` | `boolean` | Determines whether or not to quote strings (using `quoteCharacter`'s value). Whether or not this is set, `\r`, `\n`, and `fieldSeparator` will be quoted. |
| `quoteCharacter` | `'"'` | `string` | Sets the quote character to use. |
| `decimalSeparator` | `"."` | `string` | Defines the decimal separator character (default is .). If set to "locale", it uses the [language sensitive representation of the number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString). |
| `showTitle` | `false` | `boolean` | Sets whether or not to add the value of `title` to the start of the CSV. (This is not supported by all CSV readers) |
| `title` | `"My Generated Report"` | `string` | The title to display as the first line of the CSV file. (This **is not** the name of the file [see `filename`]) |
| `showColumnHeaders` | `true` | `boolean` | Determines if columns should have headers. When set to `false`, the first row of the CSV will be data. |
| `columnHeaders` | `[]` | `Array<string \| {key: string, displayLabel: string}>` | **Use this option if column/header order is important!** Determines the headers to use as the first line of the CSV data. If the item is a `string`, it will be used for lookup in your collection AND as the header label. If the item is an object, `key` will be used for lookup and `displayLabel` will be used as the header label. |
| `useKeysAsHeaders` | `false` | `boolean` | If set, the CSV will use the key names in your collection as headers. **Warning: `headers` recommended for large collections. If set, it'll override the `headers` option. Column/header order also not guaranteed. Use `headers` only if order is important!** |
| `boolDisplay` | `{true: "TRUE", false: "FALSE"}` | `{true: string, false: string}` | Determines how to display boolean values in the CSV. **This only works for `true` and `false`. `1` and `0` will not be coerced and will display as `1` and `0`.** |
| `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 is 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. |

@@ -116,0 +116,0 @@ # Alternatives

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc