Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

add gt and lt filters for datetime #63

Open
wants to merge 3 commits into
base: mock-server
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions odata-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,29 @@ function removeFirstNavigationPath(propertyName) {
*/
function applyFilter(result, singleFilter) {
if (singleFilter.includes(' eq \'')) {
const filterRegex = /^\(?(\w+) eq '(.*)'\)?$/;
const filterRegex = /^\(*(\w+) eq '(.*)'\)*$/;
const [, filterProperty, filterValue] = filterRegex.exec(singleFilter);
return applyEqFilter(result, filterProperty, filterValue);
} else if (singleFilter.includes(' ne \'')) {
const filterRegex = /^\(?(\w+) ne '(.*)'\)?$/;
const filterRegex = /^\(*(\w+) ne '(.*)'\)*$/;
const [, filterProperty, filterValue] = filterRegex.exec(singleFilter);
return applyNeFilter(result, filterProperty, filterValue);
} else if (singleFilter.includes(' ge datetime\'')) {
const filterRegex = /^\(?(\w+) ge datetime'(.*)'\)?$/;
const filterRegex = /^\(*(\w+) ge datetime'(.*)'\)*$/;
const [, filterProperty, filterValue] = filterRegex.exec(singleFilter);
return applyGeFilter(result, filterProperty, transformDateTime(filterValue));
} else if (singleFilter.includes(' le datetime\'')) {
const filterRegex = /^\(?(\w+) le datetime'(.*)'\)?$/;
const filterRegex = /^\(*(\w+) le datetime'(.*)'\)*$/;
const [, filterProperty, filterValue] = filterRegex.exec(singleFilter);
return applyLeFilter(result, filterProperty, transformDateTime(filterValue));
} else if (singleFilter.includes(' gt datetime\'')) {
const filterRegex = /^\(*(\w+) gt datetime'(.*)'\)*$/;
const [, filterProperty, filterValue] = filterRegex.exec(singleFilter);
return applyGtFilter(result, filterProperty, transformDateTime(filterValue));
} else if (singleFilter.includes(' lt datetime\'')) {
const filterRegex = /^\(*(\w+) lt datetime'(.*)'\)*$/;
const [, filterProperty, filterValue] = filterRegex.exec(singleFilter);
return applyLtFilter(result, filterProperty, transformDateTime(filterValue));
}
}

Expand Down Expand Up @@ -65,6 +73,14 @@ function applyLeFilter(result, filterProperty, filterValue) {
return result.filter(item => item[filterProperty] <= filterValue);
}

function applyGtFilter(result, filterProperty, filterValue) {
return result.filter(item => item[filterProperty] > filterValue);
}

function applyLtFilter(result, filterProperty, filterValue) {
return result.filter(item => item[filterProperty] < filterValue);
}

/**
* Deals with navigation properties of the supplied entity per the expand specification.
* Navigation properties that are part of the expanded properties are included in the result object.
Expand Down Expand Up @@ -130,7 +146,7 @@ const reduceEntityToSelect = function(entity, selectedProperties = []) {
return Object.entries(entity).reduce(function(result, [key, value]) {
const isNavProperty = isNavigationProperty(key);
if('__metadata' === key || selectedProperties.includes(key) ||
(selectedProperties.includes('*')) && !isNavProperty) {
(selectedProperties.filter(a => a.includes("*")).length > 0) && !isNavProperty){
result[key] = value;
}
else if(isNavProperty) {
Expand Down