Skip to content

Commit

Permalink
implemented objValueInArray function and test
Browse files Browse the repository at this point in the history
  • Loading branch information
davidep87 committed Aug 3, 2017
1 parent 5a5130a commit 701a544
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/asserts/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,32 @@ Arrays.arrayOfFunctions = (value) => {
return Types.all.function(value);
};

/**
* Check if is a key of an object with determinated value is in array
*
* **Interfaces**: `not`, `err`
*
* @function
* @name objValueInArray
* @param array {array} array
* @param key {string} object key that you are looking for
* @param value {any} the value of the key that you are looking for
* @returns {*|boolean}
* @example
* be.objValInArray([{ id: 1, name: '...'}], 'id', 1) // true
*/
Arrays.objValueInArray = (array, key, value) => {
let result = [];
if(Types.all.object(array)){
result = array.map((a) => {
return a.hasOwnProperty([key]) && a[key] === value;
})
}
return Types.any.booleanTrue(result);
};

Arrays.objValueInArray.multiple = false;

Arrays = Interface.create(Arrays);

module.exports = Arrays;
15 changes: 14 additions & 1 deletion test/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,17 @@ describe('arrayOfFunctions', function () {
console.log(result);
assert.equal(result, false);
});
});
});

describe('objValueInArray', function () {
it('should be return true', function () {
var result = be.objValueInArray([{id: 2, name: 'test'},{ id: 1, name: '...'}], "id", 1);
console.log(result);
assert.equal(result, true);
});
it('should be return false', function () {
var result = be.objValueInArray([{id: 2, name: 'test'},{ id: 1, name: '...'}], "id", 3);
console.log(result);
assert.equal(result, false);
});
});

0 comments on commit 701a544

Please sign in to comment.