test-cases.js 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827
  1. var chai;
  2. var Papa;
  3. if (typeof module !== 'undefined' && module.exports) {
  4. chai = require('chai');
  5. Papa = require('../papaparse.js');
  6. }
  7. var assert = chai.assert;
  8. var BASE_PATH = (typeof document === 'undefined') ? './' : document.getElementById('test-cases').src.replace(/test-cases\.js$/, '');
  9. var RECORD_SEP = String.fromCharCode(30);
  10. var UNIT_SEP = String.fromCharCode(31);
  11. var FILES_ENABLED = false;
  12. try {
  13. new File([""], ""); // eslint-disable-line no-new
  14. FILES_ENABLED = true;
  15. } catch (e) {} // safari, ie
  16. var XHR_ENABLED = false;
  17. try {
  18. new XMLHttpRequest(); // eslint-disable-line no-new
  19. XHR_ENABLED = true;
  20. } catch (e) {} // safari, ie
  21. // Tests for the core parser using new Papa.Parser().parse() (CSV to JSON)
  22. var CORE_PARSER_TESTS = [
  23. {
  24. description: "One row",
  25. input: 'A,b,c',
  26. expected: {
  27. data: [['A', 'b', 'c']],
  28. errors: [],
  29. meta: {delimiter: ',', renamedHeaders: null}
  30. }
  31. },
  32. {
  33. description: "Two rows",
  34. input: 'A,b,c\nd,E,f',
  35. expected: {
  36. data: [['A', 'b', 'c'], ['d', 'E', 'f']],
  37. errors: []
  38. }
  39. },
  40. {
  41. description: "Three rows",
  42. input: 'A,b,c\nd,E,f\nG,h,i',
  43. expected: {
  44. data: [['A', 'b', 'c'], ['d', 'E', 'f'], ['G', 'h', 'i']],
  45. errors: []
  46. }
  47. },
  48. {
  49. description: "Whitespace at edges of unquoted field",
  50. input: 'a, b ,c',
  51. notes: "Extra whitespace should graciously be preserved",
  52. expected: {
  53. data: [['a', ' b ', 'c']],
  54. errors: []
  55. }
  56. },
  57. {
  58. description: "Quoted field",
  59. input: 'A,"B",C',
  60. expected: {
  61. data: [['A', 'B', 'C']],
  62. errors: []
  63. }
  64. },
  65. {
  66. description: "Quoted field with extra whitespace on edges",
  67. input: 'A," B ",C',
  68. expected: {
  69. data: [['A', ' B ', 'C']],
  70. errors: []
  71. }
  72. },
  73. {
  74. description: "Quoted field with delimiter",
  75. input: 'A,"B,B",C',
  76. expected: {
  77. data: [['A', 'B,B', 'C']],
  78. errors: []
  79. }
  80. },
  81. {
  82. description: "Quoted field with line break",
  83. input: 'A,"B\nB",C',
  84. expected: {
  85. data: [['A', 'B\nB', 'C']],
  86. errors: []
  87. }
  88. },
  89. {
  90. description: "Quoted fields with line breaks",
  91. input: 'A,"B\nB","C\nC\nC"',
  92. expected: {
  93. data: [['A', 'B\nB', 'C\nC\nC']],
  94. errors: []
  95. }
  96. },
  97. {
  98. description: "Quoted fields at end of row with delimiter and line break",
  99. input: 'a,b,"c,c\nc"\nd,e,f',
  100. expected: {
  101. data: [['a', 'b', 'c,c\nc'], ['d', 'e', 'f']],
  102. errors: []
  103. }
  104. },
  105. {
  106. description: "Quoted field with escaped quotes",
  107. input: 'A,"B""B""B",C',
  108. expected: {
  109. data: [['A', 'B"B"B', 'C']],
  110. errors: []
  111. }
  112. },
  113. {
  114. description: "Quoted field with escaped quotes at boundaries",
  115. input: 'A,"""B""",C',
  116. expected: {
  117. data: [['A', '"B"', 'C']],
  118. errors: []
  119. }
  120. },
  121. {
  122. description: "Unquoted field with quotes at end of field",
  123. notes: "The quotes character is misplaced, but shouldn't generate an error or break the parser",
  124. input: 'A,B",C',
  125. expected: {
  126. data: [['A', 'B"', 'C']],
  127. errors: []
  128. }
  129. },
  130. {
  131. description: "Quoted field with quotes around delimiter",
  132. input: 'A,""",""",C',
  133. notes: "For a boundary to exist immediately before the quotes, we must not already be in quotes",
  134. expected: {
  135. data: [['A', '","', 'C']],
  136. errors: []
  137. }
  138. },
  139. {
  140. description: "Quoted field with quotes on right side of delimiter",
  141. input: 'A,",""",C',
  142. notes: "Similar to the test above but with quotes only after the comma",
  143. expected: {
  144. data: [['A', ',"', 'C']],
  145. errors: []
  146. }
  147. },
  148. {
  149. description: "Quoted field with quotes on left side of delimiter",
  150. input: 'A,""",",C',
  151. notes: "Similar to the test above but with quotes only before the comma",
  152. expected: {
  153. data: [['A', '",', 'C']],
  154. errors: []
  155. }
  156. },
  157. {
  158. description: "Quoted field with 5 quotes in a row and a delimiter in there, too",
  159. input: '"1","cnonce="""",nc=""""","2"',
  160. notes: "Actual input reported in issue #121",
  161. expected: {
  162. data: [['1', 'cnonce="",nc=""', '2']],
  163. errors: []
  164. }
  165. },
  166. {
  167. description: "Quoted field with whitespace around quotes",
  168. input: 'A, "B" ,C',
  169. notes: "The quotes must be immediately adjacent to the delimiter to indicate a quoted field",
  170. expected: {
  171. data: [['A', ' "B" ', 'C']],
  172. errors: []
  173. }
  174. },
  175. {
  176. description: "Misplaced quotes in data, not as opening quotes",
  177. input: 'A,B "B",C',
  178. notes: "The input is technically malformed, but this syntax should not cause an error",
  179. expected: {
  180. data: [['A', 'B "B"', 'C']],
  181. errors: []
  182. }
  183. },
  184. {
  185. description: "Quoted field has no closing quote",
  186. input: 'a,"b,c\nd,e,f',
  187. expected: {
  188. data: [['a', 'b,c\nd,e,f']],
  189. errors: [{
  190. "type": "Quotes",
  191. "code": "MissingQuotes",
  192. "message": "Quoted field unterminated",
  193. "row": 0,
  194. "index": 3
  195. }]
  196. }
  197. },
  198. {
  199. description: "Quoted field has invalid trailing quote after delimiter with a valid closer",
  200. input: '"a,"b,c"\nd,e,f',
  201. notes: "The input is malformed, opening quotes identified, trailing quote is malformed. Trailing quote should be escaped or followed by valid new line or delimiter to be valid",
  202. expected: {
  203. data: [['a,"b,c'], ['d', 'e', 'f']],
  204. errors: [{
  205. "type": "Quotes",
  206. "code": "InvalidQuotes",
  207. "message": "Trailing quote on quoted field is malformed",
  208. "row": 0,
  209. "index": 1
  210. }]
  211. }
  212. },
  213. {
  214. description: "Quoted field has invalid trailing quote after delimiter",
  215. input: 'a,"b,"c\nd,e,f',
  216. notes: "The input is malformed, opening quotes identified, trailing quote is malformed. Trailing quote should be escaped or followed by valid new line or delimiter to be valid",
  217. expected: {
  218. data: [['a', 'b,"c\nd,e,f']],
  219. errors: [{
  220. "type": "Quotes",
  221. "code": "InvalidQuotes",
  222. "message": "Trailing quote on quoted field is malformed",
  223. "row": 0,
  224. "index": 3
  225. },
  226. {
  227. "type": "Quotes",
  228. "code": "MissingQuotes",
  229. "message": "Quoted field unterminated",
  230. "row": 0,
  231. "index": 3
  232. }]
  233. }
  234. },
  235. {
  236. description: "Quoted field has invalid trailing quote before delimiter",
  237. input: 'a,"b"c,d\ne,f,g',
  238. notes: "The input is malformed, opening quotes identified, trailing quote is malformed. Trailing quote should be escaped or followed by valid new line or delimiter to be valid",
  239. expected: {
  240. data: [['a', 'b"c,d\ne,f,g']],
  241. errors: [{
  242. "type": "Quotes",
  243. "code": "InvalidQuotes",
  244. "message": "Trailing quote on quoted field is malformed",
  245. "row": 0,
  246. "index": 3
  247. },
  248. {
  249. "type": "Quotes",
  250. "code": "MissingQuotes",
  251. "message": "Quoted field unterminated",
  252. "row": 0,
  253. "index": 3
  254. }]
  255. }
  256. },
  257. {
  258. description: "Quoted field has invalid trailing quote after new line",
  259. input: 'a,"b,c\nd"e,f,g',
  260. notes: "The input is malformed, opening quotes identified, trailing quote is malformed. Trailing quote should be escaped or followed by valid new line or delimiter to be valid",
  261. expected: {
  262. data: [['a', 'b,c\nd"e,f,g']],
  263. errors: [{
  264. "type": "Quotes",
  265. "code": "InvalidQuotes",
  266. "message": "Trailing quote on quoted field is malformed",
  267. "row": 0,
  268. "index": 3
  269. },
  270. {
  271. "type": "Quotes",
  272. "code": "MissingQuotes",
  273. "message": "Quoted field unterminated",
  274. "row": 0,
  275. "index": 3
  276. }]
  277. }
  278. },
  279. {
  280. description: "Quoted field has valid trailing quote via delimiter",
  281. input: 'a,"b",c\nd,e,f',
  282. notes: "Trailing quote is valid due to trailing delimiter",
  283. expected: {
  284. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  285. errors: []
  286. }
  287. },
  288. {
  289. description: "Quoted field has valid trailing quote via \\n",
  290. input: 'a,b,"c"\nd,e,f',
  291. notes: "Trailing quote is valid due to trailing new line delimiter",
  292. expected: {
  293. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  294. errors: []
  295. }
  296. },
  297. {
  298. description: "Quoted field has valid trailing quote via EOF",
  299. input: 'a,b,c\nd,e,"f"',
  300. notes: "Trailing quote is valid due to EOF",
  301. expected: {
  302. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  303. errors: []
  304. }
  305. },
  306. {
  307. description: "Quoted field contains delimiters and \\n with valid trailing quote",
  308. input: 'a,"b,c\nd,e,f"',
  309. notes: "Trailing quote is valid due to trailing delimiter",
  310. expected: {
  311. data: [['a', 'b,c\nd,e,f']],
  312. errors: []
  313. }
  314. },
  315. {
  316. description: "Line starts with quoted field",
  317. input: 'a,b,c\n"d",e,f',
  318. expected: {
  319. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  320. errors: []
  321. }
  322. },
  323. {
  324. description: "Line starts with unquoted empty field",
  325. input: ',b,c\n"d",e,f',
  326. expected: {
  327. data: [['', 'b', 'c'], ['d', 'e', 'f']],
  328. errors: []
  329. }
  330. },
  331. {
  332. description: "Line ends with quoted field",
  333. input: 'a,b,c\nd,e,f\n"g","h","i"\n"j","k","l"',
  334. expected: {
  335. data: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'], ['j', 'k', 'l']],
  336. errors: []
  337. }
  338. },
  339. {
  340. description: "Line ends with quoted field, first field of next line is empty, \\n",
  341. input: 'a,b,c\n,e,f\n,"h","i"\n,"k","l"',
  342. config: {
  343. newline: '\n',
  344. },
  345. expected: {
  346. data: [['a', 'b', 'c'], ['', 'e', 'f'], ['', 'h', 'i'], ['', 'k', 'l']],
  347. errors: []
  348. }
  349. },
  350. {
  351. description: "Quoted field at end of row (but not at EOF) has quotes",
  352. input: 'a,b,"c""c"""\nd,e,f',
  353. expected: {
  354. data: [['a', 'b', 'c"c"'], ['d', 'e', 'f']],
  355. errors: []
  356. }
  357. },
  358. {
  359. description: "Empty quoted field at EOF is empty",
  360. input: 'a,b,""\na,b,""',
  361. expected: {
  362. data: [['a', 'b', ''], ['a', 'b', '']],
  363. errors: []
  364. }
  365. },
  366. {
  367. description: "Multiple consecutive empty fields",
  368. input: 'a,b,,,c,d\n,,e,,,f',
  369. expected: {
  370. data: [['a', 'b', '', '', 'c', 'd'], ['', '', 'e', '', '', 'f']],
  371. errors: []
  372. }
  373. },
  374. {
  375. description: "Empty input string",
  376. input: '',
  377. expected: {
  378. data: [],
  379. errors: []
  380. }
  381. },
  382. {
  383. description: "Input is just the delimiter (2 empty fields)",
  384. input: ',',
  385. expected: {
  386. data: [['', '']],
  387. errors: []
  388. }
  389. },
  390. {
  391. description: "Input is just empty fields",
  392. input: ',,\n,,,',
  393. expected: {
  394. data: [['', '', ''], ['', '', '', '']],
  395. errors: []
  396. }
  397. },
  398. {
  399. description: "Input is just a string (a single field)",
  400. input: 'Abc def',
  401. expected: {
  402. data: [['Abc def']],
  403. errors: []
  404. }
  405. },
  406. {
  407. description: "Commented line at beginning",
  408. input: '# Comment!\na,b,c',
  409. config: { comments: true },
  410. expected: {
  411. data: [['a', 'b', 'c']],
  412. errors: []
  413. }
  414. },
  415. {
  416. description: "Commented line in middle",
  417. input: 'a,b,c\n# Comment\nd,e,f',
  418. config: { comments: true },
  419. expected: {
  420. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  421. errors: []
  422. }
  423. },
  424. {
  425. description: "Commented line at end",
  426. input: 'a,true,false\n# Comment',
  427. config: { comments: true },
  428. expected: {
  429. data: [['a', 'true', 'false']],
  430. errors: []
  431. }
  432. },
  433. {
  434. description: "Two comment lines consecutively",
  435. input: 'a,b,c\n#comment1\n#comment2\nd,e,f',
  436. config: { comments: true },
  437. expected: {
  438. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  439. errors: []
  440. }
  441. },
  442. {
  443. description: "Two comment lines consecutively at end of file",
  444. input: 'a,b,c\n#comment1\n#comment2',
  445. config: { comments: true },
  446. expected: {
  447. data: [['a', 'b', 'c']],
  448. errors: []
  449. }
  450. },
  451. {
  452. description: "Three comment lines consecutively at beginning of file",
  453. input: '#comment1\n#comment2\n#comment3\na,b,c',
  454. config: { comments: true },
  455. expected: {
  456. data: [['a', 'b', 'c']],
  457. errors: []
  458. }
  459. },
  460. {
  461. description: "Entire file is comment lines",
  462. input: '#comment1\n#comment2\n#comment3',
  463. config: { comments: true },
  464. expected: {
  465. data: [],
  466. errors: []
  467. }
  468. },
  469. {
  470. description: "Comment with non-default character",
  471. input: 'a,b,c\n!Comment goes here\nd,e,f',
  472. config: { comments: '!' },
  473. expected: {
  474. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  475. errors: []
  476. }
  477. },
  478. {
  479. description: "Bad comments value specified",
  480. notes: "Should silently disable comment parsing",
  481. input: 'a,b,c\n5comment\nd,e,f',
  482. config: { comments: 5 },
  483. expected: {
  484. data: [['a', 'b', 'c'], ['5comment'], ['d', 'e', 'f']],
  485. errors: []
  486. }
  487. },
  488. {
  489. description: "Multi-character comment string",
  490. input: 'a,b,c\n=N(Comment)\nd,e,f',
  491. config: { comments: "=N(" },
  492. expected: {
  493. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  494. errors: []
  495. }
  496. },
  497. {
  498. description: "Input with only a commented line",
  499. input: '#commented line',
  500. config: { comments: true, delimiter: ',' },
  501. expected: {
  502. data: [],
  503. errors: []
  504. }
  505. },
  506. {
  507. description: "Input with only a commented line and blank line after",
  508. input: '#commented line\n',
  509. config: { comments: true, delimiter: ',' },
  510. expected: {
  511. data: [['']],
  512. errors: []
  513. }
  514. },
  515. {
  516. description: "Input with only a commented line, without comments enabled",
  517. input: '#commented line',
  518. config: { delimiter: ',' },
  519. expected: {
  520. data: [['#commented line']],
  521. errors: []
  522. }
  523. },
  524. {
  525. description: "Input without comments with line starting with whitespace",
  526. input: 'a\n b\nc',
  527. config: { delimiter: ',' },
  528. notes: "\" \" == false, but \" \" !== false, so === comparison is required",
  529. expected: {
  530. data: [['a'], [' b'], ['c']],
  531. errors: []
  532. }
  533. },
  534. {
  535. description: "Multiple rows, one column (no delimiter found)",
  536. input: 'a\nb\nc\nd\ne',
  537. expected: {
  538. data: [['a'], ['b'], ['c'], ['d'], ['e']],
  539. errors: []
  540. }
  541. },
  542. {
  543. description: "One column input with empty fields",
  544. input: 'a\nb\n\n\nc\nd\ne\n',
  545. expected: {
  546. data: [['a'], ['b'], [''], [''], ['c'], ['d'], ['e'], ['']],
  547. errors: []
  548. }
  549. },
  550. {
  551. description: "Fast mode, basic",
  552. input: 'a,b,c\nd,e,f',
  553. config: { fastMode: true },
  554. expected: {
  555. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  556. errors: []
  557. }
  558. },
  559. {
  560. description: "Fast mode with comments",
  561. input: '// Commented line\na,b,c',
  562. config: { fastMode: true, comments: "//" },
  563. expected: {
  564. data: [['a', 'b', 'c']],
  565. errors: []
  566. }
  567. },
  568. {
  569. description: "Fast mode with preview",
  570. input: 'a,b,c\nd,e,f\nh,j,i\n',
  571. config: { fastMode: true, preview: 2 },
  572. expected: {
  573. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  574. errors: []
  575. }
  576. },
  577. {
  578. description: "Fast mode with blank line at end",
  579. input: 'a,b,c\n',
  580. config: { fastMode: true },
  581. expected: {
  582. data: [['a', 'b', 'c'], ['']],
  583. errors: []
  584. }
  585. },
  586. {
  587. description: "Simple duplicated header names",
  588. input: 'A,A,A,A\n1,2,3,4',
  589. config: { header: true },
  590. expected: {
  591. data: [['A', 'A_1', 'A_2', 'A_3'], ['1', '2', '3', '4']],
  592. errors: [],
  593. meta: {
  594. renamedHeaders: {A_1: 'A', A_2: 'A', A_3: 'A'},
  595. cursor: 15
  596. }
  597. }
  598. },
  599. {
  600. description: "Duplicated header names with headerTransform",
  601. input: 'A,A,A,A\n1,2,3,4',
  602. config: { header: true, transformHeader: function(header) { return header.toLowerCase(); } },
  603. expected: {
  604. data: [['a', 'a_1', 'a_2', 'a_3'], ['1', '2', '3', '4']],
  605. errors: [],
  606. meta: {
  607. renamedHeaders: {a_1: 'a', a_2: 'a', a_3: 'a'},
  608. cursor: 15
  609. }
  610. }
  611. },
  612. {
  613. description: "Duplicated header names existing column",
  614. input: 'c,c,c,c_1\n1,2,3,4',
  615. config: { header: true },
  616. expected: {
  617. data: [['c', 'c_2', 'c_3', 'c_1'], ['1', '2', '3', '4']],
  618. errors: [],
  619. meta: {
  620. renamedHeaders: {c_2: 'c', c_3: 'c'},
  621. cursor: 17
  622. }
  623. }
  624. },
  625. ];
  626. describe('Core Parser Tests', function() {
  627. function generateTest(test) {
  628. (test.disabled ? it.skip : it)(test.description, function() {
  629. var actual = new Papa.Parser(test.config).parse(test.input);
  630. assert.deepEqual(actual.errors, test.expected.errors);
  631. assert.deepEqual(actual.data, test.expected.data);
  632. assert.deepNestedInclude(actual.meta, test.expected.meta || {});
  633. });
  634. }
  635. for (var i = 0; i < CORE_PARSER_TESTS.length; i++) {
  636. generateTest(CORE_PARSER_TESTS[i]);
  637. }
  638. });
  639. // Tests for Papa.parse() function -- high-level wrapped parser (CSV to JSON)
  640. var PARSE_TESTS = [
  641. {
  642. description: "Two rows, just \\r",
  643. input: 'A,b,c\rd,E,f',
  644. expected: {
  645. data: [['A', 'b', 'c'], ['d', 'E', 'f']],
  646. errors: []
  647. }
  648. },
  649. {
  650. description: "Two rows, \\r\\n",
  651. input: 'A,b,c\r\nd,E,f',
  652. expected: {
  653. data: [['A', 'b', 'c'], ['d', 'E', 'f']],
  654. errors: []
  655. }
  656. },
  657. {
  658. description: "Quoted field with \\r\\n",
  659. input: 'A,"B\r\nB",C',
  660. expected: {
  661. data: [['A', 'B\r\nB', 'C']],
  662. errors: []
  663. }
  664. },
  665. {
  666. description: "Quoted field with \\r",
  667. input: 'A,"B\rB",C',
  668. expected: {
  669. data: [['A', 'B\rB', 'C']],
  670. errors: []
  671. }
  672. },
  673. {
  674. description: "Quoted field with \\n",
  675. input: 'A,"B\nB",C',
  676. expected: {
  677. data: [['A', 'B\nB', 'C']],
  678. errors: []
  679. }
  680. },
  681. {
  682. description: "Quoted fields with spaces between closing quote and next delimiter",
  683. input: 'A,"B" ,C,D\r\nE,F,"G" ,H',
  684. expected: {
  685. data: [['A', 'B', 'C','D'],['E', 'F', 'G','H']],
  686. errors: []
  687. }
  688. },
  689. {
  690. description: "Quoted fields with spaces between closing quote and next new line",
  691. input: 'A,B,C,"D" \r\nE,F,G,"H" \r\nQ,W,E,R',
  692. expected: {
  693. data: [['A', 'B', 'C','D'],['E', 'F', 'G','H'],['Q', 'W', 'E','R']],
  694. errors: []
  695. }
  696. },
  697. {
  698. description: "Quoted fields with spaces after closing quote",
  699. input: 'A,"B" ,C,"D" \r\nE,F,"G" ,"H" \r\nQ,W,"E" ,R',
  700. expected: {
  701. data: [['A', 'B', 'C','D'],['E', 'F', 'G','H'],['Q', 'W', 'E','R']],
  702. errors: []
  703. }
  704. },
  705. {
  706. description: "Misplaced quotes in data twice, not as opening quotes",
  707. input: 'A,B",C\nD,E",F',
  708. expected: {
  709. data: [['A', 'B"', 'C'], ['D', 'E"', 'F']],
  710. errors: []
  711. }
  712. },
  713. {
  714. description: "Mixed slash n and slash r should choose first as precident",
  715. input: 'a,b,c\nd,e,f\rg,h,i\n',
  716. expected: {
  717. data: [['a', 'b', 'c'], ['d', 'e', 'f\rg', 'h', 'i'], ['']],
  718. errors: []
  719. }
  720. },
  721. {
  722. description: "Header row with one row of data",
  723. input: 'A,B,C\r\na,b,c',
  724. config: { header: true },
  725. expected: {
  726. data: [{"A": "a", "B": "b", "C": "c"}],
  727. errors: []
  728. }
  729. },
  730. {
  731. description: "Header row only",
  732. input: 'A,B,C',
  733. config: { header: true },
  734. expected: {
  735. data: [],
  736. errors: []
  737. }
  738. },
  739. {
  740. description: "Row with too few fields",
  741. input: 'A,B,C\r\na,b',
  742. config: { header: true },
  743. expected: {
  744. data: [{"A": "a", "B": "b"}],
  745. errors: [{
  746. "type": "FieldMismatch",
  747. "code": "TooFewFields",
  748. "message": "Too few fields: expected 3 fields but parsed 2",
  749. "row": 0
  750. }]
  751. }
  752. },
  753. {
  754. description: "Row with too many fields",
  755. input: 'A,B,C\r\na,b,c,d,e\r\nf,g,h',
  756. config: { header: true },
  757. expected: {
  758. data: [{"A": "a", "B": "b", "C": "c", "__parsed_extra": ["d", "e"]}, {"A": "f", "B": "g", "C": "h"}],
  759. errors: [{
  760. "type": "FieldMismatch",
  761. "code": "TooManyFields",
  762. "message": "Too many fields: expected 3 fields but parsed 5",
  763. "row": 0
  764. }]
  765. }
  766. },
  767. {
  768. description: "Row with enough fields but blank field in the begining",
  769. input: 'A,B,C\r\n,b1,c1\r\na2,b2,c2',
  770. expected: {
  771. data: [["A", "B", "C"], ['', 'b1', 'c1'], ['a2', 'b2', 'c2']],
  772. errors: []
  773. }
  774. },
  775. {
  776. description: "Row with enough fields but blank field in the begining using headers",
  777. input: 'A,B,C\r\n,b1,c1\r\n,b2,c2',
  778. config: { header: true },
  779. expected: {
  780. data: [{"A": "", "B": "b1", "C": "c1"}, {"A": "", "B": "b2", "C": "c2"}],
  781. errors: []
  782. }
  783. },
  784. {
  785. description: "Row with enough fields but blank field at end",
  786. input: 'A,B,C\r\na,b,',
  787. config: { header: true },
  788. expected: {
  789. data: [{"A": "a", "B": "b", "C": ""}],
  790. errors: []
  791. }
  792. },
  793. {
  794. description: "Header rows are transformed when transformHeader function is provided",
  795. input: 'A,B,C\r\na,b,c',
  796. config: { header: true, transformHeader: function(header) { return header.toLowerCase(); } },
  797. expected: {
  798. data: [{"a": "a", "b": "b", "c": "c"}],
  799. errors: []
  800. }
  801. },
  802. {
  803. description: "transformHeader accepts and optional index attribute",
  804. input: 'A,B,C\r\na,b,c',
  805. config: { header: true, transformHeader: function(header, i) { return i % 2 ? header.toLowerCase() : header; } },
  806. expected: {
  807. data: [{"A": "a", "b": "b", "C": "c"}],
  808. errors: []
  809. }
  810. },
  811. {
  812. description: "Line ends with quoted field, first field of next line is empty using headers",
  813. input: 'a,b,"c"\r\nd,e,"f"\r\n,"h","i"\r\n,"k","l"',
  814. config: {
  815. header: true,
  816. newline: '\r\n',
  817. },
  818. expected: {
  819. data: [
  820. {a: 'd', b: 'e', c: 'f'},
  821. {a: '', b: 'h', c: 'i'},
  822. {a: '', b: 'k', c: 'l'}
  823. ],
  824. errors: []
  825. }
  826. },
  827. {
  828. description: "Tab delimiter",
  829. input: 'a\tb\tc\r\nd\te\tf',
  830. config: { delimiter: "\t" },
  831. expected: {
  832. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  833. errors: []
  834. }
  835. },
  836. {
  837. description: "Pipe delimiter",
  838. input: 'a|b|c\r\nd|e|f',
  839. config: { delimiter: "|" },
  840. expected: {
  841. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  842. errors: []
  843. }
  844. },
  845. {
  846. description: "ASCII 30 delimiter",
  847. input: 'a' + RECORD_SEP + 'b' + RECORD_SEP + 'c\r\nd' + RECORD_SEP + 'e' + RECORD_SEP + 'f',
  848. config: { delimiter: RECORD_SEP },
  849. expected: {
  850. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  851. errors: []
  852. }
  853. },
  854. {
  855. description: "ASCII 31 delimiter",
  856. input: 'a' + UNIT_SEP + 'b' + UNIT_SEP + 'c\r\nd' + UNIT_SEP + 'e' + UNIT_SEP + 'f',
  857. config: { delimiter: UNIT_SEP },
  858. expected: {
  859. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  860. errors: []
  861. }
  862. },
  863. {
  864. description: "Bad delimiter (\\n)",
  865. input: 'a,b,c',
  866. config: { delimiter: "\n" },
  867. notes: "Should silently default to comma",
  868. expected: {
  869. data: [['a', 'b', 'c']],
  870. errors: []
  871. }
  872. },
  873. {
  874. description: "Multi-character delimiter",
  875. input: 'a, b, c',
  876. config: { delimiter: ", " },
  877. expected: {
  878. data: [['a', 'b', 'c']],
  879. errors: []
  880. }
  881. },
  882. {
  883. description: "Multi-character delimiter (length 2) with quoted field",
  884. input: 'a, b, "c, e", d',
  885. config: { delimiter: ", " },
  886. notes: "The quotes must be immediately adjacent to the delimiter to indicate a quoted field",
  887. expected: {
  888. data: [['a', 'b', 'c, e', 'd']],
  889. errors: []
  890. }
  891. },
  892. {
  893. description: "Callback delimiter",
  894. input: 'a$ b$ c',
  895. config: { delimiter: function(input) { return input[1] + ' '; } },
  896. expected: {
  897. data: [['a', 'b', 'c']],
  898. errors: []
  899. }
  900. },
  901. {
  902. description: "Dynamic typing converts numeric literals and maintains precision",
  903. input: '1,2.2,1e3\r\n-4,-4.5,-4e-5\r\n-,5a,5-2\r\n16142028098527942586,9007199254740991,-9007199254740992',
  904. config: { dynamicTyping: true },
  905. expected: {
  906. data: [[1, 2.2, 1000], [-4, -4.5, -0.00004], ["-", "5a", "5-2"], ["16142028098527942586", 9007199254740991, "-9007199254740992"]],
  907. errors: []
  908. }
  909. },
  910. {
  911. description: "Dynamic typing converts boolean literals",
  912. input: 'true,false,T,F,TRUE,FALSE,True,False',
  913. config: { dynamicTyping: true },
  914. expected: {
  915. data: [[true, false, "T", "F", true, false, "True", "False"]],
  916. errors: []
  917. }
  918. },
  919. {
  920. description: "Dynamic typing doesn't convert other types",
  921. input: 'A,B,C\r\nundefined,null,[\r\nvar,float,if',
  922. config: { dynamicTyping: true },
  923. expected: {
  924. data: [["A", "B", "C"], ["undefined", "null", "["], ["var", "float", "if"]],
  925. errors: []
  926. }
  927. },
  928. {
  929. description: "Dynamic typing applies to specific columns",
  930. input: 'A,B,C\r\n1,2.2,1e3\r\n-4,-4.5,-4e-5',
  931. config: { header: true, dynamicTyping: { A: true, C: true } },
  932. expected: {
  933. data: [{"A": 1, "B": "2.2", "C": 1000}, {"A": -4, "B": "-4.5", "C": -0.00004}],
  934. errors: []
  935. }
  936. },
  937. {
  938. description: "Dynamic typing applies to specific columns by index",
  939. input: '1,2.2,1e3\r\n-4,-4.5,-4e-5\r\n-,5a,5-2',
  940. config: { dynamicTyping: { 1: true } },
  941. expected: {
  942. data: [["1", 2.2, "1e3"], ["-4", -4.5, "-4e-5"], ["-", "5a", "5-2"]],
  943. errors: []
  944. }
  945. },
  946. {
  947. description: "Dynamic typing can be applied to `__parsed_extra`",
  948. input: 'A,B,C\r\n1,2.2,1e3,5.5\r\n-4,-4.5,-4e-5',
  949. config: { header: true, dynamicTyping: { A: true, C: true, __parsed_extra: true } },
  950. expected: {
  951. data: [{"A": 1, "B": "2.2", "C": 1000, "__parsed_extra": [5.5]}, {"A": -4, "B": "-4.5", "C": -0.00004}],
  952. errors: [{
  953. "type": "FieldMismatch",
  954. "code": "TooManyFields",
  955. "message": "Too many fields: expected 3 fields but parsed 4",
  956. "row": 0
  957. }]
  958. }
  959. },
  960. {
  961. description: "Dynamic typing by indices can be determined by function",
  962. input: '001,002,003',
  963. config: { dynamicTyping: function(field) { return (field % 2) === 0; } },
  964. expected: {
  965. data: [[1, "002", 3]],
  966. errors: []
  967. }
  968. },
  969. {
  970. description: "Dynamic typing by headers can be determined by function",
  971. input: 'A_as_int,B,C_as_int\r\n001,002,003',
  972. config: { header: true, dynamicTyping: function(field) { return /_as_int$/.test(field); } },
  973. expected: {
  974. data: [{"A_as_int": 1, "B": "002", "C_as_int": 3}],
  975. errors: []
  976. }
  977. },
  978. {
  979. description: "Dynamic typing converts empty values into NULL",
  980. input: '1,2.2,1e3\r\n,NULL,\r\n-,5a,null',
  981. config: { dynamicTyping: true },
  982. expected: {
  983. data: [[1, 2.2, 1000], [null, "NULL", null], ["-", "5a", "null"]],
  984. errors: []
  985. }
  986. },
  987. {
  988. description: "Custom transform function is applied to values",
  989. input: 'A,B,C\r\nd,e,f',
  990. config: {
  991. transform: function(value) {
  992. return value.toLowerCase();
  993. }
  994. },
  995. expected: {
  996. data: [["a","b","c"], ["d","e","f"]],
  997. errors: []
  998. }
  999. },
  1000. {
  1001. description: "Custom transform accepts column number also",
  1002. input: 'A,B,C\r\nd,e,f',
  1003. config: {
  1004. transform: function(value, column) {
  1005. if (column % 2) {
  1006. value = value.toLowerCase();
  1007. }
  1008. return value;
  1009. }
  1010. },
  1011. expected: {
  1012. data: [["A","b","C"], ["d","e","f"]],
  1013. errors: []
  1014. }
  1015. },
  1016. {
  1017. description: "Custom transform accepts header name when using header",
  1018. input: 'A,B,C\r\nd,e,f',
  1019. config: {
  1020. header: true,
  1021. transform: function(value, name) {
  1022. if (name === 'B') {
  1023. value = value.toUpperCase();
  1024. }
  1025. return value;
  1026. }
  1027. },
  1028. expected: {
  1029. data: [{'A': "d", 'B': "E", 'C': "f"}],
  1030. errors: []
  1031. }
  1032. },
  1033. {
  1034. description: "Dynamic typing converts ISO date strings to Dates",
  1035. input: 'ISO date,long date\r\n2018-05-04T21:08:03.269Z,Fri May 04 2018 14:08:03 GMT-0700 (PDT)\r\n2018-05-08T15:20:22.642Z,Tue May 08 2018 08:20:22 GMT-0700 (PDT)',
  1036. config: { dynamicTyping: true },
  1037. expected: {
  1038. data: [["ISO date", "long date"], [new Date("2018-05-04T21:08:03.269Z"), "Fri May 04 2018 14:08:03 GMT-0700 (PDT)"], [new Date("2018-05-08T15:20:22.642Z"), "Tue May 08 2018 08:20:22 GMT-0700 (PDT)"]],
  1039. errors: []
  1040. }
  1041. },
  1042. {
  1043. description: "Dynamic typing skips ISO date strings ocurring in other strings",
  1044. input: 'ISO date,String with ISO date\r\n2018-05-04T21:08:03.269Z,The date is 2018-05-04T21:08:03.269Z\r\n2018-05-08T15:20:22.642Z,The date is 2018-05-08T15:20:22.642Z',
  1045. config: { dynamicTyping: true },
  1046. expected: {
  1047. data: [["ISO date", "String with ISO date"], [new Date("2018-05-04T21:08:03.269Z"), "The date is 2018-05-04T21:08:03.269Z"], [new Date("2018-05-08T15:20:22.642Z"), "The date is 2018-05-08T15:20:22.642Z"]],
  1048. errors: []
  1049. }
  1050. },
  1051. {
  1052. description: "Blank line at beginning",
  1053. input: '\r\na,b,c\r\nd,e,f',
  1054. config: { newline: '\r\n' },
  1055. expected: {
  1056. data: [[''], ['a', 'b', 'c'], ['d', 'e', 'f']],
  1057. errors: []
  1058. }
  1059. },
  1060. {
  1061. description: "Blank line in middle",
  1062. input: 'a,b,c\r\n\r\nd,e,f',
  1063. config: { newline: '\r\n' },
  1064. expected: {
  1065. data: [['a', 'b', 'c'], [''], ['d', 'e', 'f']],
  1066. errors: []
  1067. }
  1068. },
  1069. {
  1070. description: "Blank lines at end",
  1071. input: 'a,b,c\nd,e,f\n\n',
  1072. expected: {
  1073. data: [['a', 'b', 'c'], ['d', 'e', 'f'], [''], ['']],
  1074. errors: []
  1075. }
  1076. },
  1077. {
  1078. description: "Blank line in middle with whitespace",
  1079. input: 'a,b,c\r\n \r\nd,e,f',
  1080. expected: {
  1081. data: [['a', 'b', 'c'], [" "], ['d', 'e', 'f']],
  1082. errors: []
  1083. }
  1084. },
  1085. {
  1086. description: "First field of a line is empty",
  1087. input: 'a,b,c\r\n,e,f',
  1088. expected: {
  1089. data: [['a', 'b', 'c'], ['', 'e', 'f']],
  1090. errors: []
  1091. }
  1092. },
  1093. {
  1094. description: "Last field of a line is empty",
  1095. input: 'a,b,\r\nd,e,f',
  1096. expected: {
  1097. data: [['a', 'b', ''], ['d', 'e', 'f']],
  1098. errors: []
  1099. }
  1100. },
  1101. {
  1102. description: "Other fields are empty",
  1103. input: 'a,,c\r\n,,',
  1104. expected: {
  1105. data: [['a', '', 'c'], ['', '', '']],
  1106. errors: []
  1107. }
  1108. },
  1109. {
  1110. description: "Empty input string",
  1111. input: '',
  1112. expected: {
  1113. data: [],
  1114. errors: [{
  1115. "type": "Delimiter",
  1116. "code": "UndetectableDelimiter",
  1117. "message": "Unable to auto-detect delimiting character; defaulted to ','"
  1118. }]
  1119. }
  1120. },
  1121. {
  1122. description: "Input is just the delimiter (2 empty fields)",
  1123. input: ',',
  1124. expected: {
  1125. data: [['', '']],
  1126. errors: []
  1127. }
  1128. },
  1129. {
  1130. description: "Input is just a string (a single field)",
  1131. input: 'Abc def',
  1132. expected: {
  1133. data: [['Abc def']],
  1134. errors: [
  1135. {
  1136. "type": "Delimiter",
  1137. "code": "UndetectableDelimiter",
  1138. "message": "Unable to auto-detect delimiting character; defaulted to ','"
  1139. }
  1140. ]
  1141. }
  1142. },
  1143. {
  1144. description: "Preview 0 rows should default to parsing all",
  1145. input: 'a,b,c\r\nd,e,f\r\ng,h,i',
  1146. config: { preview: 0 },
  1147. expected: {
  1148. data: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']],
  1149. errors: []
  1150. }
  1151. },
  1152. {
  1153. description: "Preview 1 row",
  1154. input: 'a,b,c\r\nd,e,f\r\ng,h,i',
  1155. config: { preview: 1 },
  1156. expected: {
  1157. data: [['a', 'b', 'c']],
  1158. errors: []
  1159. }
  1160. },
  1161. {
  1162. description: "Preview 2 rows",
  1163. input: 'a,b,c\r\nd,e,f\r\ng,h,i',
  1164. config: { preview: 2 },
  1165. expected: {
  1166. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  1167. errors: []
  1168. }
  1169. },
  1170. {
  1171. description: "Preview all (3) rows",
  1172. input: 'a,b,c\r\nd,e,f\r\ng,h,i',
  1173. config: { preview: 3 },
  1174. expected: {
  1175. data: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']],
  1176. errors: []
  1177. }
  1178. },
  1179. {
  1180. description: "Preview more rows than input has",
  1181. input: 'a,b,c\r\nd,e,f\r\ng,h,i',
  1182. config: { preview: 4 },
  1183. expected: {
  1184. data: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']],
  1185. errors: []
  1186. }
  1187. },
  1188. {
  1189. description: "Preview should count rows, not lines",
  1190. input: 'a,b,c\r\nd,e,"f\r\nf",g,h,i',
  1191. config: { preview: 2 },
  1192. expected: {
  1193. data: [['a', 'b', 'c'], ['d', 'e', 'f\r\nf', 'g', 'h', 'i']],
  1194. errors: []
  1195. }
  1196. },
  1197. {
  1198. description: "Preview with header row",
  1199. notes: "Preview is defined to be number of rows of input not including header row",
  1200. input: 'a,b,c\r\nd,e,f\r\ng,h,i\r\nj,k,l',
  1201. config: { header: true, preview: 2 },
  1202. expected: {
  1203. data: [{"a": "d", "b": "e", "c": "f"}, {"a": "g", "b": "h", "c": "i"}],
  1204. errors: []
  1205. }
  1206. },
  1207. {
  1208. description: "Empty lines",
  1209. input: '\na,b,c\n\nd,e,f\n\n',
  1210. config: { delimiter: ',' },
  1211. expected: {
  1212. data: [[''], ['a', 'b', 'c'], [''], ['d', 'e', 'f'], [''], ['']],
  1213. errors: []
  1214. }
  1215. },
  1216. {
  1217. description: "Skip empty lines",
  1218. input: 'a,b,c\n\nd,e,f',
  1219. config: { skipEmptyLines: true },
  1220. expected: {
  1221. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  1222. errors: []
  1223. }
  1224. },
  1225. {
  1226. description: "Skip empty lines, with newline at end of input",
  1227. input: 'a,b,c\r\n\r\nd,e,f\r\n',
  1228. config: { skipEmptyLines: true },
  1229. expected: {
  1230. data: [['a', 'b', 'c'], ['d', 'e', 'f']],
  1231. errors: []
  1232. }
  1233. },
  1234. {
  1235. description: "Skip empty lines, with empty input",
  1236. input: '',
  1237. config: { skipEmptyLines: true },
  1238. expected: {
  1239. data: [],
  1240. errors: [
  1241. {
  1242. "type": "Delimiter",
  1243. "code": "UndetectableDelimiter",
  1244. "message": "Unable to auto-detect delimiting character; defaulted to ','"
  1245. }
  1246. ]
  1247. }
  1248. },
  1249. {
  1250. description: "Skip empty lines, with first line only whitespace",
  1251. notes: "A line must be absolutely empty to be considered empty",
  1252. input: ' \na,b,c',
  1253. config: { skipEmptyLines: true, delimiter: ',' },
  1254. expected: {
  1255. data: [[" "], ['a', 'b', 'c']],
  1256. errors: []
  1257. }
  1258. },
  1259. {
  1260. description: "Skip empty lines while detecting delimiter",
  1261. notes: "Parsing correctly newline-terminated short data with delimiter:auto and skipEmptyLines:true",
  1262. input: 'a,b\n1,2\n3,4\n',
  1263. config: { header: true, skipEmptyLines: true },
  1264. expected: {
  1265. data: [{'a': '1', 'b': '2'}, {'a': '3', 'b': '4'}],
  1266. errors: []
  1267. }
  1268. },
  1269. {
  1270. description: "Lines with comments are not used when guessing the delimiter in an escaped file",
  1271. notes: "Guessing the delimiter should work even if there are many lines of comments at the start of the file",
  1272. input: '#1\n#2\n#3\n#4\n#5\n#6\n#7\n#8\n#9\n#10\none,"t,w,o",three\nfour,five,six',
  1273. config: { comments: '#' },
  1274. expected: {
  1275. data: [['one','t,w,o','three'],['four','five','six']],
  1276. errors: []
  1277. }
  1278. },
  1279. {
  1280. description: "Lines with comments are not used when guessing the delimiter in a non-escaped file",
  1281. notes: "Guessing the delimiter should work even if there are many lines of comments at the start of the file",
  1282. input: '#1\n#2\n#3\n#4\n#5\n#6\n#7\n#8\n#9\n#10\n#11\none,two,three\nfour,five,six',
  1283. config: { comments: '#' },
  1284. expected: {
  1285. data: [['one','two','three'],['four','five','six']],
  1286. errors: []
  1287. }
  1288. },
  1289. {
  1290. description: "Pipe delimiter is guessed correctly when mixed with comas",
  1291. notes: "Guessing the delimiter should work even if there are many lines of comments at the start of the file",
  1292. input: 'one|two,two|three\nfour|five,five|six',
  1293. config: {},
  1294. expected: {
  1295. data: [['one','two,two','three'],['four','five,five','six']],
  1296. errors: []
  1297. }
  1298. },
  1299. {
  1300. description: "Pipe delimiter is guessed correctly choose avgFildCount max one",
  1301. notes: "Guessing the delimiter should work choose the min delta one and the max one",
  1302. config: {},
  1303. input: 'a,b,c\na,b,c|d|e|f',
  1304. expected: {
  1305. data: [['a', 'b', 'c'], ['a','b','c|d|e|f']],
  1306. errors: []
  1307. }
  1308. },
  1309. {
  1310. description: "Pipe delimiter is guessed correctly when first field are enclosed in quotes and contain delimiter characters",
  1311. notes: "Guessing the delimiter should work if the first field is enclosed in quotes, but others are not",
  1312. input: '"Field1,1,1";Field2;"Field3";Field4;Field5;Field6',
  1313. config: {},
  1314. expected: {
  1315. data: [['Field1,1,1','Field2','Field3', 'Field4', 'Field5', 'Field6']],
  1316. errors: []
  1317. }
  1318. },
  1319. {
  1320. description: "Pipe delimiter is guessed correctly when some fields are enclosed in quotes and contain delimiter characters and escaoped quotes",
  1321. notes: "Guessing the delimiter should work even if the first field is not enclosed in quotes, but others are",
  1322. input: 'Field1;Field2;"Field,3,""3,3";Field4;Field5;"Field6,6"',
  1323. config: {},
  1324. expected: {
  1325. data: [['Field1','Field2','Field,3,"3,3', 'Field4', 'Field5', 'Field6,6']],
  1326. errors: []
  1327. }
  1328. },
  1329. {
  1330. description: "Single quote as quote character",
  1331. notes: "Must parse correctly when single quote is specified as a quote character",
  1332. input: "a,b,'c,d'",
  1333. config: { quoteChar: "'" },
  1334. expected: {
  1335. data: [['a', 'b', 'c,d']],
  1336. errors: []
  1337. }
  1338. },
  1339. {
  1340. description: "Custom escape character in the middle",
  1341. notes: "Must parse correctly if the backslash sign (\\) is configured as a custom escape character",
  1342. input: 'a,b,"c\\"d\\"f"',
  1343. config: { escapeChar: '\\' },
  1344. expected: {
  1345. data: [['a', 'b', 'c"d"f']],
  1346. errors: []
  1347. }
  1348. },
  1349. {
  1350. description: "Custom escape character at the end",
  1351. notes: "Must parse correctly if the backslash sign (\\) is configured as a custom escape character and the escaped quote character appears at the end of the column",
  1352. input: 'a,b,"c\\"d\\""',
  1353. config: { escapeChar: '\\' },
  1354. expected: {
  1355. data: [['a', 'b', 'c"d"']],
  1356. errors: []
  1357. }
  1358. },
  1359. {
  1360. description: "Custom escape character not used for escaping",
  1361. notes: "Must parse correctly if the backslash sign (\\) is configured as a custom escape character and appears as regular character in the text",
  1362. input: 'a,b,"c\\d"',
  1363. config: { escapeChar: '\\' },
  1364. expected: {
  1365. data: [['a', 'b', 'c\\d']],
  1366. errors: []
  1367. }
  1368. },
  1369. {
  1370. description: "Header row with preceding comment",
  1371. notes: "Must parse correctly headers if they are preceded by comments",
  1372. input: '#Comment\na,b\nc,d\n',
  1373. config: { header: true, comments: '#', skipEmptyLines: true, delimiter: ',' },
  1374. expected: {
  1375. data: [{'a': 'c', 'b': 'd'}],
  1376. errors: []
  1377. }
  1378. },
  1379. {
  1380. description: "Carriage return in header inside quotes, with line feed endings",
  1381. input: '"a\r\na","b"\n"c","d"\n"e","f"\n"g","h"\n"i","j"',
  1382. config: {},
  1383. expected: {
  1384. data: [['a\r\na', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i', 'j']],
  1385. errors: []
  1386. }
  1387. },
  1388. {
  1389. description: "Line feed in header inside quotes, with carriage return + line feed endings",
  1390. input: '"a\na","b"\r\n"c","d"\r\n"e","f"\r\n"g","h"\r\n"i","j"',
  1391. config: {},
  1392. expected: {
  1393. data: [['a\na', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i', 'j']],
  1394. errors: []
  1395. }
  1396. },
  1397. {
  1398. description: "Using \\r\\n endings uses \\r\\n linebreak",
  1399. input: 'a,b\r\nc,d\r\ne,f\r\ng,h\r\ni,j',
  1400. config: {},
  1401. expected: {
  1402. data: [['a', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i', 'j']],
  1403. errors: [],
  1404. meta: {
  1405. linebreak: '\r\n',
  1406. delimiter: ',',
  1407. cursor: 23,
  1408. aborted: false,
  1409. truncated: false,
  1410. renamedHeaders: null
  1411. }
  1412. }
  1413. },
  1414. {
  1415. description: "Using \\n endings uses \\n linebreak",
  1416. input: 'a,b\nc,d\ne,f\ng,h\ni,j',
  1417. config: {},
  1418. expected: {
  1419. data: [['a', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i', 'j']],
  1420. errors: [],
  1421. meta: {
  1422. linebreak: '\n',
  1423. delimiter: ',',
  1424. cursor: 19,
  1425. aborted: false,
  1426. truncated: false,
  1427. renamedHeaders: null
  1428. }
  1429. }
  1430. },
  1431. {
  1432. description: "Using \\r\\n endings with \\r\\n in header field uses \\r\\n linebreak",
  1433. input: '"a\r\na",b\r\nc,d\r\ne,f\r\ng,h\r\ni,j',
  1434. config: {},
  1435. expected: {
  1436. data: [['a\r\na', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i', 'j']],
  1437. errors: [],
  1438. meta: {
  1439. linebreak: '\r\n',
  1440. delimiter: ',',
  1441. cursor: 28,
  1442. aborted: false,
  1443. truncated: false,
  1444. renamedHeaders: null
  1445. }
  1446. }
  1447. },
  1448. {
  1449. description: "Using \\r\\n endings with \\n in header field uses \\r\\n linebreak",
  1450. input: '"a\na",b\r\nc,d\r\ne,f\r\ng,h\r\ni,j',
  1451. config: {},
  1452. expected: {
  1453. data: [['a\na', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i', 'j']],
  1454. errors: [],
  1455. meta: {
  1456. linebreak: '\r\n',
  1457. delimiter: ',',
  1458. cursor: 27,
  1459. aborted: false,
  1460. truncated: false,
  1461. renamedHeaders: null
  1462. }
  1463. }
  1464. },
  1465. {
  1466. description: "Using \\r\\n endings with \\n in header field with skip empty lines uses \\r\\n linebreak",
  1467. input: '"a\na",b\r\nc,d\r\ne,f\r\ng,h\r\ni,j\r\n',
  1468. config: {skipEmptyLines: true},
  1469. expected: {
  1470. data: [['a\na', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i', 'j']],
  1471. errors: [],
  1472. meta: {
  1473. linebreak: '\r\n',
  1474. delimiter: ',',
  1475. cursor: 29,
  1476. aborted: false,
  1477. truncated: false,
  1478. renamedHeaders: null
  1479. }
  1480. }
  1481. },
  1482. {
  1483. description: "Using \\n endings with \\r\\n in header field uses \\n linebreak",
  1484. input: '"a\r\na",b\nc,d\ne,f\ng,h\ni,j',
  1485. config: {},
  1486. expected: {
  1487. data: [['a\r\na', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i', 'j']],
  1488. errors: [],
  1489. meta: {
  1490. linebreak: '\n',
  1491. delimiter: ',',
  1492. cursor: 24,
  1493. aborted: false,
  1494. truncated: false,
  1495. renamedHeaders: null
  1496. }
  1497. }
  1498. },
  1499. {
  1500. description: "Using reserved regex character . as quote character",
  1501. input: '.a\na.,b\r\nc,d\r\ne,f\r\ng,h\r\ni,j',
  1502. config: { quoteChar: '.' },
  1503. expected: {
  1504. data: [['a\na', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i', 'j']],
  1505. errors: [],
  1506. meta: {
  1507. linebreak: '\r\n',
  1508. delimiter: ',',
  1509. cursor: 27,
  1510. aborted: false,
  1511. truncated: false,
  1512. renamedHeaders: null
  1513. }
  1514. }
  1515. },
  1516. {
  1517. description: "Using reserved regex character | as quote character",
  1518. input: '|a\na|,b\r\nc,d\r\ne,f\r\ng,h\r\ni,j',
  1519. config: { quoteChar: '|' },
  1520. expected: {
  1521. data: [['a\na', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i', 'j']],
  1522. errors: [],
  1523. meta: {
  1524. linebreak: '\r\n',
  1525. delimiter: ',',
  1526. cursor: 27,
  1527. aborted: false,
  1528. truncated: false,
  1529. renamedHeaders: null
  1530. }
  1531. }
  1532. },
  1533. {
  1534. description: "Parsing with skipEmptyLines set to 'greedy'",
  1535. notes: "Must parse correctly without lines with no content",
  1536. input: 'a,b\n\n,\nc,d\n , \n""," "\n , \n,,,,\n',
  1537. config: { skipEmptyLines: 'greedy' },
  1538. expected: {
  1539. data: [['a', 'b'], ['c', 'd']],
  1540. errors: []
  1541. }
  1542. },
  1543. {
  1544. description: "Parsing with skipEmptyLines set to 'greedy' with quotes and delimiters as content",
  1545. notes: "Must include lines with escaped delimiters and quotes",
  1546. input: 'a,b\n\n,\nc,d\n" , ",","\n""" """,""""""\n\n\n',
  1547. config: { skipEmptyLines: 'greedy' },
  1548. expected: {
  1549. data: [['a', 'b'], ['c', 'd'], [' , ', ','], ['" "', '""']],
  1550. errors: []
  1551. }
  1552. },
  1553. {
  1554. description: "Quoted fields with spaces between closing quote and next delimiter and contains delimiter",
  1555. input: 'A,",B" ,C,D\nE,F,G,H',
  1556. expected: {
  1557. data: [['A', ',B', 'C', 'D'],['E', 'F', 'G', 'H']],
  1558. errors: []
  1559. }
  1560. },
  1561. {
  1562. description: "Quoted fields with spaces between closing quote and newline and contains newline",
  1563. input: 'a,b,"c\n" \nd,e,f',
  1564. expected: {
  1565. data: [['a', 'b', 'c\n'], ['d', 'e', 'f']],
  1566. errors: []
  1567. }
  1568. },
  1569. {
  1570. description: "Skip First N number of lines , with header and 2 rows",
  1571. input: 'a,b,c,d\n1,2,3,4',
  1572. config: { header: true, skipFirstNLines: 1 },
  1573. expected: {
  1574. data: [],
  1575. errors: []
  1576. }
  1577. },
  1578. {
  1579. description: "Skip First N number of lines , with header and 2 rows",
  1580. input: 'to-be-ignored\na,b,c,d\n1,2,3,4',
  1581. config: { header: true, skipFirstNLines: 1 },
  1582. expected: {
  1583. data: [{a: '1', b: '2', c: '3', d: '4'}],
  1584. errors: []
  1585. }
  1586. },
  1587. {
  1588. description: "Skip First N number of lines , with header false",
  1589. input: 'a,b,c,d\n1,2,3,4\n4,5,6,7',
  1590. config: { header: false, skipFirstNLines: 1 },
  1591. expected: {
  1592. data: [['1','2','3','4'],['4','5','6','7']],
  1593. errors: []
  1594. }
  1595. },
  1596. {
  1597. description: "Skip First N number of lines , with header false and skipFirstNLines as negative value",
  1598. input: 'a,b,c,d\n1,2,3,4\n4,5,6,7',
  1599. config: { header: false, skipFirstNLines: -2 },
  1600. expected: {
  1601. data: [['a','b','c','d'],['1','2','3','4'],['4','5','6','7']],
  1602. errors: []
  1603. }
  1604. },
  1605. {
  1606. description: "Skip first 2 lines , with custom newline character",
  1607. input: 'skip-this\rskip-this\r1,2,3,4',
  1608. config: { header: false, skipFirstNLines: 2, newline: '\r' },
  1609. expected: {
  1610. data: [['1','2','3','4']],
  1611. errors: []
  1612. }
  1613. }
  1614. ];
  1615. describe('Parse Tests', function() {
  1616. function generateTest(test) {
  1617. (test.disabled ? it.skip : it)(test.description, function() {
  1618. var actual = Papa.parse(test.input, test.config);
  1619. // allows for testing the meta object if present in the test
  1620. if (test.expected.meta) {
  1621. assert.deepEqual(actual.meta, test.expected.meta);
  1622. }
  1623. assert.deepEqual(actual.errors, test.expected.errors);
  1624. assert.deepEqual(actual.data, test.expected.data);
  1625. });
  1626. }
  1627. for (var i = 0; i < PARSE_TESTS.length; i++) {
  1628. generateTest(PARSE_TESTS[i]);
  1629. }
  1630. });
  1631. // Tests for Papa.parse() that involve asynchronous operation
  1632. var PARSE_ASYNC_TESTS = [
  1633. {
  1634. description: "Simple worker",
  1635. input: "A,B,C\nX,Y,Z",
  1636. config: {
  1637. worker: true,
  1638. },
  1639. expected: {
  1640. data: [['A','B','C'],['X','Y','Z']],
  1641. errors: []
  1642. }
  1643. },
  1644. {
  1645. description: "Simple download",
  1646. input: BASE_PATH + "sample.csv",
  1647. config: {
  1648. download: true
  1649. },
  1650. disabled: !XHR_ENABLED,
  1651. expected: {
  1652. data: [['A','B','C'],['X','Y','Z']],
  1653. errors: []
  1654. }
  1655. },
  1656. {
  1657. description: "Simple download + worker",
  1658. input: BASE_PATH + "sample.csv",
  1659. config: {
  1660. worker: true,
  1661. download: true
  1662. },
  1663. disabled: !XHR_ENABLED,
  1664. expected: {
  1665. data: [['A','B','C'],['X','Y','Z']],
  1666. errors: []
  1667. }
  1668. },
  1669. {
  1670. description: "Simple file",
  1671. disabled: !FILES_ENABLED,
  1672. input: FILES_ENABLED ? new File(["A,B,C\nX,Y,Z"], "sample.csv") : false,
  1673. config: {
  1674. },
  1675. expected: {
  1676. data: [['A','B','C'],['X','Y','Z']],
  1677. errors: []
  1678. }
  1679. },
  1680. {
  1681. description: "Simple file + worker",
  1682. disabled: !FILES_ENABLED,
  1683. input: FILES_ENABLED ? new File(["A,B,C\nX,Y,Z"], "sample.csv") : false,
  1684. config: {
  1685. worker: true,
  1686. },
  1687. expected: {
  1688. data: [['A','B','C'],['X','Y','Z']],
  1689. errors: []
  1690. }
  1691. },
  1692. {
  1693. description: "File with a few regular and lots of empty lines",
  1694. disabled: !FILES_ENABLED,
  1695. input: FILES_ENABLED ? new File(["A,B,C\nX,Y,Z\n" + new Array(500000).fill(",,").join("\n")], "sample.csv") : false,
  1696. config: {
  1697. skipEmptyLines: "greedy"
  1698. },
  1699. expected: {
  1700. data: [['A','B','C'],['X','Y','Z']],
  1701. errors: []
  1702. }
  1703. },
  1704. {
  1705. description: "File with a few regular and lots of empty lines + worker",
  1706. disabled: !FILES_ENABLED,
  1707. input: FILES_ENABLED ? new File(["A,B,C\nX,Y,Z\n" + new Array(500000).fill(",,").join("\n")], "sample.csv") : false,
  1708. config: {
  1709. worker: true,
  1710. skipEmptyLines: "greedy"
  1711. },
  1712. expected: {
  1713. data: [['A','B','C'],['X','Y','Z']],
  1714. errors: []
  1715. }
  1716. }
  1717. ];
  1718. describe('Parse Async Tests', function() {
  1719. function generateTest(test) {
  1720. (test.disabled ? it.skip : it)(test.description, function(done) {
  1721. var config = test.config;
  1722. config.complete = function(actual) {
  1723. assert.deepEqual(actual.errors, test.expected.errors);
  1724. assert.deepEqual(actual.data, test.expected.data);
  1725. done();
  1726. };
  1727. config.error = function(err) {
  1728. throw err;
  1729. };
  1730. Papa.parse(test.input, config);
  1731. });
  1732. }
  1733. for (var i = 0; i < PARSE_ASYNC_TESTS.length; i++) {
  1734. generateTest(PARSE_ASYNC_TESTS[i]);
  1735. }
  1736. });
  1737. // Tests for Papa.unparse() function (JSON to CSV)
  1738. var UNPARSE_TESTS = [
  1739. {
  1740. description: "A simple row",
  1741. notes: "Comma should be default delimiter",
  1742. input: [['A', 'b', 'c']],
  1743. expected: 'A,b,c'
  1744. },
  1745. {
  1746. description: "Two rows",
  1747. input: [['A', 'b', 'c'], ['d', 'E', 'f']],
  1748. expected: 'A,b,c\r\nd,E,f'
  1749. },
  1750. {
  1751. description: "Data with quotes",
  1752. input: [['a', '"b"', 'c'], ['"d"', 'e', 'f']],
  1753. expected: 'a,"""b""",c\r\n"""d""",e,f'
  1754. },
  1755. {
  1756. description: "Data with newlines",
  1757. input: [['a', 'b\nb', 'c'], ['d', 'e', 'f\r\nf']],
  1758. expected: 'a,"b\nb",c\r\nd,e,"f\r\nf"'
  1759. },
  1760. {
  1761. description: "Array of objects (header row)",
  1762. input: [{ "Col1": "a", "Col2": "b", "Col3": "c" }, { "Col1": "d", "Col2": "e", "Col3": "f" }],
  1763. expected: 'Col1,Col2,Col3\r\na,b,c\r\nd,e,f'
  1764. },
  1765. {
  1766. description: "With header row, missing a field in a row",
  1767. input: [{ "Col1": "a", "Col2": "b", "Col3": "c" }, { "Col1": "d", "Col3": "f" }],
  1768. expected: 'Col1,Col2,Col3\r\na,b,c\r\nd,,f'
  1769. },
  1770. {
  1771. description: "With header row, with extra field in a row",
  1772. notes: "Extra field should be ignored; first object in array dictates header row",
  1773. input: [{ "Col1": "a", "Col2": "b", "Col3": "c" }, { "Col1": "d", "Col2": "e", "Extra": "g", "Col3": "f" }],
  1774. expected: 'Col1,Col2,Col3\r\na,b,c\r\nd,e,f'
  1775. },
  1776. {
  1777. description: "Specifying column names and data separately",
  1778. input: { fields: ["Col1", "Col2", "Col3"], data: [["a", "b", "c"], ["d", "e", "f"]] },
  1779. expected: 'Col1,Col2,Col3\r\na,b,c\r\nd,e,f'
  1780. },
  1781. {
  1782. description: "Specifying column names only (no data)",
  1783. notes: "Papa should add a data property that is an empty array to prevent errors (no copy is made)",
  1784. input: { fields: ["Col1", "Col2", "Col3"] },
  1785. expected: 'Col1,Col2,Col3'
  1786. },
  1787. {
  1788. description: "Specifying data only (no field names), improperly",
  1789. notes: "A single array for a single row is wrong, but it can be compensated.<br>Papa should add empty fields property to prevent errors.",
  1790. input: { data: ["abc", "d", "ef"] },
  1791. expected: 'abc,d,ef'
  1792. },
  1793. {
  1794. description: "Specifying data only (no field names), properly",
  1795. notes: "An array of arrays, even if just a single row.<br>Papa should add empty fields property to prevent errors.",
  1796. input: { data: [["a", "b", "c"]] },
  1797. expected: 'a,b,c'
  1798. },
  1799. {
  1800. description: "Custom delimiter (semicolon)",
  1801. input: [['A', 'b', 'c'], ['d', 'e', 'f']],
  1802. config: { delimiter: ';' },
  1803. expected: 'A;b;c\r\nd;e;f'
  1804. },
  1805. {
  1806. description: "Custom delimiter (tab)",
  1807. input: [['Ab', 'cd', 'ef'], ['g', 'h', 'ij']],
  1808. config: { delimiter: '\t' },
  1809. expected: 'Ab\tcd\tef\r\ng\th\tij'
  1810. },
  1811. {
  1812. description: "Custom delimiter (ASCII 30)",
  1813. input: [['a', 'b', 'c'], ['d', 'e', 'f']],
  1814. config: { delimiter: RECORD_SEP },
  1815. expected: 'a' + RECORD_SEP + 'b' + RECORD_SEP + 'c\r\nd' + RECORD_SEP + 'e' + RECORD_SEP + 'f'
  1816. },
  1817. {
  1818. description: "Custom delimiter (Multi-character)",
  1819. input: [['A', 'b', 'c'], ['d', 'e', 'f']],
  1820. config: { delimiter: ', ' },
  1821. expected: 'A, b, c\r\nd, e, f'
  1822. },
  1823. {
  1824. description: "Custom delimiter (Multi-character), field contains custom delimiter",
  1825. input: [['A', 'b', 'c'], ['d', 'e', 'f, g']],
  1826. config: { delimiter: ', ' },
  1827. expected: 'A, b, c\r\nd, e, "f, g"'
  1828. },
  1829. {
  1830. description: "Bad delimiter (\\n)",
  1831. notes: "Should default to comma",
  1832. input: [['a', 'b', 'c'], ['d', 'e', 'f']],
  1833. config: { delimiter: '\n' },
  1834. expected: 'a,b,c\r\nd,e,f'
  1835. },
  1836. {
  1837. description: "Custom line ending (\\r)",
  1838. input: [['a', 'b', 'c'], ['d', 'e', 'f']],
  1839. config: { newline: '\r' },
  1840. expected: 'a,b,c\rd,e,f'
  1841. },
  1842. {
  1843. description: "Custom line ending (\\n)",
  1844. input: [['a', 'b', 'c'], ['d', 'e', 'f']],
  1845. config: { newline: '\n' },
  1846. expected: 'a,b,c\nd,e,f'
  1847. },
  1848. {
  1849. description: "Custom, but strange, line ending ($)",
  1850. input: [['a', 'b', 'c'], ['d', 'e', 'f']],
  1851. config: { newline: '$' },
  1852. expected: 'a,b,c$d,e,f'
  1853. },
  1854. {
  1855. description: "Force quotes around all fields",
  1856. input: [['a', 'b', 'c'], ['d', 'e', 'f']],
  1857. config: { quotes: true },
  1858. expected: '"a","b","c"\r\n"d","e","f"'
  1859. },
  1860. {
  1861. description: "Force quotes around all fields (with header row)",
  1862. input: [{ "Col1": "a", "Col2": "b", "Col3": "c" }, { "Col1": "d", "Col2": "e", "Col3": "f" }],
  1863. config: { quotes: true },
  1864. expected: '"Col1","Col2","Col3"\r\n"a","b","c"\r\n"d","e","f"'
  1865. },
  1866. {
  1867. description: "Force quotes around certain fields only",
  1868. input: [['a', 'b', 'c'], ['d', 'e', 'f']],
  1869. config: { quotes: [true, false, true] },
  1870. expected: '"a",b,"c"\r\n"d",e,"f"'
  1871. },
  1872. {
  1873. description: "Force quotes around certain fields only (with header row)",
  1874. input: [{ "Col1": "a", "Col2": "b", "Col3": "c" }, { "Col1": "d", "Col2": "e", "Col3": "f" }],
  1875. config: { quotes: [true, false, true] },
  1876. expected: '"Col1",Col2,"Col3"\r\n"a",b,"c"\r\n"d",e,"f"'
  1877. },
  1878. {
  1879. description: "Force quotes around string fields only",
  1880. input: [['a', 'b', 'c'], ['d', 10, true]],
  1881. config: { quotes: function(value) { return typeof value === 'string'; } },
  1882. expected: '"a","b","c"\r\n"d",10,true'
  1883. },
  1884. {
  1885. description: "Force quotes around string fields only (with header row)",
  1886. input: [{ "Col1": "a", "Col2": "b", "Col3": "c" }, { "Col1": "d", "Col2": 10, "Col3": true }],
  1887. config: { quotes: function(value) { return typeof value === 'string'; } },
  1888. expected: '"Col1","Col2","Col3"\r\n"a","b","c"\r\n"d",10,true'
  1889. },
  1890. {
  1891. description: "Empty input",
  1892. input: [],
  1893. expected: ''
  1894. },
  1895. {
  1896. description: "Mismatched field counts in rows",
  1897. input: [['a', 'b', 'c'], ['d', 'e'], ['f']],
  1898. expected: 'a,b,c\r\nd,e\r\nf'
  1899. },
  1900. {
  1901. description: "JSON null is treated as empty value",
  1902. input: [{ "Col1": "a", "Col2": null, "Col3": "c" }],
  1903. expected: 'Col1,Col2,Col3\r\na,,c'
  1904. },
  1905. {
  1906. description: "Custom quote character (single quote)",
  1907. input: [['a,d','b','c']],
  1908. config: { quoteChar: "'"},
  1909. expected: "'a,d',b,c"
  1910. },
  1911. {
  1912. description: "Don't print header if header:false option specified",
  1913. input: [{"Col1": "a", "Col2": "b", "Col3": "c"}, {"Col1": "d", "Col2": "e", "Col3": "f"}],
  1914. config: {header: false},
  1915. expected: 'a,b,c\r\nd,e,f'
  1916. },
  1917. {
  1918. description: "Date objects are exported in its ISO representation",
  1919. input: [{date: new Date("2018-05-04T21:08:03.269Z"), "not a date": 16}, {date: new Date("Tue May 08 2018 08:20:22 GMT-0700 (PDT)"), "not a date": 32}],
  1920. expected: 'date,not a date\r\n2018-05-04T21:08:03.269Z,16\r\n2018-05-08T15:20:22.000Z,32'
  1921. },
  1922. {
  1923. description: "Returns empty rows when empty rows are passed and skipEmptyLines is false",
  1924. input: [[null, ' '], [], ['1', '2']],
  1925. config: {skipEmptyLines: false},
  1926. expected: '," "\r\n\r\n1,2'
  1927. },
  1928. {
  1929. description: "Returns without empty rows when skipEmptyLines is true",
  1930. input: [[null, ' '], [], ['1', '2']],
  1931. config: {skipEmptyLines: true},
  1932. expected: '," "\r\n1,2'
  1933. },
  1934. {
  1935. description: "Returns without rows with no content when skipEmptyLines is 'greedy'",
  1936. input: [[null, ' '], [], ['1', '2']].concat(new Array(500000).fill(['', ''])).concat([['3', '4']]),
  1937. config: {skipEmptyLines: 'greedy'},
  1938. expected: '1,2\r\n3,4'
  1939. },
  1940. {
  1941. description: "Returns empty rows when empty rows are passed and skipEmptyLines is false with headers",
  1942. input: [{a: null, b: ' '}, {}, {a: '1', b: '2'}],
  1943. config: {skipEmptyLines: false, header: true},
  1944. expected: 'a,b\r\n," "\r\n\r\n1,2'
  1945. },
  1946. {
  1947. description: "Returns without empty rows when skipEmptyLines is true with headers",
  1948. input: [{a: null, b: ' '}, {}, {a: '1', b: '2'}],
  1949. config: {skipEmptyLines: true, header: true},
  1950. expected: 'a,b\r\n," "\r\n1,2'
  1951. },
  1952. {
  1953. description: "Returns without rows with no content when skipEmptyLines is 'greedy' with headers",
  1954. input: [{a: null, b: ' '}, {}, {a: '1', b: '2'}],
  1955. config: {skipEmptyLines: 'greedy', header: true},
  1956. expected: 'a,b\r\n1,2'
  1957. },
  1958. {
  1959. description: "Column option used to manually specify keys",
  1960. notes: "Should not throw any error when attempting to serialize key not present in object. Columns are different than keys of the first object. When an object is missing a key then the serialized value should be an empty string.",
  1961. input: [{a: 1, b: '2'}, {}, {a: 3, d: 'd', c: 4,}],
  1962. config: {columns: ['a', 'b', 'c']},
  1963. expected: 'a,b,c\r\n1,2,\r\n\r\n3,,4'
  1964. },
  1965. {
  1966. description: "Column option used to manually specify keys with input type object",
  1967. notes: "Should not throw any error when attempting to serialize key not present in object. Columns are different than keys of the first object. When an object is missing a key then the serialized value should be an empty string.",
  1968. input: { data: [{a: 1, b: '2'}, {}, {a: 3, d: 'd', c: 4,}] },
  1969. config: {columns: ['a', 'b', 'c']},
  1970. expected: 'a,b,c\r\n1,2,\r\n\r\n3,,4'
  1971. },
  1972. {
  1973. description: "Use different escapeChar",
  1974. input: [{a: 'foo', b: '"quoted"'}],
  1975. config: {header: false, escapeChar: '\\'},
  1976. expected: 'foo,"\\"quoted\\""'
  1977. },
  1978. {
  1979. description: "test defeault escapeChar",
  1980. input: [{a: 'foo', b: '"quoted"'}],
  1981. config: {header: false},
  1982. expected: 'foo,"""quoted"""'
  1983. },
  1984. {
  1985. description: "Escape formulae",
  1986. input: [{ "Col1": "=danger", "Col2": "@danger", "Col3": "safe" }, { "Col1": "safe=safe", "Col2": "+danger", "Col3": "-danger, danger" }, { "Col1": "'+safe", "Col2": "'@safe", "Col3": "safe, safe" }],
  1987. config: { escapeFormulae: true },
  1988. expected: 'Col1,Col2,Col3\r\n"\'=danger","\'@danger",safe\r\nsafe=safe,"\'+danger","\'-danger, danger"\r\n\'+safe,\'@safe,"safe, safe"'
  1989. },
  1990. {
  1991. description: "Don't escape formulae by default",
  1992. input: [{ "Col1": "=danger", "Col2": "@danger", "Col3": "safe" }, { "Col1": "safe=safe", "Col2": "+danger", "Col3": "-danger, danger" }, { "Col1": "'+safe", "Col2": "'@safe", "Col3": "safe, safe" }],
  1993. expected: 'Col1,Col2,Col3\r\n=danger,@danger,safe\r\nsafe=safe,+danger,"-danger, danger"\r\n\'+safe,\'@safe,"safe, safe"'
  1994. },
  1995. {
  1996. description: "Escape formulae with forced quotes",
  1997. input: [{ "Col1": "=danger", "Col2": "@danger", "Col3": "safe" }, { "Col1": "safe=safe", "Col2": "+danger", "Col3": "-danger, danger" }, { "Col1": "'+safe", "Col2": "'@safe", "Col3": "safe, safe" }],
  1998. config: { escapeFormulae: true, quotes: true },
  1999. expected: '"Col1","Col2","Col3"\r\n"\'=danger","\'@danger","safe"\r\n"safe=safe","\'+danger","\'-danger, danger"\r\n"\'+safe","\'@safe","safe, safe"'
  2000. },
  2001. {
  2002. description: "Escape formulae with single-quote quoteChar and escapeChar",
  2003. input: [{ "Col1": "=danger", "Col2": "@danger", "Col3": "safe" }, { "Col1": "safe=safe", "Col2": "+danger", "Col3": "-danger, danger" }, { "Col1": "'+safe", "Col2": "'@safe", "Col3": "safe, safe" }],
  2004. config: { escapeFormulae: true, quoteChar: "'", escapeChar: "'" },
  2005. expected: 'Col1,Col2,Col3\r\n\'\'\'=danger\',\'\'\'@danger\',safe\r\nsafe=safe,\'\'\'+danger\',\'\'\'-danger, danger\'\r\n\'\'+safe,\'\'@safe,\'safe, safe\''
  2006. },
  2007. {
  2008. description: "Escape formulae with single-quote quoteChar and escapeChar and forced quotes",
  2009. input: [{ "Col1": "=danger", "Col2": "@danger", "Col3": "safe" }, { "Col1": "safe=safe", "Col2": "+danger", "Col3": "-danger, danger" }, { "Col1": "'+safe", "Col2": "'@safe", "Col3": "safe, safe" }],
  2010. config: { escapeFormulae: true, quotes: true, quoteChar: "'", escapeChar: "'" },
  2011. expected: '\'Col1\',\'Col2\',\'Col3\'\r\n\'\'\'=danger\',\'\'\'@danger\',\'safe\'\r\n\'safe=safe\',\'\'\'+danger\',\'\'\'-danger, danger\'\r\n\'\'\'+safe\',\'\'\'@safe\',\'safe, safe\''
  2012. },
  2013. // new escapeFormulae values:
  2014. {
  2015. description: "Escape formulae with tab and carriage-return",
  2016. input: [{ "Col1": "\tdanger", "Col2": "\rdanger,", "Col3": "safe\t\r" }],
  2017. config: { escapeFormulae: true },
  2018. expected: 'Col1,Col2,Col3\r\n"\'\tdanger","\'\rdanger,","safe\t\r"'
  2019. },
  2020. {
  2021. description: "Escape formulae with tab and carriage-return, with forced quotes",
  2022. input: [{ "Col1": " danger", "Col2": "\rdanger,", "Col3": "safe\t\r" }],
  2023. config: { escapeFormulae: true, quotes: true },
  2024. expected: '"Col1","Col2","Col3"\r\n"\'\tdanger","\'\rdanger,","safe\t\r"'
  2025. },
  2026. {
  2027. description: "Escape formulae with tab and carriage-return, with single-quote quoteChar and escapeChar",
  2028. input: [{ "Col1": " danger", "Col2": "\rdanger,", "Col3": "safe, \t\r" }],
  2029. config: { escapeFormulae: true, quoteChar: "'", escapeChar: "'" },
  2030. expected: 'Col1,Col2,Col3\r\n\'\'\'\tdanger\',\'\'\'\rdanger,\',\'safe, \t\r\''
  2031. },
  2032. {
  2033. description: "Escape formulae with tab and carriage-return, with single-quote quoteChar and escapeChar and forced quotes",
  2034. input: [{ "Col1": " danger", "Col2": "\rdanger,", "Col3": "safe, \t\r" }],
  2035. config: { escapeFormulae: true, quotes: true, quoteChar: "'", escapeChar: "'" },
  2036. expected: '\'Col1\',\'Col2\',\'Col3\'\r\n\'\'\'\tdanger\',\'\'\'\rdanger,\',\'safe, \t\r\''
  2037. },
  2038. ];
  2039. describe('Unparse Tests', function() {
  2040. function generateTest(test) {
  2041. (test.disabled ? it.skip : it)(test.description, function() {
  2042. var actual;
  2043. try {
  2044. actual = Papa.unparse(test.input, test.config);
  2045. } catch (e) {
  2046. if (e instanceof Error) {
  2047. throw e;
  2048. }
  2049. actual = e;
  2050. }
  2051. assert.strictEqual(actual, test.expected);
  2052. });
  2053. }
  2054. for (var i = 0; i < UNPARSE_TESTS.length; i++) {
  2055. generateTest(UNPARSE_TESTS[i]);
  2056. }
  2057. });
  2058. var CUSTOM_TESTS = [
  2059. {
  2060. description: "Pause and resume works (Regression Test for Bug #636)",
  2061. disabled: !XHR_ENABLED,
  2062. timeout: 30000,
  2063. expected: [2001, [
  2064. ["Etiam a dolor vitae est vestibulum","84","DEF"],
  2065. ["Etiam a dolor vitae est vestibulum","84","DEF"],
  2066. ["Lorem ipsum dolor sit","42","ABC"],
  2067. ["Etiam a dolor vitae est vestibulum","84","DEF"],
  2068. ["Etiam a dolor vitae est vestibulum","84"],
  2069. ["Lorem ipsum dolor sit","42","ABC"],
  2070. ["Etiam a dolor vitae est vestibulum","84","DEF"],
  2071. ["Etiam a dolor vitae est vestibulum","84","DEF"],
  2072. ["Lorem ipsum dolor sit","42","ABC"],
  2073. ["Lorem ipsum dolor sit","42"]
  2074. ], 0],
  2075. run: function(callback) {
  2076. var stepped = 0;
  2077. var dataRows = [];
  2078. var errorCount = 0;
  2079. var output = [];
  2080. Papa.parse(BASE_PATH + "verylong-sample.csv", {
  2081. download: true,
  2082. step: function(results, parser) {
  2083. stepped++;
  2084. if (results)
  2085. {
  2086. parser.pause();
  2087. parser.resume();
  2088. if (results.data && stepped % 200 === 0) {
  2089. dataRows.push(results.data);
  2090. }
  2091. }
  2092. },
  2093. complete: function() {
  2094. output.push(stepped);
  2095. output.push(dataRows);
  2096. output.push(errorCount);
  2097. callback(output);
  2098. }
  2099. });
  2100. }
  2101. },
  2102. {
  2103. description: "Pause and resume works for chunks with NetworkStreamer",
  2104. disabled: !XHR_ENABLED,
  2105. timeout: 30000,
  2106. expected: ["Etiam a dolor vitae est vestibulum", "84", "DEF"],
  2107. run: function(callback) {
  2108. var chunkNum = 0;
  2109. Papa.parse(BASE_PATH + "verylong-sample.csv", {
  2110. download: true,
  2111. chunkSize: 1000,
  2112. chunk: function(results, parser) {
  2113. chunkNum++;
  2114. parser.pause();
  2115. if (chunkNum === 2) {
  2116. callback(results.data[0]);
  2117. return;
  2118. }
  2119. parser.resume();
  2120. },
  2121. complete: function() {
  2122. callback(new Error("Should have found matched row before parsing whole file"));
  2123. }
  2124. });
  2125. }
  2126. },
  2127. {
  2128. description: "Pause and resume works for chunks with FileStreamer",
  2129. disabled: !XHR_ENABLED,
  2130. timeout: 30000,
  2131. expected: ["Etiam a dolor vitae est vestibulum", "84", "DEF"],
  2132. run: function(callback) {
  2133. var chunkNum = 0;
  2134. var xhr = new XMLHttpRequest();
  2135. xhr.onload = function() {
  2136. Papa.parse(new File([xhr.responseText], './verylong-sample.csv'), {
  2137. chunkSize: 1000,
  2138. chunk: function(results, parser) {
  2139. chunkNum++;
  2140. parser.pause();
  2141. if (chunkNum === 2) {
  2142. callback(results.data[0]);
  2143. return;
  2144. }
  2145. parser.resume();
  2146. },
  2147. complete: function() {
  2148. callback(new Error("Should have found matched row before parsing whole file"));
  2149. }
  2150. });
  2151. };
  2152. xhr.open("GET", BASE_PATH + "verylong-sample.csv");
  2153. try {
  2154. xhr.send();
  2155. } catch (err) {
  2156. callback(err);
  2157. return;
  2158. }
  2159. }
  2160. },
  2161. {
  2162. description: "Pause and resume works for chunks with StringStreamer",
  2163. disabled: !XHR_ENABLED,
  2164. timeout: 30000,
  2165. // Test also with string as byte size may be diferent
  2166. expected: ["Etiam a dolor vitae est vestibulum", "84", "DEF"],
  2167. run: function(callback) {
  2168. var chunkNum = 0;
  2169. var xhr = new XMLHttpRequest();
  2170. xhr.onload = function() {
  2171. Papa.parse(xhr.responseText, {
  2172. chunkSize: 1000,
  2173. chunk: function(results, parser) {
  2174. chunkNum++;
  2175. parser.pause();
  2176. if (chunkNum === 2) {
  2177. callback(results.data[0]);
  2178. return;
  2179. }
  2180. parser.resume();
  2181. },
  2182. complete: function() {
  2183. callback(new Error("Should have found matched row before parsing whole file"));
  2184. }
  2185. });
  2186. };
  2187. xhr.open("GET", BASE_PATH + "verylong-sample.csv");
  2188. try {
  2189. xhr.send();
  2190. } catch (err) {
  2191. callback(err);
  2192. return;
  2193. }
  2194. }
  2195. },
  2196. {
  2197. description: "Complete is called with all results if neither step nor chunk is defined",
  2198. expected: [['A', 'b', 'c'], ['d', 'E', 'f'], ['G', 'h', 'i']],
  2199. disabled: !FILES_ENABLED,
  2200. run: function(callback) {
  2201. Papa.parse(new File(['A,b,c\nd,E,f\nG,h,i'], 'sample.csv'), {
  2202. chunkSize: 3,
  2203. complete: function(response) {
  2204. callback(response.data);
  2205. }
  2206. });
  2207. }
  2208. },
  2209. {
  2210. description: "Step is called for each row",
  2211. expected: 2,
  2212. run: function(callback) {
  2213. var callCount = 0;
  2214. Papa.parse('A,b,c\nd,E,f', {
  2215. step: function() {
  2216. callCount++;
  2217. },
  2218. complete: function() {
  2219. callback(callCount);
  2220. }
  2221. });
  2222. }
  2223. },
  2224. {
  2225. description: "Data is correctly parsed with steps",
  2226. expected: [['A', 'b', 'c'], ['d', 'E', 'f']],
  2227. run: function(callback) {
  2228. var data = [];
  2229. Papa.parse('A,b,c\nd,E,f', {
  2230. step: function(results) {
  2231. data.push(results.data);
  2232. },
  2233. complete: function() {
  2234. callback(data);
  2235. }
  2236. });
  2237. }
  2238. },
  2239. {
  2240. description: "Data is correctly parsed with steps (headers)",
  2241. expected: [{One: 'A', Two: 'b', Three: 'c'}, {One: 'd', Two: 'E', Three: 'f'}],
  2242. run: function(callback) {
  2243. var data = [];
  2244. Papa.parse('One,Two,Three\nA,b,c\nd,E,f', {
  2245. header: true,
  2246. step: function(results) {
  2247. data.push(results.data);
  2248. },
  2249. complete: function() {
  2250. callback(data);
  2251. }
  2252. });
  2253. }
  2254. },
  2255. {
  2256. description: "Data is correctly parsed with steps and worker (headers)",
  2257. expected: [{One: 'A', Two: 'b', Three: 'c'}, {One: 'd', Two: 'E', Three: 'f'}],
  2258. run: function(callback) {
  2259. var data = [];
  2260. Papa.parse('One,Two,Three\nA,b,c\nd,E,f', {
  2261. header: true,
  2262. worker: true,
  2263. step: function(results) {
  2264. data.push(results.data);
  2265. },
  2266. complete: function() {
  2267. callback(data);
  2268. }
  2269. });
  2270. }
  2271. },
  2272. {
  2273. description: "Data is correctly parsed with steps and worker",
  2274. expected: [['A', 'b', 'c'], ['d', 'E', 'f']],
  2275. run: function(callback) {
  2276. var data = [];
  2277. Papa.parse('A,b,c\nd,E,f', {
  2278. worker: true,
  2279. step: function(results) {
  2280. data.push(results.data);
  2281. },
  2282. complete: function() {
  2283. callback(data);
  2284. }
  2285. });
  2286. }
  2287. },
  2288. {
  2289. description: "Data is correctly parsed with steps when skipping empty lines",
  2290. expected: [['A', 'b', 'c'], ['d', 'E', 'f']],
  2291. run: function(callback) {
  2292. var data = [];
  2293. Papa.parse('A,b,c\n\nd,E,f', {
  2294. skipEmptyLines: true,
  2295. step: function(results) {
  2296. data.push(results.data);
  2297. },
  2298. complete: function() {
  2299. callback(data);
  2300. }
  2301. });
  2302. }
  2303. },
  2304. {
  2305. description: "Data is correctly parsed with steps when there are empty values",
  2306. expected: [{A: 'a', B: 'b', C: 'c', D: 'd'}, {A: 'a', B: '', C: '', D: ''}],
  2307. run: function(callback) {
  2308. var data = [];
  2309. Papa.parse('A,B,C,D\na,b,c,d\na,,,', {
  2310. header: true,
  2311. step: function(results) {
  2312. data.push(results.data);
  2313. },
  2314. complete: function() {
  2315. callback(data);
  2316. }
  2317. });
  2318. }
  2319. },
  2320. {
  2321. description: "Step is called with the contents of the row",
  2322. expected: ['A', 'b', 'c'],
  2323. run: function(callback) {
  2324. Papa.parse('A,b,c', {
  2325. step: function(response) {
  2326. callback(response.data);
  2327. }
  2328. });
  2329. }
  2330. },
  2331. {
  2332. description: "Step is called with the last cursor position",
  2333. expected: [6, 12, 17],
  2334. run: function(callback) {
  2335. var updates = [];
  2336. Papa.parse('A,b,c\nd,E,f\nG,h,i', {
  2337. step: function(response) {
  2338. updates.push(response.meta.cursor);
  2339. },
  2340. complete: function() {
  2341. callback(updates);
  2342. }
  2343. });
  2344. }
  2345. },
  2346. {
  2347. description: "Step exposes cursor for downloads",
  2348. expected: [129, 287, 452, 595, 727, 865, 1031, 1209],
  2349. disabled: !XHR_ENABLED,
  2350. run: function(callback) {
  2351. var updates = [];
  2352. Papa.parse(BASE_PATH + "long-sample.csv", {
  2353. download: true,
  2354. step: function(response) {
  2355. updates.push(response.meta.cursor);
  2356. },
  2357. complete: function() {
  2358. callback(updates);
  2359. }
  2360. });
  2361. }
  2362. },
  2363. {
  2364. description: "Step exposes cursor for chunked downloads",
  2365. expected: [129, 287, 452, 595, 727, 865, 1031, 1209],
  2366. disabled: !XHR_ENABLED,
  2367. run: function(callback) {
  2368. var updates = [];
  2369. Papa.parse(BASE_PATH + "long-sample.csv", {
  2370. download: true,
  2371. chunkSize: 500,
  2372. step: function(response) {
  2373. updates.push(response.meta.cursor);
  2374. },
  2375. complete: function() {
  2376. callback(updates);
  2377. }
  2378. });
  2379. }
  2380. },
  2381. {
  2382. description: "Step exposes cursor for workers",
  2383. expected: [452, 452, 452, 865, 865, 865, 1209, 1209],
  2384. disabled: !XHR_ENABLED,
  2385. run: function(callback) {
  2386. var updates = [];
  2387. Papa.parse(BASE_PATH + "long-sample.csv", {
  2388. download: true,
  2389. chunkSize: 500,
  2390. worker: true,
  2391. step: function(response) {
  2392. updates.push(response.meta.cursor);
  2393. },
  2394. complete: function() {
  2395. callback(updates);
  2396. }
  2397. });
  2398. }
  2399. },
  2400. {
  2401. description: "Chunk is called for each chunk",
  2402. expected: [3, 3, 2],
  2403. disabled: !XHR_ENABLED,
  2404. run: function(callback) {
  2405. var updates = [];
  2406. Papa.parse(BASE_PATH + "long-sample.csv", {
  2407. download: true,
  2408. chunkSize: 500,
  2409. chunk: function(response) {
  2410. updates.push(response.data.length);
  2411. },
  2412. complete: function() {
  2413. callback(updates);
  2414. }
  2415. });
  2416. }
  2417. },
  2418. {
  2419. description: "Chunk is called with cursor position",
  2420. expected: [452, 865, 1209],
  2421. disabled: !XHR_ENABLED,
  2422. run: function(callback) {
  2423. var updates = [];
  2424. Papa.parse(BASE_PATH + "long-sample.csv", {
  2425. download: true,
  2426. chunkSize: 500,
  2427. chunk: function(response) {
  2428. updates.push(response.meta.cursor);
  2429. },
  2430. complete: function() {
  2431. callback(updates);
  2432. }
  2433. });
  2434. }
  2435. },
  2436. {
  2437. description: "Chunk functions can pause parsing",
  2438. expected: [
  2439. [['A', 'b', 'c']]
  2440. ],
  2441. run: function(callback) {
  2442. var updates = [];
  2443. Papa.parse('A,b,c\nd,E,f\nG,h,i', {
  2444. chunkSize: 10,
  2445. chunk: function(response, handle) {
  2446. updates.push(response.data);
  2447. handle.pause();
  2448. callback(updates);
  2449. },
  2450. complete: function() {
  2451. callback(new Error('incorrect complete callback'));
  2452. }
  2453. });
  2454. }
  2455. },
  2456. {
  2457. description: "Chunk functions can resume parsing",
  2458. expected: [
  2459. [['A', 'b', 'c']],
  2460. [['d', 'E', 'f'], ['G', 'h', 'i']]
  2461. ],
  2462. run: function(callback) {
  2463. var updates = [];
  2464. var handle = null;
  2465. var first = true;
  2466. Papa.parse('A,b,c\nd,E,f\nG,h,i', {
  2467. chunkSize: 10,
  2468. chunk: function(response, h) {
  2469. updates.push(response.data);
  2470. if (!first) return;
  2471. handle = h;
  2472. handle.pause();
  2473. first = false;
  2474. },
  2475. complete: function() {
  2476. callback(updates);
  2477. }
  2478. });
  2479. setTimeout(function() {
  2480. handle.resume();
  2481. }, 500);
  2482. }
  2483. },
  2484. {
  2485. description: "Chunk functions can abort parsing",
  2486. expected: [
  2487. [['A', 'b', 'c']]
  2488. ],
  2489. run: function(callback) {
  2490. var updates = [];
  2491. Papa.parse('A,b,c\nd,E,f\nG,h,i', {
  2492. chunkSize: 1,
  2493. chunk: function(response, handle) {
  2494. if (response.data.length) {
  2495. updates.push(response.data);
  2496. handle.abort();
  2497. }
  2498. },
  2499. complete: function(response) {
  2500. callback(updates);
  2501. }
  2502. });
  2503. }
  2504. },
  2505. {
  2506. description: "Step exposes indexes for files",
  2507. expected: [6, 12, 17],
  2508. disabled: !FILES_ENABLED,
  2509. run: function(callback) {
  2510. var updates = [];
  2511. Papa.parse(new File(['A,b,c\nd,E,f\nG,h,i'], 'sample.csv'), {
  2512. download: true,
  2513. step: function(response) {
  2514. updates.push(response.meta.cursor);
  2515. },
  2516. complete: function() {
  2517. callback(updates);
  2518. }
  2519. });
  2520. }
  2521. },
  2522. {
  2523. description: "Step exposes indexes for chunked files",
  2524. expected: [6, 12, 17],
  2525. disabled: !FILES_ENABLED,
  2526. run: function(callback) {
  2527. var updates = [];
  2528. Papa.parse(new File(['A,b,c\nd,E,f\nG,h,i'], 'sample.csv'), {
  2529. chunkSize: 3,
  2530. step: function(response) {
  2531. updates.push(response.meta.cursor);
  2532. },
  2533. complete: function() {
  2534. callback(updates);
  2535. }
  2536. });
  2537. }
  2538. },
  2539. {
  2540. description: "Quoted line breaks near chunk boundaries are handled",
  2541. expected: [['A', 'B', 'C'], ['X', 'Y\n1\n2\n3', 'Z']],
  2542. disabled: !FILES_ENABLED,
  2543. run: function(callback) {
  2544. var updates = [];
  2545. Papa.parse(new File(['A,B,C\nX,"Y\n1\n2\n3",Z'], 'sample.csv'), {
  2546. chunkSize: 3,
  2547. step: function(response) {
  2548. updates.push(response.data);
  2549. },
  2550. complete: function() {
  2551. callback(updates);
  2552. }
  2553. });
  2554. }
  2555. },
  2556. {
  2557. description: "Step functions can abort parsing",
  2558. expected: [['A', 'b', 'c']],
  2559. run: function(callback) {
  2560. var updates = [];
  2561. Papa.parse('A,b,c\nd,E,f\nG,h,i', {
  2562. step: function(response, handle) {
  2563. updates.push(response.data);
  2564. handle.abort();
  2565. callback(updates);
  2566. },
  2567. chunkSize: 6
  2568. });
  2569. }
  2570. },
  2571. {
  2572. description: "Complete is called after aborting",
  2573. expected: true,
  2574. run: function(callback) {
  2575. Papa.parse('A,b,c\nd,E,f\nG,h,i', {
  2576. step: function(response, handle) {
  2577. handle.abort();
  2578. },
  2579. chunkSize: 6,
  2580. complete: function(response) {
  2581. callback(response.meta.aborted);
  2582. }
  2583. });
  2584. }
  2585. },
  2586. {
  2587. description: "Step functions can pause parsing",
  2588. expected: [['A', 'b', 'c']],
  2589. run: function(callback) {
  2590. var updates = [];
  2591. Papa.parse('A,b,c\nd,E,f\nG,h,i', {
  2592. step: function(response, handle) {
  2593. updates.push(response.data);
  2594. handle.pause();
  2595. callback(updates);
  2596. },
  2597. complete: function() {
  2598. callback('incorrect complete callback');
  2599. }
  2600. });
  2601. }
  2602. },
  2603. {
  2604. description: "Step functions can resume parsing",
  2605. expected: [['A', 'b', 'c'], ['d', 'E', 'f'], ['G', 'h', 'i']],
  2606. run: function(callback) {
  2607. var updates = [];
  2608. var handle = null;
  2609. var first = true;
  2610. Papa.parse('A,b,c\nd,E,f\nG,h,i', {
  2611. step: function(response, h) {
  2612. updates.push(response.data);
  2613. if (!first) return;
  2614. handle = h;
  2615. handle.pause();
  2616. first = false;
  2617. },
  2618. complete: function() {
  2619. callback(updates);
  2620. }
  2621. });
  2622. setTimeout(function() {
  2623. handle.resume();
  2624. }, 500);
  2625. }
  2626. },
  2627. {
  2628. description: "Step functions can abort workers",
  2629. expected: 1,
  2630. disabled: !XHR_ENABLED,
  2631. run: function(callback) {
  2632. var updates = 0;
  2633. Papa.parse(BASE_PATH + "long-sample.csv", {
  2634. worker: true,
  2635. download: true,
  2636. chunkSize: 500,
  2637. step: function(response, handle) {
  2638. updates++;
  2639. handle.abort();
  2640. },
  2641. complete: function() {
  2642. callback(updates);
  2643. }
  2644. });
  2645. }
  2646. },
  2647. {
  2648. description: "beforeFirstChunk manipulates only first chunk",
  2649. expected: 7,
  2650. disabled: !XHR_ENABLED,
  2651. run: function(callback) {
  2652. var updates = 0;
  2653. Papa.parse(BASE_PATH + "long-sample.csv", {
  2654. download: true,
  2655. chunkSize: 500,
  2656. beforeFirstChunk: function(chunk) {
  2657. return chunk.replace(/.*?\n/, '');
  2658. },
  2659. step: function(response) {
  2660. updates++;
  2661. },
  2662. complete: function() {
  2663. callback(updates);
  2664. }
  2665. });
  2666. }
  2667. },
  2668. {
  2669. description: "First chunk not modified if beforeFirstChunk returns nothing",
  2670. expected: 8,
  2671. disabled: !XHR_ENABLED,
  2672. run: function(callback) {
  2673. var updates = 0;
  2674. Papa.parse(BASE_PATH + "long-sample.csv", {
  2675. download: true,
  2676. chunkSize: 500,
  2677. beforeFirstChunk: function(chunk) {
  2678. },
  2679. step: function(response) {
  2680. updates++;
  2681. },
  2682. complete: function() {
  2683. callback(updates);
  2684. }
  2685. });
  2686. }
  2687. },
  2688. {
  2689. description: "Should correctly guess custom delimiter when passed delimiters to guess.",
  2690. expected: "~",
  2691. run: function(callback) {
  2692. var results = Papa.parse('"A"~"B"~"C"~"D"', {
  2693. delimitersToGuess: ['~', '@', '%']
  2694. });
  2695. callback(results.meta.delimiter);
  2696. }
  2697. },
  2698. {
  2699. description: "Should still correctly guess default delimiters when delimiters to guess are not given.",
  2700. expected: ",",
  2701. run: function(callback) {
  2702. var results = Papa.parse('"A","B","C","D"');
  2703. callback(results.meta.delimiter);
  2704. }
  2705. },
  2706. {
  2707. description: "Data is correctly parsed with chunks and duplicated headers",
  2708. expected: [{h0: 'a', h1: 'a'}, {h0: 'b', h1: 'b'}],
  2709. run: function(callback) {
  2710. var data = [];
  2711. Papa.parse('h0,h1\na,a\nb,b', {
  2712. header: true,
  2713. chunkSize: 10,
  2714. chunk: function(results) {
  2715. data.push(results.data[0]);
  2716. },
  2717. complete: function() {
  2718. callback(data);
  2719. }
  2720. });
  2721. }
  2722. },
  2723. ];
  2724. describe('Custom Tests', function() {
  2725. function generateTest(test) {
  2726. (test.disabled ? it.skip : it)(test.description, function(done) {
  2727. if(test.timeout) {
  2728. this.timeout(test.timeout);
  2729. }
  2730. test.run(function(actual) {
  2731. assert.deepEqual(actual, test.expected);
  2732. done();
  2733. });
  2734. });
  2735. }
  2736. for (var i = 0; i < CUSTOM_TESTS.length; i++) {
  2737. generateTest(CUSTOM_TESTS[i]);
  2738. }
  2739. });
  2740. (typeof window !== "undefined" ? describe : describe.skip)("Browser Tests", () => {
  2741. it("When parsing synchronously inside a web-worker not owned by PapaParse we should not invoke postMessage", async() => {
  2742. // Arrange
  2743. const papaParseScriptPath = new URL("../papaparse.js", window.document.baseURI).href;
  2744. // Define our custom web-worker that loads PapaParse and executes a synchronous parse
  2745. const blob = new Blob([
  2746. `
  2747. importScripts('${papaParseScriptPath}');
  2748. self.addEventListener("message", function(event) {
  2749. if (event.data === "ExecuteParse") {
  2750. // Perform our synchronous parse, as requested
  2751. const results = Papa.parse('x\\ny\\n');
  2752. postMessage({type: "ParseExecutedSuccessfully", results});
  2753. } else {
  2754. // Otherwise, send whatever we received back. We shouldn't be hitting this (!) If we're reached
  2755. // this it means PapaParse thinks it is running inside a web-worker that it owns
  2756. postMessage(event.data);
  2757. }
  2758. });
  2759. `
  2760. ], {type: 'text/javascript'});
  2761. const blobURL = window.URL.createObjectURL(blob);
  2762. const webWorker = new Worker(blobURL);
  2763. const receiveMessagePromise = new Promise((resolve, reject) => {
  2764. webWorker.addEventListener("message", (event) => {
  2765. if (event.data.type === "ParseExecutedSuccessfully") {
  2766. resolve(event.data);
  2767. } else {
  2768. const error = new Error(`Received unexpected message: ${JSON.stringify(event.data, null, 2)}`);
  2769. error.data = event.data;
  2770. reject(error);
  2771. }
  2772. });
  2773. });
  2774. // Act
  2775. webWorker.postMessage("ExecuteParse");
  2776. const webWorkerMessage = await receiveMessagePromise;
  2777. // Assert
  2778. assert.equal("ParseExecutedSuccessfully", webWorkerMessage.type);
  2779. assert.equal(3, webWorkerMessage.results.data.length);
  2780. });
  2781. });