js-string-escape 0.1.0

Escape a string for use as a JavaScript string literal body. A faithful port of the js-string-escape npm package. Zero deps, no_std.
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented2 out of 2 items with examples
  • Size
  • Source code size: 22.52 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 110.52 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • trananhtung/js-string-escape
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • trananhtung

js-string-escape

All Contributors

crates.io docs.rs CI license

Escape a string for use as a JavaScript string literal body.

Escapes the characters that aren't valid inside a single- or double-quoted JavaScript string literal: ", ', \, and the line terminators \n, \r, U+2028, and U+2029. Everything else passes through unchanged. A faithful Rust port of the js-string-escape npm package.

  • Zero dependencies, #![no_std]
  • Differential-tested against the reference js-string-escape implementation

Install

[dependencies]
js-string-escape = "0.1"

Usage

use js_string_escape::js_string_escape;

assert_eq!(js_string_escape(r#"a"b'c\d"#), r#"a\"b\'c\\d"#);
assert_eq!(js_string_escape("line1\nline2"), "line1\\nline2");

// Tabs, other control characters, and non-ASCII pass through:
assert_eq!(js_string_escape("café\t😀"), "café\t😀");

For richer escaping (ASCII-only output, JSON mode, backtick quotes, …) see jsesc.

Contributors ✨

This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.

Thanks goes to these wonderful people:

License

Licensed under either of MIT or Apache-2.0 at your option.