Module: rgjs/str

RGJS6 Str module.

Methods


<static> calcStrToAutoBytes(str)

Calculate how many bytes a given string requires (approx) and then make it human readable.

Parameters:
Name Type Description
str string

The string to calculate with.

Returns:

Something a human can read. Maybe monkeys too.

Type
string
Example
rgjs.str.calcStrToAutoBytes(some_string) // '110 bytes'

<static> calcStrToBytes(str)

Calculate how many bytes a given string requires (approx).

Parameters:
Name Type Description
str string

The string to calculate with.

Returns:

The number of bytes the provided string requires.

Type
number
Example
rgjs.str.calcStrToBytes('hello world') // 11

<static> calcStrToKbytes(str)

Calculate how many kilobytes a given string requires (approx).

Parameters:
Name Type Description
str string

The string to calculate with.

Returns:

The number of kilobytes the provided string requires.

Type
number
Example
rgjs.str.calcStrToKbytes('hello world') // 1

<static> camelToDash(str)

Convert camcelCase to dash-syntax.

Parameters:
Name Type Description
str string

A string

Returns:

A string

Type
string
Example
rgjs.str.dashToCamel('helloWorld') // 'hello-world'

<static> camelToUnderscore(str)

Convert camelCase to underscore_syntax.

Parameters:
Name Type Description
str string
Returns:
Type
string
Example
rgjs.str.dashToCamel('helloWorld') // 'hello_world'

<static> capAll(str)

Capitalize every word in a string.

Parameters:
Name Type Description
str string

The string that contains words to capitalize.

Returns:

The string, capitalized.

Type
string
Example
rgjs.str.capAll('hello world') // 'Hello World'

<static> capitalize(str, everyword)

Capitalize a string.

Parameters:
Name Type Description
str string

The string to capitalize.

everyword boolean

If true, Every Word Will Have An Uppercase Character.

Returns:

The string, capitalized.

Type
string
Example
rgjs.str.capitalize('hello world') // 'Hello world'

<static> concat(str, args)

Concat a string.

Parameters:
Name Type Argument Description
str string

A string.

args * <repeatable>

More strings.

Returns:

The concatonated string.

Type
string
Example
rgjs.str.concat('hello', ' ', 'world') // 'hello world'

<static> concatDelim(delim, str, args)

Concat a string with delimiters.

Parameters:
Name Type Argument Description
delim string

The delimiter.

str string

A string.

args * <repeatable>

More strings.

Returns:

The concatonated string.

Type
string
Example
rgjs.str.concatDelim('.', 'path', 'to', 'value'); // 'path.to.value'

<static> countWords(str)

Count number of words.

Parameters:
Name Type Description
str string

A string of words.

Returns:

The number of words in str.

