Skip to content

Commit

Permalink
fix(search): allow NOT as a standalone modifier
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-portal

ISSUES CLOSED: #542
  • Loading branch information
jgravois committed Apr 24, 2019
1 parent 39fc213 commit 36a6bca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
15 changes: 10 additions & 5 deletions packages/arcgis-rest-portal/src/util/SearchQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,19 @@ export class SearchQueryBuilder implements IParamBuilder {
}

/**
* Joins two sets of queries with a `NOT` clause.
* Joins two sets of queries with a `NOT` clause. Another option for filtering results is the [prohibit operator '-'](https://developers.arcgis.com/rest/users-groups-and-items/search-reference.htm#ESRI_SECTION1_5C6C35DB9E4A4F4492C5B937BDA2BF67).
*
* ```js
* // omit results with "Rivers" in their title
* const query = new SearchQueryBuilder()
* .match("Water")
* .in("title")
* .not()
* .match("Rivers")
* .in("title")
*
* // equivalent
* const query = new SearchQueryBuilder()
* .match("Rivers")
* .in("-title")
* ```
*/
public not(this: SearchQueryBuilder) {
Expand Down Expand Up @@ -283,15 +287,16 @@ export class SearchQueryBuilder implements IParamBuilder {

this.commit();

if (this.q === "") {
if (this.q === "" && modifier !== "not") {
warn(
`You have called \`${modifier}()\` without calling another method to modify your query first. Try calling \`match()\` first.`
);
return this;
}

this.currentModifer = modifier;
this.q += ` ${modifier.toUpperCase()} `;
this.q += this.q === "" ? "" : " ";
this.q += `${modifier.toUpperCase()} `;
return this;
}

Expand Down
18 changes: 14 additions & 4 deletions packages/arcgis-rest-portal/test/util/SearchQueryBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,19 @@ describe("SearchQueryBuilder", () => {
);
});

it("should allow .not to be called without a preceding search value", () => {
const query = new SearchQueryBuilder()
.not()
.match("public")
.in("access")
.not()
.match("code attachment")
.in("type")
.toParam();

expect(query).toEqual(`NOT access:public NOT type:"code attachment"`);
});

it("should clone searches for modification", () => {
const myAppsQuery = new SearchQueryBuilder()
.match("fred")
Expand Down Expand Up @@ -195,11 +208,8 @@ describe("SearchQueryBuilder", () => {

it("should not allow chains of logic modifiers, and warn user", () => {
const query = new SearchQueryBuilder()
.not()
.and()
.or()
.not()
.or()
.or()
.toParam();

Expand Down Expand Up @@ -323,7 +333,7 @@ describe("SearchQueryBuilder", () => {
expect(query).toEqual("title:test");
});

it("should", () => {
it("should produce an empty string when no methods are called", () => {
const query = new SearchQueryBuilder().toParam();
expect(query).toEqual("");
});
Expand Down

0 comments on commit 36a6bca

Please sign in to comment.