Skip to content

Commit 7d4613e

Browse files
changed ITEM_ROOT value to empty string, fixed INDEXOF_REGEX, FUNCTION_REGEX
1 parent 849fd59 commit 7d4613e

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export type QueryOptions<T> = ExpandOptions<T> & {
8686
aliases: Alias[];
8787
}
8888

89-
export const ITEM_ROOT = '__item';
89+
export const ITEM_ROOT = "";
9090

9191
export default function <T>({
9292
select: $select,
@@ -188,9 +188,9 @@ function buildFilter(filters: Filter = {}, propPrefix = ''): string {
188188
if (filterKey === ITEM_ROOT) {
189189
propName = propPrefix;
190190
} else if (INDEXOF_REGEX.test(filterKey)) {
191-
propName = filterKey.replace(INDEXOF_REGEX, `(${propPrefix}/$1)`);
191+
propName = filterKey.replace(INDEXOF_REGEX, (_,$1)=>$1.trim() === ITEM_ROOT ? `(${propPrefix})` : `(${propPrefix}/${$1.trim()})`);
192192
} else if (FUNCTION_REGEX.test(filterKey)) {
193-
propName = filterKey.replace(FUNCTION_REGEX, `(${propPrefix}/$1)`);
193+
propName = filterKey.replace(FUNCTION_REGEX, (_,$1)=>$1.trim() === ITEM_ROOT ? `(${propPrefix})` : `(${propPrefix}/${$1.trim()})`);
194194
} else {
195195
propName = `${propPrefix}/${filterKey}`;
196196
}

test/index.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,20 @@ describe('filter', () => {
703703
const actual = buildQuery({ filter });
704704
expect(actual).toEqual(expected);
705705
})
706+
707+
it('should handle collection operator with a function on the item of a simple collection', () => {
708+
const filter = {
709+
tags: {
710+
any: {
711+
[`tolower(${ITEM_ROOT})`]: 'tag1'
712+
}
713+
}
714+
}
715+
const expected =
716+
"?$filter=tags/any(tags:tolower(tags) eq 'tag1')";
717+
const actual = buildQuery({ filter });
718+
expect(actual).toEqual(expected);
719+
});
706720
});
707721

708722
describe('data types', () => {

0 commit comments

Comments
 (0)