Skip to content

Commit

Permalink
Fix errors in isFile & isDirectory method, optimize codes
Browse files Browse the repository at this point in the history
  • Loading branch information
welefen committed Aug 16, 2017
1 parent 831f102 commit d74b8cc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 53 deletions.
78 changes: 26 additions & 52 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ exports.snakeCase = snakeCase;
* @return {Boolean} []
*/
function isNumberString(obj) {
if (!obj) {
return false;
}
if (!obj) return false;
return numberReg.test(obj);
}
exports.isNumberString = isNumberString;
Expand All @@ -161,12 +159,8 @@ exports.isNumberString = isNumberString;
* @return {Boolean} []
*/
function isTrueEmpty(obj) {
if (obj === undefined || obj === null || obj === '') {
return true;
}
if (exports.isNumber(obj) && isNaN(obj)) {
return true;
}
if (obj === undefined || obj === null || obj === '') return true;
if (exports.isNumber(obj) && isNaN(obj)) return true;
return false;
}
exports.isTrueEmpty = isTrueEmpty;
Expand All @@ -177,9 +171,7 @@ exports.isTrueEmpty = isTrueEmpty;
* @return {Boolean} []
*/
function isEmpty(obj) {
if (isTrueEmpty(obj)) {
return true;
}
if (isTrueEmpty(obj)) return true;
if (exports.isRegExp(obj)) {
return false;
} else if (exports.isDate(obj)) {
Expand Down Expand Up @@ -224,9 +216,7 @@ exports.defer = defer;
* @return {String} [content md5]
*/
function md5(str) {
const instance = crypto.createHash('md5');
instance.update(str + '', 'utf8');
return instance.digest('hex');
return crypto.createHash('md5').update(str + '', 'utf8').digest('hex');
}
exports.md5 = md5;

Expand Down Expand Up @@ -293,9 +283,7 @@ exports.datetime = datetime;
* @return {String} []
*/
exports.uuid = function(version) {
if (version === 'v1') {
return uuid.v1();
}
if (version === 'v1') return uuid.v1();
return uuid.v4();
};

Expand Down Expand Up @@ -343,9 +331,7 @@ exports.parseAdapterConfig = (config = {}, ...extConfig) => {
* transform humanize time to ms
*/
exports.ms = function(time) {
if (typeof time === 'number') {
return time;
}
if (typeof time === 'number') return time;
const result = ms(time);
if (result === undefined) {
throw new Error(`think-ms('${time}') result is undefined`);
Expand Down Expand Up @@ -375,7 +361,6 @@ exports.omit = function(obj, props) {
*/
function isExist(dir) {
dir = path.normalize(dir);

try {
fs.accessSync(dir, fs.R_OK);
return true;
Expand All @@ -390,23 +375,27 @@ exports.isExist = isExist;
* check filepath is file
*/
function isFile(filePath) {
if (!isExist(filePath)) {
if (!isExist(filePath)) return false;
try {
const stat = fs.statSync(filePath);
return stat.isFile();
} catch (e) {
return false;
}
const stat = fs.statSync(filePath);
return stat.isFile();
}
exports.isFile = isFile;

/**
* check path is directory
*/
function isDirectory(filePath) {
if (!isExist(filePath)) {
if (!isExist(filePath)) return false;
try {
const stat = fs.statSync(filePath);
return stat.isDirectory();
} catch (e) {
return false;
}
const stat = fs.statSync(filePath);
return stat.isDirectory();
}
exports.isDirectory = isDirectory;

Expand All @@ -417,25 +406,21 @@ exports.isDirectory = isDirectory;
* @return {Boolean} []
*/
function chmod(p, mode = '0777') {
if (!isExist(p)) {
return false;
}
if (!isExist(p)) return false;
try {
fs.chmodSync(p, mode);
return true;
} catch (e) {
return false;
}
return true;
}
exports.chmod = chmod;

/**
* make dir
*/
function mkdir(dir, mode = '0777') {
if (isExist(dir)) {
return chmod(dir, mode);
}
if (isExist(dir)) return chmod(dir, mode);
const pp = path.dirname(dir);
if (isExist(pp)) {
try {
Expand All @@ -445,11 +430,8 @@ function mkdir(dir, mode = '0777') {
return false;
}
}
if (mkdir(pp, mode)) {
return mkdir(dir, mode);
} else {
return false;
}
if (mkdir(pp, mode)) return mkdir(dir, mode);
return false;
}
exports.mkdir = mkdir;

Expand All @@ -461,9 +443,7 @@ exports.mkdir = mkdir;
*/
function getdirFiles(dir, prefix = '') {
dir = path.normalize(dir);
if (!fs.existsSync(dir)) {
return [];
}
if (!fs.existsSync(dir)) return [];
const files = fs.readdirSync(dir);
let result = [];
files.forEach(item => {
Expand All @@ -488,21 +468,15 @@ exports.getdirFiles = getdirFiles;
* @return {Promise} []
*/
function rmdir(p, reserve) {
if (!isDirectory(p)) {
return Promise.resolve();
}
if (!isDirectory(p)) return Promise.resolve();
return fsReaddir(p).then(files => {
const promises = files.map(item => {
const filepath = path.join(p, item);
if (isDirectory(filepath)) {
return rmdir(filepath, false);
}
if (isDirectory(filepath)) return rmdir(filepath, false);
return fsUnlink(filepath);
});
return Promise.all(promises).then(() => {
if (!reserve) {
return fsRmdir(p);
}
if (!reserve) return fsRmdir(p);
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ test('isBuffer', t => {
});

test('isBuffer 2', t => {
var value = isBuffer(Buffer.alloc('test'));
var value = isBuffer(Buffer.from('test'));
t.deepEqual(value, true);
});

Expand Down

0 comments on commit d74b8cc

Please sign in to comment.