Skip to content

Commit

Permalink
feat(strings): add currency formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Dec 1, 2021
1 parent 112ba7e commit 52b7340
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/csv/src/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
5 changes: 3 additions & 2 deletions packages/strings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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`
Expand Down Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions packages/strings/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@
"./center": {
"import": "./center.js"
},
"./currency": {
"import": "./currency.js"
},
"./cursor": {
"import": "./cursor.js"
},
Expand Down
19 changes: 19 additions & 0 deletions packages/strings/src/currency.ts
Original file line number Diff line number Diff line change
@@ -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<number>
>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("¥");
1 change: 1 addition & 0 deletions packages/strings/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 2 additions & 1 deletion packages/strings/tpl.readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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`
Expand Down

0 comments on commit 52b7340

Please sign in to comment.