Overview
Common string function collection
Table of contents
- camelCase (function)
- camelCaseToArray (function)
- escapeHtml (function)
- escapeRegExp (function)
- escapeUnicode (function)
- hyphenate (function)
- lowerCase (function)
- lpad (function)
- ltrim (function)
- plural (function)
- quoteString (function)
- removeNonASCII (function)
- removeNonWord (function)
- repeat (function)
- repeatSpace (function)
- replaceAccents (function)
- rtrim (function)
- slugify (function)
- toUpperFirstLetter (function)
- trim (function)
- truncate (function)
- unCamelCase (function)
- unescapeHtml (function)
- unescapeUnicode (function)
- unhyphenate (function)
- upperCase (function)
camelCase (function)
Convert string to camelCase text.
Signature
export const camelCase = (str: string): string => ...
Example
import { camelCase } from 'macoolka-string'
expect(camelCase('first color hover')).toEqual('firstColorHover')
expect(camelCase('first_color_hover')).toEqual('firstColorHover')
Added in v0.2.0
camelCaseToArray (function)
split camelCase text to array
Signature
export const camelCaseToArray = (str: string): Array<string> => ...
Example
import { camelCaseToArray } from 'macoolka-string'
expect(camelCaseToArray('firstColorHover')).toEqual(['first', 'color', 'hover'])
Added in v0.2.0
escapeHtml (function)
Escapes a string for insertion into HTML.
Signature
export const escapeHtml = (str: string): string => ...
Example
import { escapeHtml } from 'macoolka-string'
expect(escapeHtml(`<h1>"&title" 'a1'<h1>`)).toEqual('<h1>"&title" 'a1'<h1>')
Added in v0.2.0
escapeRegExp (function)
Escape RegExp string chars.
Signature
export const escapeRegExp = (str: string): string => ...
Example
import { escapeRegExp } from 'macoolka-string'
expect(escapeRegExp('/^a/')).toEqual('\\/\\^a\\/')
Added in v0.2.0
escapeUnicode (function)
Escape string into unicode sequences
Signature
export const escapeUnicode = (shouldEscapePrintable: boolean) => (str: string): string => ...
Example
import { escapeUnicode } from 'macoolka-string'
expect(escapeUnicode(true)('我们')).toEqual('\\u6211\\u4eec')
Added in v0.2.0
hyphenate (function)
Replaces spaces with hyphens, split camelCase text, remove non-word chars, remove accents and convert to lower case.
Signature
export const hyphenate = (split: string = '-') => (str: string) => ...
Example
import { hyphenate } from 'macoolka-string'
expect(hyphenate('_')('firstColorHover')).toEqual('first_color_hover')
expect(hyphenate()('firstColorHover')).toEqual('first-color-hover')
Added in v0.2.0
lowerCase (function)
The given string to lower case
Signature
export const lowerCase = (str: string): string => ...
Example
import { lowerCase } from 'macoolka-string'
expect(lowerCase('aBc')).toEqual('abc')
Added in v0.2.0
lpad (function)
Pad string with char
if its’ length is smaller than minLen
Signature
export const lpad = (minLength: number, mark: string = ' ') => (str: string): string => ...
Example
import { lpad } from 'macoolka-string'
expect(lpad(5)('a')).toEqual(' a')
expect(lpad(5, '#')('a')).toEqual('####a')
expect(lpad(5)('abcd')).toEqual(' abcd')
expect(lpad(5)('abcdef')).toEqual('abcdef')
Added in v0.2.0
ltrim (function)
Remove chars from beginning of string.
Signature
export const ltrim = (chars: Array<string> = WHITE_SPACES) => (str: string): string => ...
Example
import { ltrim } from 'macoolka-string'
expect(ltrim()(' abc ')).toEqual('abc ')
expect(ltrim(['_'])('__abc__')).toEqual('abc__')
Added in v0.2.0
plural (function)
Pluralizes a word like the prisma server would do.
Example
import { pluralWord } from 'macoolka-string'
expect(plural('car')).toEqual('cars')
expect(plural('gas')).toEqual('gases')
Added in v0.2.0
quoteString (function)
adding quotes for a string .
Signature
export const quoteString = (value: string): string => ...
Example
import { quoteString } from 'macoolka-string'
expect(quoteString('a')).toEqual('"a"')
Added in v0.2.0
removeNonASCII (function)
Remove non-printable ASCII chars
Signature
export const removeNonASCII = (str: string): string => ...
Example
import { removeNonASCII } from 'macoolka-string'
expect(removeNonASCII('\xD0a*^%b#c1')).toEqual('a*^%b#c1')
Added in v0.2.0
removeNonWord (function)
Remove non-word chars.
Signature
export const removeNonWord = (str: string): string => ...
Example
import { removeNonWord } from 'macoolka-string'
expect(removeNonWord('a*^%b#c1')).toEqual('abc1')
Added in v0.2.0
repeat (function)
Repeat string n times
Signature
export const repeat = (times: number) => (str: string): string => ...
Example
import { repeat } from 'macoolka-string'
expect(repeat(5)('a')).toEqual('aaaaa')
Added in v0.2.0
repeatSpace (function)
Repeat space n times
Signature
export const repeatSpace = (a: number) => ...
Example
import { repeatSpace } from 'macoolka-string'
expect(repeatSpace(5)).toEqual(' ')
Added in v0.2.0
replaceAccents (function)
Replaces all accented chars with regular ones
Signature
export const replaceAccents = (str: string): string => ...
Example
import { replaceAccents } from 'macoolka-string'
expect(replaceAccents('\xC8')).toEqual('E')
Added in v0.2.0
rtrim (function)
Remove chars from end of string.
Signature
export const rtrim = (chars: Array<string> = WHITE_SPACES) => (str: string): string => ...
Example
import { rtrim } from 'macoolka-string'
expect(rtrim()(' abc ')).toEqual(' abc')
expect(rtrim(['_'])('__abc__')).toEqual('__abc')
Added in v0.2.0
slugify (function)
Convert to lower case, remove accents, remove non-word chars and replace spaces with the specified delimeter. Does not split camelCase text.
Signature
export const slugify = (delimeter: string = '-') => (str: string): string => ...
Example
import { slugify } from 'macoolka-string'
expect(slugify('_')('first Color Hover')).toEqual('first_color_hover')
expect(slugify(' ')('first Color Hover')).toEqual('first color hover')
Added in v0.2.0
toUpperFirstLetter (function)
First lettter to upper
Signature
export const toUpperFirstLetter=(a:string)=>a.length>0?`${a.substring(0,1).toUpperCase()}${a.substring(1)}`:a => ...
Example
import { toUpperFirstLetter } from 'macoolka-string'
expect(toUpperFirstLetter('firstColorHover')).toEqual('FirstColorHover')
Added in v0.2.0
trim (function)
Remove given string from beginning and end of string.
Signature
export const trim = (chars: Array<string> = WHITE_SPACES) => (str: string): string => ...
Example
import { trim } from 'macoolka-string'
expect(trim()(' abc ')).toEqual('abc')
expect(trim(['_'])('__abc__')).toEqual('abc')
Added in v0.2.0
truncate (function)
Limit number of chars.
Signature
export const truncate = (maxChars: number, append: string = '...', onlyFullWords: boolean = true) => (str: string): string => ...
Example
import { truncate } from 'macoolka-string'
expect(truncate(10)('abc def ghi hing')).toEqual('abc def...')
expect(truncate(10, '~~~')('abc def ghi hing')).toEqual('abc def~~~')
expect(truncate(10, '~~~', false)('1234567890abcd d')).toEqual('1234567~~~')
Added in v0.2.0
unCamelCase (function)
Add space between camelCase text.
Signature
export const unCamelCase = (delimiter: string = ' ') => (str: string): string => ...
Example
import { unCamelCase } from 'macoolka-string'
expect(unCamelCase('_')('firstColorHover')).toEqual('first_color_hover')
expect(unCamelCase(' ')('firstColorHover')).toEqual('first color hover')
Added in v0.2.0
unescapeHtml (function)
Unescapes HTML special chars.
Signature
export const unescapeHtml = (str: string): string => ...
Example
import { unescapeHtml } from 'macoolka-string'
expect(unescapeHtml(`<h1>"&title" 'a1'<h1>`)).toEqual(`<h1>"&title" 'a1'<h1>`)
Added in v0.2.0
unescapeUnicode (function)
Unescape unicode char sequences
Signature
export const unescapeUnicode = (str: string): string =>
str.replace(/\\u[0-9a-f]{4}/g, (ch) => ...
Example
import { unescapeUnicode } from 'macoolka-string'
expect(unescapeUnicode('\\u6211\\u4eec')).toEqual('我们')
Added in v0.2.0
unhyphenate (function)
Replaces hyphens with spaces. (only hyphens between word chars)
Signature
export const unhyphenate = (str: string): string => ...
Example
import { unhyphenate } from 'macoolka-string'
expect(unhyphenate('a-apple')).toEqual('a apple')
Added in v0.2.0
upperCase (function)
The given string to upper case
Signature
export const upperCase = (str: string): string => ...
Example
import { upperCase } from 'macoolka-string'
expect(upperCase('aBc')).toEqual('ABC')
Added in v0.2.0