Skip to content

Commit

Permalink
Simplify utils methods
Browse files Browse the repository at this point in the history
  • Loading branch information
xtian committed Jan 14, 2017
1 parent 0a533f2 commit cf55115
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 83 deletions.
62 changes: 10 additions & 52 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,11 @@ test('getKey', (t) => {
});

test('wrap', (t) => {
let obj = {
auth: { privateKey: 0, publicKey: 0 },
defaults: 1,
_get: 1,
key: 1
};

let wrapper = function(val) {
return val + 1;
};

let obj = { _get: 1, key: 1 };
let wrapper = (val) => val + 1;
let wrapped = utils.wrap(obj, wrapper);

t.equal(wrapped.key, 2, 'got key with wrapper applied');
t.equal(wrapped.auth, obj.auth, 'auth excluded from wrapper');
t.equal(wrapped.defaults, 1, 'defaults excluded from wrapper');
t.equal(wrapped._get, 1, '_get excluded from wrapper');

t.end();
Expand All @@ -37,63 +26,32 @@ test('wrap', (t) => {
test('initParams', (t) => {
t.type(utils.initParams(), 'function', 'returned wrapped function');

t.test('should initialize options object if callback passed', (t) => {
let fn = utils.initParams(function(options) {
t.type(options, 'object', 'options was initialized');
t.type(options.headers, 'object', 'headers was initialized');
t.test('should initialize options', (t) => {
let fn = utils.initParams((options) => {
t.type(options._query, 'object', '_query was initialized');
});

fn(function() {});
t.end();
});

t.test('should set the id prop if options object not passed', (t) => {
let fn = utils.initParams(function(options) {
t.equal(options.id, 1, 'id property was set to passed number param');
});

fn(1);
t.end();
});

t.test('should set the id prop if array passed', (t) => {
let fn = utils.initParams(function(options) {
t.deepEqual(options.id, [1], 'id property was set to passed array param');
});

fn([1]);
fn({});
t.end();
});

t.test('should copy name prop to id prop', (t) => {
let fn = utils.initParams(function(options) {
t.equal(options.id, 1, 'id was set to value of name property');
let fn = utils.initParams((options) => {
t.equal(options.id, 1);
});

fn({ name: 1 });
t.end();
});

t.test('should call function with the passed context', (t) => {
let self = {};
let fn = utils.initParams(function() {
t.equal(this, self, 'this value was equal to passed context param');
}, self);

fn({});
t.end();
});

t.end();
});

test('pick', (t) => {
let obj = { one: 1, two: 2, three: 3 };
let picked = utils.pick(obj, ['one', 'three']);
let picked = utils.pick(obj, ['one']);

t.equal(picked.one, 1, '"one" was copied to new object');
t.equal(picked.two, undefined, '"two" was not copied to new object');
t.equal(picked.three, 3, '"three" was copied to new object');
t.equal(picked.one, 1);
t.equal(picked.two, undefined);
t.end();
});
44 changes: 13 additions & 31 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Returns the value of an object's key if it exists.
exports.getKey = function(obj, key) {
exports.getKey = (obj, key) => {
if (obj && obj[key]) {
return obj[key];
} else {
Expand All @@ -8,50 +8,32 @@ exports.getKey = function(obj, key) {
};

// Returns a new instance of the module with a wrapper applied.
exports.wrap = function(target, wrapper) {
let wrapped = {
auth: target.auth,
defaults: target.defaults,
_get: target._get
};
exports.wrap = (target, wrapper) => {
let wrapped = { _get: target._get };

for (let prop in target) {
if (wrapped[prop] === undefined) {
wrapped[prop] = wrapper(target[prop], target);
wrapped[prop] = wrapper(target[prop]);
}
}

return wrapped;
};

// A wrapper to initialize the options object.
exports.initParams = function(fn, context) {
return function(options, callback) {
let newOptions = {};

if (typeof options === 'object' && !Array.isArray(options)) {
newOptions = options;
newOptions.id = options.id || options.name;

} else if (typeof options === 'function') {
callback = options;
newOptions = {};

} else {
newOptions.id = options;
}

newOptions.headers = newOptions.headers || {};
newOptions._query = {};
exports.initParams = (fn) => {
return (options, callback) => {
options.id = options.id || options.name;
options._query = {};

return fn.call(context, newOptions, callback);
return fn(options, callback);
};
};

// Returns new object with specific keys copied from passed object
exports.pick = function(obj, keys) {
return keys.reduce(function(cumm, key) {
cumm[key] = obj[key];
return cumm;
exports.pick = (obj, keys) => {
return keys.reduce((acc, key) => {
acc[key] = obj[key];
return acc;
}, {});
};

0 comments on commit cf55115

Please sign in to comment.