Skip to content
This repository has been archived by the owner on Mar 8, 2021. It is now read-only.

Commit

Permalink
Add the ES6 Map's 'has' method
Browse files Browse the repository at this point in the history
  • Loading branch information
David Ellis committed Mar 21, 2014
1 parent a7d39bf commit 6810ea3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
28 changes: 22 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ function CacheDir(config) {
}
}

CacheDir.prototype.keyPath = function(key) {
return path.join(this.dir, this.namespace, key);
};

CacheDir.prototype.setSync = function(key, val) {
this.cache[key] = val;
fs.writeFileSync(path.join(this.dir, this.namespace, key), JSON.stringify(val));
fs.writeFileSync(this.keyPath(key), JSON.stringify(val));
};

CacheDir.prototype.set = function(key, val, callback) {
this.cache[key] = val;
fs.writeFile(path.join(this.dir, this.namespace, key), JSON.stringify(val), callback);
fs.writeFile(this.keyPath(key), JSON.stringify(val), callback);
};

CacheDir.prototype.getSync = function(key) {
if (this.cache.hasOwnProperty(key)) return this.cache[key];
var source = path.join(this.dir, this.namespace, key);
var source = this.keyPath(key);
if (fs.existsSync(source)) {
try {
this.cache[key] = JSON.parse(fs.readFileSync(source, 'utf8'));
Expand All @@ -44,7 +48,7 @@ CacheDir.prototype.getSync = function(key) {

CacheDir.prototype.get = function(key, callback) {
if (this.cache.hasOwnProperty(key)) return process.nextTick(callback.bind(this, null, this.cache[key]));
var source = path.join(this.dir, this.namespace, key);
var source = this.keyPath(key);
fs.exists(source, function(exists) {
if (!exists) return callback();
fs.readFile(source, 'utf8', function(err, result) {
Expand Down Expand Up @@ -76,14 +80,26 @@ CacheDir.prototype.values = function(callback) {
fs.readdir(path.join(this.dir, this.namespace), function(err, keys) {
if (err) return callback(err);
async.map(keys, function(key, next) {
fs.readFile(path.join(this.dir, this.namespace, key), 'utf8', function(err, val) {
fs.readFile(this.keyPath(key), 'utf8', function(err, val) {
if (err) return next(err);
next(null, JSON.parse(val));
});
}.bind(this), callback);
}.bind(this));
};

// TODO: Add other ES6 Map methods: items, has, forEach, iterator, delete, clear, toString, and the property size
CacheDir.prototype.has = function(key, callback) {
if (this.cache.hasOwnProperty(key)) {
process.nextTick(callback.bind(this, true));
} else {
fs.exists(this.keyPath(key), callback);
}
};

CacheDir.prototype.hasSync = function(key) {
return this.cache.hasOwnProperty(key) || fs.existsSync(this.keyPath(key));
};

// TODO: Add other ES6 Map methods: items, forEach, iterator, delete, clear, toString, and the property size

module.exports = CacheDir;
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,26 @@ test('valuesReadFileFailure', mutate(fs, 'readFile', function(path, mode, callba
});
}));

test('has', function(assert) {
assert.plan(2);
var cache = createCache();
cache.has('foo', function(has) {
assert.equal(has, true, 'has that key');
cache.has('lolno', function(has) {
assert.equal(has, false, 'does not have that key');
assert.end();
});
});
});

test('hasSync', function(assert) {
assert.plan(2);
var cache = createCache();
assert.equal(cache.hasSync('foo'), true, 'has that key');
assert.equal(cache.hasSync('lolno'), false, 'does not have that key');
assert.end();
});

test('clearAgain', function (assert) {
exec('rm -rf ' + path.join(__dirname, 'cacheTest'), function() {
assert.end();
Expand Down

0 comments on commit 6810ea3

Please sign in to comment.