+34
-6
@@ -5,3 +5,3 @@ const fs = require('fs'); | ||
| class getFile { | ||
| constructor(root,suffix) { | ||
| constructor(root, suffix) { | ||
| this.root = root; | ||
@@ -36,10 +36,13 @@ this.arr = []; | ||
| // const _this = this; | ||
| const dir = opation.root || this.root; | ||
| const dir = opation.root || this.root; | ||
| const suffix = opation.suffix || this.suffix; | ||
| return this.hasFile(dir).then(stats => { | ||
| if (stats.isDirectory()) { | ||
| // 如果是目录的话,则递归 | ||
| // 如果是目录的话,则递归 | ||
| const result = this.getDir(dir).then(list => | ||
| Promise.all(list.map(item => | ||
| this.getResult({ | ||
| root: dir, | ||
| suffix: suffix, | ||
| root: path.resolve(dir, item) | ||
@@ -51,8 +54,11 @@ }) | ||
| return opation.callback ? | ||
| result.then(()=>opation.callback(this.arr) ) | ||
| : result; | ||
| result.then(() => opation.callback(this.arr)) : | ||
| result; | ||
| } else { | ||
| // 如果不是目录 | ||
| if (dir.indexOf(`.${this.suffix}`) >= 0) { | ||
| // if (dir.split('.').pop() === suffix) { | ||
| // this.arr.push(dir); | ||
| // } | ||
| if (this.judgeSuffix(dir, suffix)) { | ||
| this.arr.push(dir); | ||
@@ -63,3 +69,25 @@ } | ||
| } | ||
| judgeSuffix(dir, suffix) { | ||
| if (Object.prototype.toString.call(suffix) === '[object Array]') { | ||
| for (var item of suffix) { | ||
| if (dir.split('.').pop() === item) { | ||
| return true; | ||
| } | ||
| } | ||
| } else if (Object.prototype.toString.call(suffix) === '[object String]') { | ||
| if (dir.split('.').pop() === suffix) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // const A = new getFile('demo','js'); | ||
| // | ||
| // A.getResult({ | ||
| // callback: function(arr){ | ||
| // console.log(arr) | ||
| // } | ||
| // }); | ||
| module.exports = getFile; |
+1
-1
| { | ||
| "name": "getfiles", | ||
| "version": "1.0.0", | ||
| "version": "1.1.0", | ||
| "description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+41
-1
| ### 说明 | ||
| 利用promise简单实现了异步获取指定目录下,指定文件的功能 | ||
| ## 用法1 | ||
| ``` javascript | ||
| const getFile = require('./app/getFile'); | ||
| const getFile = require('getFiles'); | ||
@@ -17,1 +19,39 @@ const A = new getFile('demo','js'); | ||
| ``` | ||
| ## 用法2 | ||
| ``` javascript | ||
| const getFile = require('getFiles'); | ||
| const A = new getFile('demo'); | ||
| A.getResult({ | ||
| suffix:['js','css'], | ||
| callback: function(arr){ | ||
| console.log(arr) | ||
| } | ||
| }); | ||
| ``` | ||
| ## 用法3 | ||
| ``` javascript | ||
| const getFile = require('getFiles'); | ||
| const A = new getFile(); | ||
| A.getResult({ | ||
| root:'demo', | ||
| suffix:['js','css'], | ||
| callback: function(arr){ | ||
| console.log(arr) | ||
| } | ||
| }); | ||
| ``` | ||
| 简单地说,可以构建实例的时候就确定参数,也可以构建一个实例,在调用的时候在传参。已getResult入参为准 |
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
3625
64.55%84
44.83%57
235.29%1
Infinity%