js_ergo 0.2.0

Ergonomic, JavaScript-style string helpers for Rust (padStart and friends).
Documentation
  • Coverage
  • 100%
    6 out of 6 items documented2 out of 2 items with examples
  • Size
  • Source code size: 21.35 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 291.28 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 4s Average build duration of successful builds.
  • all releases: 3s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • chaffRs/js_ergo
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • LonelyFellas

js_ergo

Ergonomic, JavaScript-style string helpers for Rust.

js_ergo brings the convenience of JavaScript's String methods to Rust as a zero-dependency extension trait, while keeping idiomatic, Unicode-correct semantics.

Install

[dependencies]
js_ergo = "0.1"

Usage

Bring the JsStrExt trait into scope and call the helpers on any &str:

use js_ergo::JsStrExt;

// Pad the start with a single character.
assert_eq!("123".pad_start(5, '0'), "00123");

// Pad the end, too.
assert_eq!("123".pad_end(5, '0'), "12300");

// A multi-character pad is repeated and truncated (like JS `padStart`).
assert_eq!("5".pad_start(4, "ab"), "aba5");

// Already long enough? Returned unchanged.
assert_eq!("hello".pad_start(3, '.'), "hello");

The pad argument accepts a char, &str, &String, or String. A multi-character pad is repeated and truncated to fill the gap.

Note on length

length is counted in Unicode scalar values (chars), not UTF-16 code units as in JavaScript. A character outside the Basic Multilingual Plane such as '🦀' counts as 1 here but as 2 in JavaScript.

Minimum supported Rust version

Rust 1.85 (edition 2024).

License

Licensed under the MIT License.