I'm designing an isIn method that takes a value:any and checks whether it exists in an array. It's working, but I'm wondering whether I missed any edge cases:
/**
* Checks if given value is in the target array of allowed values.
*
* @param value The value being checked.
* @param target The target value to perform the check against.
* @return True if the value is in the target array, false otherwise.
*/
export function isIn(value: any, target: any[]): boolean {
if (!isArray(value)) {
return !isArray(target) || target.indexOf(value)>-1;
}
else {
return (JSON.stringify(target)).indexOf(JSON.stringify(value)) != -1;
}
}
describe("isIn", () => {
it(`should return true when the value is in the array`, () => {
expect(isIn(2, [2])).to.be.true;
expect(isIn('a', ['a', 'b'])).to.be.true;
expect(isIn('a', ['a'])).to.be.true;
expect(isIn([2,3], [[2,3]])).to.be.true;
});
it(`should return false when the value is not in the array`, () => {
expect(isIn('a', ['b'])).to.be.false;
expect(isIn([2,4], [[2,3]])).to.be.false;
});
});