I'm trying to figure out how to pass an array of propertynames (or fieldnames) of a given object, without using so called magic strings - because typos are easily made! In essence, I'm looking for something relevant to csharp's "Expression<>" I think.
E.g. with magic strings:
searchFilter(model, 'searchParameter', ['id', 'name'])
E.g. typed, or how I would like to call the function:
searchFilter(model, 'searchParameter', [m => m.id, m => m.name])
As a reference, this function looks a bit like this:
with magic strings: (or how I was trying to do it typed)
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 was trying to do it typed, but this ofcourse just gives back functions.. I'd need a relevant 'function expression' as in C# to extract the called property, to get it's name)
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;
}