Functional string utilities, inspired by Laravel's Str
helpers. Because why not?
yarn add strrr
chainability built in. e.g.
import { str } from 'strrr';
str('lorem ipsum dolor sit amet')
.title()
.limit(10)
.pipe(s => doSomething(s))
.get();
// = 'Lorem Ipsuβ¦'
Limit a string to a given length with a suffix (ellipsis).
limit(limit = 100, end = 'β¦');
import { str } from 'strrr';
str('lorem ipsum dolor sit amet').limit(20).get();
// = 'Lorem ipsum dolor siβ¦'
Limit the number of words in a string with a suffix (ellipsis).
words(words = 100, end = 'β¦');
import { str } from 'strrr';
str('lorem ipsum dolor sit amet').words(3).get();
// = 'Lorem ipsum dolorβ¦'
Generate a "random" alpha-numeric string.
random(length = 32)
import { random } from 'strrr';
random().get(); // = 'nKusDo5JIFrI1tJswwzpEyGLpvML1Mxp'
random(16).get(); // = 'Ky6zJuGnGyrnvw1y'
Transliterate a UTF-8 value to ASCII.
ascii()
import { str } from 'strrr';
str('I β₯ javascript').ascii().get() // = 'I love javascript'
str('@Γ°αΊ»-βα€Ω').ascii().get() // = 'atde-0ff'
Generate a URL friendly "slug" from the string.
slug(separator = '-')
import { str } from 'strrr';
str('FOO bar baz').slug().get() // = 'foo-bar-baz'
str('I β₯ javascript').slug('_').get() // = 'i_love_javascript'
Convert the string to Title case.
title()
import { str } from 'strrr';
str('lorem ipsum dolor sit amet').title().get();
// = 'Lorem Ipsum Dolor Sit Amet'
Convert the string to Studly case. Also known as pascal case
studly()
import { str } from 'strrr';
str('lorem_ipsum_dolor_sit_amet').studly().get();
// = 'LoremIpsumDolorSitAmet'
Convert the string to camel case
camel()
import { str } from 'strrr';
str('lorem_ipsum_dolor_sit_amet').camel().get();
// = 'loremIpsumDolorSitAmet'
Convert the string to snake case
snake(delimeter = '_')
import { str } from 'strrr';
str('LoremIpsumDolorSitAmet').snake().get();
// = 'lorem_ipsum_dolor_sit_amet'
Convert the string to kebab case - Which is similar to snake case but with dashes.
kebab()
import { str } from 'strrr';
str('LoremIpsumDolorSitAmet').kebab().get();
// = 'lorem-ipsum-dolor-sit-amet'
Capitalize the first character in a string.
ucfirst()
import { str } from 'strrr';
str('foo bar').ucfirst().get(); // = 'Foo bar'
lower case the first character in a string.
lcfirst()
import { str } from 'strrr';
str('Lorem ipsum').lcfirst().get(); // = 'lorem ipsum'
Determine if a given string contains a given string.
contains(val, position = 0)
import { str } from 'strrr';
str('foobarbaz').contains('bar'); // = true
str('foobarbaz').contains('bob'); // = false
Determine if a given string starts with a given string.
startsWith(val, position = 0)
import { str } from 'strrr';
str('Lorem ipsum dolor sit amet').startsWith('Lorem') // = true
str('Lorem ipsum dolor sit amet').startsWith('ipsum') // = false
Determine if a given string ends with a given string.
endsWith(val, position = 0)
import { str } from 'strrr';
str('Lorem ipsum dolor sit amet').endsWith('amet'); // = true
str('Lorem ipsum dolor sit amet').endsWith('ipsum'); // = false
Determine if the string is lowercase.
isLowerCase()
import { str } from 'strrr';
str('lorem ipsum dolor sit amet').isLowerCase(); // = true
str('Lorem ipsum dolor sit amet').isLowerCase(); // = false
Determine if the string is uppercase.
isUpperCase()
import { str } from 'strrr';
str('LORUM').isUpperCase(); // = true
str('LoRuM').isUpperCase(); // = false
Strip all whitespace from a string.
strip()
import { str } from 'strrr';
str(' Lorem ipsum dolor sit amet ').strip().get()
// = 'Loremipsumdolorsitamet'