Skip to content

Commit

Permalink
docs(format): update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
oumar-fall committed Oct 24, 2024
1 parent a76b052 commit 2a474b8
Showing 1 changed file with 50 additions and 9 deletions.
59 changes: 50 additions & 9 deletions packages/morpho-ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Here are all the available formatters:
- [**Short Formatter**](#short-formatter): Formats the value in a short notation with units (e.g., `k`, `M`).
- [**Percent Formatter**](#percent-formatter): Formats the value as a percentage.

### Usage
#### Usage

Each formatter can be accessed through the `format` object and provides chainable methods to customize the output. The formatted value can be obtained calling `.of(value)` for `number` or `.of(value, decimals)` for `bigint`.
The return value will retain the nullability of the input value (giving priority to `value` over `decimals` for bigints, if none is defined), unless a `.default()` method is applied (refer to [Number Formatter](#2-number-formatter) for details).
Expand All @@ -59,15 +59,33 @@ The return value will retain the nullability of the input value (giving priority
> import { format } from "@morpho-org/morpho-ts";
>
> const dollarFormatter = format.short
> .digits(2)
> .smallValuesWithCommas()
> .unit("$");
>
> dollarFormatter.of(123456.789); // "$123.45k"
> dollarFormatter.of(123456789n, 4); // "$12.34k"
> dollarFormatter
> .digits(2).of(123456.789); // "$123.45k"
> dollarFormatter
> .digits(4).of(123456789n, 4); // "$12.3456k"
> ```
### Hex Formatter
##### `createOf`
Alternatively, you can create a standalone formatting function by calling `createOf` on your populated formatter.
```typescript
import { format } from "@morpho-org/morpho-ts";
const formatDollar = format.short
.smallValuesWithCommas()
.digits(2)
.unit("$")
.createOf();
formatDollar(123456.789); // "$123.45k"
formatDollar(123456789n, 4); // "$12.34k"
```
#### Hex Formatter

Formats a value as a hexadecimal string.

Expand All @@ -85,7 +103,7 @@ format.hex.of(255n); // "ff"

---

### Number Formatter
#### Number Formatter

Formats a value as a standard number with optional customization.

Expand All @@ -110,7 +128,7 @@ format.number.of(123.45); // "123.45"

---

### Commas Formatter
#### Commas Formatter

Formats a value as a comma-separated string.

Expand All @@ -129,7 +147,7 @@ format.commas.digits(2).unit("$").of(1234567); // "$1,234,567.00"

---

### Short Formatter
#### Short Formatter

Formats a value in a short notation with units (e.g., `k`, `M`, `B`).

Expand All @@ -149,7 +167,7 @@ format.short.digits(2).smallValuesWithCommas().of(1000000_00000000n, 8); // "1.0

---

### Percent Formatter
#### Percent Formatter

Formats a value as a percentage.

Expand All @@ -168,6 +186,29 @@ format.percent.digits(1).sign().of(0.123456); // "+12.3%"

---

#### Create custom formatters

You can create a custom `format` object with default options that will be applied to all formatters created from it.

```typescript
import { createFormat } from "@morpho-org/morpho-ts";

const customFormat = createFormat({
all: { digits: 2 }, // all formatters will format with 2 digits
short: { digits: 3 }, // all short formatters will format with 3 digits
number: { sign: true }, // all number formatters will display signed values
...
})

customFormat.short.of(1234.5678); // "1234.567"
customFormat.number.of(1234.5678); // "+1234.56"

// Default options can be normally overriden
customFormat.short.digits(1).of(1234.5678); // "1234.5"
```

---

### Time

The `Time` utility provides a robust way to handle and convert time units in TypeScript, making it easier to work with various time durations.
Expand Down

0 comments on commit 2a474b8

Please sign in to comment.