Skip to content

Commit a0e9801

Browse files
author
Carson Logan
committed
Added support for any and all queries on simple string collections.
1 parent dab1a64 commit a0e9801

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

src/index.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -294,18 +294,30 @@ function buildFilter(filters: Filter = {}, propPrefix = ''): string {
294294
return filterExpr;
295295
}
296296

297-
function buildCollectionClause(lambdaParameter: string, value: any, op: string, propName: string) {
298-
let clause = "";
299-
if (value) {
300-
// normalize {any:[{prop1: 1}, {prop2: 1}]} --> {any:{prop1: 1, prop2: 1}}; same for 'all'
301-
const filter = buildFilterCore(
302-
Array.isArray(value)
303-
? value.reduce((acc, item) => ({ ...acc, ...item }), {})
304-
: value, lambdaParameter);
305-
clause = `${propName}/${op}(${filter ? `${lambdaParameter}:${filter}` : ""})`;
306-
}
307-
return clause;
308-
}
297+
function buildCollectionClause(lambdaParameter: string, value: any, op: string, propName: string) {
298+
let clause = '';
299+
300+
if (typeof value === 'string' || value instanceof String) {
301+
clause = getStringCollectionClause(lambdaParameter, value, op, propName);
302+
303+
} else if (value) {
304+
// normalize {any:[{prop1: 1}, {prop2: 1}]} --> {any:{prop1: 1, prop2: 1}}; same for 'all'
305+
const filter = buildFilterCore(
306+
Array.isArray(value)
307+
? value.reduce((acc, item) => ({ ...acc, ...item }), {})
308+
: value, lambdaParameter);
309+
clause = `${propName}/${op}(${filter ? `${lambdaParameter}:${filter}` : ''})`;
310+
}
311+
return clause;
312+
}
313+
}
314+
315+
function getStringCollectionClause(lambdaParameter: string, value: any, collectionOperator: string, propName: string) {
316+
let clause = '';
317+
const conditionOperator = collectionOperator == 'all' ? 'ne' : 'eq';
318+
clause = `${propName}/${collectionOperator}(${lambdaParameter}: ${lambdaParameter} ${conditionOperator} '${value}')`
319+
320+
return clause;
309321
}
310322

311323
function escapeIllegalChars(string: string) {

test/index.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,28 @@ describe('filter', () => {
650650
const actual = buildQuery({ filter });
651651
expect(actual).toEqual(expected);
652652
});
653+
654+
it('should handle collection operator over simple string collection with any', () => {
655+
const filter = {
656+
MacAddress: {
657+
any: "3C:4A:92:F1:98:E2"
658+
}
659+
};
660+
const expected = "?$filter=MacAddress/any(macaddress: macaddress eq '3C:4A:92:F1:98:E2')";
661+
const actual = buildQuery({ filter });
662+
expect(actual).toEqual(expected);
663+
})
664+
665+
it('should handle collection operator over simple string collection with all', () => {
666+
const filter = {
667+
MacAddress: {
668+
all: "3C:4A:92:F1:98:E2"
669+
}
670+
};
671+
const expected = "?$filter=MacAddress/all(macaddress: macaddress ne '3C:4A:92:F1:98:E2')";
672+
const actual = buildQuery({ filter });
673+
expect(actual).toEqual(expected);
674+
})
653675
});
654676

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

0 commit comments

Comments
 (0)