-
-
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
8 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
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
39 changes: 39 additions & 0 deletions
39
src/tests/unit/input-validators/calculator-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,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); | ||
} | ||
}); | ||
}); | ||
}); |
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,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); | ||
} | ||
}); | ||
}); | ||
}); |
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,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; | ||
} | ||
} |
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,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, | ||
]; | ||
} | ||
} |