Namespace

string

string

The string namespace of Bijou.js, containing functions to map strings, remove accents from strings, speak text, syntax highlight JS, HTML and CSS and much more!

View Source _copyright.js, line 141

Methods

# static exports.byteSize(str) → {Number}

Returns the size of a string in bytes.

Parameters:
Name Type Description
str String

View Source string.js, line 470

The byte size of the string.

Number
Example
console.log(_$.byteSize("Hello world")); 11

# static exports.camelCase(str) → {String}

camelCases a string.

Parameters:
Name Type Description
str String

The string of non-camelCased text.

View Source string.js, line 356

The camelCased string.

String
Example
console.log(_$.camelCase("Hello world")); // "helloWorld"

# static exports.capitalize(str) → {String}

Capitalizes the first letter of the string

Parameters:
Name Type Description
str String

The string to capitalize.

View Source string.js, line 974

The capitalized string.

String
Example
console.log(_$.capitalize("hello world")); // "Hello world"

# static exports.deburr(str) → {String}

Removes the accents from a string.

Parameters:
Name Type Description
str String

The string to use.

View Source string.js, line 251

The string without accents.

String
Example
console.log(_$.decurr("déjà vu")); // "deja vu"

# static exports.editDistance(a, b) → {Number}

Gets the edit distance between two strings.

Parameters:
Name Type Description
a String

The first string

b String

The seconds string

View Source string.js, line 421

The edit distance between two strings

Number
Example
console.log(_$.editDistance("hello", "Hello")); // 1

# static exports.escapeHTML(str) → {String}

Escapes a string of HTML

Parameters:
Name Type Description
str String

The string of HTML to escape.

View Source string.js, line 1004

The escaped HTML.

String
Example
console.log(_$.escapeHTML("<div>")); // "&lt;div&gt;"

# static exports.forTemplateLiteral(arr, callback) → {String}

Lets you use a for loop in template literals.

Parameters:
Name Type Description
arr Array

The array to loop.

callback mapCallback

The callback to return strings

View Source string.js, line 223

String that has been for looped

String
Example
console.log(`Things: ${_$.forTemplateLiteral(["apple", "orange"], (item, i) => {return `an ${item}`})}`)
// "Things: an apple an orange

# static exports.hash(val) → {Promise}

Hashes a string using the crypto api.

Parameters:
Name Type Description
val String

The string to hash

View Source string.js, line 192

A promise that resolves into the hashed string.

Promise
Example
_$.hash(
	JSON.stringify({ a: 'a', b: [1, 2, 3, 4], foo: { c: 'bar' } })
  ).then(console.log);
  // '04aa106279f5977f59f9067fa9712afc4aedc6f5862a8defc34552d8c7206393'

# static exports.hashString(str, seedopt) → {Number}

Hashes a string to a unique integer (This cannot be decrypted easily).

Parameters:
Name Type Attributes Default Description
str String

The String to hash.

seed Number <optional>
0

The seed of the hash.

View Source string.js, line 394

The hashed string.

Number
Example
console.log(_$.hashString("Hello world")); // 3494146707865688

# static exports.jaroDistance(a, b) → {Number}

Compares two strings using the Jaro-Winkler Distance algorithm.

Parameters:
Name Type Description
a String

The first string

b String

The second string

View Source string.js, line 12

A number representing how similar the two strings are, where 1 is exactly the same and 0 is totally different

Number
Example
_$.jaroDistance('test', 'tes');//0.9416666666666667

# static exports.mapString(str, fn)

Maps a string like an array.

Parameters:
Name Type Description
str String

The string to map

fn mapCallback

The callback function to run to map the string.

View Source string.js, line 238

Example
_$.mapString("Hello world", (e) => e.toUpperCase());//Returns "HELLO WORLD"

# static exports.markdownToHTML(src) → {String}

Converts markdown to HTML.

Parameters:
Name Type Description
src String

The markdown to convert to HTML.

View Source string.js, line 685

The string of HTML converted from the markdown input.

String
Example
console.log(_$.markdownToHTML("_Italic text_, **bold text**")); // "<em>Italic text</em>, <b>bold text</b>"

# static exports.parseCookie(str)

Parses a cookie string into object and value pairs.

Parameters:
Name Type Description
str String

The string to parse.

View Source string.js, line 170

Example
_$.parseCookie("foo=bar; something=hello%20world");//Returns {foo: "bar", something: "hello world"};

# static exports.prefixCSS(prop) → {String}

Prefixes the given CSS property for the current browser.

Parameters:
Name Type Description
prop String

The property to prefix.

View Source string.js, line 144

The prefixed value (camelCased, instead of css-case, so mozAppearance instead of -moz-appearance).

String
Example
document.body.style[_$.prefix("appearance")] = "hidden";//Sets the document body's appearance property to "hidden".

# static exports.previousPage() → {String}

Returns the previous page that the user visited.

View Source string.js, line 1045

The url of the previous page the user visited.

String
Example
console.log(_$.previousPage()); // e.g. "https://bijou.js.org"

# static exports.processList(list) → {Array}

Processes a markdown list by extracting the content of each list item and returning an array of objects with the content and its index in the list.

Parameters:
Name Type Description
list string

