list-contents
Advanced tools
Comparing version 3.0.0 to 3.1.0
23
index.js
@@ -0,1 +1,3 @@ | ||
/* global Function */ | ||
const fs = require('fs'); | ||
@@ -6,17 +8,24 @@ const path = require('path'); | ||
const moveOn = require('move-on'); | ||
const type = require('of-type'); | ||
const error = cliColor.red; | ||
module.exports = function(p,callback){ | ||
args(arguments,['string','function'],(o)=>{ | ||
module.exports = function(p,b,c){ | ||
const types = type(c,Function) ? [String,[Number,Object,null],Function]:type(b,Function) ? [String,Function]:[String,[Number,Object,Function,null],Function]; | ||
args(arguments,types,(o)=>{ | ||
var err = new TypeError(error(o.message)); | ||
throw err; | ||
}); | ||
const getDeep = type(b,Number) ? b:type(b,Object) ? type(b.deep,Number) ? b.deep:0:0; | ||
const getCallback = type(b,Function) ? b:c; | ||
const getPath = path.resolve(p); | ||
var userContext = {dirs:[], files:[], inaccessible:[], path:getPath}; | ||
moveOn([explore],userContext,callback,()=>{}); | ||
moveOn([explore],userContext,getCallback,()=>{}); | ||
function explore(resolve,reject,r){ | ||
var relative = typeof r==='undefined' ? './':r; | ||
function explore(resolve,reject,r,l){ | ||
var relative = typeof r === 'undefined' ? './':r; | ||
var absolute = path.join(this.path,relative); | ||
var deep = typeof l === 'undefined' ? 0:l; | ||
var nextDeep = deep + 1; | ||
fs.readdir(absolute,(err,contents)=>{ | ||
@@ -32,4 +41,4 @@ if(err){ | ||
checkItem.call(this,relative,contents[i],(isDir,relative)=>{ | ||
if(isDir) explore.call(this,iter,reject,relative); | ||
if(!isDir) iter(); | ||
if(isDir&&nextDeep!==getDeep) return explore.call(this,iter,reject,relative,nextDeep); | ||
iter(); | ||
},resolve); | ||
@@ -36,0 +45,0 @@ } |
{ | ||
"name": "list-contents", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Returns a list of paths to the subfolders and subfiles of the specified location.", | ||
@@ -23,2 +23,3 @@ "main": "index.js", | ||
"move-on": "^1.0.0", | ||
"of-type": "^2.1.0", | ||
"typeof-arguments": "^3.0.1" | ||
@@ -25,0 +26,0 @@ }, |
@@ -16,3 +16,3 @@ # Description | ||
# Usage | ||
### `list(path,callback)` | ||
### `list(path,[config,]callback)` | ||
##### `path` **[String]** | ||
@@ -23,2 +23,22 @@ * It should indicate the path to the chosen directory, which subfolders and subfiles should be listed | ||
##### `config` **[Object|Number|null]** | ||
* if **omitted**, the parameters are set to their default values *(see below)* | ||
* if [Object], it takes the following properties: | ||
* **`deep`** [Number|null] *(default:`null`)* | ||
It indicates how deep the `list-contents` should explore the folders in the given `path` directory. | ||
If set to `null` *(default)* it lists all subfiles and subfolders of all levels of the `path` directory. | ||
If set to `1` it lists only the folders and files of the `path` directory. | ||
If set to `2` it lists the elements of the `path` directory and the contents of the `path` directory's folders. | ||
etc. | ||
* if [Number], it sets `deep` to `[Number]` | ||
* if [null], it sets `deep` to `null` | ||
```javascript | ||
const listContents = require('list-contents'); | ||
listContents('./dist', (data)=>{/*...*/}); | ||
listContents('./dist', null, (data)=>{/*...*/}); | ||
listContents('./dist', 3, (data)=>{/*...*/}); | ||
listContents('./dist', {deep: 5}, (data)=>{/*...*/}); | ||
``` | ||
##### `callback` **[Function]** | ||
@@ -25,0 +45,0 @@ * the [Object] argument is passed through **`callback`** function. It has 4 properties: |
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
8513
62
108
4
+ Addedof-type@^2.1.0