Skip to content

Latest commit

 

History

History
208 lines (141 loc) · 3.46 KB

API_REFERENCE.md

File metadata and controls

208 lines (141 loc) · 3.46 KB

API Reference

Table of contents

String

Constructor

isString(x)

Returns true if x is a string.

String.isString('123') === true;
String.isString(123) === false;

Prototype

capitalize()

Capitalizes this string.

const s = 'foo bar';

s.capitalize() === 'Foo bar';

contains(pattern)

Returns true if the given pattern matches a slice of a string. Returns false if it does not.

const s = 'bananas';

s.contains('nana') === true;
s.contains('apples') === false;

firstIndex()

Returns the first index in this string.

const s = 'foobar';

s.firstIndex() === 0;

firstItem()

Returns the first characer in this string.

'foobar'.firstItem() === 'f';
''.firstItem() === '';

insert(index, string)

Inserts a string into this string at specified index.

const s = '';

s.insert(0, 'f') === 'f';
s === '';

isEmpty()

Returns true if this string has a length of zero, and false otherwise.

''.isEmpty() === true;
's'.isEmpty() === false;

lastIndex()

Returns the last index in this string.

'foobar'.lastIndex() === 5;
''.lastIndex() === 0;

lastItem()

Returns the last character in this string.

'foobar'.lastItem() === 'r';
''.lastItem() === '';

len()

Returns the length of this string.

'foo'.len() === 3;

lines()

Returns an array containing lines of a string. Lines are ended with either a newline (\n) or a carrige return with a line feed (\r\n). The final line ending is optional.

const a = 'foo\r\nbar\n\nbaz\n';
const al = a.lines();

al[0] === 'foo';
al[1] === 'bar';
al[2] === '';
al[3] === 'baz';

const b = 'foo\nbar\n\r\nbaz';
const bl = b.lines();

bl[0] === 'foo';
bl[1] === 'bar';
bl[2] === '';
bl[3] === 'baz';

retain(predicate)

Retains only the characters specified by the predicate.

const s = 'f_o_ob_ar';

s.retain(c => c !== '_') === 'foobar';
s === 'f_o_ob_ar';

splitWhitespace()

Splits a string by whitespace.

'A few words'.splitWhitespace(); /** ['A', 'few', 'words'] */
' Mary   had\ta\u{2009}little  \n\t lamb'.splitWhitespace(); /** ['Mary', 'had', 'a', 'little', 'lamb'] */

toCamelCase()

Converts this string to camelCase.

'foo-bar'.toCamelCase() === 'fooBar';

toKebabCase()

Converts this string to kebab-case.

'foo_bar'.toKebabCase() === 'foo-bar';

toPascalCase()

Converts this string to PascalCase.

'foo-bar'.toPascalCase() === 'FooBar';

toSnakeCase()

Converts this string to snake_case.

'foo-bar'.toSnakeCase() === 'foo_bar';

toStartCase()

Converts this string to Start Case.

'foo_bar'.toStartCase() === 'Foo Bar';