Skip to content

Commit

Permalink
Merge ffa15fd into 03b531c
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhas committed Apr 30, 2018
2 parents 03b531c + ffa15fd commit 5e3535f
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ This is an alt+space launcher for Windows and macOS.
* Press `Enter` to open the file or folder
* Press `Tab` for autocompletion

### Calculator

* Calculate simple math, matrix, symbolic function, convert unit and a lot more.
* Example:
* `23 * 24 / 2 + (6 * 7) ^ 2`
* `1 km/h to mile/h`
* `a = [1, 2, 3]; a * 2`

### Keyboard shortcuts

* `Ctrl+o` to open the selected program or file at it's location
Expand Down Expand Up @@ -160,4 +168,4 @@ $ yarn package

Copyright (c) Oliver Schwendener. All rights reserved.

Licensed under the [MIT](LICENSE) License.
Licensed under the [MIT](LICENSE) License.
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
39 changes: 39 additions & 0 deletions src/tests/unit/input-validators/calculator-input-validator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Injector } from "../../../ts/injector";
import { CalculatorInputValidator } from "../../../ts/input-validators/calculator-input-validator";

describe(CalculatorInputValidator.name, (): void => {
const validator = new CalculatorInputValidator();

describe(validator.isValidForSearchResults.name, (): void => {
it("should return true when passing in a valid argument", (): void => {
const validInputs = [
"10",
"23 * 24 / 2 + (6 * 7) ^ 2",
"1 hundredweight to ton",
"sqrt(pi * 2) / sin(e)",
"[[1,2,3] * 2, [4,5,6]] + [[6,7,8],[9,10,11]]",
"a = 2; b = 3; c = a / b; [a, b, c]",
"pow(2,6) == 2^(re(10i + 6))",
];

for (const validInput of validInputs) {
const actual = validator.isValidForSearchResults(validInput);
expect(actual).toBe(true);
}
});

it("should return false when passing in an invalid argument", (): void => {
const invalidInputs = [
"s",
"kg",
"log()",
"1 blackhole to mg",
];

for (const invalidInput of invalidInputs) {
const actual = validator.isValidForSearchResults(invalidInput);
expect(actual).toBe(false);
}
});
});
});
36 changes: 36 additions & 0 deletions src/tests/unit/searcher/calculator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Config } from "../../../ts/config";
import { SearchResultItem } from "../../../ts/search-result-item";
import { Calculator } from "../../../ts/searcher/calculator";
import { InputOutputCombination } from "../test-helpers";

describe(Calculator.name, (): void => {
const searcher = new Calculator();

describe(searcher.getSearchResult.name, (): void => {
it("should return a correct search result", (): void => {
const combinations = [
{
input: `[[1,2,3] * 2, [4,5,6]] + [[6,7,8],[9,10,11]]`,
output: {
executionArgument: "",
name: "= [[8, 11, 14], [13, 15, 17]]",
} as SearchResultItem,
} as InputOutputCombination,
{
input: "pow(2,6) == 2^(re(10i + 6))",
output: {
executionArgument: "",
name: "= true",
} as SearchResultItem,
} as InputOutputCombination,
];

for (const combination of combinations) {
const actual = searcher.getSearchResult(combination.input);
expect(actual.filter.length).toBe(1);
expect(actual[0].name).toBe(combination.output.name);
expect(actual[0].executionArgument).toBe(combination.output.executionArgument);
}
});
});
});
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
30 changes: 30 additions & 0 deletions src/ts/input-validators/calculator-input-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Config } from "../config";
import { InputValidator } from "./input-validator";
import * as math from "mathjs";

export class CalculatorInputValidator implements InputValidator {
public isValidForSearchResults(userInput: string): boolean {
let result;
try {
// Mathjs throws an error when input cannot be evaluated
result = math.eval(userInput);
} catch (e) {
return false;
}
return !isNaN(result) || this.isValidObject(result) || false;
}

private isValidObject(input: any): boolean {
if (typeof(input) !== "object") {
return false;
}

const mathType = math.typeof(input);

if (mathType === "Unit" && input.value === null) {
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 5e3535f

Please sign in to comment.