Skip to content

Commit

Permalink
Merge 4487b6d into 03b531c
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhas committed Apr 29, 2018
2 parents 03b531c + 4487b6d commit 9fd527c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"devDependencies": {
"@types/electron-is-dev": "^0.3.0",
"@types/jest": "^22.1.3",
"@types/mathjs": "^3.20.0",
"coveralls": "^3.0.0",
"css-loader": "^0.28.11",
"electron": "^1.7.12",
Expand All @@ -49,6 +50,7 @@
"file-loader": "^1.1.11",
"husky": "^0.15.0-rc.8",
"jest": "^22.4.2",
"mathjs": "^4.1.2",
"node-sass": "^4.7.2",
"sass-loader": "^6.0.7",
"style-loader": "^0.20.3",
Expand Down
4 changes: 4 additions & 0 deletions src/ts/execution-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export class ExecutionService {
}

public execute(executionArgument: string): void {
if (!executionArgument) {
return;
}

for (const combi of this.validatorExecutorCombinations) {
if (combi.validator.isValidForExecution(executionArgument)) {
combi.executor.execute(executionArgument);
Expand Down
6 changes: 6 additions & 0 deletions src/ts/input-validation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ import { WebUrlSearcher } from "./searcher/web-url-searcher";
import { InputValidatorSearcherCombination } from "./input-validator-searcher-combination";
import { EmailAddressSearcher } from "./searcher/email-address-searcher";
import { EmailAddressInputValidator } from "./input-validators/email-address-input-validator";
import { CalculatorInputValidator } from "./input-validators/calculator-input-validator";
import { Calculator } from "./searcher/calculator";

export class InputValidationService {
private validatorSearcherCombinations = [
{
searcher: new Calculator(),
validator: new CalculatorInputValidator(),
},
{
searcher: new FilePathSearcher(),
validator: new FilePathInputValidator(),
Expand Down
15 changes: 15 additions & 0 deletions src/ts/input-validators/calculator-input-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Config } from "../config";
import { InputValidator } from "./input-validator";
import * as math from "mathjs";

export class CalculatorInputValidator implements InputValidator {
public isValidForSearchResults(userInput: string): boolean {
try {
// Mathjs throws an error when input cannot be evaluate
math.eval(userInput);
} catch (e) {
return false;
}
return true;
}
}
22 changes: 22 additions & 0 deletions src/ts/searcher/calculator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Config } from "../config";
import { SearchResultItem } from "../search-result-item";
import { Searcher } from "./searcher";
import * as math from "mathjs";

export class Calculator implements Searcher {
private icon = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 32 32" version="1.1">
<path xmlns="http://www.w3.org/2000/svg" d="M26.133,0c1.177,0 2.134,0.957 2.134,2.133l0,27.734c0,1.176 -0.957,2.133 -2.134,2.133l-20.266,0c-1.177,0 -2.134,-0.957 -2.134,-2.133l0,-27.734c0,-1.176 0.957,-2.133 2.134,-2.133l20.266,0Zm1.067,29.867l0,-27.734c0,-0.588 -0.478,-1.066 -1.067,-1.066l-20.266,0c-0.589,0 -1.067,0.478 -1.067,1.066l0,27.734c0,0.588 0.478,1.066 1.067,1.066l20.266,0c0.589,0 1.067,-0.478 1.067,-1.066l0,0Zm-17.6,-4.267c0.294,0 0.533,0.238 0.533,0.533l0,2.667c0,0.295 -0.239,0.533 -0.533,0.533l-3.2,0c-0.294,0 -0.533,-0.238 -0.533,-0.533l0,-2.667c0,-0.295 0.239,-0.533 0.533,-0.533l3.2,0Zm5.333,0c0.295,0 0.534,0.238 0.534,0.533l0,2.667c0,0.295 -0.239,0.533 -0.534,0.533l-3.2,0c-0.294,0 -0.533,-0.238 -0.533,-0.533l0,-2.667c0,-0.295 0.239,-0.533 0.533,-0.533l3.2,0Zm5.334,0c0.295,0 0.533,0.238 0.533,0.533l0,2.667c0,0.295 -0.238,0.533 -0.533,0.533l-3.2,0c-0.295,0 -0.534,-0.238 -0.534,-0.533l0,-2.667c0,-0.295 0.239,-0.533 0.534,-0.533l3.2,0Zm5.333,0c0.295,0 0.533,0.238 0.533,0.533l0,2.667c0,0.295 -0.238,0.533 -0.533,0.533l-3.2,0c-0.295,0 -0.533,-0.238 -0.533,-0.533l0,-2.667c0,-0.295 0.238,-0.533 0.533,-0.533l3.2,0Zm-16.533,2.667l0,-1.6l-2.134,0l0,1.6l2.134,0Zm5.333,0l0,-1.6l-2.133,0l0,1.6l2.133,0Zm5.333,0l0,-1.6l-2.133,0l0,1.6l2.133,0Zm5.334,0l0,-1.6l-2.134,0l0,1.6l2.134,0Zm-15.467,-7.467c0.294,0 0.533,0.238 0.533,0.533l0,2.667c0,0.295 -0.239,0.533 -0.533,0.533l-3.2,0c-0.294,0 -0.533,-0.238 -0.533,-0.533l0,-2.667c0,-0.295 0.239,-0.533 0.533,-0.533l3.2,0Zm5.333,0c0.295,0 0.534,0.238 0.534,0.533l0,2.667c0,0.295 -0.239,0.533 -0.534,0.533l-3.2,0c-0.294,0 -0.533,-0.238 -0.533,-0.533l0,-2.667c0,-0.295 0.239,-0.533 0.533,-0.533l3.2,0Zm5.334,0c0.295,0 0.533,0.238 0.533,0.533l0,2.667c0,0.295 -0.238,0.533 -0.533,0.533l-3.2,0c-0.295,0 -0.534,-0.238 -0.534,-0.533l0,-2.667c0,-0.295 0.239,-0.533 0.534,-0.533l3.2,0Zm5.333,0c0.295,0 0.533,0.238 0.533,0.533l0,2.667c0,0.295 -0.238,0.533 -0.533,0.533l-3.2,0c-0.295,0 -0.533,-0.238 -0.533,-0.533l0,-2.667c0,-0.295 0.238,-0.533 0.533,-0.533l3.2,0Zm-16.533,2.667l0,-1.6l-2.134,0l0,1.6l2.134,0Zm5.333,0l0,-1.6l-2.133,0l0,1.6l2.133,0Zm5.333,0l0,-1.6l-2.133,0l0,1.6l2.133,0Zm5.334,0l0,-1.6l-2.134,0l0,1.6l2.134,0Zm-15.467,-7.467c0.294,0 0.533,0.238 0.533,0.533l0,2.667c0,0.295 -0.239,0.533 -0.533,0.533l-3.2,0c-0.294,0 -0.533,-0.238 -0.533,-0.533l0,-2.667c0,-0.295 0.239,-0.533 0.533,-0.533l3.2,0Zm16,0c0.295,0 0.533,0.238 0.533,0.533l0,2.667c0,0.295 -0.238,0.533 -0.533,0.533l-3.2,0c-0.295,0 -0.533,-0.238 -0.533,-0.533l0,-2.667c0,-0.295 0.238,-0.533 0.533,-0.533l3.2,0Zm-10.667,0c0.295,0 0.534,0.238 0.534,0.533l0,2.667c0,0.295 -0.239,0.533 -0.534,0.533l-3.2,0c-0.294,0 -0.533,-0.238 -0.533,-0.533l0,-2.667c0,-0.295 0.239,-0.533 0.533,-0.533l3.2,0Zm5.334,0c0.295,0 0.533,0.238 0.533,0.533l0,2.667c0,0.295 -0.238,0.533 -0.533,0.533l-3.2,0c-0.295,0 -0.534,-0.238 -0.534,-0.533l0,-2.667c0,-0.295 0.239,-0.533 0.534,-0.533l3.2,0Zm-11.2,2.667l0,-1.6l-2.134,0l0,1.6l2.134,0Zm5.333,0l0,-1.6l-2.133,0l0,1.6l2.133,0Zm5.333,0l0,-1.6l-2.133,0l0,1.6l2.133,0Zm5.334,0l0,-1.6l-2.134,0l0,1.6l2.134,0Zm-16.534,-6.934c0.882,0 1.6,0.718 1.6,1.6c0,0.882 -0.718,1.6 -1.6,1.6l-1.066,0c-0.882,0 -1.6,-0.718 -1.6,-1.6c0,-0.882 0.717,-1.6 1.6,-1.6l1.066,0Zm5.334,0c0.882,0 1.6,0.718 1.6,1.6c0,0.882 -0.718,1.6 -1.6,1.6l-1.067,0c-0.882,0 -1.6,-0.718 -1.6,-1.6c0,-0.882 0.718,-1.6 1.6,-1.6l1.067,0Zm5.333,0c0.882,0 1.6,0.718 1.6,1.6c0,0.882 -0.718,1.6 -1.6,1.6l-1.067,0c-0.882,0 -1.6,-0.718 -1.6,-1.6c0,-0.882 0.718,-1.6 1.6,-1.6l1.067,0Zm5.333,0c0.882,0 1.6,0.718 1.6,1.6c0,0.882 -0.718,1.6 -1.6,1.6l-1.066,0c-0.882,0 -1.6,-0.718 -1.6,-1.6c0,-0.882 0.717,-1.6 1.6,-1.6l1.066,0Zm-16,1.067l-1.066,0c-0.294,0 -0.534,0.24 -0.534,0.533c0,0.294 0.24,0.534 0.534,0.534l1.066,0c0.294,0 0.534,-0.24 0.534,-0.534c0,-0.293 -0.24,-0.533 -0.534,-0.533Zm5.334,1.067c0.294,0 0.533,-0.24 0.533,-0.534c0,-0.294 -0.239,-0.533 -0.533,-0.533l-1.067,0c-0.294,0 -0.533,0.239 -0.533,0.533c0,0.294 0.239,0.534 0.533,0.534l1.067,0Zm5.333,-1.067l-1.067,0c-0.294,0 -0.533,0.24 -0.533,0.533c0,0.294 0.239,0.534 0.533,0.534l1.067,0c0.294,0 0.533,-0.24 0.533,-0.534c0,-0.293 -0.239,-0.533 -0.533,-0.533Zm5.333,1.067c0.295,0 0.534,-0.24 0.534,-0.534c0,-0.294 -0.239,-0.533 -0.534,-0.533l-1.066,0c-0.295,0 -0.534,0.239 -0.534,0.533c0,0.294 0.239,0.534 0.534,0.534l1.066,0Zm1.067,-10.667c0.295,0 0.533,0.239 0.533,0.533l0,6.4c0,0.295 -0.238,0.534 -0.533,0.534l-19.2,0c-0.294,0 -0.533,-0.239 -0.533,-0.534l0,-6.4c0,-0.294 0.239,-0.533 0.533,-0.533l19.2,0Zm-0.533,6.4l0,-5.333l-18.134,0l0,5.333l18.134,0Zm-2.134,-2.133l1.067,0l0,1.066l-1.067,0l0,-1.066Zm-2.133,0l1.067,0l0,1.066l-1.067,0l0,-1.066Zm-2.133,0l1.066,0l0,1.066l-1.066,0l0,-1.066Zm-1.067,0l0,1.066l-1.067,0l0,-1.066l1.067,0Z" style="fill:#fff;"/>
</svg>`;

public getSearchResult(userInput: string): SearchResultItem[] {
const result = math.eval(userInput);
return [
{
executionArgument: "",
icon: this.icon,
name: `= ${result}`,
tags: [],
} as SearchResultItem,
];
}
}

0 comments on commit 9fd527c

Please sign in to comment.