Comparing version 0.3.1 to 0.3.2
32
index.js
@@ -5,2 +5,10 @@ 'use strict'; | ||
function arrify(a) { | ||
if (!Array.isArray(a)) { | ||
a = [a]; | ||
} | ||
return a.slice(0); | ||
} | ||
function findone(targets, cb) { | ||
@@ -13,4 +21,4 @@ if (targets.length === 0) { | ||
var target = targets.shift(); | ||
fs.stat(target, function (err) { | ||
if (err) { | ||
fs.stat(target, function (err, stat) { | ||
if (err || !stat.isFile()) { | ||
findone(targets, cb); | ||
@@ -24,8 +32,20 @@ } else { | ||
function findoneSync(targets) { | ||
for (var i = 0; i < targets.length; ++i) { | ||
try { | ||
if (fs.statSync(targets[i]).isFile()) { | ||
return targets[i]; | ||
} | ||
} catch (e) {} | ||
} | ||
return null; | ||
} | ||
function afile(targets, cb) { | ||
if (!Array.isArray(targets)) { | ||
targets = [targets]; | ||
if (cb) { | ||
return findone(arrify(targets), cb); | ||
} | ||
findone(targets.slice(0), cb); | ||
return findoneSync(arrify(targets)); | ||
} | ||
@@ -45,2 +65,2 @@ | ||
module.exports.cb = afile; | ||
module.exports.cb = module.exports.sync = afile; |
{ | ||
"name": "afile", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "Find a file on the list", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -34,6 +34,11 @@ # afile [![Build Status](https://travis-ci.org/ragingwind/afile.svg?branch=master)](https://travis-ci.org/ragingwind/afile) | ||
// callback style | ||
afile(targets, f => { | ||
afile.cb(targets, f => { | ||
console.log(f); | ||
//=> /home/yours/.bowerrc | ||
}); | ||
// sync style | ||
afile.sync(targets); | ||
//=> /home/yours/.bowerrc | ||
}); | ||
``` | ||
@@ -50,6 +55,10 @@ | ||
callback style, will pass with target file or not. | ||
callback style, will pass with target file path or not. | ||
### afile.sync(targets) | ||
return target file path or null. | ||
## License | ||
MIT © [Jimmy Moon](http://ragingwind.me) |
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
3599
51
63