mellotron 0.2.6
Synthetic string orchestra. Curated list of string manipulation curried functions
yarn add mellotron
npm install mellotron
capitalize concat contains deburr endsWith format fromQuery fromURL isString isURL join leftPad length match pad replace reverse rightPad slugify split startsWith test toLower toQuery toString toUpper toURL trim trimLeft trimRight
Capitalize the first letter of a string
.
From lodash/capitalize
Name | Type | Description | |
---|---|---|---|
string='' | string |
The string to convert. | Optional |
capitalize('united states');
// => 'United states'
string
Returns the capitalized string.
Concatenate the given strings
.
From ramda/concat
Name | Type | Description | |
---|---|---|---|
string='' | string |
The first string to add. | Optional |
string='' | string |
The second string to add. | Optional |
concat('ABC', 'DEF');
// => 'ABCDEF'
const prefix = concat('cyber');
prefix('space');
// => 'cyberspace'
string
Returns the result of concatenating the givenstrings
.
Inserts the separator string
between each element and concatenating all the elements into a single string
.
From ramda/contains
Name | Type | Description | |
---|---|---|---|
a | string |
The item to compare against. | |
list | string |
The string to consider |
contains('á', 'Zárate')
// => true
const taco = contains('salsa');
taco('Un taco sin salsa no es taco');
// => true
boolean
Returnstrue
if an equivalent item is in list,false
otherwise.
Deburrs string
by converting letters to basic Latin letters and removing combining diacritical marks.
From lodash/deburr
Name | Type | Description | |
---|---|---|---|
string='' | string |
The string to deburr. | Optional |
deburr('déjà vu');
// => 'deja vu'
string
Returns the deburred string.
Checks if string
ends with the given target string
.
From lodash/endsWith
Name | Type | Description | |
---|---|---|---|
target | string |
The string to search for. | |
string | string |
The string to inspect. |
endsWith('c', 'abc');
// => true
const endsWithR = endsWith('r');
endsWithR('bar');
// => true
boolean
Returnstrue
ifstring
ends withtarget
, elsefalse
.
Values are interpolated on a template string
.
Name | Type | Description | |
---|---|---|---|
template | string |
The `string` with placeholders. | |
values | Array.<*> Object |
Values to be interpolated |
// Allows creating templates:
const readMessages = format('{0}, you have {1} unread message{2}')
readMessages(['Holly', 2, 's'])
// => 'Holly, you have 2 unread messages'
// Unmatched placeholders produce no output:
readMessages(['Steve', 1])
// => 'Steve, you have 1 unread message'
// Supports property access via dot notation
const bobby = { first: 'Bobby', last: 'Fischer' };
const garry = { first: 'Garry', last: 'Kasparov' };
format('{0.first} {0.last} vs. {1.first} {1.last}', [bobby, garry])
// => 'Bobby Fischer vs. Garry Kasparov'
// Supports property access via object property
const jamesBond = { firstname: 'James', lastname: 'Bond' };
format('The name is {lastname}. {firstname} {lastname}.', jamesBond)
// => 'The name is Bond. James Bond.'
string
Returns the result of replacing each {…} placeholder in the template string with its corresponding replacement.
Parse a query string into an object. From querystring/parse
Name | Type | Description | |
---|---|---|---|
str | string |
The URL query string to parse. | |
sep='&' | string |
The substring to delimit key and value pairs. | Optional |
eq='=' | string |
The substring to delimit keys and values. | Optional |
options | Object |
Optional |
fromQuery('foo=1&foo=2&foo=3');
// => { foo: ['1', '2', '3' ] }
fromQuery('foo:1|foo:2|foo:3', '|', ':');
// => { foo: ['1', '2', '3' ] }
Object
Returns the parsed URL query string (str) into a collection of key and value pairs.
Parse a query string into an object. Leading ? or # are ignored, so you can pass location.search or location.hash directly. From url/parse
Name | Type | Description | |
---|---|---|---|
urlString | string |
The URL string to parse. | |
parseQueryString=false | boolean |
The query property will be set to an object returned by the querystring module's parse() method if `true`. | Optional |
slashesDenoteHost=false | boolean |
The first token after the literal string // and preceding the next / will be interpreted as the host if `true`. | Optional |
fromURL('https://www.dgmlive.com/kingcrimson/?album=discipline#track-1');
// =>
// {
// protocol: 'https:',
// slashes: true,
// auth: null,
// host: 'www.dgmlive.com',
// port: null,
// hostname: 'www.dgmlive.com',
// hash: '#track-1',
// search: '?album=discipline',
// query: 'album=discipline',
// pathname: '/kingcrimson/',
// path: '/kingcrimson/?album=discipline',
// href: 'https://www.dgmlive.com/kingcrimson/?album=discipline#track-1'
// }
Object
Returns the parsed URL into a collection of key and value pairs.
See if an object is an instance of the String constructor. From ramda/is
Name | Type | Description | |
---|---|---|---|
val | string |
The value to test. |
isString('s');
// => true
isString(new String(''));
// => true
boolean
Returns if the value is a String.
Loosely validate a URL. From is-url
Name | Type | Description | |
---|---|---|---|
string | string |
The URL string to validate. |
isURL('http://symbolics.com');
// => true
isURL('http://')
// => false
isURL('bbn.com')
// => false
Object
Returns true if string is a URL, false otherwise.
Inserts the separator string
between each element and concatenating all the elements into a single string
.
From ramda/join
Name | Type | Description | |
---|---|---|---|
separator | string |
The string used to separate the elements. | |
xs | string |
The elements to join into a string. |
join(' ', ['a', 2, 3.4]);
// => 'a 2 3.4'
const piper = join('|');
piper(['Pied', 'Piper', 'of', 'Hamelin']);
// => 'Pied|Piper|of|Hamelin'
string
Returns thestring
made by concatenatingxs
withseparator
.
Pads string
on the left side if it's shorter than length
. Padding characters are truncated if they exceed length
.
From lodash/padStart
Name | Type | Description | |
---|---|---|---|
length | number |
The padding length. | |
string | string |
The string to pad. |
leftPad(6, 'abc');
// => ' abc'
string
Returns the padded string.
Count visual length of javascript string. From charcount
Name | Type | Description | |
---|---|---|---|
s | string |
The string to count. |
length('🐒🐒🐒🐒🐒🐒🐒🐒🐒🐒🐒🐒');
// => 12
number
Returns length of the string.
Tests a regular expression against a string
.
From ramda/match
Name | Type | Description | |
---|---|---|---|
rx | string |
A regular expression or a substring to match. | |
str | string |
The string to match against. |
match(/([a-z]a)/g, 'bananas');
// => ['ba', 'na', 'na']
Array
The list of matches or emptyarray
.
Pads string
on the left and right sides if it's shorter than length
. Padding characters are truncated if they can't be evenly divided by length
.
From lodash/pad
Name | Type | Description | |
---|---|---|---|
length | number |
The padding length. | |
string | string |
The string to pad. |
pad(8, 'abc');
// => ' abc '
string
Returns the padded string.
Inserts the separator string
between each element and concatenating all the elements into a single string
.
From ramda/replace
Name | Type | Description | |
---|---|---|---|
regex | string |
A regular expression or a substring to match. | |
replacement | string |
The string to replace the matches with. | |
str | string |
The String to do the search and replacement in. |
replace(/foo/g, 'bar', 'foo foo foo');
// => 'bar bar bar'
const censor = replace('the night', 'some time');
censor("Let's spend the night together")
// => "Let's spend some time together"
string
Returns the resultedstring
.
Reverse a string
.
From ramda/reverse
Name | Type | Description | |
---|---|---|---|
list | string |
The string to reverse. |
reverse('stressed');
// => 'desserts'
string
Returns a new string with the characters in reverse order.
Pads string
on the right side if it's shorter than length
. Padding characters are truncated if they exceed length
.
From lodash/padEnd
Name | Type | Description | |
---|---|---|---|
length | number |
The padding length. | |
string | string |
The string to pad. |
rightPad(6, 'abc');
// => 'abc '
string
Returns the padded string.
Converts string
to kebab case.
From lodash/kebabCase
Name | Type | Description | |
---|---|---|---|
string='' | string |
The string to convert. | Optional |
slugify('This, That and the Other! An Outré Collection');
// => 'this-that-and-the-other-an-outre-collection'
string
Returns the kebab cased string.
Splits a string
into an array
of strings based on the given separator.
From ramda/split
Name | Type | Description | |
---|---|---|---|
sep | string |
The pattern. | |
str | string |
The string to separate into an array. |
const path = split('/');
path('/usr/local/bin/node');
// => ['', 'usr', 'local', 'bin', 'node']
Array
Returns the resulting array.
Checks if string
starts with the given target string
.
From lodash/startsWith
Name | Type | Description | |
---|---|---|---|
target | string |
The string to search for. | |
string | string |
The string to inspect. |
startsWith('a', 'abc');
// => true
const startsWithM = startsWith('M');
startsWithM('Mellotron');
// => true
boolean
Returnstrue
ifstring
starts withtarget
, elsefalse
.
Determines whether a given string
matches a given regular expression.
From ramda/test
Name | Type | Description | |
---|---|---|---|
pattern | string |
A regular expression or a substring to match. | |
str | string |
The string to match against. |
test(/^x/, 'xyz');
// => true
test(/^y/, 'xyz');
// => false
boolean
The result of the test.
Convert to lower case. From ramda/toLower
Name | Type | Description | |
---|---|---|---|
str | string |
The string to lower case. |
toLower('XYZ');
// => 'xyz'
string
Returns the lower case version ofstr
.
Stringify an object into a query string, sorting the keys. From querystring/stringify
Name | Type | Description | |
---|---|---|---|
str | string |
The URL query string to parse. | |
sep='&' | string |
The substring to delimit key and value pairs. | Optional |
eq='=' | string |
The substring to delimit keys and values. | Optional |
options | Object |
Optional |
toQuery({ foo: 'bar', baz: ['qux', 'quux'], corge: '' });
// => 'foo=bar&baz=qux&baz=quux&corge='
toQuery({ foo: 'bar', baz: 'qux' }, '|', ':');
// => foo:bar|baz:qux'
Object
Returns the parsed URL query string (str) into a collection of key and value pairs.
Convert to string
.
From ramda/toString
Name | Type | Description | |
---|---|---|---|
val | string |
The value to convert. |
toString(42);
// => '42'
toString([1, 2, 3]);
//=> '[1, 2, 3]'
toString({ foo: 1, bar: 2, baz: 3 });
//=> '{"bar": 2, "baz": 3, "foo": 1}'
string
Returns the string representation of the given value
Convert to upper case. From ramda/toUpper
Name | Type | Description | |
---|---|---|---|
str | string |
The string to upper case. |
toUpper('abc');
// => 'ABC'
string
Returns the upper case version ofstr
.
Convert an url object to URL. From url/format
Name | Type | Description | |
---|---|---|---|
urlObject | Object string |
A URL object (as returned by url.parse() or constructed otherwise). If a string, it is converted to an object by passing it to url.parse(). |
toURL({
protocol: 'https',
host: 'www.dgmlive.com',
hash: '#track-1',
query: { album: 'discipline' },
pathname: '/kingcrimson'
})
// => "https://www.dgmlive.com/kingcrimson?album=discipline#track-1"
string
Returns the resulted URL.
Removes (strips) whitespace from both ends of the string. From ramda/trim
Name | Type | Description | |
---|---|---|---|
str | string |
The string to trim. |
trim(' xyz ');
// => 'xyz'
string
Returns the trimmed version ofstr
.
Removes leading whitespace or specified characters from string
.
From lodash/trimStart
Name | Type | Description | |
---|---|---|---|
string='' | string |
The string to trim. | Optional |
trimStart(' abc ');
// => 'abc '
string
Returns the trimmed string.
Removes trailing whitespace or specified characters from string
.
From lodash/trimEnd
Name | Type | Description | |
---|---|---|---|
string='' | string |
The string to trim. | Optional |
trimRight(' abc ');
// => ' abc'
string
Returns the trimmed string.