-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathline-navigator.min.js
1 lines (1 loc) · 3.62 KB
/
line-navigator.min.js
1
var getLineNavigatorClass=function(){function e(e,n){var t=this;n=n?n:{};var i=n.encoding?n.encoding:"utf8",r=n.chunkSize?n.chunkSize:4096,o=void 0!==n.throwOnLongLines&&n.throwOnLongLines,f=[],a=new FileWrapper(e,i),s=a.getSize(),l=function(n){return s=s>n?s:a.getSize(e)},u=function(e){var n=l(e);return Math.round(100*e/n)};t.readSomeLines=function(e,n){var s=t.getPlaceToStart(e,f);a.readChunk(s.offset,r,function l(g,h,d){if(g)return n(g,e);var L=d<r,c=t.examineChunk(h,d,L);if(void 0===c){if(!(d>0))return n("Line "+e+" is out of index, last available: "+(f.length>0?f[f.length-1].lastLine:"none"),e);if(o)return n("Line "+e+" is longer than chunk size ("+r+")",e);c={lines:1,length:d-1,noLineEnding:!0}}var p={firstLine:s.firstLine,lastLine:s.firstLine+c.lines-1,offset:s.offset,length:c.length+1};if(s.isNew&&f.push(p),p.firstLine<=e&&e<=p.lastLine){var v=0!==s.offset?0:t.getBomOffset(h,i);a.decode(h.slice(v,p.length),function(i){var r=(p.lastLine,p.firstLine,i.split(t.splitLinesPattern));L||c.noLineEnding||(r=r.slice(0,r.length-1)),e!=p.firstLine&&(r=r.splice(e-p.firstLine)),n(void 0,e,r,L,u(p.offset+p.length),p)})}else{if(L)return n("Line "+e+" is out of index, last available: "+p.lastLine,e);s=t.getPlaceToStart(e,f),a.readChunk(s.offset,r,l)}})},t.readLines=function(e,n,i){if(0===n)return i(void 0,e,[],!1,0);var r=[];t.readSomeLines(e,function o(f,a,s,u,g,h){if(f)return i(f,e);var d=!!u&&a+s.length<=e+n;if(r=r.concat(s),r.length>=n||u){r=r.splice(0,n);return i(void 0,e,r,d,t.getProgress(h,e+r.length-1,l(h.offset+h.length)))}t.readSomeLines(a+s.length,o)})},t.find=function(e,n,i){t.readSomeLines(n,function n(r,o,f,a,s){if(r)return i(r);for(var l=0;l<f.length;l++){var u=t.searchInLine(e,f[l]);if(u)return i(void 0,o+l,u)}if(a)return i();t.readSomeLines(o+f.length+1,n)})},t.findAll=function(e,n,i,r){var o=[];t.readSomeLines(n,function f(a,s,l,u){if(a)return r(a,n);for(var g=0;g<l.length;g++){var h=t.searchInLine(e,l[g]);if(h&&(h.index=s+g,o.push(h),o.length>=i))return r(void 0,n,!0,o)}if(u)return r(void 0,n,!1,o);t.readSomeLines(s+l.length,f)})}}e.prototype.splitLinesPattern=/\r\n|\n|\r/,e.prototype.getProgress=function(e,n,t){var i=e.lastLine-e.firstLine+1,r=n-e.firstLine,o=n!==e.lastLine?e.offset+e.length/i*r:e.offset+e.length;return Math.floor(o/t*100)},e.prototype.searchInLine=function(e,n){var t=e.exec(n);return t?{offset:n.indexOf(t[0]),length:t[0].length,line:n}:null},e.prototype.getPlaceToStart=function(e,n){for(var t=n.length-1;t>=0;t--)if(n[t].lastLine<e)return{firstLine:n[t].lastLine+1,offset:n[t].offset+n[t].length,isNew:t===n.length-1};return{firstLine:0,offset:0,isNew:0===n.length}},e.prototype.getLineEnd=function(e,n,t,i){for(var r="\n".charCodeAt(0),o=("\r".charCodeAt(0),n);o<t;o++)if(e[o]===r)return o!==t&&0===e[o+1]?o+1:o},e.prototype.examineChunk=function(n,t,i){var r=0,o=0;do{var f=e.prototype.getLineEnd(n,o,t,i);void 0!==f&&(r++,o=f+1)}while(void 0!==f);return i&&(r++,o=t),o>0?{lines:r,length:o-1}:void 0};var n=[239,187,191],t=[255,254],i=function(e,n){for(var t=0;t<e.length&&t<n.length;t++){if(e[t]!==n[t])return!1;if(t==n.length-1)return!0}return!1};return e.prototype.getBomOffset=function(e,r){switch(r.toLowerCase()){case"utf8":return i(e,n)?n.length:0;case"utf16le":return i(e,t)?t.length:0;default:return 0}},e};if("undefined"!=typeof module&&module.exports)FileWrapper=require("./file-wrapper.min.js"),module.exports=getLineNavigatorClass();else if("function"==typeof define)define(["./file-wrapper.min"],function(e){return FileWrapper=e,getLineNavigatorClass()});else{if("undefined"==typeof FileWrapper)throw"For vanilla JS please add 'file-wrapper.min.js' script tag before this one.";LineNavigator=getLineNavigatorClass()}