Skip to content

Commit

Permalink
feat: add Util.value method to help retrieve alternate value from obj…
Browse files Browse the repository at this point in the history
…ect using alternate keys
  • Loading branch information
teclone committed Nov 12, 2018
1 parent bee4a8a commit 9d143a2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/modules/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import path from 'path';
let toString = Object.prototype.toString;

export default {

/**
* tests if a variable is a string
*@param {*} variable - variable to test
*@returns {boolean}
*/
isString(variable) {
return typeof variable === 'string';
},

/**
* tests if a variable is a number
*@param {*} variable - variable to test
Expand Down Expand Up @@ -255,5 +265,27 @@ export default {
}
}
return target;
}
},

/**
* returns the value for the first key in the keys array that exists in the object
* otherwise, return the default value
*
*@param {string[]|string} keys - array of keys or a single string key
*@param {Object} object - the object
*@param {mixed} [defaultValue=undefined] - the default value to return if otherwise.
* defaults to undefined
*@return mixed
*/
value(keys, object, defaultValue) {
keys = this.makeArray(keys);

if (this.isPlainObject(object)) {
for (let key of keys) {
if (this.isString(key) && typeof object[key] !== 'undefined')
return object[key];
}
}
return defaultValue;
},
};
29 changes: 29 additions & 0 deletions test/modules/Util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import path from 'path';
import rimraf from 'rimraf';

describe('Util module', function() {
describe('.isString(variable)', function() {
it('should return true if argument is of type string', function() {
expect(Util.isString('')).to.be.true;
});

it('should return false if argument is not of type string', function() {
expect(Util.isString({})).to.be.false;
expect(Util.isString(2)).to.be.false;
});
});

describe('.isNumber(variable)', function() {
it('should return true if argument is a number', function() {
expect(Util.isNumber(3.2)).to.be.true;
Expand Down Expand Up @@ -260,4 +271,22 @@ describe('Util module', function() {
expect(Util.mkDirSync('/')).to.be.true;
});
});

describe('.value(keys, object, defaultValue=undefined)', function() {
let object = null;
beforeEach(function() {
object = {name: 'Harrison', age: 22};
});

it(`should return the value for the first key that exists in the object`, function() {
expect(Util.value(['firstName', 'name'], object)).to.equals('Harrison');
});

it(`should return the default value if none of the keys exists
in the object or if argument two is not an object`, function() {
expect(Util.value('height', object)).to.be.undefined;
expect(Util.value('height', object, '5ft')).to.equal('5ft');
expect(Util.value('height', null, '5ft')).to.equal('5ft');
});
});
});

0 comments on commit 9d143a2

Please sign in to comment.