package/package.json000644 000765 000024 0000001170 13040016120012777 0ustar00000000 000000 { "name": "fs-exists-cached", "version": "1.0.0", "description": "Just like `fs.exists` and `fs.existsSync`, but cached", "main": "index.js", "scripts": { "test": "tap test.js --100" }, "devDependencies": { "tap": "9 || 10" }, "repository": { "type": "git", "url": "git+https://github.com/isaacs/fs-exists-cached.git" }, "keywords": [], "author": "Isaac Z. Schlueter (http://blog.izs.me/)", "license": "ISC", "bugs": { "url": "https://github.com/isaacs/fs-exists-cached/issues" }, "homepage": "https://github.com/isaacs/fs-exists-cached#readme", "dependencies": {} } package/.npmignore000644 000765 000024 0000000031 13040016237012514 0ustar00000000 000000 .nyc_output node_modules package/README.md000644 000765 000024 0000000112 13040015117011770 0ustar00000000 000000 # fs-exists-cached Just like `fs.exists` and `fs.existsSync`, but cached package/index.js000644 000765 000024 0000001003 13040015600012153 0ustar00000000 000000 module.exports = exists exists.sync = sync var fs = require('fs') var existsCache = Object.create(null) function exists (file, cb) { if (file in existsCache) return process.nextTick(cb.bind(null, existsCache[file])) fs.lstat(file, function (er) { cb(existsCache[file] = !er) }) } function sync (file) { if (file in existsCache) return existsCache[file] try { fs.lstatSync(file) existsCache[file] = true } catch (er) { existsCache[file] = false } return existsCache[file] } package/test.js000644 000765 000024 0000002274 13040016223012040 0ustar00000000 000000 var exists = require('./') var t = require('tap') var touch = require('touch') var rimraf = require('rimraf') t.test('setup', function (t) { touch.sync('one') touch.sync('two') touch.sync('three') touch.sync('four') t.end() }) t.test('existing file same way', function (t) { t.plan(4) t.ok(exists.sync('one')) t.ok(exists.sync('one')) exists('two', function (e) { t.ok(e) exists('two', t.ok) }) }) t.test('existing file different ways', function (t) { t.plan(4) t.ok(exists.sync('three')) t.ok(exists.sync('four')) exists('three', function (e) { t.ok(e) exists('four', t.ok) }) }) t.test('non-existing file same way', function (t) { t.plan(4) t.notOk(exists.sync('one-no')) t.notOk(exists.sync('one-no')) exists('two-no', function (e) { t.notOk(e) exists('two-no', t.notOk) }) }) t.test('non-existing file different ways', function (t) { t.plan(4) t.notOk(exists.sync('three-no')) t.notOk(exists.sync('four-no')) exists('three-no', function (e) { t.notOk(e) exists('four-no', t.notOk) }) }) t.test('cleanup', function (t) { rimraf.sync('one') rimraf.sync('two') rimraf.sync('three') rimraf.sync('four') t.end() })