I am trying to implement a custom matcher for Jasmine where I would like to check if the given object property values lie within the range of the other two object property values.
Here is what I got so far:
let matcher = {
toLieWithin: function (util: jasmine.MatchersUtil, customEqualityTesters: Array<jasmine.CustomEqualityTester>): jasmine.CustomMatcher {
return {
compare: function (actual: any, expected: any): jasmine.CustomMatcherResult {
let result: jasmine.CustomMatcherResult = {
pass: false,
message: ''
};
result.pass = liesWithin(actual, expected);
return result;
}
}
}
};
function liesWithin<T>(objActual: T, objExpected: T[]): boolean {
let output: boolean;
if(objExpected) {
output = objActual.x > objExpected[0].x && objActual.x < objExpected[1].x && objActual.y > objExpected[0].y && objExpected[1].y;
}
return output;
}
Here, I am assuming, the actual has two properties x
and y
. And the expected is an array of two objects which also has two properties each x
and y
.
actual = {x: -5, y: -10}; expected = [{x: -10, y: -17},{x: 0, y: 0}];
Now, this scenario works I believe for the above given simple example. But when I am trying to implement it as a generic, how do I find what properties does the object have? And is my approach correct? Could anyone give me some ideas how can I implement such a method.
Thank you.