Skip to content

Commit

Permalink
feat: add StartsWith/EndsWith (a*z) to OData/GraphQL (#1217)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding authored Jun 8, 2024
1 parent 677fd4c commit c8122e7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
12 changes: 8 additions & 4 deletions packages/demo/src/examples/slickgrid/example5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,16 @@ export class Example5 {
}
}
}
if (filterBy.includes('startswith')) {
if (filterBy.includes('startswith') && filterBy.includes('endswith')) {
const filterStartMatch = filterBy.match(/startswith\(([a-zA-Z ]*),\s?'(.*?)'/) || [];
const filterEndMatch = filterBy.match(/endswith\(([a-zA-Z ]*),\s?'(.*?)'/) || [];
const fieldName = filterStartMatch[1].trim();
columnFilters[fieldName] = { type: 'starts+ends', term: [filterStartMatch[2].trim(), filterEndMatch[2].trim()] };
} else if (filterBy.includes('startswith')) {
const filterMatch = filterBy.match(/startswith\(([a-zA-Z ]*),\s?'(.*?)'/);
const fieldName = filterMatch![1].trim();
(columnFilters as any)[fieldName] = { type: 'starts', term: filterMatch![2].trim() };
}
if (filterBy.includes('endswith')) {
} else if (filterBy.includes('endswith')) {
const filterMatch = filterBy.match(/endswith\(([a-zA-Z ]*),\s?'(.*?)'/);
const fieldName = filterMatch![1].trim();
(columnFilters as any)[fieldName] = { type: 'ends', term: filterMatch![2].trim() };
Expand Down Expand Up @@ -327,7 +331,7 @@ export class Example5 {
const filterType = (columnFilters as any)[columnId].type;
const searchTerm = (columnFilters as any)[columnId].term;
let colId = columnId;
if (columnId && columnId.indexOf(' ') !== -1) {
if (columnId?.indexOf(' ') !== -1) {
const splitIds = columnId.split(' ');
colId = splitIds[splitIds.length - 1];
}
Expand Down
6 changes: 4 additions & 2 deletions packages/demo/src/examples/slickgrid/example6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ export class Example6 {
filters: [
// you can use OperatorType or type them as string, e.g.: operator: 'EQ'
{ columnId: 'gender', searchTerms: ['male'], operator: OperatorType.equal },
{ columnId: 'name', searchTerms: ['John Doe'], operator: OperatorType.contains },
// { columnId: 'name', searchTerms: ['John Doe'], operator: OperatorType.contains },
{ columnId: 'name', searchTerms: ['Joh*oe'], operator: OperatorType.startsWithEndsWith },
{ columnId: 'company', searchTerms: ['xyz'], operator: 'IN' },

// use a date range with 2 searchTerms values
Expand Down Expand Up @@ -345,7 +346,8 @@ export class Example6 {
this.aureliaGrid.filterService.updateFilters([
// you can use OperatorType or type them as string, e.g.: operator: 'EQ'
{ columnId: 'gender', searchTerms: ['male'], operator: OperatorType.equal },
{ columnId: 'name', searchTerms: ['John Doe'], operator: OperatorType.contains },
// { columnId: 'name', searchTerms: ['John Doe'], operator: OperatorType.contains },
{ columnId: 'name', searchTerms: ['Joh*oe'], operator: OperatorType.startsWithEndsWith },
{ columnId: 'company', searchTerms: ['xyz'], operator: 'IN' },

// use a date range with 2 searchTerms values
Expand Down
21 changes: 21 additions & 0 deletions test/cypress/e2e/example05.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,27 @@ describe('Example 5 - OData Grid', () => {
cy.get('[data-test=total-items]')
.contains('50');
});

it('should return 2 rows using "C*n" (starts with "C" + ends with "n")', () => {
cy.get('input.filter-name')
.clear()
.type('C*n');

// wait for the query to finish
cy.get('[data-test=status]').should('contain', 'finished');

cy.get('[data-test=odata-query-result]')
.should(($span) => {
expect($span.text()).to.eq(`$top=10&$orderby=Name desc&$filter=(Gender eq 'female' and startswith(Name, 'C') and endswith(Name, 'n'))`);
});

cy.get(`[style="top: ${GRID_ROW_HEIGHT * 0}px;"] > .slick-cell:nth(1)`).should('contain', 'Consuelo Dickson');
cy.get(`[style="top: ${GRID_ROW_HEIGHT * 1}px;"] > .slick-cell:nth(1)`).should('contain', 'Christine Compton');

// clear filter before next test
cy.get('input.filter-name')
.clear();
});
});

describe('Select and Expand Behaviors', () => {
Expand Down
33 changes: 21 additions & 12 deletions test/cypress/e2e/example06.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Example 6 - GraphQL Grid', () => {
cy.get('.search-filter.filter-name')
.find('input')
.invoke('val')
.then(text => expect(text).to.eq('John Doe'));
.then(text => expect(text).to.eq('Joh*oe'));

cy.get('.search-filter.filter-gender .ms-choice > span')
.contains('Male');
Expand All @@ -59,7 +59,8 @@ describe('Example 6 - GraphQL Grid', () => {
expect(text).to.eq(removeWhitespaces(`query{users(first:20,offset:20,
orderBy:[{field:"name",direction:ASC},{field:"company",direction:DESC}],
filterBy:[
{field:"gender",operator:EQ,value:"male"},{field:"name",operator:Contains,value:"JohnDoe"},
{field:"gender",operator:EQ,value:"male"},
{field:"name",operator:StartsWith,value:"Joh"},{field:"name",operator:EndsWith,value:"oe"},
{field:"company",operator:IN,value:"xyz"},{field:"finish",operator:GE,value:"${presetLowestDay}"},{field:"finish",operator:LE,value:"${presetHighestDay}"}
],locale:"en",userId:123){
totalCount,nodes{id,name,gender,company,billing{address{street,zip}},finish}}}`));
Expand All @@ -84,7 +85,8 @@ describe('Example 6 - GraphQL Grid', () => {
expect(text).to.eq(removeWhitespaces(`query{users(first:20,offset:40,
orderBy:[{field:"name",direction:ASC},{field:"company",direction:DESC}],
filterBy:[
{field:"gender",operator:EQ,value:"male"},{field:"name",operator:Contains,value:"JohnDoe"},
{field:"gender",operator:EQ,value:"male"},
{field:"name",operator:StartsWith,value:"Joh"},{field:"name",operator:EndsWith,value:"oe"},
{field:"company",operator:IN,value:"xyz"},{field:"finish",operator:GE,value:"${presetLowestDay}"},{field:"finish",operator:LE,value:"${presetHighestDay}"}
],locale:"en",userId:123){totalCount,nodes{id,name,gender,company,billing{address{street,zip}},finish}}}`));
});
Expand All @@ -102,7 +104,8 @@ describe('Example 6 - GraphQL Grid', () => {
expect(text).to.eq(removeWhitespaces(`query{users(first:20,offset:80,
orderBy:[{field:"name",direction:ASC},{field:"company",direction:DESC}],
filterBy:[
{field:"gender",operator:EQ,value:"male"},{field:"name",operator:Contains,value:"JohnDoe"},
{field:"gender",operator:EQ,value:"male"},
{field:"name",operator:StartsWith,value:"Joh"},{field:"name",operator:EndsWith,value:"oe"},
{field:"company",operator:IN,value:"xyz"},{field:"finish",operator:GE,value:"${presetLowestDay}"},{field:"finish",operator:LE,value:"${presetHighestDay}"}
],locale:"en",userId:123){totalCount,nodes{id,name,gender,company,billing{address{street,zip}},finish}}}`));
});
Expand All @@ -122,7 +125,7 @@ describe('Example 6 - GraphQL Grid', () => {
orderBy:[{field:"name",direction:ASC},{field:"company",direction:DESC}],
filterBy:[
{field:"gender",operator:EQ,value:"male"},
{field:"name",operator:Contains,value:"John Doe"},
{field:"name",operator:StartsWith,value:"Joh"},{field:"name",operator:EndsWith,value:"oe"},
{field:"company",operator:IN,value:"xyz"},
{field:"finish",operator:GE,value:"${presetLowestDay}"},
{field:"finish",operator:LE,value:"${presetHighestDay}"}
Expand All @@ -143,7 +146,8 @@ describe('Example 6 - GraphQL Grid', () => {
expect(text).to.eq(removeWhitespaces(`query{users(first:20,offset:80,
orderBy:[{field:"name",direction:ASC},{field:"company",direction:DESC}],
filterBy:[
{field:"gender",operator:EQ,value:"male"},{field:"name",operator:Contains,value:"JohnDoe"},
{field:"gender",operator:EQ,value:"male"},
{field:"name",operator:StartsWith,value:"Joh"},{field:"name",operator:EndsWith,value:"oe"},
{field:"company",operator:IN,value:"xyz"},{field:"finish",operator:GE,value:"${presetLowestDay}"},{field:"finish",operator:LE,value:"${presetHighestDay}"}
],locale:"en",userId:123){totalCount,nodes{id,name,gender,company,billing{address{street,zip}},finish}}}`));
});
Expand All @@ -163,7 +167,8 @@ describe('Example 6 - GraphQL Grid', () => {
expect(text).to.eq(removeWhitespaces(`query{users(first:30,offset:0,
orderBy:[{field:"name",direction:ASC},{field:"company",direction:DESC}],
filterBy:[
{field:"gender",operator:EQ,value:"male"},{field:"name",operator:Contains,value:"JohnDoe"},
{field:"gender",operator:EQ,value:"male"},
{field:"name",operator:StartsWith,value:"Joh"},{field:"name",operator:EndsWith,value:"oe"},
{field:"company",operator:IN,value:"xyz"},{field:"finish",operator:GE,value:"${presetLowestDay}"},{field:"finish",operator:LE,value:"${presetHighestDay}"}
],locale:"en",userId:123){totalCount,nodes{id,name,gender,company,billing{address{street,zip}},finish}}}`));
});
Expand All @@ -174,7 +179,7 @@ describe('Example 6 - GraphQL Grid', () => {
.find('.slick-header-left .slick-header-column:nth(0)')
.trigger('mouseover')
.children('.slick-header-menu-button')
// .should('be.hidden')
.should('be.hidden')
.invoke('show')
.click();

Expand Down Expand Up @@ -731,7 +736,8 @@ describe('Example 6 - GraphQL Grid', () => {
expect(text).to.eq(removeWhitespaces(`query{users(last:20,
orderBy:[{field:"name",direction:ASC},{field:"company",direction:DESC}],
filterBy:[
{field:"gender",operator:EQ,value:"male"},{field:"name",operator:Contains,value:"JohnDoe"},
{field:"gender",operator:EQ,value:"male"},
{field:"name",operator:StartsWith,value:"Joh"},{field:"name",operator:EndsWith,value:"oe"},
{field:"company",operator:IN,value:"xyz"},{field:"finish",operator:GE,value:"${presetLowestDay}"},{field:"finish",operator:LE,value:"${presetHighestDay}"}
],locale:"en",userId:123){totalCount,nodes{id,name,gender,company,billing{address{street,zip}},finish},pageInfo{hasNextPage,hasPreviousPage,endCursor,startCursor},edges{cursor}}}`));
});
Expand All @@ -751,7 +757,8 @@ describe('Example 6 - GraphQL Grid', () => {
expect(text).to.eq(removeWhitespaces(`query{users(first:20,
orderBy:[{field:"name",direction:ASC},{field:"company",direction:DESC}],
filterBy:[
{field:"gender",operator:EQ,value:"male"},{field:"name",operator:Contains,value:"JohnDoe"},
{field:"gender",operator:EQ,value:"male"},
{field:"name",operator:StartsWith,value:"Joh"},{field:"name",operator:EndsWith,value:"oe"},
{field:"company",operator:IN,value:"xyz"},{field:"finish",operator:GE,value:"${presetLowestDay}"},{field:"finish",operator:LE,value:"${presetHighestDay}"}
],locale:"en",userId:123){totalCount,nodes{id,name,gender,company,billing{address{street,zip}},finish},pageInfo{hasNextPage,hasPreviousPage,endCursor,startCursor},edges{cursor}}}`));
});
Expand Down Expand Up @@ -779,7 +786,8 @@ describe('Example 6 - GraphQL Grid', () => {
expect(text).to.eq(removeWhitespaces(`query{users(first:20,after:"${afterCursor}",
orderBy:[{field:"name",direction:ASC},{field:"company",direction:DESC}],
filterBy:[
{field:"gender",operator:EQ,value:"male"},{field:"name",operator:Contains,value:"JohnDoe"},
{field:"gender",operator:EQ,value:"male"},
{field:"name",operator:StartsWith,value:"Joh"},{field:"name",operator:EndsWith,value:"oe"},
{field:"company",operator:IN,value:"xyz"},{field:"finish",operator:GE,value:"${presetLowestDay}"},{field:"finish",operator:LE,value:"${presetHighestDay}"}
],locale:"en",userId:123){totalCount,nodes{id,name,gender,company,billing{address{street,zip}},finish},pageInfo{hasNextPage,hasPreviousPage,endCursor,startCursor},edges{cursor}}}`));
});
Expand Down Expand Up @@ -808,7 +816,8 @@ describe('Example 6 - GraphQL Grid', () => {
expect(text).to.eq(removeWhitespaces(`query{users(last:20,before:"${beforeCursor}",
orderBy:[{field:"name",direction:ASC},{field:"company",direction:DESC}],
filterBy:[
{field:"gender",operator:EQ,value:"male"},{field:"name",operator:Contains,value:"JohnDoe"},
{field:"gender",operator:EQ,value:"male"},
{field:"name",operator:StartsWith,value:"Joh"},{field:"name",operator:EndsWith,value:"oe"},
{field:"company",operator:IN,value:"xyz"},{field:"finish",operator:GE,value:"${presetLowestDay}"},{field:"finish",operator:LE,value:"${presetHighestDay}"}
],locale:"en",userId:123){totalCount,nodes{id,name,gender,company,billing{address{street,zip}},finish},pageInfo{hasNextPage,hasPreviousPage,endCursor,startCursor},edges{cursor}}}`));
});
Expand Down

0 comments on commit c8122e7

Please sign in to comment.