Skip to content

Commit

Permalink
Only apply web search's whitespace char to execution url
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Dec 23, 2018
1 parent 8fb2941 commit 850b78f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
21 changes: 10 additions & 11 deletions src/tests/unit/builders/web-search-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ describe(WebSearchBuilder.name, (): void => {

expect(actual).toBe(webSearchUrl.replace(UeliHelpers.websearchQueryPlaceholder, userInput));
});

it("should replace all whitespace by specified whitespace string if it is set", (): void => {
const userInput = "this contains whitespace";
const webSearchUrl = "https://my-search-engine.com/?query=";
const whitespaceCharacter = "+";
const webSearch = { url: webSearchUrl, whitespaceCharacter } as WebSearch;
const expected = `${webSearchUrl}this+contains+whitespace`;
const actual = WebSearchBuilder.buildExecutionUrl(userInput, webSearch);
expect(actual).toBe(expected);
});
});

describe(WebSearchBuilder.buildSearchResultItem.name, (): void => {
Expand Down Expand Up @@ -72,16 +82,5 @@ describe(WebSearchBuilder.name, (): void => {

expect(actual).toBe(searchTerm);
});

it("should replace all whitespaces of user input with the specified character if it is set", (): void => {
const whitespaceCharacter = "+";
const prefix = "m";
const searchTerm = "this is a string with whitespace";
const userInput = `${prefix}${WebSearchHelpers.webSearchSeparator}${searchTerm}`;
const webSearch = { prefix, whitespaceCharacter } as WebSearch;
const expected = "this+is+a+string+with+whitespace";
const actual = WebSearchBuilder.buildSearchTerm(userInput, webSearch);
expect(actual).toBe(expected);
});
});
});
14 changes: 6 additions & 8 deletions src/ts/builders/web-search-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import { StringHelpers } from "../helpers/string-helpers";

export class WebSearchBuilder {
public static buildSearchTerm(userInput: string, webSearch: WebSearch): string {
let result = userInput.replace(`${webSearch.prefix}${WebSearchHelpers.webSearchSeparator}`, "");

if (webSearch.whitespaceCharacter !== undefined && webSearch.whitespaceCharacter.length > 0) {
result = StringHelpers.replaceWhitespaceWithString(result, webSearch.whitespaceCharacter);
}

return result;
return userInput.replace(`${webSearch.prefix}${WebSearchHelpers.webSearchSeparator}`, "");
}

public static buildExecutionUrl(userInput: string, webSearch: WebSearch): string {
const searchTerm = this.buildSearchTerm(userInput, webSearch);
let searchTerm = this.buildSearchTerm(userInput, webSearch);

if (webSearch.whitespaceCharacter !== undefined && webSearch.whitespaceCharacter.length > 0) {
searchTerm = StringHelpers.replaceWhitespaceWithString(searchTerm, webSearch.whitespaceCharacter);
}

if (webSearch.url !== undefined && webSearch.url.indexOf(UeliHelpers.websearchQueryPlaceholder) > -1) {
return webSearch.url.replace(UeliHelpers.websearchQueryPlaceholder, searchTerm);
Expand Down

0 comments on commit 850b78f

Please sign in to comment.