is-regular-file
Advanced tools
Comparing version 1.0.1 to 1.1.0
16
index.js
@@ -21,2 +21,18 @@ 'use strict'; | ||
function isRegularFileSync(path) { | ||
try { | ||
const stats = fs.statSync(path); | ||
return stats.isFile(); | ||
} catch (err) { | ||
if (err.code === 'ENOENT') { | ||
return false; | ||
} | ||
throw err; | ||
} | ||
} | ||
module.exports = isRegularFile; | ||
module.exports.sync = isRegularFileSync; |
{ | ||
"name": "is-regular-file", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Checks if a given path is a regular file", | ||
"main": "index.js", | ||
"scripts": { | ||
"lint": "eslint '{*.js,lib/**/*.js,reporters/**/*.js,test/**/*.js}' --ignore-pattern=test/coverage", | ||
"lint": "eslint '{*.js,test/**/*.js}' --ignore-pattern=test/coverage", | ||
"test": "mocha --bail", | ||
@@ -27,10 +27,10 @@ "test-cov": "istanbul cover --dir test/coverage _mocha -- --bail && echo Coverage lies in test/coverage/lcov-report/index.html", | ||
"devDependencies": { | ||
"@satazor/eslint-config": "^2.3.0", | ||
"@satazor/eslint-config": "^3.0.0", | ||
"betray": "^1.3.0", | ||
"chai": "^3.5.0", | ||
"chai": "^4.0.2", | ||
"coveralls": "^2.11.9", | ||
"eslint": "^2.9.0", | ||
"eslint": "^3.0.0", | ||
"istanbul": "^0.4.3", | ||
"mkdirp": "^0.5.1", | ||
"mocha": "^2.4.5", | ||
"mocha": "^3.2.0", | ||
"rimraf": "^2.5.2" | ||
@@ -37,0 +37,0 @@ }, |
# is-regular-file | ||
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url] | ||
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url] [![Greenkeeper badge][greenkeeper-image]][greenkeeper-url] | ||
@@ -14,4 +14,6 @@ [npm-url]:https://npmjs.org/package/is-regular-file | ||
[david-dm-image]:https://img.shields.io/david/IndigoUnited/node-is-regular-file.svg | ||
[david-dm-dev-url]:https://david-dm.org/IndigoUnited/node-is-regular-file#info=devDependencies | ||
[david-dm-dev-url]:https://david-dm.org/IndigoUnited/node-is-regular-file?type=dev | ||
[david-dm-dev-image]:https://img.shields.io/david/dev/IndigoUnited/node-is-regular-file.svg | ||
[greenkeeper-image]:https://badges.greenkeeper.io/IndigoUnited/node-is-regular-file.svg | ||
[greenkeeper-url]:https://greenkeeper.io/ | ||
@@ -32,7 +34,15 @@ Checks if a path is a regular file. | ||
isRegularFile('path-to-file') | ||
.then((is) => console.log('result', is)) | ||
.catch((err) => console.log('error', err)); | ||
.then((is) => console.log('result', is)); | ||
``` | ||
or if you prefer sync: | ||
```js | ||
const isRegularFileSync = require('is-regular-file').sync; | ||
const is = isRegularFileSync('path-to-file') | ||
console.log('result', is); | ||
``` | ||
## Tests | ||
@@ -39,0 +49,0 @@ |
@@ -9,6 +9,7 @@ 'use strict'; | ||
const isRegularFile = require('../'); | ||
const isRegularFileSync = isRegularFile.sync; | ||
const tmpDir = `${__dirname}/tmp`; | ||
describe('is-regular-file', () => { | ||
describe('is-regular-file async', () => { | ||
before(() => rimraf.sync(tmpDir)); | ||
@@ -18,3 +19,3 @@ beforeEach(() => mkdirp.sync(tmpDir)); | ||
it('should return the `stats` object for regular files', () => { | ||
it('should return the true for regular files', () => { | ||
fs.writeFileSync(`${tmpDir}/some-file`, 'zzzz'); | ||
@@ -53,1 +54,38 @@ | ||
}); | ||
describe('is-regular-file sync', () => { | ||
before(() => rimraf.sync(tmpDir)); | ||
beforeEach(() => mkdirp.sync(tmpDir)); | ||
afterEach(() => rimraf.sync(tmpDir)); | ||
it('should return the true for regular files', () => { | ||
fs.writeFileSync(`${tmpDir}/some-file`, 'zzzz'); | ||
expect(isRegularFileSync(`${tmpDir}/some-file`)).to.equal(true); | ||
}); | ||
it('should return false for directories', () => { | ||
fs.mkdirSync(`${tmpDir}/dir`); | ||
expect(isRegularFileSync(`${tmpDir}/dir`)).to.equal(false); | ||
}); | ||
it('should return false if it doesn\'t exist', () => { | ||
expect(isRegularFileSync(`${tmpDir}/foo`)).to.equal(false); | ||
}); | ||
it('should throw an error if code is different than ENOENT', () => { | ||
fs.writeFileSync(`${tmpDir}/error-file`, 'zzzz'); | ||
const betrayed = betray(fs, 'statSync', () => { throw new Error('foo'); }); | ||
try { | ||
isRegularFileSync(`${tmpDir}/error-file`); | ||
throw new Error('Should have failed'); | ||
} catch (err) { | ||
expect(err.message).to.equal('foo'); | ||
} finally { | ||
betrayed.restore(); | ||
} | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
8321
109
55