Work out handling of `'@'` field in example

This commit is contained in:
jos 2020-01-04 11:41:11 +01:00
parent 32871f85fd
commit 9ed5d5c5ae
1 changed files with 11 additions and 3 deletions

View File

@ -54,13 +54,21 @@
let query = 'data' let query = 'data'
if (filter) { if (filter) {
// note that the comparisons embrace type coercion, // Note that the comparisons embrace type coercion,
// so a filter value like '5' (text) will match numbers like 5 too. // so a filter value like '5' (text) will match numbers like 5 too.
query = `_.filter(${query}, item => _.get(item, '${filter.field}') ${filter.relation} '${filter.value}')` const getActualValue = filter.field !== '@'
? `item => _.get(item, '${filter.field}')`
: `item => item`
query = `_.filter(${query}, ${getActualValue} ${filter.relation} '${filter.value}')`
} }
if (sort) { if (sort) {
query = `_.orderBy(${query}, '${sort.field}', '${sort.direction}')` // The '@' field name is a special case,
// which means that the field itself is selected.
// For example when we have an array containing numbers.
query = sort.field !== '@'
? `_.orderBy(${query}, '${sort.field}', '${sort.direction}')`
: `_.sortBy(${query}, '${sort.direction}')`
} }
if (projection) { if (projection) {