-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/tests/unit/input-validators/variable-input-validator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { VariableInputValidator } from "../../../ts/input-validators/variable-input-validator"; | ||
|
||
describe(VariableInputValidator.name, (): void => { | ||
const validator = new VariableInputValidator(); | ||
describe(validator.isValidForSearchResults.name, (): void => { | ||
it("Should return true if user input has $ as prefix", (): void => { | ||
expect(validator.isValidForSearchResults("$ayyyy")).toBe(true); | ||
}); | ||
it("Should return false if user input doesn't have $ as prefix", (): void => { | ||
expect(validator.isValidForSearchResults(">yoooo")).toBe(false); | ||
expect(validator.isValidForSearchResults("!aklsdjflks")).toBe(false); | ||
expect(validator.isValidForSearchResults("asdasdsad")).toBe(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { VariableSearcher } from "../../../ts/searcher/variable-searcher"; | ||
import { ConfigOptions } from "../../../ts/config-options"; | ||
import { Injector } from "../../../ts/injector"; | ||
import { InputValidator } from "../../../ts/input-validators/input-validator"; | ||
|
||
const defaultConfig = { | ||
directoryVariables: [ | ||
{ | ||
name: "custom", | ||
path: "F:\\Custom\\Path", | ||
}, | ||
], | ||
} as ConfigOptions; | ||
|
||
class FilePathInputValidator implements InputValidator { | ||
public isValidForSearchResults(userInput: string): boolean { | ||
const regex = Injector.getFilePathRegExp("win32"); | ||
return regex.test(userInput); | ||
} | ||
} | ||
|
||
describe(VariableSearcher.name, (): void => { | ||
const fakeVariables = { | ||
Gallery: "E:\\Camera\\DCIM", | ||
notAPath: "420", | ||
topsecretfolder: "D:\\Softwares\\Installers\\temp\\LoliPic", | ||
}; | ||
const searcher = new VariableSearcher(defaultConfig, new FilePathInputValidator(), fakeVariables); | ||
|
||
describe(searcher.getSearchResult.name, (): void => { | ||
it("Should return directory path as execution argument", (): void => { | ||
expect(searcher.getSearchResult("topsecretfolder")[0].executionArgument).toBe(fakeVariables.topsecretfolder); | ||
}); | ||
|
||
it("Should return original name", (): void => { | ||
expect(searcher.getSearchResult("GALLERY")[0].name).toBe("Gallery"); | ||
}); | ||
|
||
it("Should not return variable that isn't a path", (): void => { | ||
expect(searcher.getSearchResult("notAPath").length).toBe(0); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Injector } from "../injector"; | ||
import { InputValidator } from "./input-validator"; | ||
import { platform } from "os"; | ||
|
||
export class VariableInputValidator implements InputValidator { | ||
public readonly variablePrefix = "$"; | ||
public isValidForSearchResults(userInput: string): boolean { | ||
return userInput.startsWith(this.variablePrefix); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Searcher } from "./searcher"; | ||
import { ConfigOptions } from "../config-options"; | ||
import { Injector } from "../injector"; | ||
import { FilePathInputValidator } from "../input-validators/file-path-input-validator"; | ||
import { SearchEngine } from "../search-engine"; | ||
import { SearchResultItem } from "../search-result-item"; | ||
import { homedir, platform } from "os"; | ||
|
||
export class VariableSearcher implements Searcher { | ||
private config: ConfigOptions; | ||
private collection: SearchResultItem[]; | ||
|
||
constructor(config: ConfigOptions, pathValidator: FilePathInputValidator, environmentVariables: { [key: string]: string }) { | ||
this.config = config; | ||
this.collection = []; | ||
|
||
const validator = pathValidator; | ||
const icon = Injector.getIconSet(platform()).variableIcon; | ||
for (const variableName of Object.keys(environmentVariables)) { | ||
const value = environmentVariables[variableName]; | ||
if (validator.isValidForSearchResults(value)) { | ||
this.collection.push({ | ||
executionArgument: value, | ||
icon, | ||
name: `${variableName}`, | ||
tags: [], | ||
}); | ||
} | ||
} | ||
|
||
const customVariable = this.config.directoryVariables; | ||
customVariable.forEach((variable) => { | ||
this.collection.push({ | ||
executionArgument: variable.path, | ||
icon, | ||
name: `${variable.name}`, | ||
tags: [], | ||
}); | ||
}); | ||
} | ||
|
||
public getSearchResult(userInput: string): SearchResultItem[] { | ||
const searchEngine = new SearchEngine(this.collection, this.config.searchEngineThreshold); | ||
return searchEngine.search(userInput); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export class Variable { | ||
public name: string; | ||
public path: string; | ||
} |