the list to be processed

View Source string.js, line 1071

  • An array of objects with the content and its index in the list
Array
Example
const list = '- Item 1\n- Item 2\n- Item 3';
const processedList = processList(list);

// Returns:
// [
//   { content: 'Item 1', idx: 0 },
//   { content: 'Item 2', idx: 1 },
//   { content: 'Item 3', idx: 2 }
// ]

# static exports.removeTags(html) → {String}

Removes tags from the HTML string specified.

Parameters:
Name Type Description
html String

The string of HTML to remove tags from.

View Source string.js, line 263

THe string of HTML without the tags.

String
Example
console.log(_$.removeTags("<div>Hello</div>")); // "Hello"

# static exports.replaceBetween(string, start, end, what) → {String}

Replaces between two indexes of a string.

Parameters:
Name Type Description
string String

The string to operate on.

start Number

The start index

end Number

The end index

what String

What to replace with.

View Source string.js, line 989

The replaced string

String
Example
console.log(_$.replaceBetween("Hello world", 6, 11, "earthlings")); // "Hello earthlings"

# static exports.replaceMultiple(text, replace) → {String}

Finds and replace multiple values with multiple other values.

Parameters:
Name Type Description
text String

The text to operate the replace on.

replace Object

The object with find and replace values.

View Source string.js, line 483

The replaced string

String
Example
_$.replaceMultiple("I have a cat, a dog, and a goat.", {dog: "cat", goat: "dog", cat: "goat"});//Returns "I have a goat, a cat and a dog"

# static exports.sanitize(html, tagsopt, attributesopt) → {String}

Sanitizes an HTML string. It is quite possible that this is not production ready so use with caution. (I did my best though >=( )

Parameters:
Name Type Attributes Description
html String

The input string to sanitize.

tags Array <optional>

The array of tags to allow, there is a default list though.

attributes Array <optional>

The array of attributes to allow. By default only allows "href" and "src" attributes.

View Source string.js, line 529

The sanitized HTML string.

String
Example
console.log(_$.sanitizeHTML("<script>alert('hello')></script><b>A normal tag</b>")); // "<b>A normal tag</b>"

# static exports.scrambleString(str) → {String}

Scrambles the order of characters in a string. Thanks to @\Touchcreator for the suggestion for this.

Parameters:
Name Type Description
str String

The string to be scrambled

View Source string.js, line 372

The scrambled text.

String
Example
console.log(_$.scrambleString("Hello world")); // e.g. "owllH rdloe"

# static exports.speak(text, langopt, volumeopt, voiceopt, pitchopt, volumeopt, rateopt) → {SpeechSynthesisUtterance}

Speaks the text given.

Parameters:
Name Type Attributes Default Description
text String

The text to split

lang String <optional>
en-US

The language to speak with.

volume Number <optional>
1

The volume

voice String | Number <optional>
1

The voice to use.

pitch Number <optional>
1

The pitch

volume Number <optional>
1

The volume

rate Number <optional>
1

The speed.

View Source string.js, line 281

The SpeechSynthesisUtterance

SpeechSynthesisUtterance
Example
_$.speak("Bijou is awesome!"); // speaks "Bijou is awesome!"

# static exports.syllables(word) → {Number}

Counts the syllables in the word given.

Parameters:
Name Type Description
word String

The word to count syllables of

View Source string.js, line 902

The number of syllables in the specified word.

Number
Example
console.log(_$.syllables("Hello")); // 2

# static exports.titleCase(str) → {String}

Converts a string to title case

Parameters:
Name Type Description
str String

The string to convert to title case.

View Source string.js, line 928

The string in title case.

String
Example
_$.titleCase("hello world");//Returns "Hello World"

# static exports.unCamelCase(str) → {String}

Undoes camelCase.

Parameters:
Name Type Description
str String

The string to unCamelCase.

View Source string.js, line 338

The string of unCamelCased code.

String
Example
console.log(_$.unCamelCase("helloWorld")); // "Hello World"

# static exports.unescapeHTML(str) → {String}

Unescapes a string of HTML

Parameters:
Name Type Description
str String

The string of HTML to unescape.

View Source string.js, line 1025

The unescaped HTML.

String
Example
console.log(_$.unescapeHTML("&lt;div&gt;")); // "<div>"

# static exports.urlQuery(query, urlopt) → {String}

Returns the queries from a given url (Or just the current url)

Parameters:
Name Type Attributes Default Description
query String

The url query to get.

url String <optional>
window.location.href

The url to find the query in. (By default this is the current url)

View Source string.js, line 506

The url query

String
Example
// If the website adress of the current page was "https://example.com/?q=hello&hello=world"
console.log(_$.urlQuery("hello")); // "world"
// Or on a custom url:
console.log(_$.urlQuery("q", "https://google.com/search?q=something")); // "something"

# static exports.widows(text) → {String}

Returns the last space in the string given replaced with " "

Parameters:
Name Type Description
text String

The string to replace

View Source string.js, line 315

The replaced string.

String
Example
document.querySelector("h1").innerHTML = _$.widows(document.querySelector("h1").innerHTML);
//Replaces the last space in the <h1>'s innerText with "&nbsp;"