Members
(constant) $
Get the first element in the document matching selector. Shorthand for `querySelector`.
- Source
const $elA = $('.foo')
const $elB = $('#bar')
const $elC = $('section')
(constant) $$
Get all elements in the document matching selector. Shorthand for `querySelectorAll`
- Source
const $elA = $$('.example')
const $elB = $$('input[value]')
(constant) base64ToBlob
Converts a Base64 encoded string to a Blob object
- Source
const b64String = '...';
const contentType = 'image/png';
const blob = base64ToBlob(b64String, contentType);
(constant) endsWith
Check if a string ends with a specified substring, case-insensitive.
- Source
// returns true
const result = endsWith('JavaScript', 'script');
(constant) entityMap :Object.<string, string>
Map of special characters to their corresponding HTML entities.
Type:
- Object.<string, string>
- Source
(constant) escapeHTML
Escape special characters in an HTML string.
- Source
const html = '<div class="example">Hello & welcome!</div>';
// returns '<div class="example">Hello & welcome!</div>'
const escaped = escapeHTML(html);
(constant) findUsernames
Searches and returns a string for any social accounts Adapted from: https://stackoverflow.com/questions/15265605/how-to-pull-mentions-out-of-strings-like-twitter-in-javascript Full/Official version: https://github.com/twitter/twitter-text
- Source
const haystack = '@troy what is up man? Are you hanging out with @abed'
const users = findUsernames(haystack) // ['@troy', '@abed']
(constant) formatBytes
Format bytes into a human-readable string with unit.
- Source
// returns '1.0 MB'
formatBytes(1048576)
// returns '1.0-MB'
formatBytes(1048576, '-')
(constant) getLanguage
Get the browser's language.
- Source
// returns 'en-US' or other language code
const language = getLanguage();
(constant) getMessage
Get a message based on the provided language.
- Source
const i18n = { en: 'Hello', es: 'Hola' };
// returns 'Hello'
const message = getMessage('en');
(constant) isBrowser
Determine if the code is running in a browser environment.
- Source
// returns true if running in a browser, false if running in Node.js
const browserEnvironment = isBrowser();
(constant) isJSON
Safely verifies input argument is valid JSON.
- Source
const validJSON = '{"name": "John", "age": 30}'
const invalidJSON = '{name: "John", age: 30}'
console.log(isJSON(validJSON)) // true
console.log(isJSON(invalidJSON)) // false
(constant) isNil
Checks if `value` is `null` or `undefined`.
- Source
isNil(null);
// => true
isNil(undefined);
// => true
isNil(NaN);
// => false
(constant) isNull
Checks if `value` is `null`.
- Source
isNull(null)
// => true
isNull(void 0)
// => false
(constant) isS3
Check if the given path is a valid S3 path.
- Source
const validS3Path = 's3://bucket-name/key-name';
// returns true
const isValid = isS3(validS3Path);
(constant) isServer
Determine if the code is running in a server (non-browser) environment.
- Source
// returns true if running in Node.js, false if running in a browser
const serverEnvironment = isServer();
(constant) isUndefined
Checks if `value` is `undefined`.
- Source
isUndefined(void 0)
// => true
isUndefined(null)
// => false
(constant) parseS3
Parse an S3 path and return the bucket and key.
- Source
const s3Path = 's3://bucket-name/key-name';
// returns { Bucket: 'bucket-name', Key: 'key-name' }
const parsed = parseS3(s3Path);
(constant) sleep
Pause the execution of code for a specified duration.
- Source
async function example() {
console.log('Start');
await sleep(1000);
console.log('After 1 second');
}
(constant) startsWith
Determines if a haystack string starts with a specified needle substring, case-insensitive.
- Source
const haystack = 'Hello, world!';
startsWith(haystack, 'hello'); // returns true
Methods
addClass($element, className)
Add a CSS class name to an element.
Parameters:
Name | Type | Description |
---|---|---|
$element | DOMElement | A DOM element to check. |
className | String | A class to add to the DOM element. |
- Source
Example
addClass($element, 'another-class')
attr($element, attributeName, attributeValueopt) → {*}
Set attribute `name` to `val`, or get attr `name`. Derived from: github.com/bpmn-io/min-dom/blob/master/lib/attr.js
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
$element | Element | DOM Element | |
attributeName | String | The name of the DOM element attribute. | |
attributeValue | String | <optional> | The new value for the DOM element. |
- Source
Returns:
- Type:
- *
Examples
const style = attr($('.el'), 'style')
attr($('.el') 'style', null)
attr($('.el'), 'id', 'new-element-id')
buildQuery(data) → {string}
Build a query string from an object of data.
Parameters:
Name | Type | Description |
---|---|---|
data | Object | The data to turn into a query string. |
- Source
Returns:
The query string.
- Type:
- string
Example
const data = { param1: 'value1', param2: 'value2' };
// returns 'param1=value1¶m2=value2'
const queryString = buildQuery(data);
clone(obj) → {Object}
Creates an immutable clone of an object.
Parameters:
Name | Type | Description |
---|---|---|
obj | Object | The object to clone. |
- Source
Returns:
- The clone of the object.
- Type:
- Object
Example
const original = { a: 1, b: { c: 2 } };
const cloned = clone(original);
cloned.a = 3;
cloned.b.c = 4;
console.log(original); // { a: 1, b: { c: 2 } }
console.log(cloned); // { a: 3, b: { c: 4 } }
dedupe(arr) → {Array}
Remove duplicate items from an array. Creates a new array with duplicate items removed. Adapted from: (c) 2019 Chris Ferdinandi, MIT License https://gomakethings.com
Parameters:
Name | Type | Description |
---|---|---|
arr | Array | The input array. |
- Source
Returns:
A new array with duplicates removed.
- Type:
- Array
Example
// Remove duplicates from an array
const uniqueArray = dedupe([1, 2, 2, 3, 4, 4, 5]);
console.log(uniqueArray); // Output: [1, 2, 3, 4, 5]
domify(html) → {Element}
Convert an HTML string to a DOM object. Parses an HTML string and returns the first child element of the resulting DOM tree.
Parameters:
Name | Type | Description |
---|---|---|
html | string | A raw HTML string. |
- Source
Returns:
The first child element of the DOM tree generated from the input HTML string.
- Type:
- Element
Example
// Convert an HTML string to a DOM object
const $el = domify('<p>example</p>');
console.log($el); // Output: <p>example</p>
console.log($el instanceof HTMLElement); // Output: true
filter(obj, callback) → {Object}
Create a new object composed of properties that meet specific criteria.
Parameters:
Name | Type | Description |
---|---|---|
obj | Object | The original object. |
callback | function | The callback test to run. |
- Source
Returns:
- The new, filtered object.
- Type:
- Object
fromArray(arr) → {string|undefined}
Converts an array to a comma-separated string
Parameters:
Name | Type | Description |
---|---|---|
arr | Array | The input array |
- Source
Returns:
- The comma-separated string or undefined if the input is not an array
- Type:
- string |
undefined
Example
const inputArray = ['a', 'b', 'c'];
const result = fromArray(inputArray); // 'a,b,c'
fromBase64(stropt) → {string}
Decodes a Base64 encoded string to ASCII
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
str | string | <optional> | '' | The Base64 encoded string |
- Source
Returns:
- The decoded ASCII string
- Type:
- string
Example
const base64String = 'aGVsbG8gd29ybGQ=';
const result = fromBase64(base64String); // 'hello world'
getAvailablePort(startingAtopt) → {Promise.<number>}
Finds an available port for listening.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
startingAt | number | <optional> | 3000 | Port to begin scanning from. |
- Source
Returns:
- Returns a Promise that resolves to an unused port.
- Type:
- Promise.<number>
Example
const port = await getAvailablePort(8080);
console.log(`${port} is available`);
getContrast(hexCodeopt) → {string}
Find the appropriate contrast color (either black or white) for a given hex code.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
hexCode | string | <optional> | '#008000' | Three or six character hex code, with `#` optional. |
- Source
Returns:
The contrasting color (black or white).
- Type:
- string
Example
getContrast('#ff0000')
// => '#ffffff'
getContrast('000')
// => '#ffffff'
getCustomCSSVariables() → {Array}
Return all custom variables from the document. Derived from: stackoverflow.com/questions/45763121/list-css-custom-properties-css-variables
Returns:
- Type:
- Array
Example
const allCustomCSSVariables = getCustomCSSVariables('<p>example</p>')
getDataAttributes($element) → {DOMStringMap}
Returns all HTML `data-*` attributes attached to an element.
Parameters:
Name | Type | Description |
---|---|---|
$element | HTMLElement |
- Source
Returns:
dataset
- Type:
- DOMStringMap
Example
Can be used as promise:
const dataAttributes = getDataAttributes($el)
getDirectory(dir) → {Array.<string>}
Recursively reads a directory and returns an array of all files.
Parameters:
Name | Type | Description |
---|---|---|
dir | string | The directory path to read. |
- Source
Returns:
An array of file paths.
- Type:
- Array.<string>
getDirectoryTree(directoryPath) → {Array.<string>}
Recursively reads a directory and returns an array of all files.
Parameters:
Name | Type | Description |
---|---|---|
directoryPath | string | The directory path to read. |
- Source
Returns:
An array of file paths.
- Type:
- Array.<string>
Example
getDirectory('/path/to/directory') // returns ['/path/to/directory/file1.txt', '/path/to/directory/subdirectory/file2.txt', ...]
getFileType(extension) → {string}
Returns the type of a file based on its extension
Parameters:
Name | Type | Description |
---|---|---|
extension | string | The file extension to check |
- Source
Returns:
The type of the file (document, video, audio, archive, image, or unknown)
- Type:
- string
Example
getFileType('jpg') // returns 'image'
getQueryString(field, urlopt) → {string|null}
Get the value of a query string from a URL.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
field | string | The field to get the value of. | ||
url | string | <optional> | window.location.href | The URL to get the value from (optional). |
- Source
Returns:
The value of the query string or null if not found.
- Type:
- string |
null
Example
const url = 'https://example.com?param1=value1¶m2=value2';
// returns 'value1'
const value = getQueryString('param1', url);
getURLParams(url) → {Object}
Get the URL parameters from a given URL string.
Parameters:
Name | Type | Description |
---|---|---|
url | string | The URL to extract parameters from. |
- Source
Returns:
The URL parameters as key-value pairs.
- Type:
- Object
Example
const url = 'https://example.com?param1=value1¶m2=value2';
// returns { param1: 'value1', param2: 'value2' }
const params = getUrlParams(url);
has(target, key) → {Boolean}
Return true, if target owns a property with the given key.
Parameters:
Name | Type | Description |
---|---|---|
target | Object | |
key | String |
- Source
Returns:
- Type:
- Boolean
hasClass($element, className) → {Boolean}
Check if an element has a given CSS class name.
Parameters:
Name | Type | Description |
---|---|---|
$element | DOMElement | A DOM element to check. |
className | String | A class to check the DOM element for. |
- Source
Returns:
Returns true if element has class, false if not.
- Type:
- Boolean
Example
const result = hasClass($element, 'some-class-to-check')
isDefined(obj) → {Boolean}
Checks if a value is defined.
Parameters:
Name | Type | Description |
---|---|---|
obj | * | The value to check. |
- Source
Returns:
Whether or not the value is defined.
- Type:
- Boolean
Example
if (isDefined(myVar)) {
// myVar is defined
}
isDirectory(directoryPath, fileName) → {boolean}
Determines if a given file in a directory is a directory itself.
Parameters:
Name | Type | Description |
---|---|---|
directoryPath | string | The directory path containing the file. |
fileName | string | The name of the file. |
- Source
Returns:
`true` if the file is a directory, `false` otherwise.
- Type:
- boolean
Example
isDirectory('/path/to/directory', 'subdirectory') // returns true
isDirectory(directoryItems, file) → {boolean}
Determines if a given file in a directory is a directory itself.
Parameters:
Name | Type | Description |
---|---|---|
directoryItems | string | The directory path containing the file. |
file | string | The name of the file. |
- Source
Returns:
`true` if the file is a directory, `false` otherwise.
- Type:
- boolean
isValidHexSimpleColor(hexString) → {boolean}
Check if a given color string is a valid simple color.
Parameters:
Name | Type | Description |
---|---|---|
hexString | string | The hexadecimal color string to check. |
Returns:
Returns true if the provided color string is a valid simple color, otherwise false.
- Type:
- boolean
Example
// Check if a color string is a valid simple color
isValidHexSimpleColor('#FFFFFF'); // returns true
isValidHexSimpleColor('#123456'); // returns true
isValidHexSimpleColor('#FFF'); // returns false
isValidHexSimpleColor('#xyz123'); // returns false
parseJSONFromBytes(bytes) → {Object}
Parses a JSON string from a Buffer or byte array
Parameters:
Name | Type | Description |
---|---|---|
bytes | Buffer | | The Buffer or byte array containing the JSON string |
- Source
Returns:
- The parsed JSON object
- Type:
- Object
Example
const jsonString = '{"key": "value"}';
const buffer = Buffer.from(jsonString, 'utf-8');
const result = parseJSONFromBytes(buffer); // { key: 'value' }
pick(obj, props) → {Object}
Creates a new object composed of properties picked from another object.
Parameters:
Name | Type | Description |
---|---|---|
obj | Object | The object to pick properties from. |
props | Array.<string> | An array of property names to use. |
- Source
Returns:
- The new object.
- Type:
- Object
Example
const sourceObj = {
a: 1,
b: 2,
c: 3
}
const pickedObj = pick(sourceObj, ['a', 'c'])
// pickedObj is now { a: 1, c: 3 }
readFile(path) → {Promise.<string>}
Reads a file and returns a promise that resolves with the file's contents.
Parameters:
Name | Type | Description |
---|---|---|
path | string | The path of the file to read. |
- Source
Returns:
A promise that resolves with the file's contents.
- Type:
- Promise.<string>
reloadPageWhenOnline(windowObject)
Reload the page when the connection is back to get back to the app.
Parameters:
Name | Type | Description |
---|---|---|
windowObject | Window | The window object to attach the event listener to. |
Example
reloadPageWhenOnline(window);
removeClass($element, className)
Remove a CSS class name from an element.
Parameters:
Name | Type | Description |
---|---|---|
$element | DOMElement | A DOM element to remove the class name from. |
className | String | A class to remove from the DOM element. |
- Source
Example
removeClass($element, 'another-class')
sh(cmd) → {Promise.<string>}
Executes a shell command and returns it as a Promise.
Parameters:
Name | Type | Description |
---|---|---|
cmd | string | The shell command to execute. |
- Source
Returns:
- A Promise that resolves with the output of the shell command.
- Type:
- Promise.<string>
toBase64(stropt) → {string}
Encodes a string to Base64
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
str | string | <optional> | '' | The input string |
- Source
Returns:
- The Base64 encoded string
- Type:
- string
Example
const inputString = 'hello world';
const result = toBase64(inputString); // 'aGVsbG8gd29ybGQ='
toggleClass($element, className)
Toggle a CSS class name from an element.
Parameters:
Name | Type | Description |
---|---|---|
$element | DOMElement | A DOM element to toggle the class name on. |
className | String | A class to toggle off/on the DOM element. |
- Source
Example
toggleClass($element, 'another-class')