Skip to content
stdlib
Esc
↑↓navigate↵open⌘Jpreview
On this page

String

String.camelCase("hello world") // "helloWorld"
String.capitalize("hello world") // "Hello world"
String.constantCase("hello world") // "HELLO_WORLD"
String.kebabCase("hello world") // "hello-world"
String.pascalCase("hello world") // "HelloWorld"
String.snakeCase("hello world") // "hello_world"
String.titleCase("hello world") // "Hello World"
String.uncapitalize("Hello World") // "hello World"

String.dedent("    a\\n      b") // "a\\n  b"
String.score("Settings", "set") // 0.89
String.slug("Hello World!") // "hello-world"
String.escapeHTML("<b>Tom & Jerry</b>") // "&lt;b&gt;Tom &amp; Jerry&lt;/b&gt;"

;[...String.graphemes("hello")] // ["h", "e", "l", "l", "o"]
;[...String.words("Hello, world!")] // ["Hello", "world"]
;[...String.links("See example.com")] // [{ start: 4, end: 15, text: "example.com", url: "https://example.com" }]

String.score

This function is designed for general purpose fuzzy searching, such as menus, command palettes or pickers, a user filtering a list of names or email addresses for example. For something more specific, like filtering file paths, reach for a different algorithm.

const results = items
    .map((item) => ({ item, score: String.score(item.title, query) }))
    .filter((result) => result.score > 0.4)
    .sort((a, b) => b.score - a.score)
    .map((result) => result.item)

Credits

Differences

String.links is 1.5 KB instead of the 4.5 KB of linkify-it. It finds links without a scheme, which linkify-it only does with fuzzyLink: true, and compared to that it is 2.4 to 30 times faster. Where a link ends is not always clear-cut, and in rare cases the two decide differently.

String.slug transliterates like @sindresorhus/slugify, at 6.1 KB instead of 8.8 KB gzipped, and 100 to 1000 times faster. Unlike it, it doesn’t split words where the case changes: GitHub is github instead of git-hub, and Ψυχή is psyxi instead of p-syxi. It also keeps Œ as oe, which it drops, and drops symbols like ♥ instead of spelling them out.

String.score scores the way fuzzysort does, at 2.7 KB instead of 7.6 KB gzipped, without the cache of every string it scored that only fuzzysort.cleanup() clears. Without that cache it takes about 1.5 times as long, which is still about 0.04ms per keystroke for filtering 1000 items.