🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

printf-ts

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

printf-ts

printf for typescript

2.0.1
latest
Source
npm
Version published
Weekly downloads
5
-37.5%
Maintainers
1
Weekly downloads
 
Created
Source

printf-ts

codecov

pure typescript implementation of printf-family with type checking

preview

Usage

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 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
specifierargument typedescription
%cstringstr[0] ?? ''
%cnumberutf8 code point
%sstring
%nnullplease 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

Keywords

printf

FAQs

Package last updated on 13 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts