string-unified
All you need to handle Unicode strings properly in JS.
This library builds on the power of grapheme-splitter to provide all essential functions for accurate Unicode string manipulations.
Installation
Node.js
npm i string-unified
Browsers
jsDelivr
https://cdn.jsdelivr.net/npm/string-unified@latest
Unpkg
https://unpkg.com/string-unified@latest/dist/index.js
And import string-unified like this
// ES2015+;; // CommonJSconst unified = const length charAt substring = ;
Usage
- length()
- charAt()
- substring()
- indexOf()
- lastIndexOf()
- includes()
- split()
- startsWith()
- endsWith()
- match()
All functions except match() are transpiled to ES5. See below for why match() requires ES2015+.
length
Get the length of a string.
Param | Type | Default | Description |
---|---|---|---|
str | string | none | string to find length for |
Examples
// 3 // 2 // 5 // 2 // 5 // 5 // 6 // 1
charAt
Get the character at an index.
function charAt(str, index)
Param | Type | Default | Description |
---|---|---|---|
str | string | none | string to get char from |
index | number | none | index to get char |
Examples
// 'あ' // undefined // negative indices also work!// same as charAt('谢谢你😊', 3) // '😊'
substring
Get a substring of a string.
function substring(str, start?, end?)
Param | Type | Default | Description |
---|---|---|---|
str | string | none | string to retrieve substring from |
start | number | 0 | start index of substring |
end | number | end of string | end index of substring |
Examples
// '🍜🍚' // '🍤🍜🍚' // negative indices also work!// same as substring('🥪🥗🍤🍜🍚', 2, 3) // '🍤'
indexOf
Get the index of the first occurence of a search string.
Param | Type | Default | Description |
---|---|---|---|
str | string | none | string to search in |
searchStr | string | none | string to search for |
start | number | 0 | start index of the search |
end | number | end of string | end index of the search |
Examples
// get index of the *first* occurence // 3 // return undefined if not found // undefined
lastIndexOf
Get the index of the last occurence of a search string.
Param | Type | Default | Description |
---|---|---|---|
str | string | none | string to search in |
searchStr | string | none | string to search for |
start | number | 0 | start index of the search |
end | number | end of string | end index of the search |
Examples
// get index of the *last* occurence // 5 // return undefined if not found // undefined
includes
Test if a search string appears in a string.
Param | Type | Default | Description |
---|---|---|---|
str | string | none | string to search in |
searchStr | string | none | string to search for |
start | number | 0 | start index of the search |
end | number | end of string | end index of the search |
Examples
// true // same as includes('🎃🎉🧧', '🎁') // false // same as includes('🎉', '🎉') // true // negative indices also work!// same as includes('🎃🎉🧧', '🎁') // false
split
Split a string by a separator (can be a string or regex).
Param | Type | Default | Description |
---|---|---|---|
str | string | none | string to split |
separator | string or RegExp | none | separator to split string |
limit | number | +Infinity | limit of the number of splits returned |
Examples
// ['👩','👨','🧑','👧','👦','🧒']) // ['👩','👨','🧑'] // limit can be larger than array size // ['👩','👨','🧑'] // no separator returns a copy of the string inside an array // ['👩🔹👨🔹🧑🔹👧🔹👦🔹🧒'] // if separator is an empty string '', split whole string into a character array // ['👩','🔹','👨','🔹','🧑','🔹','👧','🔹','👦','🔹','🧒']) // If separator is a regular expression containing capturing parentheses (), matched results are included) // ["Hello👋 ", "1", " word. Sentence #️⃣ ", "2", "."]
startsWith
Test is a string starts with a search string.
Param | Type | Default | Description |
---|---|---|---|
str | string | none | string to search in |
searchStr | string | none | string to search for |
start | number | 0 | start index of the search |
end | number | end of string | end index of the search |
Examples
// true // same as startsWith('🎪🎭🎡🎠', '🎢') // false // same as startsWith('🎪🎭', '🎪') // true // negative indices also work!// same as startsWith('🎡🎠', '🎡') // true
endsWith
Test is a string ends with a search string.
Param | Type | Default | Description |
---|---|---|---|
str | string | none | string to search in |
searchStr | string | none | string to search for |
start | number | 0 | start index of the search |
end | number | end of string | end index of the search |
Examples
// true // same as endsWith('🎭🎡🎠', '🎠') // true // negative indices also work!// same as endsWith('🎪🎭', '🎭') // true
match
Note that match() requires ES2015+ because of limited support of unicode regexp in lower versions and limitations of current source code transpilers like regexpu.
Test if a string matches a regular expression or another string.
Param | Type | Default | Description |
---|---|---|---|
str | string | none | string to search in |
regexp | RegExp or string | none | Regular expression or string to search for |
Examples
// Test if a string matches a RegExp// Without global flag // ['🐺', index: 2, input: '🐵🐶🐺🐱', groups: undefined] // null // Must add 'u' flag otherwise throw "Range out of order in character class" // ['💩', index: 0, input: '💩', groups: undefined] // all operators like '.' includes unicode characters // ['foo👋bar', '👋', index: 0, input: 'foo👋bar', groups: undefined) // With global flag // ['🐺', '🐺'] // ['🏳️🌈⛳', '🏳️🌈🎌'] // Test if a string matches another string // ['🏳️🌈', index: 1, input: '🏁🏳️🌈🏴🏳️🌈⛳🚩', groups: undefined]; // Special case when regexp is undefined // null