From 1d33e17b203e2acf0848b846aa47152af3271d18 Mon Sep 17 00:00:00 2001 From: Fabio Bertone Date: Tue, 4 Jun 2019 16:25:13 -0400 Subject: [PATCH] PR #34 indexof with nested function now applies proper prefix for collections --- src/index.js | 18 +++++++++++++----- src/index.test.js | 18 +++++++++++++++++- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index f57cd45..76f746b 100644 --- a/src/index.js +++ b/src/index.js @@ -11,6 +11,7 @@ const SUPPORTED_EXPAND_PROPERTIES = [ ]; const FUNCTION_REGEX = /\((.*)\)/; +const INDEXOF_REGEX = /(?!indexof)\((\w+)\)/; export default function({ select, @@ -145,11 +146,18 @@ function buildFilter(filters = {}, propPrefix = '') { } else if (typeof filters === 'object') { const filtersArray = Object.keys(filters).reduce((result, filterKey) => { const value = filters[filterKey]; - const propName = propPrefix - ? FUNCTION_REGEX.test(filterKey) - ? filterKey.replace(FUNCTION_REGEX, `(${propPrefix}/$1)`) - : `${propPrefix}/${filterKey}` - : filterKey; + let propName = ''; + if(propPrefix){ + if(INDEXOF_REGEX.test(filterKey)) { + propName = filterKey.replace(INDEXOF_REGEX, `(${propPrefix}/$1)`); + } else if(FUNCTION_REGEX.test(filterKey)) { + propName = filterKey.replace(FUNCTION_REGEX, `(${propPrefix}/$1)`); + } else { + propName = `${propPrefix}/${filterKey}`; + } + } else { + propName = filterKey; + } if ( ['number', 'string', 'boolean'].indexOf(typeof value) !== -1 || diff --git a/src/index.test.js b/src/index.test.js index c2dfe32..5126718 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -575,7 +575,23 @@ describe('filter', () => { const actual = buildQuery({ filter }); expect(actual).toEqual(expected); }); - }); + + it('should handle collection operator with a function indexof', () => { + const filter = { + Tasks: { + any: { + "indexof(toupper(searchProp),'foo')": { + eq: -1 + } + } + } + }; + const expected = + "?$filter=Tasks/any(tasks:indexof(toupper(tasks/searchProp),'foo') eq -1)"; + const actual = buildQuery({ filter }); + expect(actual).toEqual(expected); + }); +}); describe('data types', () => { it('should handle a number', () => {