UNPKG

react-json-csv

Version:
1 lines 5.63 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?exports.ReactJsonCsv=t(require("react")):e.ReactJsonCsv=t(e.React)}(self,(e=>(()=>{var t={213:function(e,t,o){var n,r;void 0===(r="function"==typeof(n=function(){"use strict";function t(e,t,o){var n=new XMLHttpRequest;n.open("GET",e),n.responseType="blob",n.onload=function(){s(n.response,t,o)},n.onerror=function(){console.error("could not download file")},n.send()}function n(e){var t=new XMLHttpRequest;t.open("HEAD",e,!1);try{t.send()}catch(e){}return 200<=t.status&&299>=t.status}function r(e){try{e.dispatchEvent(new MouseEvent("click"))}catch(o){var t=document.createEvent("MouseEvents");t.initMouseEvent("click",!0,!0,window,0,0,0,80,20,!1,!1,!1,!1,0,null),e.dispatchEvent(t)}}var a="object"==typeof window&&window.window===window?window:"object"==typeof self&&self.self===self?self:"object"==typeof o.g&&o.g.global===o.g?o.g:void 0,s=a.saveAs||("object"!=typeof window||window!==a?function(){}:"download"in HTMLAnchorElement.prototype?function(e,o,s){var i=a.URL||a.webkitURL,c=document.createElement("a");o=o||e.name||"download",c.download=o,c.rel="noopener","string"==typeof e?(c.href=e,c.origin===location.origin?r(c):n(c.href)?t(e,o,s):r(c,c.target="_blank")):(c.href=i.createObjectURL(e),setTimeout((function(){i.revokeObjectURL(c.href)}),4e4),setTimeout((function(){r(c)}),0))}:"msSaveOrOpenBlob"in navigator?function(e,o,a){if(o=o||e.name||"download","string"!=typeof e)navigator.msSaveOrOpenBlob(function(e,t){return void 0===t?t={autoBom:!1}:"object"!=typeof t&&(console.warn("Deprecated: Expected third argument to be a object"),t={autoBom:!t}),t.autoBom&&/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(e.type)?new Blob(["\ufeff",e],{type:e.type}):e}(e,a),o);else if(n(e))t(e,o,a);else{var s=document.createElement("a");s.href=e,s.target="_blank",setTimeout((function(){r(s)}))}}:function(e,o,n,r){if((r=r||open("","_blank"))&&(r.document.title=r.document.body.innerText="downloading..."),"string"==typeof e)return t(e,o,n);var s="application/octet-stream"===e.type,i=/constructor/i.test(a.HTMLElement)||a.safari,c=/CriOS\/[\d]+/.test(navigator.userAgent);if((c||s&&i)&&"object"==typeof FileReader){var l=new FileReader;l.onloadend=function(){var e=l.result;e=c?e:e.replace(/^data:[^;]*;/,"data:attachment/file;"),r?r.location.href=e:location=e,r=null},l.readAsDataURL(e)}else{var f=a.URL||a.webkitURL,p=f.createObjectURL(e);r?r.location=p:location.href=p,r=null,setTimeout((function(){f.revokeObjectURL(p)}),4e4)}});a.saveAs=s.saveAs=s,e.exports=s})?n.apply(t,[]):n)||(e.exports=r)},442:t=>{"use strict";t.exports=e},556:(e,t,o)=>{e.exports=o(694)()},694:(e,t,o)=>{"use strict";var n=o(925);function r(){}function a(){}a.resetWarningCache=r,e.exports=function(){function e(e,t,o,r,a,s){if(s!==n){var i=new Error("Calling PropTypes validators directly is not supported by the `prop-types` package. Use PropTypes.checkPropTypes() to call them. Read more at http://fb.me/use-check-prop-types");throw i.name="Invariant Violation",i}}function t(){return e}e.isRequired=e;var o={array:e,bool:e,func:e,number:e,object:e,string:e,symbol:e,any:e,arrayOf:t,element:e,elementType:e,instanceOf:t,node:e,objectOf:t,oneOf:t,oneOfType:t,shape:t,exact:t,checkPropTypes:a,resetWarningCache:r};return o.PropTypes=o,o}},925:e=>{"use strict";e.exports="SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"}},o={};function n(e){var r=o[e];if(void 0!==r)return r.exports;var a=o[e]={exports:{}};return t[e].call(a.exports,a,a.exports,n),a.exports}n.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return n.d(t,{a:t}),t},n.d=(e,t)=>{for(var o in t)n.o(t,o)&&!n.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},n.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var r={};return(()=>{"use strict";n.r(r),n.d(r,{JsonToCsv:()=>p,useJsonToCsv:()=>u});var e=n(442),t=n.n(e),o=n(556),a=n.n(o),s=n(213);const i="json-to-csv",c=({data:e,fileformat:t,filename:o})=>{const n=new Blob([e],{type:"text/plain",charset:"utf-8"});(0,s.saveAs)(n,[o+"."+t])},l=({data:e,fields:t,headers:o,separator:n})=>{const r=(({data:e,fields:t,separator:o})=>e.map((e=>t.map((t=>e.hasOwnProperty(t)?e[t]:null)).join(o))).join("\n"))({data:e,fields:t,separator:n});return o.join(n)+"\n"+r},f=o=>{const[n,r]=(0,e.useState)([]),[a,s]=(0,e.useState)([]),[i,f]=(0,e.useState)([]);return(0,e.useEffect)((()=>{r(o.data),s(Object.keys(o.fields)),f(Object.keys(o.fields).map((e=>o.fields[e])))}),[]),t().createElement("button",{onClick:()=>{const{fileformat:e,filename:t,separator:r}=o;c({data:l({data:n,fields:a,headers:i,separator:r}),fileformat:e,filename:t})},style:o.style,"data-testid":"json-to-csv"},o.text)};f.propTypes={data:a().array.isRequired,fileformat:a().string,filename:a().string,fields:a().object,separator:a().string,style:a().object,text:a().string},f.defaultProps={fileformat:"csv",filename:i,separator:",",style:{},text:"Convert Json to Csv"};const p=f,u=()=>({saveAsCsv:({data:e,fields:t,fileformat:o="csv",filename:n=i,separator:r=","})=>{const a=Object.keys(t),s=Object.keys(t).map((e=>t[e]));c({data:l({data:e,fields:a,headers:s,separator:r}),fileformat:o,filename:n})}})})(),r})()));