Skip to content

Commit

Permalink
fix(parse): duplicates & ordering handling
Browse files Browse the repository at this point in the history
Automatically de-duplicate and sort lists of values.
  • Loading branch information
danielrbradley committed Nov 8, 2020
1 parent 0b7868c commit 616d4d5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ function parseField(input: string, field: Field): number[] | undefined {
return range;
}

return input.split(",").map(parseValue);
return Array.from(new Set(input.split(",").map(parseValue))).sort(
(a, b) => a - b
);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,15 @@ test("too few fields", () => {
expect(error.message).toBe("Too few fields");
}
});

test("de-duplicates list items", () => {
expect(parse("1,2,2,3 * * * *")).toEqual(
schedule([0], [1, 2, 3], u, u, u, u, u)
);
});

test("orders list items", () => {
expect(parse("1,3,2 * * * *")).toEqual(
schedule([0], [1, 2, 3], u, u, u, u, u)
);
});

0 comments on commit 616d4d5

Please sign in to comment.