@billdaddy/strkit
v0.1.1
Published
Tiny, type-safe string utilities — truncate, template, escapeHtml/escapeRegExp, dedent, center, and prefix/suffix helpers. Zero dependencies.
Downloads
229
Maintainers
Readme
strkit
Tiny, type-safe string utilities —
truncate,template,escapeHtml/escapeRegExp,dedent,center, and prefix/suffix helpers. Zero dependencies.
JS strings got padStart, trimEnd, and replaceAll — but you still hand-roll
truncate, escapeHtml, escapeRegExp, and a tiny {{template}} in every
project. strkit is those everyday string helpers, done once and tested, fully
typed and tree-shakeable.
import { truncate, template, escapeHtml } from "@billdaddy/strkit";
truncate("The quick brown fox", 12); // "The quick b…"
template("Hi {{name}}", { name: "Sam" }); // "Hi Sam"
escapeHtml('<a href="x">'); // "<a href="x">"Why strkit?
- Smart truncation.
truncatecounts the ellipsis in the limit and can cut on a word boundary;truncateMiddlekeeps both ends (paths, hashes). - Safe escaping.
escapeHtml/unescapeHtmlfor markup,escapeRegExpfor building patterns from user input. - Tiny templating.
templatefills{{placeholders}}with custom delimiters and a fallback. - The little fixers.
capitalize,center,dedent,ensurePrefix/Suffix,stripPrefix/Suffix. - Tree-shakeable.
sideEffects: false, ESM + CJS, zero dependencies.
Install
npm install @billdaddy/strkit
# or: pnpm add @billdaddy/strkit / yarn add @billdaddy/strkit / bun add @billdaddy/strkitTruncation
import { truncate, truncateMiddle } from "@billdaddy/strkit";
truncate("The quick brown fox", 9); // "The quic…"
truncate("The quick brown fox", 9, { wordBoundary: true }); // "The…"
truncate("abcdefgh", 6, { ellipsis: "..." }); // "abc..."
truncateMiddle("0x1234567890abcdef", 11); // "0x123…bcdef"Escaping & templating
import { escapeHtml, unescapeHtml, escapeRegExp, template } from "@billdaddy/strkit";
escapeHtml("Tom & Jerry"); // "Tom & Jerry"
unescapeHtml("it's"); // "it's"
new RegExp(escapeRegExp("a.b(c)")); // matches the literal text
template("{{a}} + {{b}} = {{c}}", { a: 1, b: 2, c: 3 }); // "1 + 2 = 3"
template("[%x%]", { x: "y" }, { open: "[%", close: "%]" }); // "y"
template("{{a}} {{b}}", { a: 1 }, { fallback: "?" }); // "1 ?"Unknown keys are left as-is by default; null/undefined become empty strings.
Transforms
import {
capitalize, uncapitalize, center, dedent,
ensurePrefix, ensureSuffix, stripPrefix, stripSuffix,
} from "@billdaddy/strkit";
capitalize("hello"); // "Hello"
center("hi", 6); // " hi "
ensurePrefix("path", "/"); // "/path"
stripSuffix("file.ts", ".ts"); // "file"
dedent(`
SELECT *
FROM users
`); // "SELECT *\n FROM users"Pairs well with
| Need | Use |
| --- | --- |
| Convert case (camel/snake/kebab) | @billdaddy/casekit |
| Slugify text for URLs | slugkit |
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
MIT © Tung Tran
