Skip to content

Commit

Permalink
Merge pull request #38 from fabio-b:master
Browse files Browse the repository at this point in the history
PR #34 indexof with nested function now applies proper prefix for col…
  • Loading branch information
techniq authored Jun 12, 2019
2 parents cf55983 + 1d33e17 commit 935d21b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const SUPPORTED_EXPAND_PROPERTIES = [
];

const FUNCTION_REGEX = /\((.*)\)/;
const INDEXOF_REGEX = /(?!indexof)\((\w+)\)/;

export default function({
select,
Expand Down Expand Up @@ -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 ||
Expand Down
18 changes: 17 additions & 1 deletion src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 935d21b

Please sign in to comment.