Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow arbitrary casings of 'now' #34

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/helpers/units.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as chrono from "chrono-node";
import dayjs from "dayjs";
import type { Nullable } from "../types/empty";

Expand Down Expand Up @@ -99,3 +100,17 @@ export function centsToDollars(cents: Cents): number {
export function dollarsToCents(dollars: number): Cents {
return Math.ceil(dollars * 100);
}

export function parseStartDate(startDate: string): Date | "NOW" | null {
const nowRe = /\b(?:"|')?[nN][oO][wW](?:"|')?\b/;
if (nowRe.test(startDate)) {
return "NOW";
}

const chronoDate = chrono.parseDate(startDate);
if (!chronoDate) {
return null;
}

return chronoDate;
}
7 changes: 3 additions & 4 deletions src/lib/buy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { confirm } from "@inquirer/prompts";
import c from "chalk";
import * as chrono from "chrono-node";
import type { Command } from "commander";
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";
Expand All @@ -21,6 +20,7 @@ import {
type Cents,
centsToDollarsFormatted,
computeApproximateDurationSeconds,
parseStartDate,
priceWholeToCents,
roundEndDate,
roundStartDate,
Expand Down Expand Up @@ -117,13 +117,12 @@ async function buyOrderAction(options: SfBuyOptions) {
switch (options.start) {
case null:
case undefined:
case "NOW":
startDate = "NOW";
break;
default: {
const parsed = chrono.parseDate(options.start);
const parsed = parseStartDate(options.start);
if (!parsed) {
return logAndQuit("Invalid start date");
return logAndQuit(`Invalid start date: ${options.start}`);
}
startDate = parsed;
}
Expand Down
Loading