-
Notifications
You must be signed in to change notification settings - Fork 0
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
Johan
committed
Oct 16, 2019
1 parent
186989e
commit 93a67ba
Showing
11 changed files
with
180 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { StringComparison } from "../types"; | ||
import { create, empty, ParseResult } from "../types/ParseResult"; | ||
import { parse } from "./enumHelper"; | ||
import { swap } from "./mapHelper"; | ||
|
||
export enum CronSchedule { | ||
Custom, | ||
Daily, | ||
Hourly, | ||
Monthly, | ||
Weekly, | ||
Yearly | ||
} | ||
|
||
const cronScheduleToTab = new Map<CronSchedule, string>([ | ||
[CronSchedule.Daily, "0 0 * * *"], | ||
[CronSchedule.Hourly, "0 * * * *"], | ||
[CronSchedule.Monthly, "0 0 1 * *"], | ||
[CronSchedule.Weekly, "0 0 * * 0"], | ||
[CronSchedule.Yearly, "0 0 1 1 *"] | ||
]); | ||
|
||
const cronTabToSchedule = swap(cronScheduleToTab); | ||
|
||
export const parseSchedule = (cronTab: string): ParseResult<CronSchedule> => | ||
create(cronTabToSchedule.get(cronTab)); | ||
|
||
export const parseCronTab = ( | ||
schedule: CronSchedule | string | ||
): ParseResult<string> => { | ||
const key = | ||
typeof schedule === "string" | ||
? parse(CronSchedule, schedule, StringComparison.OrdinalIgnoreCase).value | ||
: schedule; | ||
|
||
const value = (key && cronScheduleToTab.get(key)) || undefined; | ||
|
||
return create(value); | ||
}; |
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,28 @@ | ||
import { ParseResult, StringComparison } from "../types"; | ||
|
||
export { StringComparison }; | ||
|
||
export const parse = <T>( | ||
enumType: T, | ||
value: string, | ||
comparision = StringComparison.Ordinal | ||
): ParseResult<T[keyof T]> => { | ||
const result: ParseResult<T[keyof T]> = { | ||
hasValue: false, | ||
value: undefined | ||
}; | ||
|
||
const matchingKey = Object.keys(enumType).find(x => | ||
comparision === StringComparison.OrdinalIgnoreCase | ||
? x.toLowerCase() === value.toLowerCase() | ||
: x === value | ||
); | ||
|
||
if (matchingKey !== undefined) { | ||
result.value = enumType[matchingKey as keyof typeof enumType]; | ||
} | ||
|
||
result.hasValue = result.value !== undefined; | ||
|
||
return result; | ||
}; |
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,5 @@ | ||
export const swap = <K, V>(map: Map<K, V>): Map<V, K> => { | ||
return new Map<V, K>( | ||
[...map.entries()].map((value: [K, V]) => [value[1], value[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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export interface ParseResult<T> { | ||
hasValue: boolean; | ||
value?: T; | ||
} | ||
|
||
export const empty = <T>(): ParseResult<T> => ({ hasValue: false }); | ||
|
||
export const create = <T>(value?: T): ParseResult<T> => ({ | ||
value, | ||
hasValue: value !== undefined | ||
}); |
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 enum StringComparison { | ||
Ordinal, | ||
OrdinalIgnoreCase | ||
} |
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,7 @@ | ||
describe("jobs", () => { | ||
describe("add", () => { | ||
it("should work", () => { | ||
test.todo("todo"); | ||
}); | ||
}); | ||
}); |
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,20 @@ | ||
import { | ||
parseCronTab, | ||
parseSchedule, | ||
CronSchedule | ||
} from "../src/lib/cronParser"; | ||
|
||
const daily = "0 0 * * *"; | ||
|
||
describe("cronParser", () => { | ||
describe("asCronTab", () => { | ||
it("can parse string", () => { | ||
expect(parseCronTab("Daily").value).toBe(daily); | ||
expect(parseCronTab("daily").value).toBe(daily); | ||
}); | ||
|
||
it("can parse cron schedule", () => { | ||
expect(parseSchedule(daily).value).toBe(CronSchedule.Daily); | ||
}); | ||
}); | ||
}); |
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,42 @@ | ||
import { parse, StringComparison } from "../src/lib/enumHelper"; | ||
|
||
enum TestEnum { | ||
First, | ||
Second | ||
} | ||
|
||
describe("enumHelper", () => { | ||
describe("parse", () => { | ||
it("should have hasValue set to true when value matches enum value", () => { | ||
expect(parse(TestEnum, "Second").hasValue).toBe(true); | ||
}); | ||
|
||
it("should have hasValue set to false when value matches enum value with other casing", () => { | ||
expect(parse(TestEnum, "sEcONd").hasValue).toBe(false); | ||
}); | ||
|
||
it("should have hasValue set to true when value matches enum value and ignoring case", () => { | ||
expect( | ||
parse(TestEnum, "second", StringComparison.OrdinalIgnoreCase).hasValue | ||
).toBe(true); | ||
}); | ||
|
||
it("should have hasValue set to false when value is invalid", () => { | ||
expect(parse(TestEnum, "invalid").hasValue).toBe(false); | ||
}); | ||
|
||
it("should have value defined", () => { | ||
expect(parse(TestEnum, "Second").value).toBeDefined(); | ||
expect( | ||
parse(TestEnum, "second", StringComparison.OrdinalIgnoreCase).value | ||
).toBeDefined(); | ||
}); | ||
|
||
it("should have value undefined", () => { | ||
expect(parse(TestEnum, "invalid").value).toBeUndefined(); | ||
expect( | ||
parse(TestEnum, "second", StringComparison.Ordinal).value | ||
).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
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,18 @@ | ||
import { swap } from "../src/lib/mapHelper"; | ||
|
||
describe("mapHelper", () => { | ||
describe("swap", () => { | ||
it("can swap keys with values", () => { | ||
const map = new Map<string, string>([ | ||
["key1", "value1"], | ||
["key2", "value2"] | ||
]); | ||
|
||
const result = swap(map); | ||
|
||
expect(result.size).toBe(2); | ||
expect(result.get("value1")).toBe("key1"); | ||
expect(result.get("value2")).toBe("key2"); | ||
}); | ||
}); | ||
}); |