Skip to content

Commit

Permalink
make del sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Oct 21, 2015
1 parent 643750a commit ec8a5c1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 30 deletions.
30 changes: 14 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

var path = require('path');
var util = require('util');
var base = require('base-methods');
var Base = base.namespace('data');
var proto = Base.prototype;
Expand Down Expand Up @@ -225,17 +224,19 @@ Store.prototype.save = function(dest) {
* @api public
*/

Store.prototype.del = function(keys, options, cb) {
var args = [].slice.call(arguments);
var last = utils.last(args);

cb = typeof last === 'function' ? args.pop() : utils.noop;
Store.prototype.del = function(keys, options) {
var isArray = Array.isArray(keys);
if (typeof keys === 'string' || isArray) {
keys = utils.arrayify(keys);
} else {
options = keys;
keys = null;
}

last = utils.last(args);
options = utils.typeOf(last) === 'object' ? args.pop() : {};
options = options || {};

if (args.length) {
utils.arrayify(keys).forEach(function (key) {
if (keys) {
keys.forEach(function (key) {
proto.del.call(this, key);
}.bind(this));
this.save();
Expand All @@ -248,12 +249,9 @@ Store.prototype.del = function(keys, options, cb) {
}

// if no keys are passed, delete the entire store
utils.del(this.path, options, function (err) {
if (err) return cb(err);
this.data = {};
this.emit('del', keys);
cb(null, keys);
}.bind(this));
utils.del.sync(this.path, options);
this.data = {};
this.emit('del', keys);
return this;
};

Expand Down
15 changes: 1 addition & 14 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ describe('events', function () {
store.del({force: true});
});

it('should expose `err` to a callback:', function () {
it('should throw an error if force is not passed', function () {
store = new Store('lll');
store.set('a', 'b');
store.set('c', 'd');
Expand All @@ -295,17 +295,4 @@ describe('events', function () {
store.del();
}).should.throw('options.force is required to delete the entire cache.');
});

it('should expose `err` to a callback:', function (done) {
store = new Store('lll');
store.set('a', 'b');
store.set('c', 'd');
store.set('e', 'f');

store.del({force: true}, function (err, keys) {
keys.should.eql(['a', 'c', 'e']);
assert(Object.keys(store.data).length === 0);
done();
});
});
});

0 comments on commit ec8a5c1

Please # to comment.