Words split once, correctly
XMLHttpRequest becomes XML, Http, Request; camel, Pascal, snake, kebab and any punctuation are understood the same way by every case.
Convert between every case, build URL slugs, search without accents and shape text for display, from code or straight from HTML attributes, with words split the same way everywhere.
Install it, type in the live preview, then read the full reference.
npm install @samline/stringAbout 5 KB gzip, tree-shakeableZero dependenciesMIT licensed
Try XMLHttpRequest or user_first_name: the cases keep accents, the slug removes them.
ONE SMALL SCRIPT. RIGHT AT HOME IN YOUR STACK.
XMLHttpRequest becomes XML, Http, Request; camel, Pascal, snake, kebab and any punctuation are understood the same way by every case.
Accents and any script are kept as letters, every function takes a locale (Turkish İ and ı), and emoji such as 👨👩👧 are never cut in half.
slugify removes accents, transliterates Cyrillic, Greek and German, applies your replacements, and uniqueSlug picks the first free one (hola-3).
data-string makes a slug field follow its title until edited; counters, copy buttons, fitted file names and avatar colors need no script either.
contains finds Café with cafe, highlight returns safe pieces to mark, compareNatural sorts file2 before file10 and closest suggests a fix for typos.
truncate cuts at a whole word or in the middle of file names, pluralize uses each language’s rules, and template, readingTime and stripTags shape the rest.
The functions never touch the DOM: browsers, Node, workers and edge runtimes. The HTML layer is a separate entry that does nothing without a DOM.
Package
Every pure function as ESM or CommonJS, tree-shakeable.
HTML layer
string(), search(), count(), fit() and copy() run data-string-* attributes.
HTML / CDN
A classic script that starts every attribute when the page loads.
Cases and slugs
Every case, URL slugs, free slugs and conversions picked by name.
Search
Accent-insensitive matching, natural sorting and typo suggestions.
Text
Display helpers, plurals, placeholders, reading time and HTML text.
A slug field that fills itself from the title, with attributes and one call.
Read the installation guideInstall
npm
npm install @samline/stringpnpm
pnpm add @samline/stringyarn
yarn add @samline/stringbun
bun add @samline/stringHTML / CDN
<script src="https://cdn.jsdelivr.net/npm/@samline/string@2.0.1/dist/browser/string.global.js" defer></script>Add markup
data-string names the conversion and data-string-source the field it follows.
<label for="title">Title</label>
<input id="title" type="text" value="Café & Té" />
<label for="slug">Slug</label>
<input id="slug" data-string="slug" data-string-source="#title" />Start the HTML layer
import { string } from '@samline/string/vanilla'
// The slug follows the title until the user edits it.
string()Continue with every function and its options.
Explore the optionsThree common jobs, all built on the same rules for words, accents and case.
Read snake_case fields as camelCase, and send them back unchanged.
Read the APIimport { camelCase, snakeCase } from '@samline/string'
const mapKeys = (object, convert) =>
Object.fromEntries(Object.entries(object).map(([key, value]) => [convert(key), value]))
mapKeys({ first_name: 'Ana' }, camelCase) // { firstName: 'Ana' }
mapKeys({ firstName: 'Ana' }, snakeCase) // { first_name: 'Ana' }import { closest } from '@samline/string'
closest('mexcio', ['Chile', 'México', 'Perú']) // 'México'
closest('horchatta', ['Horchata de arroz']) // 'Horchata de arroz'Let Intl.PluralRules pick the form, including few and many.
Read the APIimport { pluralize } from '@samline/string'
const files = { one: '# plik', few: '# pliki', many: '# plików', other: '# pliku' }
pluralize(2, files, { locale: 'pl' }) // '2 pliki'
pluralize(5, files, { locale: 'pl' }) // '5 plików'Try the examples or follow the first steps.
Guides, the complete API and release notes, in one place.
Read the documentation