rewrite in javascript
This commit is contained in:
parent
b27c1f0ddb
commit
9feac88483
43 changed files with 2589 additions and 73 deletions
30
node_modules/firstline/index.js
generated
vendored
Normal file
30
node_modules/firstline/index.js
generated
vendored
Normal file
|
@ -0,0 +1,30 @@
|
|||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
module.exports = (path, usrOpts) => {
|
||||
const opts = {
|
||||
encoding: 'utf8',
|
||||
lineEnding: '\n'
|
||||
};
|
||||
Object.assign(opts, usrOpts);
|
||||
return new Promise((resolve, reject) => {
|
||||
const rs = fs.createReadStream(path, {encoding: opts.encoding});
|
||||
let acc = '';
|
||||
let pos = 0;
|
||||
let index;
|
||||
rs
|
||||
.on('data', chunk => {
|
||||
index = chunk.indexOf(opts.lineEnding);
|
||||
acc += chunk;
|
||||
if (index === -1) {
|
||||
pos += chunk.length;
|
||||
} else {
|
||||
pos += index;
|
||||
rs.close();
|
||||
}
|
||||
})
|
||||
.on('close', () => resolve(acc.slice(acc.charCodeAt(0) === 0xFEFF ? 1 : 0, pos)))
|
||||
.on('error', err => reject(err));
|
||||
});
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue