diff --git a/packages/csv/src/transforms.ts b/packages/csv/src/transforms.ts index 76ab932d98..7795168ca3 100644 --- a/packages/csv/src/transforms.ts +++ b/packages/csv/src/transforms.ts @@ -21,6 +21,13 @@ export const hex = (x) => maybeParseInt(x, defaultVal, 16); +export const date = + (defaultVal = 0): CellTransform => + (x) => { + const res = Date.parse(x); + return isNaN(res) ? defaultVal : res; + }; + // formatters export const zeroPad = (digits: number) => padLeft(digits, "0"); diff --git a/packages/strings/README.md b/packages/strings/README.md index 36dcf70a8c..43106567aa 100644 --- a/packages/strings/README.md +++ b/packages/strings/README.md @@ -32,7 +32,7 @@ This project is part of the ## About -Various (80+) string formatting, word wrapping & utility functions, some +Various (~100) string formatting, word wrapping & utility functions, some higher-order, some memoized. Partially based on Clojure version of [thi.ng/strf](http://thi.ng/strf). @@ -48,6 +48,7 @@ Partially based on Clojure version of [thi.ng/strf](http://thi.ng/strf). ### Numeric formatters +- `currency` / `chf` / `eur` / `gpb` / `usd` / `yen` - `radix` - `int` / `intLocale` - `float` / `floatFixedWidth` @@ -142,7 +143,7 @@ node --experimental-repl-await > const strings = await import("@thi.ng/strings"); ``` -Package sizes (gzipped, pre-treeshake): ESM: 4.53 KB +Package sizes (gzipped, pre-treeshake): ESM: 4.63 KB ## Dependencies diff --git a/packages/strings/package.json b/packages/strings/package.json index 686a9afca5..12aa164c52 100644 --- a/packages/strings/package.json +++ b/packages/strings/package.json @@ -97,6 +97,9 @@ "./center": { "import": "./center.js" }, + "./currency": { + "import": "./currency.js" + }, "./cursor": { "import": "./cursor.js" }, diff --git a/packages/strings/src/currency.ts b/packages/strings/src/currency.ts new file mode 100644 index 0000000000..5819568457 --- /dev/null +++ b/packages/strings/src/currency.ts @@ -0,0 +1,19 @@ +import { memoizeJ } from "@thi.ng/memoize/memoizej"; +import type { Stringer } from "./api.js"; + +export const currency = < + (sym: string, pre?: boolean, prec?: number) => Stringer +>memoizeJ((sym: string, pre = true, prec = 2) => { + const ff = (x: number) => x.toFixed(prec); + return pre ? (x: number) => sym + ff(x) : (x: number) => ff(x) + sym; +}); + +export const chf = currency("CHF "); + +export const eur = currency("€"); + +export const gbp = currency("£"); + +export const usd = currency("$"); + +export const yen = currency("¥"); diff --git a/packages/strings/src/index.ts b/packages/strings/src/index.ts index 2ad7db7d39..cf5c5df750 100644 --- a/packages/strings/src/index.ts +++ b/packages/strings/src/index.ts @@ -3,6 +3,7 @@ export * from "./api.js"; export * from "./ansi.js"; export * from "./case.js"; export * from "./center.js"; +export * from "./currency.js"; export * from "./cursor.js"; export * from "./entities.js"; export * from "./escape.js"; diff --git a/packages/strings/tpl.readme.md b/packages/strings/tpl.readme.md index 670582420a..f4cf5ba760 100644 --- a/packages/strings/tpl.readme.md +++ b/packages/strings/tpl.readme.md @@ -11,7 +11,7 @@ This project is part of the ## About -Various (80+) string formatting, word wrapping & utility functions, some +Various (~100) string formatting, word wrapping & utility functions, some higher-order, some memoized. Partially based on Clojure version of [thi.ng/strf](http://thi.ng/strf). @@ -27,6 +27,7 @@ Partially based on Clojure version of [thi.ng/strf](http://thi.ng/strf). ### Numeric formatters +- `currency` / `chf` / `eur` / `gpb` / `usd` / `yen` - `radix` - `int` / `intLocale` - `float` / `floatFixedWidth`