1"use strict"; 2 3function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } 4 5(function ($) { 6 "use strict"; 7 8 var optionsDefaults = { 9 /* action='download' options */ 10 filename: "table.csv", 11 12 /* action='output' options */ 13 appendTo: "body", 14 15 /* general options */ 16 separator: ",", 17 newline: "\n", 18 quoteFields: true, 19 trimContent: true, 20 excludeColumns: "", 21 excludeRows: "" 22 }; 23 var options = {}; 24 25 function quote(text) { 26 return "\"" + text.replace(/"/g, "\"\"") + "\""; 27 } // taken from http://stackoverflow.com/questions/3665115/create-a-file-in-memory-for-user-to-download-not-through-server 28 29 30 function download(filename, text) { 31 var element = document.createElement("a"); 32 element.setAttribute("href", "data:text/csv;charset=utf-8,\uFEFF" + encodeURIComponent(text)); 33 element.setAttribute("download", filename); 34 element.style.display = "none"; 35 document.body.appendChild(element); 36 element.click(); 37 document.body.removeChild(element); 38 } 39 40 function convert(table) { 41 var output = ""; 42 var rows = table.find("tr").not(options.excludeRows); 43 var numCols = rows.first().find("td,th").filter(":visible").not(options.excludeColumns).length; 44 rows.each(function (ignore, elem) { 45 $(elem).find("td,th").filter(":visible").not(options.excludeColumns).each(function (i, col) { 46 var column = $(col); // Strip whitespaces 47 48 var content = options.trimContent ? $.trim(column.text()) : column.text(); 49 output += options.quoteFields ? quote(content) : content; 50 51 if (i !== numCols - 1) { 52 output += options.separator; 53 } else { 54 output += options.newline; 55 } 56 }); 57 }); 58 return output; 59 } 60 61 $.fn.table2csv = function (action, opt) { 62 if (_typeof(action) === "object") { 63 opt = action; 64 action = "download"; 65 } else if (action === undefined) { 66 action = "download"; 67 } // type checking 68 69 70 if (typeof action !== "string") { 71 throw new Error("\"action\" argument must be a string"); 72 } 73 74 if (opt !== undefined && _typeof(opt) !== "object") { 75 throw new Error("\"options\" argument must be an object"); 76 } 77 78 options = $.extend({}, optionsDefaults, opt); 79 var table = this.filter("table"); // TODO use $.each 80 81 if (table.length <= 0) { 82 throw new Error("table2csv must be called on a <table> element"); 83 } 84 85 if (table.length > 1) { 86 throw new Error("converting multiple table elements at once is not supported yet"); 87 } 88 89 var csv = convert(table); 90 91 switch (action) { 92 case "download": 93 download(options.filename, csv); 94 break; 95 96 case "output": 97 $(options.appendTo).append($("<pre>").text(csv)); 98 break; 99 100 case "return": 101 return csv; 102 103 default: 104 throw new Error("\"action\" argument must be one of the supported action strings"); 105 } 106 107 return this; 108 }; 109})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.