-
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.
feat: bidirectional unit conversion (#4)
* fix: argument types * ci: cache packages * feat: bidirectional conversion * build: do not export zod from `mod.ts` * build: bump std to `0.208.0`
- Loading branch information
Showing
13 changed files
with
222 additions
and
882 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from "https://deno.land/std@0.202.0/assert/assert.ts" | ||
export * from "https://deno.land/std@0.202.0/assert/assert_equals.ts" | ||
export * from "https://deno.land/std@0.202.0/assert/assert_throws.ts" | ||
export * from "https://deno.land/std@0.208.0/assert/assert.ts" | ||
export * from "https://deno.land/std@0.208.0/assert/assert_equals.ts" | ||
export * from "https://deno.land/std@0.208.0/assert/assert_throws.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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from "https://deno.land/std@0.202.0/collections/mod.ts" | ||
export * from "https://deno.land/std@0.208.0/collections/mod.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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from "https://deno.land/std@0.202.0/fmt/colors.ts" | ||
export * from "https://deno.land/std@0.208.0/fmt/colors.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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from "https://deno.land/std@0.202.0/fs/walk.ts" | ||
export * from "https://deno.land/std@0.208.0/fs/walk.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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from "https://deno.land/std@0.202.0/path/mod.ts" | ||
export * from "https://deno.land/std@0.208.0/path/mod.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
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 |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export { baseCli } from "./utils/cli.ts" | ||
export { queryCli } from "./utils/query.ts" | ||
export { z } from "./deps/zod.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
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 |
---|---|---|
@@ -1,16 +1,31 @@ | ||
import { match } from "../deps/ts_pattern.ts" | ||
import { match, P } from "../deps/ts_pattern.ts" | ||
import { z } from "../deps/zod.ts" | ||
|
||
export const weight = z.custom<`${number} ${WeightUnits}`>((x) => /^\d+ (m|k)?g$/.test(x as string)) | ||
export type WeightUnits = "mg" | "g" | "kg" | ||
export type Weight = z.infer<typeof weight> | ||
|
||
const kilogram = 1000 | ||
|
||
export const fromGrams = (g: number): Weight => | ||
match(g % kilogram) | ||
.with(0, () => `${g / 1000} kg` as const) | ||
.otherwise(() => `${g} g` as const) | ||
|
||
/** converts weight string as grams. */ | ||
export const toGrams = (w: Weight): number => | ||
match(w) | ||
.with(P.string.regex(/(\d+) g/), (g) => parseInt(g, 10)) | ||
.with(P.string.regex(/(\d+) kg/), (kg) => parseInt(kg, 10) * kilogram) | ||
.otherwise(() => 0) | ||
|
||
/** | ||
* converts legacy weight to new weight format. | ||
* @param g legacy weight (`1 unit` = `1g`) | ||
* @return g or kg | ||
*/ | ||
export const fromLegacyWeight = (g: number): Weight => | ||
match(g % 1000) | ||
.with(0, () => `${g / 1000} kg` as const) | ||
.otherwise(() => `${g} g` as const) | ||
export const fromLegacyWeight = fromGrams | ||
|
||
/** multiplies weight string by given factor. */ | ||
export const multiplyWeight = (w: Weight, n: number): Weight => | ||
fromGrams(Math.round(toGrams(w) * n)) |
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