player.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. var stepped = 0, chunks = 0, rows = 0;
  2. var start, end;
  3. var parser;
  4. var pauseChecked = false;
  5. var printStepChecked = false;
  6. $(function()
  7. {
  8. $('#submit-parse').click(function()
  9. {
  10. stepped = 0;
  11. chunks = 0;
  12. rows = 0;
  13. var txt = $('#input').val();
  14. var localChunkSize = $('#localChunkSize').val();
  15. var remoteChunkSize = $('#remoteChunkSize').val();
  16. var files = $('#files')[0].files;
  17. var config = buildConfig();
  18. // NOTE: Chunk size does not get reset if changed and then set back to empty/default value
  19. if (localChunkSize)
  20. Papa.LocalChunkSize = localChunkSize;
  21. if (remoteChunkSize)
  22. Papa.RemoteChunkSize = remoteChunkSize;
  23. pauseChecked = $('#step-pause').prop('checked');
  24. printStepChecked = $('#print-steps').prop('checked');
  25. if (files.length > 0)
  26. {
  27. if (!$('#stream').prop('checked') && !$('#chunk').prop('checked'))
  28. {
  29. for (var i = 0; i < files.length; i++)
  30. {
  31. if (files[i].size > 1024 * 1024 * 10)
  32. {
  33. alert("A file you've selected is larger than 10 MB; please choose to stream or chunk the input to prevent the browser from crashing.");
  34. return;
  35. }
  36. }
  37. }
  38. start = performance.now();
  39. $('#files').parse({
  40. config: config,
  41. before: function(file, inputElem)
  42. {
  43. console.log("Parsing file:", file);
  44. },
  45. complete: function()
  46. {
  47. console.log("Done with all files.");
  48. }
  49. });
  50. }
  51. else
  52. {
  53. start = performance.now();
  54. var results = Papa.parse(txt, config);
  55. console.log("Synchronous parse results:", results);
  56. }
  57. });
  58. $('#submit-unparse').click(function()
  59. {
  60. var input = $('#input').val();
  61. var delim = $('#delimiter').val();
  62. var header = $('#header').prop('checked');
  63. var results = Papa.unparse(input, {
  64. delimiter: delim,
  65. header: header,
  66. });
  67. console.log("Unparse complete!");
  68. console.log("--------------------------------------");
  69. console.log(results);
  70. console.log("--------------------------------------");
  71. });
  72. $('#insert-tab').click(function()
  73. {
  74. $('#delimiter').val('\t');
  75. });
  76. });
  77. function buildConfig()
  78. {
  79. return {
  80. delimiter: $('#delimiter').val(),
  81. newline: getLineEnding(),
  82. header: $('#header').prop('checked'),
  83. dynamicTyping: $('#dynamicTyping').prop('checked'),
  84. preview: parseInt($('#preview').val() || 0),
  85. step: $('#stream').prop('checked') ? stepFn : undefined,
  86. encoding: $('#encoding').val(),
  87. worker: $('#worker').prop('checked'),
  88. comments: $('#comments').val(),
  89. complete: completeFn,
  90. error: errorFn,
  91. download: $('#download').prop('checked'),
  92. fastMode: $('#fastmode').prop('checked'),
  93. skipEmptyLines: $('#skipEmptyLines').prop('checked'),
  94. chunk: $('#chunk').prop('checked') ? chunkFn : undefined,
  95. beforeFirstChunk: undefined,
  96. skipFirstNLines: $('#skipFirstNLines').val()
  97. };
  98. function getLineEnding()
  99. {
  100. if ($('#newline-n').is(':checked'))
  101. return "\n";
  102. else if ($('#newline-r').is(':checked'))
  103. return "\r";
  104. else if ($('#newline-rn').is(':checked'))
  105. return "\r\n";
  106. else
  107. return "";
  108. }
  109. }
  110. function stepFn(results, parserHandle)
  111. {
  112. stepped++;
  113. rows += results.data.length;
  114. parser = parserHandle;
  115. if (pauseChecked)
  116. {
  117. console.log(results, results.data[0]);
  118. parserHandle.pause();
  119. return;
  120. }
  121. if (printStepChecked)
  122. console.log(results, results.data[0]);
  123. }
  124. function chunkFn(results, streamer, file)
  125. {
  126. if (!results)
  127. return;
  128. chunks++;
  129. rows += results.data.length;
  130. parser = streamer;
  131. if (printStepChecked)
  132. console.log("Chunk data:", results.data.length, results);
  133. if (pauseChecked)
  134. {
  135. console.log("Pausing; " + results.data.length + " rows in chunk; file:", file);
  136. streamer.pause();
  137. return;
  138. }
  139. }
  140. function errorFn(error, file)
  141. {
  142. console.log("ERROR:", error, file);
  143. }
  144. function completeFn()
  145. {
  146. end = performance.now();
  147. if (!$('#stream').prop('checked')
  148. && !$('#chunk').prop('checked')
  149. && arguments[0]
  150. && arguments[0].data)
  151. rows = arguments[0].data.length;
  152. console.log("Finished input (async). Time:", end-start, arguments);
  153. console.log("Rows:", rows, "Stepped:", stepped, "Chunks:", chunks);
  154. }