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!
Methods
# static exports.byteSize(str) → {Number}
Returns the size of a string in bytes.
Parameters:
| Name | Type | Description |
|---|---|---|
str |
String
|
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. |
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. |
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. |
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 |
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. |
The escaped HTML.
String
Example
console.log(_$.escapeHTML("<div>")); // "<div>"
# 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 |
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 |
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. |
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 |
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. |
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. |
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. |
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. |
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.
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 |
- 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. |
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. |
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. |
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. |
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 |
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. |
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 |
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. |
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. |
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. |
The unescaped HTML.
String
Example
console.log(_$.unescapeHTML("<div>")); // "<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) |
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 |
The replaced string.
String
Example
document.querySelector("h1").innerHTML = _$.widows(document.querySelector("h1").innerHTML);
//Replaces the last space in the <h1>'s innerText with " "