Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

Commit

Permalink
Added possibility to specify a whitespace character for web search en…
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Dec 23, 2018
1 parent 366b717 commit 8fb2941
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions main.html
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ <h1 class="setting-section-title">User Settings</h1>
<th>Name</th>
<th>Prefix</th>
<th>Url</th>
<th>Whitespace Character</th>
<th>Priority</th>
<th class="text-center">Fallback</th>
<th>Icon</th>
Expand All @@ -586,6 +587,7 @@ <h1 class="setting-section-title">User Settings</h1>
<td><input class="setting-text-input" type="text" v-model="webSearch.name" v-on:blur="updateUserConfig"></td>
<td><input class="setting-text-input" type="text" v-model="webSearch.prefix" v-on:blur="updateUserConfig"></td>
<td><input class="setting-text-input" type="text" v-model="webSearch.url" v-on:blur="updateUserConfig"></td>
<td><input class="setting-text-input" type="text" v-model="webSearch.whitespaceCharacter" v-on:blur="updateUserConfig"></td>
<td><input class="setting-text-input" type="number" min="0" v-model="webSearch.priority" v-on:blur="updateUserConfig" :disabled="!webSearch.isFallback"></td>
<td class="text-center"><input type="checkbox" v-model="webSearch.isFallback" v-on:blur="updateUserConfig"></td>
<td><input class="setting-text-input" type="text" v-model="webSearch.icon" v-on:blur="updateUserConfig"></td>
Expand All @@ -602,6 +604,7 @@ <h1 class="setting-section-title">User Settings</h1>
<td><input type="text" class="setting-text-input" v-model="configEdit.newWebSearch.name" placeholder="Name"></td>
<td><input type="text" class="setting-text-input" v-model="configEdit.newWebSearch.prefix" placeholder="Prefix"></td>
<td><input type="text" class="setting-text-input" v-model="configEdit.newWebSearch.url" placeholder="URL"></td>
<td><input type="text" class="setting-text-input" v-model="configEdit.newWebSearch.whitespaceCharacter" placeholder="Whitespace Character"></td>
<td><input type="number" class="setting-text-input" min="0" v-model="configEdit.newWebSearch.priority" placeholder="Priority" :disabled="!configEdit.newWebSearch.isFallback"></td>
<td class="text-center"><input type="checkbox" v-model="configEdit.newWebSearch.isFallback"></td>
<td><input type="text" class="setting-text-input" v-model="configEdit.newWebSearch.icon" placeholder="Icon"></td>
Expand Down
11 changes: 11 additions & 0 deletions src/tests/unit/builders/web-search-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,16 @@ 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);
});
});
});
10 changes: 10 additions & 0 deletions src/tests/unit/helpers/string-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,14 @@ describe(StringHelpers.name, (): void => {
}
});
});

describe(StringHelpers.replaceWhitespaceWithString.name, (): void => {
it("should replace whitespace of string with a specified string", (): void => {
const stringWithWhiteSpace = "this is a normal string with whitespace";
const stringToInsert = "+";
const expected = "this+is+a+normal+string+with+whitespace";
const actual = StringHelpers.replaceWhitespaceWithString(stringWithWhiteSpace, stringToInsert);
expect(actual).toBe(expected);
});
});
});
9 changes: 8 additions & 1 deletion src/ts/builders/web-search-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ import { WebSearch } from "../web-search";
import { WebSearchHelpers } from "../helpers/web-search-helper";
import { SearchResultItem } from "../search-result-item";
import { UeliHelpers } from "../helpers/ueli-helpers";
import { StringHelpers } from "../helpers/string-helpers";

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

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

return result;
}

public static buildExecutionUrl(userInput: string, webSearch: WebSearch): string {
Expand Down
4 changes: 4 additions & 0 deletions src/ts/helpers/string-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class StringHelpers {
return value.replace(/\s\s+/g, " ").trim();
}

public static replaceWhitespaceWithString(stringWithWhitespace: string, stringToInsert: string) {
return stringWithWhitespace.replace(/\s+/g, stringToInsert);
}

public static stringToWords(value: string): string[] {
const words = value.split(/\s/g);
return words.filter((w): boolean => {
Expand Down
1 change: 1 addition & 0 deletions src/ts/web-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface WebSearch {
icon: string;
priority: number;
isFallback: boolean;
whitespaceCharacter?: string;
}

0 comments on commit 8fb2941

Please sign in to comment.