Various string formatting & utility functions
npm install @thi.ng/strings
!npm downloads

> [!NOTE]
> This is one of 214 standalone projects, maintained as part
> of the @thi.ng/umbrella monorepo
> and anti-framework.
>
> 🚀 Please help me to work full-time on these projects by sponsoring me on
> GitHub. Thank you! ❤️
- About
- General
- Numeric formatters
- Casing
- Slugify
- ANSI
- Word wrapping
- Padding / wrapping
- Indentation
- Char range presets / lookup tables
- Units
- Miscellaneous
- Status
- Installation
- Dependencies
- Usage examples
- API
- Basic usage examples
- Authors
- License
Various (~100) string formatting, word wrapping & utility functions, some
higher-order, some memoized.
Partially based on Clojure version of thi.ng/strf.
- defFormat / format
- interpolate / interpolateKeys
- escape / unescape
- join / splice / split
- repeat
- stringify
- currency / chf / eur / gpb / usd / yen
- radix
- int / intLocale
- float / floatFixedWidth
- maybeParseFloat / maybeParseInt
- percent
- uuid
- vector
- B8 / B16 / B32 - fixed size binary formatters
- U8 / U16 / U24 / U32 / U64 - fixed size hex formatters
- Z2 / Z3 / Z4 - fixed sized zero padded number formatters
- lower / upper / capitalize
- camel / kebab / snake / upperSnake
- slugify / slugifyGH
- isAnsi / isAnsiEnd / isAnsiStart
- stripAnsi
- lengthAnsi
- wordWrap / wordWrapLine / wordWrapLines
- SPLIT_PLAIN / SPLIT_ANSI
- center
- padLeft / padRight
- truncate / truncateLeft / truncateRight
- trim
- wrap
- spacesToTabs / spacesToTabsLine
- tabsToSpaces / tabsToSpacesLine
- charRange
- ALPHA / ALPHA_NUM / DIGITS / LOWER / UPPER / HEX
- BOM / ESCAPES / ESCAPES_REV
- WS / PUNCTUATION
- units
- bits / bytes
- grams
- meters
- seconds
- ruler / grid
- hstr - Hollerith strings
- computeCursorPos
STABLE - used in production
Search or submit any issues for this package
``bash`
yarn add @thi.ng/strings
ESM import:
`ts`
import * as str from "@thi.ng/strings";
Browser ESM import:
`html`
For Node.js REPL:
`js`
const str = await import("@thi.ng/strings");
Package sizes (brotli'd, pre-treeshake): ESM: 5.54 KB
- @thi.ng/api
- @thi.ng/errors
- @thi.ng/hex
- @thi.ng/memoize
Note: @thi.ng/api is in _most_ cases a type-only import (not used at runtime)
Eight projects in this repo's
/examples
directory are using this package:
| Screenshot | Description | Live demo | Source |
|:---------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------|:----------------------------------------------------------|:---------------------------------------------------------------------------------------|
|
| ASCII art raymarching with thi.ng/shader-ast & thi.ng/text-canvas | Demo | Source |
|
| Large ASCII font text generator using @thi.ng/rdom | Demo | Source |
|
| Basic crypto-currency candle chart with multiple moving averages plots | Demo | Source |
| | Basic SPA example with atom-based UI router | Demo | Source |
|
| Mastodon API feed reader with support for different media types, fullscreen media modal, HTML rewriting | Demo | Source |
|
| Generative audio synth offline renderer and WAV file export | Demo | Source |
|
| rstream based spreadsheet w/ S-expression formula DSL | Demo | Source |
|
| XML/HTML/SVG to hiccup/JS conversion | Demo | Source |
`ts
import { defFormat, float, percent } from "@thi.ng/strings";
// create a custom string formatter
const fmt = defFormat([
"Price: ",
{ usd: "$", gbp: "£", eur: "€" },
float(2),
" (",
percent(2),
" off)"
]);
// use format
fmt("usd", 1.2345, 0.5);
// Price: $1.23 (50.00% off)
fmt("eur", 1.2345, 0.25)
// Price: €1.23 (25.00% off)
`
If this project contributes to an academic publication, please cite it as:
`bibtex``
@misc{thing-strings,
title = "@thi.ng/strings",
author = "Karsten Schmidt",
note = "https://thi.ng/strings",
year = 2015
}
© 2015 - 2026 Karsten Schmidt // Apache License 2.0