-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: MAJ Campagne Nantes Métropole (#2699)
* fix PDLL 2024 limits * make mode() accept more than 3 params * refactor and add tests for outside rules * improvements * improvements * add booster to PDLL * improvements * remove console.log * fix range
- Loading branch information
1 parent
5e4aa33
commit 5ea734d
Showing
9 changed files
with
1,096 additions
and
743 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Generate a list of dates between two dates | ||
* | ||
* @example | ||
* const boosterDates = [ | ||
* ...dateRange("2022-01-01", "2022-01-03"), | ||
* ]; | ||
* | ||
* @param dates | ||
* @returns | ||
*/ | ||
export function dateRange(...dates: Array<Date | string>): string[] { | ||
if (dates.length === 0) { | ||
throw new Error("At least one date is required"); | ||
} | ||
|
||
const [first, ...rest] = castAndSort(dates); | ||
const last = rest.pop(); | ||
|
||
return last ? fill(first, last) : [format(first)]; | ||
} | ||
|
||
function castAndSort(range: Array<Date | string>): Set<Date> { | ||
return range | ||
.map((d) => { | ||
const date = typeof d === "string" ? new Date(d) : d; | ||
if (Number.isNaN(date.getTime())) { | ||
throw new Error(`Invalid Date`); | ||
} | ||
date.setHours(0, 0, 0, 0); | ||
return date; | ||
}) | ||
.sort((a, b) => a.getTime() - b.getTime()) | ||
.reduce((acc, d) => { | ||
acc.add(d); | ||
return acc; | ||
}, new Set<Date>()); | ||
} | ||
|
||
function format(d: Date): string { | ||
const year = d.getFullYear(); | ||
const month = String(d.getMonth() + 1).padStart(2, "0"); | ||
const day = String(d.getDate()).padStart(2, "0"); | ||
return `${year}-${month}-${day}`; | ||
} | ||
|
||
function fill(first: Date, last: Date): Array<string> { | ||
const dates = new Set<string>(); | ||
const current = new Date(first); | ||
while (current <= last) { | ||
dates.add(format(current)); | ||
current.setDate(current.getDate() + 1); | ||
} | ||
|
||
return [...dates]; | ||
} |
98 changes: 98 additions & 0 deletions
98
api/src/pdc/services/policy/engine/helpers/dateRange.unit.spec.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,98 @@ | ||
import { assertEquals, describe, it } from "@/dev_deps.ts"; | ||
import { dateRange } from "./dateRange.ts"; | ||
|
||
describe("dateRange", () => { | ||
it("should return a range of dates", () => { | ||
const start = new Date("2022-01-01"); | ||
const end = new Date("2022-01-03"); | ||
const result = dateRange(start, end); | ||
assertEquals(result, ["2022-01-01", "2022-01-02", "2022-01-03"]); | ||
}); | ||
|
||
it("should return a range of dates when given strings", () => { | ||
const start = "2022-01-01"; | ||
const end = "2022-01-03"; | ||
const result = dateRange(start, end); | ||
assertEquals(result, ["2022-01-01", "2022-01-02", "2022-01-03"]); | ||
}); | ||
|
||
it("should return a range over several months", () => { | ||
const start = "2022-01-30"; | ||
const end = "2022-02-02"; | ||
const result = dateRange(start, end); | ||
assertEquals(result, ["2022-01-30", "2022-01-31", "2022-02-01", "2022-02-02"]); | ||
}); | ||
|
||
it("should sort input dates", () => { | ||
const start = "2022-01-03"; | ||
const end = "2022-01-01"; | ||
const result = dateRange(start, end); | ||
assertEquals(result, ["2022-01-01", "2022-01-02", "2022-01-03"]); | ||
}); | ||
|
||
it("should sort more than two dates", () => { | ||
const start = "2022-01-03"; | ||
const middle = "2022-01-02"; | ||
const end = "2022-01-01"; | ||
const result = dateRange(start, middle, end); | ||
assertEquals(result, ["2022-01-01", "2022-01-02", "2022-01-03"]); | ||
}); | ||
|
||
it("should handle no input", () => { | ||
try { | ||
dateRange(); | ||
} catch (e) { | ||
assertEquals(e.message, "At least one date is required"); | ||
} | ||
}); | ||
|
||
it("should handle invalid date format", () => { | ||
const start = "2022-13-45"; | ||
const end = "2022-01-03"; | ||
try { | ||
dateRange(start, end); | ||
} catch (e) { | ||
assertEquals(e.message, "Invalid Date"); | ||
} | ||
}); | ||
|
||
it("should handle malformed date strings", () => { | ||
const start = "not-a-date"; | ||
const end = "2022-01-03"; | ||
try { | ||
dateRange(start, end); | ||
} catch (e) { | ||
assertEquals(e.message, "Invalid Date"); | ||
} | ||
}); | ||
|
||
it("should handle empty string input", () => { | ||
const start = ""; | ||
const end = "2022-01-03"; | ||
try { | ||
dateRange(start, end); | ||
} catch (e) { | ||
assertEquals(e.message, "Invalid Date"); | ||
} | ||
}); | ||
|
||
it("should handle single date input", () => { | ||
const date = "2022-01-01"; | ||
const result = dateRange(date); | ||
assertEquals(result, ["2022-01-01"]); | ||
}); | ||
|
||
it("should handle same date for start and end", () => { | ||
const start = "2022-01-01"; | ||
const end = "2022-01-01"; | ||
const result = dateRange(start, end); | ||
assertEquals(result, ["2022-01-01"]); | ||
}); | ||
|
||
it("should handle large date ranges", () => { | ||
const start = "2022-01-01"; | ||
const end = "2022-12-31"; | ||
const result = dateRange(start, end); | ||
assertEquals(result.length, 365); | ||
}); | ||
}); |
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
Oops, something went wrong.