Members

(constant) $

Get the first element in the document matching selector. Shorthand for `querySelector`.
Example
const $elA = $('.foo')
const $elB = $('#bar')
const $elC = $('section')

(constant) $$

Get all elements in the document matching selector. Shorthand for `querySelectorAll`
Example
const $elA = $$('.example')
const $elB = $$('input[value]')

(constant) base64ToBlob

Converts a Base64 encoded string to a Blob object
Example
const b64String = '...';
const contentType = 'image/png';
const blob = base64ToBlob(b64String, contentType);

(constant) endsWith

Check if a string ends with a specified substring, case-insensitive.
Example
// 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>

(constant) escapeHTML

Escape special characters in an HTML string.
Example
const html = '<div class="example">Hello & welcome!</div>';
// returns '&lt;div class=&quot;example&quot;&gt;Hello &amp; welcome!&lt;/div&gt;'
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
Example
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.
Examples
// returns '1.0 MB'
formatBytes(1048576)
// returns '1.0-MB'
formatBytes(1048576, '-')

(constant) getLanguage

Get the browser's language.
Example
// returns 'en-US' or other language code
const language = getLanguage();

(constant) getMessage

Get a message based on the provided language.
Example
const i18n = { en: 'Hello', es: 'Hola' };
// returns 'Hello'
const message = getMessage('en');

(constant) isBrowser

Determine if the code is running in a browser environment.
Example
// 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.
Example
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`.
Example
isNil(null);
// => true

isNil(undefined);
// => true

isNil(NaN);
// => false

(constant) isNull

Checks if `value` is `null`.
Example
isNull(null)
// => true

isNull(void 0)
// => false

(constant) isS3

Check if the given path is a valid S3 path.
Example
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.
Example
// returns true if running in Node.js, false if running in a browser
const serverEnvironment = isServer();

(constant) isUndefined

Checks if `value` is `undefined`.
Example
isUndefined(void 0)
// => true

isUndefined(null)
// => false

(constant) parseS3

Parse an S3 path and return the bucket and key.
Example
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.
Example
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.
Example
const haystack = 'Hello, world!';
startsWith(haystack, 'hello'); // returns true

Methods

addClass($element, className)

Add a CSS class name to an element.
Parameters:
NameTypeDescription
$elementDOMElementA DOM element to check.
classNameStringA class to add to the DOM element.
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:
NameTypeAttributesDescription
$elementElementDOM Element
attributeNameStringThe name of the DOM element attribute.
attributeValueString<optional>
The new value for the DOM element.
Returns:
Type: 
*
Examples

Get element `style` attribute

const style = attr($('.el'), 'style')

Remove element `style` attribute

attr($('.el') 'style', null)

Set element `id` attribute

attr($('.el'), 'id', 'new-element-id')

buildQuery(data) → {string}

Build a query string from an object of data.
Parameters:
NameTypeDescription
dataObjectThe data to turn into a query string.
Returns:
The query string.
Type: 
string
Example
const data = { param1: 'value1', param2: 'value2' };
// returns 'param1=value1&param2=value2'
const queryString = buildQuery(data);

clone(obj) → {Object}

Creates an immutable clone of an object.
Parameters:
NameTypeDescription
objObjectThe object to clone.
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:
NameTypeDescription
arrArrayThe input array.
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:
NameTypeDescription
htmlstringA raw HTML string.
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:
NameTypeDescription
objObjectThe original object.
callbackfunctionThe callback test to run.
Returns:
- The new, filtered object.
Type: 
Object

fromArray(arr) → {string|undefined}

Converts an array to a comma-separated string
Parameters:
NameTypeDescription
arrArrayThe input array
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:
NameTypeAttributesDefaultDescription
strstring<optional>
''The Base64 encoded string
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:
NameTypeAttributesDefaultDescription
startingAtnumber<optional>
3000Port to begin scanning from.
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:
NameTypeAttributesDefaultDescription
hexCodestring<optional>
'#008000'Three or six character hex code, with `#` optional.
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:
NameTypeDescription
$elementHTMLElement
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:
NameTypeDescription
dirstringThe directory path to read.
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:
NameTypeDescription
directoryPathstringThe directory path to read.
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:
NameTypeDescription
extensionstringThe file extension to check
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:
NameTypeAttributesDefaultDescription
fieldstringThe field to get the value of.
urlstring<optional>
window.location.hrefThe URL to get the value from (optional).
Returns:
The value of the query string or null if not found.
Type: 
string | null
Example
const url = 'https://example.com?param1=value1&param2=value2';
// returns 'value1'
const value = getQueryString('param1', url);

getURLParams(url) → {Object}

Get the URL parameters from a given URL string.
Parameters:
NameTypeDescription
urlstringThe URL to extract parameters from.
Returns:
The URL parameters as key-value pairs.
Type: 
Object
Example
const url = 'https://example.com?param1=value1&param2=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:
NameTypeDescription
targetObject
keyString
Returns:
Type: 
Boolean

hasClass($element, className) → {Boolean}

Check if an element has a given CSS class name.
Parameters:
NameTypeDescription
$elementDOMElementA DOM element to check.
classNameStringA class to check the DOM element for.
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:
NameTypeDescription
obj*The value to check.
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:
NameTypeDescription
directoryPathstringThe directory path containing the file.
fileNamestringThe name of the file.
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:
NameTypeDescription
directoryItemsstringThe directory path containing the file.
filestringThe name of the file.
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:
NameTypeDescription
hexStringstringThe 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:
NameTypeDescription
bytesBuffer | Uint8ArrayThe Buffer or byte array containing the JSON string
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:
NameTypeDescription
objObjectThe object to pick properties from.
propsArray.<string>An array of property names to use.
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:
NameTypeDescription
pathstringThe path of the file to read.
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:
NameTypeDescription
windowObjectWindowThe window object to attach the event listener to.
Example
reloadPageWhenOnline(window);

removeClass($element, className)

Remove a CSS class name from an element.
Parameters:
NameTypeDescription
$elementDOMElementA DOM element to remove the class name from.
classNameStringA class to remove from the DOM element.
Example
removeClass($element, 'another-class')

sh(cmd) → {Promise.<string>}

Executes a shell command and returns it as a Promise.
Parameters:
NameTypeDescription
cmdstringThe shell command to execute.
Returns:
- A Promise that resolves with the output of the shell command.
Type: 
Promise.<string>

toBase64(stropt) → {string}

Encodes a string to Base64
Parameters:
NameTypeAttributesDefaultDescription
strstring<optional>
''The input string
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:
NameTypeDescription
$elementDOMElementA DOM element to toggle the class name on.
classNameStringA class to toggle off/on the DOM element.
Example
toggleClass($element, 'another-class')