# printf-ts

> printf for typescript

Latest version **2.0.1** (published 2023-11-13) · BSD-3-Clause license · 0 weekly downloads

## Install

```sh
npm install printf-ts
pnpm add printf-ts
yarn add printf-ts
bun add printf-ts
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2023-11-13 |
| First published | 2023-07-17 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 28.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | fienestar |
| Maintainers | fienestar |
| Keywords | printf, sprintf, format, printf-ts |

## Links

- npm: https://www.npmjs.com/package/printf-ts
- Repository: https://github.com/fienestar/sprintf-ts
- Homepage: https://github.com/fienestar/printf-ts#readme
- Issues: https://github.com/fienestar/printf-ts/issues
- npm.io page: https://npm.io/package/printf-ts

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.1 (latest) — 2023-11-13
- 1.0.3 — 2023-07-28
- 1.0.2 — 2023-07-20
- 1.0.0 — 2023-07-17

## README

# printf-ts

[![codecov](https://codecov.io/gh/fienestar/printf-ts/branch/main/graph/badge.svg?token=Y8GR64UX10)](https://codecov.io/gh/fienestar/printf-ts)

pure typescript implementation of printf-family with type checking

![preview](./preview.gif)

## Usage

```typescript
import { printf } from 'printf-ts';

// Hello printf 1 1.20
printf("Hello %s %d %.2lf", "printf", 1, 1.2)
```

## Features

### 1. vsprintf(format, args: [...]): string
### 2. sprintf(format, ...args: [...]): string
### 3. vprintf(format, args: [...]): number
### 4. printf(format, ...args: [...]): number
### 5. vprintfln(format, args: [...]): number
### 6. printfln(format, ...args: [...]): number

- 1 ~ 2 Returns the result as a string
- 3 ~ 4 Writes the results to the stdout, returns the number of characters printed
- 5 ~ 6 Writes the results to the stdout, returns the number of characters printed. if nodejs, append `\n` to the end(not affect to return value)

### Supported format specifiers

see [cppreference-printf](https://en.cppreference.com/w/c/io/fprintf) for each description of specifier

- flags: `+`, `-`, ` `, `#`, `0`
- `width`, `precision`
    - support wildcard(`*`)
- length modifier: `h`, `hh`, `l`, `ll`, `L`, `j`, `z`, `t`
- conversion specifier: `%c`, `%s`, `%d`, `%i`, `%u`, `%o`, `%x`, `%X`, `%f`, `%F`, `%e`, `%E`, `%g`, `%G`, `%p`, `%%`


### types

- if argument is negative and format specifier is unsigned, it will not convert to unsigned type
- argument is not converted to a number of specific bits
- if origin type was unsigned, find it in signed type

| specifier | argument type | description |
| :---: | :---: | :--- |
| `%c` | `string` | str[0] ?? '' |
| `%c` | `number` | utf8 code point |
| `%s` | `string` | |
| `%n` | `null` | please use sprintf(...).length |

#### integer
> %d, %i, %u, %o, %x, %X
- `short|int` -> `number`
- `long|long long|intmax_t|ssize_t|ptrdiff_t` -> `bigint`

#### Floating point
> %f, %F, %e, %E, %g, %G
- `double|long double` -> `number`
    - if value in [`infinity`, `-infinity`, `NaN`]
        - if specifier is uppercase(`F`,`E`,`G`,`A`) => `"INF"`, `"-INF"`, `"NAN"`
        - else => `"inf"`, `"-inf"`, `"nan"`
- NaN always treated as a positive value

#### Pointer
> %p
- `void*` -> `bigint`
    - if value is `0n` => `"(nil)"`
    - else => `"0x" + value.toString(16)`

### Wildcard
> \*
- `int` -> `number`

---
_Source: https://npm.io/package/printf-ts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
