Every case. One splitter.

String
Text in the shape you need.

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/string

About 5 KB gzip, tree-shakeableZero dependenciesMIT licensed

String previewLive preview

Try XMLHttpRequest or user_first_name: the cases keep accents, the slug removes them.

camelCase
PascalCase
kebab-case
snake_case
CONSTANT_CASE
Title Case
Sentence case
slugify

ONE SMALL SCRIPT. RIGHT AT HOME IN YOUR STACK.

JavaScript TypeScript Node.js React Vue Svelte WordPress
01 / More than toUpperCase

One splitter.
Every case agrees.

Words split once, correctly

XMLHttpRequest becomes XML, Http, Request; camel, Pascal, snake, kebab and any punctuation are understood the same way by every case.

Unicode and locales

Accents and any script are kept as letters, every function takes a locale (Turkish İ and ı), and emoji such as 👨‍👩‍👧 are never cut in half.

Slugs that read well

slugify removes accents, transliterates Cyrillic, Greek and German, applies your replacements, and uniqueSlug picks the first free one (hola-3).

Forms from HTML attributes

data-string makes a slug field follow its title until edited; counters, copy buttons, fitted file names and avatar colors need no script either.

Search without accents

contains finds Café with cafe, highlight returns safe pieces to mark, compareNatural sorts file2 before file10 and closest suggests a fix for typos.

Text ready to show

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.

02 / Fit it into your stack

Pure functions. Any runtime.

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

@samline/string

Every pure function as ESM or CommonJS, tree-shakeable.

HTML layer

@samline/string/vanilla

string(), search(), count(), fit() and copy() run data-string-* attributes.

HTML / CDN

window.Strings

A classic script that starts every attribute when the page loads.

Cases and slugs

camelCase · slugify · convert

Every case, URL slugs, free slugs and conversions picked by name.

Search

contains · highlight · closest

Accent-insensitive matching, natural sorting and typo suggestions.

Text

truncate · pluralize · template

Display helpers, plurals, placeholders, reading time and HTML text.

03 / Install and convert

One install. One live slug.

A slug field that fills itself from the title, with attributes and one call.

Read the installation guide
1

Install

npm

Terminal
npm install @samline/string

pnpm

Terminal
pnpm add @samline/string

yarn

Terminal
yarn add @samline/string

bun

Terminal
bun add @samline/string

HTML / CDN

Terminal
<script src="https://cdn.jsdelivr.net/npm/@samline/string@2.0.1/dist/browser/string.global.js" defer></script>
2

Add markup

data-string names the conversion and data-string-source the field it follows.

index.html
<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" />
3

Start the HTML layer

main.js
import { string } from '@samline/string/vanilla'

// The slug follows the title until the user edits it.
string()
Copy complete working files

Continue with every function and its options.

Explore the options
04 / Beyond slugs

Everyday text jobs.
One call each.

Three common jobs, all built on the same rules for words, accents and case.

camelCase · snakeCase

Map API keys

Read snake_case fields as camelCase, and send them back unchanged.

Read the API
main.js
import { 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' }
closest

Did you mean…?

Suggest the nearest option for a typo, ignoring accents and case.

Read the API
main.js
import { closest } from '@samline/string'

closest('mexcio', ['Chile', 'México', 'Perú']) // 'México'
closest('horchatta', ['Horchata de arroz']) // 'Horchata de arroz'
pluralize

Plurals in any language

Let Intl.PluralRules pick the form, including few and many.

Read the API
main.js
import { 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'
Explore working examples
05 / Before you build

A few details. Clearly answered.

How are words detected?
Any character that is not a letter or digit separates words, a lower-case letter followed by an upper-case one starts a new word, and acronyms end before their last capital (XMLHttp is XML and Http). Digits stay with their word.
Does it work without a bundler?
Yes. Load dist/browser/string.global.js with a classic script tag: it defines window.Strings and starts every data-string-* attribute when the page loads. Pin the version in production.
Do the HTML attributes need JavaScript?
They need the script to convert, but you write the readable text in the HTML, so it stays visible without JavaScript and for search engines. destroy() puts the original markup back.
Which locale is used for upper and lower case?
In code, the runtime's default locale, which can differ between your server and the browser: pass locale, such as 'tr' or 'de', wherever the result must be the same everywhere. HTML attributes use the closest lang attribute.
Are accents removed by the case functions?
No. camelCase and the other case functions keep accents (año nuevo becomes añoNuevo). slugify, removeAccents and the search functions remove them.
How do I translate & or % in slugs?
Pass replacements, for example { '&': 'y' } in Spanish or { '&': 'and' } in English. They apply before anything else.
Can I slugify while the user types?
Yes. data-string="slug" on a field does it for you. In code, use slugify(value, { trim: false }) on input so a trailing space becomes a separator, and slugify(value) on blur for the final value.
Does truncate split emoji or accents?
No. It counts graphemes, the characters people see, so 👨‍👩‍👧, 🇲🇽 and accented letters are never cut in half, and by default it cuts at the last whole word.
Can I use it on the server?
Yes. Every function in @samline/string is pure, with no DOM access, so it works in Node, edge runtimes, workers and build scripts. The HTML layer does nothing without a DOM.
Text, shaped once

Every case, one call away.

Try the examples or follow the first steps.

Guides, the complete API and release notes, in one place.

Read the documentation