UNPKG

newline-async-iterator

Version:

Line-by-line async iterator for the browser and node

3 lines (2 loc) 1.98 kB
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e="undefined"!=typeof globalThis?globalThis:e||self).newlineAsyncIterator=n()}(this,function(){"use strict";function e(e){return e<128?1:e<192?0:e<224?2:e<240?3:e<248?4:0}function n(e,n,r){var t;switch(r){case 1:t=e[n];break;case 2:t=(31&e[n])<<6|63&e[n+1];break;case 3:t=(15&e[n])<<12|(63&e[n+1])<<6|63&e[n+2];break;case 4:t=(7&e[n])<<18|(63&e[n+1])<<12|(63&e[n+2])<<6|63&e[n+3];break;default:return"�"}return t>65535?(t-=65536,String.fromCharCode(55296+(t>>10),56320+(1023&t))):String.fromCharCode(t)}function r(){var r=[];return function(t){var o=function(e){if("function"==typeof Array.from)return Array.from(e);for(var n=[],r=0;r<e.length;r++)n.push(e[r]);return n}(t),u=r.length>0?r.concat(o):o;r=[];for(var i="",f=0;f<u.length;){var a=e(u[f]);if(0!==a){if(f+a>u.length){r=u.slice(f);break}i+=n(u,f,a),f+=a}else i+="�",f++}return i}}function t(){return"undefined"!=typeof TextDecoder?(e=new TextDecoder("utf8"),function(n){return e.decode(n,{stream:!0})}):r();var e}var o=/\r?\n|\r/g,u="undefined"==typeof window?global:window,i=void 0===u.Symbol?{asyncIterator:void 0}:u.Symbol;return function(e){var n,r,u,f=t(),a=[],c="",l=!1,d=i.asyncIterator?e[i.asyncIterator]():e;function s(){return new Promise(function(e,n){d.next().then(function(r){r.done?l=!0:c+=f(r.value);var t=c.length>0?c[c.length-1]:"";if(l||"\r"!==t&&"\n"!==t){var u=c.split(o);c=u.pop(),u.forEach(function(e){a.unshift(e)}),l&&c.length>0&&(a.unshift(c),c="")}if(a.length>0){var i=a.pop();return l&&0===a.length&&0===i.length?e({value:null,done:!0}):e({value:i,done:!1})}if(l)return e({value:null,done:!0});s().then(e).catch(n)})})}return n={next:function(){return s()}},r=i.asyncIterator,u=function(){return this},r in n?Object.defineProperty(n,r,{value:u,enumerable:!0,configurable:!0,writable:!0}):n[r]=u,n}}); //# sourceMappingURL=newline-async-iterator.min.cjs.map