I've been exploring ways to pass an array of property names (or field names) for a specific object without resorting to using what are often referred to as "magic strings" - as they can easily lead to typos! I'm essentially searching for something similar to csharp's "Expression<>".
For example, using magic strings:
searchFilter(model, 'searchParameter', ['id', 'name'])
Alternatively, in a typed manner, or how I envision calling the function:
searchFilter(model, 'searchParameter', [m => m.id, m => m.name])
Here's a sample representation of this function:
Using magic strings: (or my attempted approach with typing)
private searchFilter(mode: Model, q: string, properties: string[]): boolean {
if (q === '') return true;
q = q.trim().toLowerCase();
for (let property of properties) {
if (vacature[property.toString()].toString().toLowerCase().indexOf(q) >= 0) {
return true;
}
}
return false;
}
Typed: (Or how I initially tried to implement it with typing, however, this simply returns functions. To effectively extract the called property and obtain its name, I would need a relevant 'function expression' akin to C#.)
private searchFilter(mode: Model, q: string, propertySelector: ((x: Model) => any | string)[]): boolean {
if (q === '') return true;
q = q.trim().toLowerCase();
for (let property of propertySelector) {
if (vacature[property.toString()].toString().toLowerCase().indexOf(q) >= 0) {
return true;
}
}
return false;
}