{"version":3,"file":"index.umd.production.min.cjs","sources":["../src/util.ts","../src/index.ts"],"sourcesContent":["import { INewLineCharacter } from './types';\nimport { EnumNewLineCharacter } from './index';\n\nexport function handleOptionNewLineCharacter(newLineCharacter?: INewLineCharacter): number[]\n{\n\tif (!newLineCharacter)\n\t{\n\t\treturn [EnumNewLineCharacter.LF];\n\t}\n\telse if (typeof newLineCharacter !== 'number')\n\t{\n\t\tconst ls = [] as number[];\n\n\t\tfor (let i = 0; i < newLineCharacter.length; i++)\n\t\t{\n\t\t\tconst c = newLineCharacter[i];\n\n\t\t\tif (typeof c !== 'number')\n\t\t\t{\n\t\t\t\tls.push(c.charCodeAt(0))\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tls.push(c)\n\t\t\t}\n\t\t}\n\n\t\treturn ls\n\t}\n\n\treturn [newLineCharacter]\n}\n\n","/**\n * Created by user on 2020/5/29.\n */\n\nexport * from './types';\nexport * from './util';\nimport { IPathLike, IOptions } from './types';\nimport { readSync, closeSync, openSync } from 'fs';\nimport {\n\thandleOptionNewLineCharacter,\n} from './util';\nimport {\n\tbufferEndWithByBuffer,\n\tbufferStripEndWithByBuffer,\n\tsplitBufferByBuffer,\n} from '@lazy-node/util-buffer';\n\nexport const enum EnumNewLineCharacter\n{\n\tLF = 0x0a,\n\tCR = 0x0d,\n}\n\nexport class LineByLine\n{\n\t#file: IPathLike;\n\t#fd: number;\n\t#options: IOptions;\n\treadonly #newLineCharacter: Buffer;\n\t#eofReached: boolean;\n\t#linesCache: Buffer[];\n\t#fdPosition: number;\n\t#readChunk: number;\n\t#lineNumber: number = -1;\n\n\tconstructor(file: IPathLike, options?: IOptions)\n\t{\n\t\tlet { readChunk, newLineCharacter } = (options = options || {});\n\n\t\tif (!readChunk) readChunk = 1024;\n\n\t\tthis.#file = file;\n\t\tthis.#options = options;\n\n\t\tthis.#newLineCharacter = Buffer.from(handleOptionNewLineCharacter(newLineCharacter));\n\t\tthis.#readChunk = readChunk;\n\n\t\tif (this.#newLineCharacter.length <= 0)\n\t\t{\n\t\t\tthrow new TypeError(`newLineCharacter should have length > 0`)\n\t\t}\n\n\t\tthis._open();\n\t\tthis.reset();\n\t}\n\n\tprotected _open()\n\t{\n\t\tif (typeof this.#file === 'number')\n\t\t{\n\t\t\tthis.#fd = this.#file;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tthis.#fd = openSync(this.#file, 'r');\n\t\t}\n\t}\n\n\tget file()\n\t{\n\t\treturn this.#file\n\t}\n\n\tget fd()\n\t{\n\t\treturn this.#fd\n\t}\n\n\tget options()\n\t{\n\t\treturn this.#options\n\t}\n\n\tget fdPosition()\n\t{\n\t\treturn this.#fdPosition\n\t}\n\n\tget newLineCharacter()\n\t{\n\t\treturn this.#newLineCharacter\n\t}\n\n\tget eofReached()\n\t{\n\t\treturn this.#eofReached\n\t}\n\n\t/**\n\t * get current lineNumber\n\t */\n\tget lineNumber()\n\t{\n\t\treturn this.#lineNumber\n\t}\n\n\treset()\n\t{\n\t\tthis.#eofReached = false;\n\t\tthis.#linesCache = [];\n\t\tthis.#fdPosition = 0;\n\t\tthis.#lineNumber = -1;\n\t}\n\n\tclose()\n\t{\n\t\tthis.#fd && closeSync(this.#fd);\n\t\tthis.#fd = null;\n\t}\n\n\tprotected _extractLines(buffer: Buffer)\n\t{\n\t\treturn splitBufferByBuffer(buffer, this.#newLineCharacter as any)\n\t};\n\n\tprotected _readChunk(lineLeftovers?: Buffer)\n\t{\n\t\tlet totalBytesRead = 0;\n\n\t\tlet bytesRead: number;\n\t\tconst buffers: Buffer[] = [];\n\t\tdo\n\t\t{\n\t\t\tconst readBuffer = Buffer.alloc(this.#readChunk);\n\n\t\t\tbytesRead = readSync(this.#fd, readBuffer as any, 0, this.#readChunk, this.#fdPosition);\n\t\t\ttotalBytesRead = totalBytesRead + bytesRead;\n\n\t\t\tthis.#fdPosition = this.#fdPosition + bytesRead;\n\n\t\t\tbuffers.push(readBuffer);\n\t\t}\n\t\twhile (bytesRead && buffers[buffers.length - 1].indexOf(this.#newLineCharacter as any) === -1);\n\n\t\tlet bufferData = Buffer.concat(buffers as any);\n\n\t\tif (bytesRead < this.#readChunk)\n\t\t{\n\t\t\tthis.#eofReached = true;\n\t\t\tbufferData = bufferData.slice(0, totalBytesRead);\n\t\t}\n\n\t\tif (totalBytesRead)\n\t\t{\n\t\t\tthis.#linesCache = this._extractLines(bufferData);\n\n\t\t\tif (lineLeftovers)\n\t\t\t{\n\t\t\t\tthis.#linesCache[0] = Buffer.concat([lineLeftovers as any, this.#linesCache[0] as any]);\n\t\t\t}\n\t\t}\n\n\t\treturn totalBytesRead;\n\t}\n\n\tnext()\n\t{\n\t\tif (!this.#fd || this.#eofReached && this.#linesCache.length === 0)\n\t\t{\n\t\t\treturn;\n\t\t}\n\n\t\tlet line: Buffer;\n\t\tlet bytesRead: number;\n\n\t\tif (!this.#linesCache.length)\n\t\t{\n\t\t\tbytesRead = this._readChunk();\n\t\t}\n\n\t\tif (this.#linesCache.length)\n\t\t{\n\t\t\tline = this.#linesCache.shift();\n\n\t\t\t//const lastLineCharacter = line[line.length - 1];\n\n\t\t\tif (!bufferEndWithByBuffer(line, this.#newLineCharacter))\n\t\t\t{\n\t\t\t\tbytesRead = this._readChunk(line);\n\n\t\t\t\tif (bytesRead)\n\t\t\t\t{\n\t\t\t\t\tline = this.#linesCache.shift();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (this.#eofReached && this.#linesCache.length === 0)\n\t\t{\n\t\t\tthis.close();\n\t\t}\n\n\t\tif (line)\n\t\t{\n\t\t\tline = bufferStripEndWithByBuffer(line, this.#newLineCharacter, 0 - this.#newLineCharacter.length)\n\t\t}\n\n\t\tthis.#lineNumber++;\n\n\t\treturn line;\n\t}\n\n\t* generator()\n\t{\n\t\tlet line: Buffer;\n\t\twhile (line = this.next())\n\t\t{\n\t\t\tyield line\n\t\t}\n\t}\n\n\tstatic* generator(file: IPathLike, options?: IOptions)\n\t{\n\t\tconst liner = new this(file, options)\n\t\tyield* liner.generator()\n\t}\n}\n\n// @ts-ignore\nif (process.env.TSDX_FORMAT !== 'esm')\n{\n\tObject.defineProperty(LineByLine, \"__esModule\", { value: true });\n\n\tObject.defineProperty(LineByLine, 'LineByLine', { value: LineByLine });\n\tObject.defineProperty(LineByLine, 'default', { value: LineByLine });\n\n\tObject.defineProperty(LineByLine, \"handleOptionNewLineCharacter\", { value: handleOptionNewLineCharacter });\n\n\t// @ts-ignore\n\tObject.defineProperty(LineByLine, \"EnumNewLineCharacter\", { value: EnumNewLineCharacter });\n}\n\nexport default LineByLine\n"],"names":["handleOptionNewLineCharacter","newLineCharacter","ls","i","length","c","push","charCodeAt","EnumNewLineCharacter","exports","LineByLine","file","fd","options","eofReached","linesCache","fdPosition","readChunk","lineNumber","constructor","this","Buffer","from","TypeError","_open","reset","openSync","close","closeSync","_extractLines","buffer","splitBufferByBuffer","_readChunk","lineLeftovers","bytesRead","totalBytesRead","buffers","readBuffer","alloc","readSync","indexOf","bufferData","concat","slice","next","line","shift","bufferEndWithByBuffer","bufferStripEndWithByBuffer","generator","liner","Object","defineProperty","value"],"mappings":";;;;EAGM,SAAUA,6BAA6BC;IAE5C,KAAKA,GAEJ,OAAO;IAEH,IAAgC,mBAArBA,GAChB;MACC,MAAMC,IAAK;MAEX,KAAK,IAAIC,IAAI,GAAGA,IAAIF,EAAiBG,QAAQD,KAC7C;QACC,MAAME,IAAIJ,EAAiBE;QAI1BD,EAAGI,KAFa,mBAAND,IAEFA,EAAEE,WAAW,KAIbF;AAEV;MAEA,OAAOH;AACR;IAEA,OAAO,EAACD;AACT;ECdA,IAAkBO;EAIjBC,EAAAD,4BAAA,IAJiBA,IAAAA,2BAAAA,EAAAA,uBAIjB,CAAA,IAFAA,EAAA,KAAA,MAAA;EACAA,EAAAA,EAAA,KAAA,MAAA;QAGYE;IAEZC;IACAC;IACAC;IACSZ;IACTa;IACAC;IACAC;IACAC;IACAC,IAAuB;IAEvBC,WAAAA,CAAYR,GAAiBE;M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