-
-
Notifications
You must be signed in to change notification settings - Fork 70
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
1 parent
42ab814
commit 48b2274
Showing
8 changed files
with
206 additions
and
13 deletions.
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
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -1,7 +1,61 @@ | ||
import { Result } from "./result"; | ||
|
||
export default interface IQuery { | ||
name: string; | ||
filter: string; | ||
autorefresh?: number; | ||
sorting?: string[]; | ||
group: boolean; | ||
} | ||
|
||
const possibleSortingOptions = ["date", "priority"]; | ||
|
||
export function parseQuery(query: any): Result<IQuery, Error> { | ||
if (!query.hasOwnProperty("name")) { | ||
return Result.Err(new Error("Missing 'name' field in query.")); | ||
} | ||
|
||
if (!query.hasOwnProperty("filter")) { | ||
return Result.Err(new Error("Missing 'filter' field in query")); | ||
} | ||
|
||
if ( | ||
query.hasOwnProperty("autorefresh") && | ||
(isNaN(query.autorefresh) || (query.autorefresh as number) < 0) | ||
) { | ||
return Result.Err( | ||
new Error("'autorefresh' field must be a positive number.") | ||
); | ||
} | ||
|
||
if (query.hasOwnProperty("sorting")) { | ||
if (!Array.isArray(query.sorting)) { | ||
return Result.Err( | ||
new Error( | ||
"'sorting' field must be an array of strings within the set ['date', 'priority']." | ||
) | ||
); | ||
} | ||
|
||
const sorting = query.sorting as any[]; | ||
|
||
for (const element of sorting) { | ||
if ( | ||
!(typeof element == "string") || | ||
possibleSortingOptions.indexOf(element as string) == -1 | ||
) { | ||
return Result.Err( | ||
new Error( | ||
"'sorting' field must be an array of strings within the set ['date', 'priority']." | ||
) | ||
); | ||
} | ||
} | ||
} | ||
|
||
if (query.hasOwnProperty("group") && typeof query.group != "boolean") { | ||
return Result.Err(new Error("'group' field must be a boolean.")); | ||
} | ||
|
||
return Result.Ok(query as IQuery); | ||
} |
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,126 @@ | ||
import "mocha"; | ||
import { assert } from "chai"; | ||
import { parseQuery } from "../src/query"; | ||
|
||
describe("Query parsing", () => { | ||
it("Name must exist", () => { | ||
const obj = { | ||
filter: "foo", | ||
}; | ||
|
||
const result = parseQuery(obj); | ||
assert.isTrue(result.isErr()); | ||
}); | ||
|
||
it("Filter must exist", () => { | ||
const obj = { | ||
name: "Tasks", | ||
}; | ||
|
||
const result = parseQuery(obj); | ||
assert.isTrue(result.isErr()); | ||
}); | ||
|
||
it("Only name & filter are required", () => { | ||
const obj = { | ||
name: "Tasks", | ||
filter: "foo", | ||
}; | ||
|
||
const result = parseQuery(obj); | ||
assert.isTrue(result.isOk()); | ||
}); | ||
|
||
it("Autorefresh must be a number", () => { | ||
const obj = { | ||
name: "Tasks", | ||
filter: "foo", | ||
autorefresh: "NaN", | ||
}; | ||
|
||
const result = parseQuery(obj); | ||
assert.isTrue(result.isErr()); | ||
}); | ||
|
||
it("Autorefresh cannot be negative", () => { | ||
const obj = { | ||
name: "Tasks", | ||
filter: "foo", | ||
autorefresh: -1, | ||
}; | ||
|
||
const result = parseQuery(obj); | ||
assert.isTrue(result.isErr()); | ||
}); | ||
|
||
it("Valid autorefresh values pass", () => { | ||
const obj = { | ||
name: "Tasks", | ||
filter: "foo", | ||
autorefresh: 1, | ||
}; | ||
|
||
const result = parseQuery(obj); | ||
assert.isTrue(result.isOk()); | ||
}); | ||
|
||
it("Sorting must be an array", () => { | ||
const obj = { | ||
name: "Tasks", | ||
filter: "foo", | ||
sorting: "Not an array", | ||
}; | ||
|
||
const result = parseQuery(obj); | ||
assert.isTrue(result.isErr()); | ||
|
||
const other = { | ||
name: "Tasks", | ||
filter: "foo", | ||
sorting: [], | ||
}; | ||
|
||
const otherResult = parseQuery(other); | ||
assert.isTrue(otherResult.isOk()); | ||
}); | ||
|
||
it("Sorting can only be a specified set of options", () => { | ||
const obj = { | ||
name: "Tasks", | ||
filter: "filter", | ||
sorting: ["not-valid"], | ||
}; | ||
|
||
const result = parseQuery(obj); | ||
assert.isTrue(result.isErr()); | ||
|
||
const validObj = { | ||
name: "Tasks", | ||
filter: "filter", | ||
sorting: ["date", "priority"], | ||
}; | ||
|
||
const otherResult = parseQuery(validObj); | ||
assert.isTrue(otherResult.isOk()); | ||
}); | ||
|
||
it("Group must be a boolean", () => { | ||
const obj = { | ||
name: "Tasks", | ||
filter: "Filter", | ||
group: "not-a-boolean", | ||
}; | ||
|
||
const result = parseQuery(obj); | ||
assert.isTrue(result.isErr()); | ||
|
||
const validObj = { | ||
name: "Tasks", | ||
filter: "Filter", | ||
group: true, | ||
}; | ||
|
||
const otherResult = parseQuery(validObj); | ||
assert.isTrue(otherResult.isOk()); | ||
}); | ||
}); |