Comparing version 0.4.0 to 0.4.1
65
index.js
@@ -7,2 +7,3 @@ 'use strict'; | ||
var path = require('path'); | ||
var readline = require('readline'); | ||
@@ -239,4 +240,4 @@ /** | ||
const extname = (filePath) => { | ||
if(!filePath) return null; | ||
if (!filePath) return null; | ||
return path.extname(filePath.toLowerCase()); | ||
@@ -252,11 +253,11 @@ }; | ||
// check | ||
if(!filePath) return; | ||
if (!filePath) return; | ||
try{ | ||
try { | ||
// opt | ||
const opt = {encoding:'utf8'}; | ||
const opt = { encoding: 'utf8' }; | ||
options = options || opt; | ||
return fs.readFileSync(filePath, options); | ||
}catch(e){ | ||
} catch (e) { | ||
console.log(e); | ||
@@ -268,2 +269,42 @@ return; | ||
/** | ||
* readFileLineByLine | ||
* @param {*} filePath | ||
* @param {*} onLine | ||
* @param {*} onClose | ||
*/ | ||
const readFileLineByLine = (filePath, onLine, onClose) => { | ||
// rl | ||
const rl = readline.createInterface({ | ||
input: fs.createReadStream(filePath, { encoding: 'utf8' }) | ||
}); | ||
// on | ||
rl.on('line', function (line) { | ||
if (onLine) onLine(line); | ||
}); | ||
rl.on('close', function () { | ||
if (onClose) onClose(); | ||
}); | ||
}; | ||
/** | ||
* readFileLineByLineSync | ||
* @param {*} filePath | ||
* @returns | ||
*/ | ||
const readFileLineByLineSync = (filePath) => { | ||
return new Promise((resolve, reject) => { | ||
// lines | ||
let lines = []; | ||
readFileLineByLine(filePath, line => { | ||
lines.push(line); | ||
}, () => { | ||
resolve(lines); | ||
lines = null; | ||
}); | ||
}); | ||
}; | ||
/** | ||
* writeFile | ||
@@ -276,5 +317,5 @@ * @param {*} filePath | ||
// check | ||
if(!filePath) return; | ||
if (!filePath) return; | ||
try{ | ||
try { | ||
// vars | ||
@@ -284,5 +325,5 @@ fileData = fileData || ''; | ||
fs.writeFileSync(filePath, fileData, options); | ||
return true; | ||
}catch(e){ | ||
} catch (e) { | ||
console.log(e); | ||
@@ -320,4 +361,6 @@ return false; | ||
exports.readFile = readFile; | ||
exports.readFileLineByLine = readFileLineByLine; | ||
exports.readFileLineByLineSync = readFileLineByLineSync; | ||
exports.rm = rm; | ||
exports.writeFile = writeFile; | ||
exports.writeFileFromLines = writeFileFromLines; |
{ | ||
"name": "qiao-file", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "nodejs file tool", | ||
@@ -17,3 +17,3 @@ "author": "uikoo9 <uikoo9@qq.com>", | ||
}, | ||
"gitHead": "0dc0049744fb462a3734126176c7239cda3f64aa" | ||
"gitHead": "5cea678c8ca3182b7f445a7c890bc22d7edd1546" | ||
} |
@@ -9,2 +9,5 @@ 'use strict'; | ||
// readline | ||
import readline from 'readline'; | ||
/** | ||
@@ -15,4 +18,4 @@ * extname | ||
export const extname = (filePath) => { | ||
if(!filePath) return null; | ||
if (!filePath) return null; | ||
return path.extname(filePath.toLowerCase()); | ||
@@ -28,11 +31,11 @@ }; | ||
// check | ||
if(!filePath) return; | ||
if (!filePath) return; | ||
try{ | ||
try { | ||
// opt | ||
const opt = {encoding:'utf8'}; | ||
const opt = { encoding: 'utf8' }; | ||
options = options || opt; | ||
return fs.readFileSync(filePath, options); | ||
}catch(e){ | ||
} catch (e) { | ||
console.log(e); | ||
@@ -44,2 +47,42 @@ return; | ||
/** | ||
* readFileLineByLine | ||
* @param {*} filePath | ||
* @param {*} onLine | ||
* @param {*} onClose | ||
*/ | ||
export const readFileLineByLine = (filePath, onLine, onClose) => { | ||
// rl | ||
const rl = readline.createInterface({ | ||
input: fs.createReadStream(filePath, { encoding: 'utf8' }) | ||
}); | ||
// on | ||
rl.on('line', function (line) { | ||
if (onLine) onLine(line); | ||
}); | ||
rl.on('close', function () { | ||
if (onClose) onClose(); | ||
}); | ||
}; | ||
/** | ||
* readFileLineByLineSync | ||
* @param {*} filePath | ||
* @returns | ||
*/ | ||
export const readFileLineByLineSync = (filePath) => { | ||
return new Promise((resolve, reject) => { | ||
// lines | ||
let lines = []; | ||
readFileLineByLine(filePath, line => { | ||
lines.push(line); | ||
}, () => { | ||
resolve(lines); | ||
lines = null; | ||
}); | ||
}); | ||
}; | ||
/** | ||
* writeFile | ||
@@ -52,5 +95,5 @@ * @param {*} filePath | ||
// check | ||
if(!filePath) return; | ||
if (!filePath) return; | ||
try{ | ||
try { | ||
// vars | ||
@@ -60,5 +103,5 @@ fileData = fileData || ''; | ||
fs.writeFileSync(filePath, fileData, options); | ||
return true; | ||
}catch(e){ | ||
} catch (e) { | ||
console.log(e); | ||
@@ -65,0 +108,0 @@ return false; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21016
722