Skip to content

Commit

Permalink
update helpers and formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrkulpinski committed Feb 14, 2024
1 parent 3f7b826 commit 3f264dc
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
20 changes: 20 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ export type WithRequired<Type, Key extends keyof Type> = Type & {
export type ArrayElement<ArrayType extends readonly unknown[]> =
ArrayType extends readonly (infer ElementType)[] ? ElementType : never

export type DeepIdx<T, K extends string> = K extends ""
? T
: K extends keyof T
? T[K]
: K extends `${infer K0}.${infer KR}`
? K0 extends keyof T
? DeepIdx<T[K0], KR>
: never
: never

export type ValidatePath<T, K extends string> = K extends ""
? ""
: K extends keyof T
? K
: K extends `${infer K0}.${infer KR}`
? K0 extends keyof T
? `${K0}.${ValidatePath<T[K0], KR>}`
: Extract<keyof T, string>
: Extract<keyof T, string>

export * from "./src/colors/colors"
export * from "./src/errors/errors"
export * from "./src/events/events"
Expand Down
17 changes: 16 additions & 1 deletion src/format/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Currency = Intl.NumberFormatOptions["currency"]
* @param currency The currency to format the amount in. Defaults to 'USD'.
* @returns The formatted currency string.
*/
export const formatCurrency = (amount: number, currency: Currency = "USD"): string => {
export const formatCurrency = (amount: number, currency: Currency = "USD") => {
const formatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency,
Expand All @@ -19,6 +19,21 @@ export const formatCurrency = (amount: number, currency: Currency = "USD"): stri
return formatter.format(amount).replace(/\D00(?=\D*$)/, "")
}

/**
* Formats a given amount with an interval to a currency string.
* @param amount The amount of money to format.
* @param interval The interval, either 'month' or 'year'. Defaults to 'month'.
* @param currency The currency to format the amount in. Defaults to 'USD'.
* @returns The formatted currency string per interval.
*/
export const formatAmount = (
amount: number,
interval: "month" | "year" = "month",
currency: Currency = "USD",
) => {
return formatCurrency(amount / (interval === "year" ? 12 : 1), currency)
}

/**
* Formats a given price into a currency string.
* @param amount The amount of money to format.
Expand Down
18 changes: 18 additions & 0 deletions src/helpers/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
convertNewlines,
getExcerpt,
getInitials,
getShortcutLabel,
isCuid,
isTruthy,
range,
Expand Down Expand Up @@ -45,6 +46,23 @@ describe("toSlugCase", () => {
})
})

describe("getShortcutLabel", () => {
it("returns the uppercase key if metaKey is not provided", () => {
expect(getShortcutLabel({ key: "a" })).toEqual("A")
expect(getShortcutLabel({ key: "z" })).toEqual("Z")
})

it("returns the uppercase key with metaKey symbol if metaKey is true", () => {
expect(getShortcutLabel({ key: "a", metaKey: true })).toEqual("⌘A")
expect(getShortcutLabel({ key: "z", metaKey: true })).toEqual("⌘Z")
})

it("returns the uppercase key without metaKey symbol if metaKey is false", () => {
expect(getShortcutLabel({ key: "a", metaKey: false })).toEqual("A")
expect(getShortcutLabel({ key: "z", metaKey: false })).toEqual("Z")
})
})

describe("stripHtml", () => {
it("strips html tags from a string", () => {
expect(stripHtml("<p>Hello, <strong>world!</strong></p>")).toEqual("Hello, world!")
Expand Down
9 changes: 9 additions & 0 deletions src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ export const toSlugCase = (string?: string) => {
return encodeURI(slug)
}

/**
* Returns a label for the first search key shortcut found.
* @returns The label for the shortcut.
*/
export const getShortcutLabel = ({ key, metaKey }: { key: string; metaKey?: boolean }) => {
const label = `${metaKey ? "⌘" : ""}${key.toUpperCase()}`
return label
}

/**
* Strip html tags from a string
* @param string - string to strip tags from
Expand Down

0 comments on commit 3f264dc

Please sign in to comment.