Type
number
Example
rgjs.str.countWords('hello world I'm here') // 4

<static> dashToCamel(str)

Convert dash-syntax to camelCase.

Parameters:
Name Type Description
str string

A string

Returns:

A string

Type
string
Example
rgjs.str.dashToCamel('hello-world') // 'helloWorld'

<static> dashToUnderscore(str)

Convert dash-syntax to underscore_syntax.

Parameters:
Name Type Description
str string
Returns:
Type
string
Example
rgjs.str.dashToCamel('hello-world') // 'hello_world'

<static> escapeHtml(html)

Escape html characters.

Parameters:
Name Type Description
html string

The string that may contain html tags.

Returns:

The string that no longer contains html tags.

Type
string
Example
rgjs.str.escapeHtml('<div attr="value"></div>') // '&lt;div attr=&quot;value&quot;&gt;&lt;/div&gt;'

<static> escapeHtmlAttr(attr)

Escape html attr characters.

Parameters:
Name Type Description
attr string

The string that may contain attr-sensitive characters.

Returns:

The string that no longer contains attr-sensitive characters.

Type
string
Example
rgjs.str.escapeHtmlAttr('<div attr="value"></div>') // '<div attr=&quot;value&quot;></div>'

<static> htmlEntitiesToUnicode(str)

Convert html entities to plain unicode.
Deprecated. Use stripHtml() instead.

Parameters:
Name Type Description
str string

The string that may contain html entities.

Deprecated:
  • Yes
Returns:

The string that no longer contains html entities.

Type
boolean
Example
rgjs.str.htmlEntitiesToUnicode('<p>This is a paragraph</p>') // 'This is a paragraph'

<static> includes(haystack, needle [, position])

Polyfill for String.includes().

Parameters:
Name Type Argument Default Description
haystack string

The haystack.

needle string

The needle.

position number <optional>
0

Optional start position.

Returns:

True or false.

Type
boolean
Example
rgjs.str.includes('hello world', 'world') // true

<static> isStr()

Check if a value is a string.
Alias of isString().

Deprecated:
  • Yes
See:
  • rgjs/str/isString

<static> isString(val)

Check if a value is a string.

Parameters:
Name Type Description
val anything

A value

Returns:

True if string, false if not.

Type
boolean
Example
const o = {a: 0, b: '', c: {}, d: []};
rgjs.str.isString(o) // false
rgjs.str.isString(o.a) // false
rgjs.str.isString(o.b) // true

<static> limit(str, maxLen, suffix)

Limit a string's length.

Parameters:
Name Type Description
str string

A string of words.

maxLen int

The max number of characters.

suffix string

The suffix for the shortened string if it exceeds maxLen.

Returns:

The shortened string (or "").

rgjs.str.limit('hello world I'm here', 8) // 'hello wo...'

Type
string

<static> limitWords(str, maxLen, suffix, separator)

Limit a string's length to X words.

Parameters:
Name Type Description
str string

A string of words.

maxLen int

The max number of words.

suffix string

The suffix for the shortened string if it exceeds maxLen.

separator string

Optional; separator used to split words, defaults to 'space' (' ').

Returns:

The shortened string (or "").

Type
string
Example
rgjs.str.limitWords('hello world I'm here', 2) // 'hello world...'

<static> replaceAll(haystack, needle, replace, regexOpts)

Replace all occurances of 'needle' in 'haystack' with 'replace'.

Parameters:
Name Type Description
haystack string

The string that looks like a haystack.

needle string

The thing that needs replacing.

replace string

The replacement for the needle.

regexOpts string

Options for the regex. Defaults to 'g'.

Returns:

A needleless haystack.

Type
string
Example
rgjs.str.replaceAll('{needle} {needle}', '{needle}', 'needle') // 'needle needle'

<static> spacifyPunctuation(str)

Ensure a space after punctuation.

Parameters:
Name Type Description
str string

The string.

Returns:

The resulting string.

Type
string
Example
rgjs.str.spacifyPunctuation('Hello.World.') // 'Hello. World.'

<static> stripHtml(str, allowed)

Strip html tags and/or attributes from a string. Uses rgjs.dom.filterNodes under the hood.

Parameters:
Name Type Description
str string

The string that may contain html.

allowed object

The tags and attributes that are allowed. Example: {link:["href", "rel", type"]}.

Returns:

The string, stripped.

Type
string
Examples
rgjs.str.stripHtml('<p>This is a paragraph</p>') // 'This is a paragraph'
rgjs.str.stripHtml('<p>This <em>is</em> a paragraph</p>', {em:[]}) // 'This <em>is</em> a paragraph'

<static> underscoreToCamel(str)

Convert underscore_syntax to camelCase.

Parameters:
Name Type Description
str string
Returns:
Type
string
Example
rgjs.str.dashToCamel('hello_world') // 'helloWorld'

<static> underscoreToDash(str)

Convert underscore_syntax to dash-syntax.

Parameters:
Name Type Description
str string
Returns:
Type
string
Example
rgjs.str.dashToCamel('hello_world') // 'hello-world'

<static> unescapeHtml(html)

Unescape html characters.

Parameters:
Name Type Description
html string

The string that may contain escaped html tags.

Returns:

The string that no longer contains escaped html tags

Type
string
Example
rgjs.str.unescapeHtml('&lt;div attr=&quot;value&quot;&gt;&lt;/div&gt;') // '<div attr="value"></div>'

<static> unescapeHtmlAttr(attr)

Unescape attr characters.

Parameters:
Name Type Description
attr string

The string that may contain escaped attr-sensitive characters.

Deprecated:
  • Yes
Returns:

The string that no longer contains escapedattr-sensitive characters.

Type
string
Example
rgjs.str.unescapeHtmlAttr('<div attr=&quot;value&quot;></div>') // '<div attr="value"></div>'

<inner> contains()

Contains.
Alias of includes().

See:
  • rgjs/str/includes