Utility Library

Introduction to the Monster Utility Library

The Monster Utility Library offers a variety of helper functions and classes to simplify common tasks in JavaScript development. This documentation provides an overview of the key utilities, their usage, and examples.

Object Utilities

clone

The clone(obj) function allows cloning of objects. If an object has a method getClone(), this method is used to create the clone. Note that Proxy, Element, HTMLDocument, and DocumentFragment instances are not cloned. Global objects such as window are also not cloned.


function clone(obj) {
    // Implementation of the clone function
}
    

Example:


const original = {a: 1, b: {c: 2}};
const copy = clone(original);
console.log(copy); // ↦ {a: 1, b: {c: 2}}
    

deepFreeze

The deepFreeze(object) function applies Object.freeze(object) recursively to freeze an object and its properties.


function deepFreeze(object) {
    // Implementation of the deepFreeze function
}
    

Example:


const obj = {a: 1, b: {c: 2}};
deepFreeze(obj);
obj.b.c = 3; // This will not change the value
console.log(obj.b.c); // ↦ 2
    

Comparison Utilities

Comparator

The Comparator class allows abstracting a comparison function. It provides methods to compare values such as lessThanOrEqual, greaterThan, and equal.


class Comparator extends Base {
    // Implementation of the Comparator class
}
    

Example:


const comparator = new Comparator();
console.log(comparator.lessThanOrEqual(2, 5)); // ↦ true
console.log(comparator.greaterThan(4, 2)); // ↦ true
console.log(comparator.equal(4, 4)); // ↦ true
console.log(comparator.equal(4, 5)); // ↦ false
    

Timing Utilities

Sleep

The Sleep(milliseconds) function allows delaying execution by a specified number of milliseconds.


export function Sleep(milliseconds) {
    return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
    

Example:


async function example() {
    console.log('Sleeping...');
    await Sleep(1000);
    console.log('Awake!');
}
example();
    

DeadMansSwitch

The DeadMansSwitch class allows setting a timer that can be reset repeatedly within a defined period.


class DeadMansSwitch extends Base {
    // Implementation of the DeadMansSwitch class
}
    

Example:


const switch = new DeadMansSwitch(1000, () => console.log('Timeout!'));
switch.touch();
    

Processing

The Processing class allows executing several functions in order with specified timeouts.


class Processing extends Base {
    // Implementation of the Processing class
}
    

Example:


const processing = new Processing();
processing.add(() => console.log('First function'), 1000);
processing.add(() => console.log('Second function'), 2000);
processing.run();
    

Environment Utilities

detectRuntimeEnvironment

The detectRuntimeEnvironment function detects and returns the current runtime environment. Possible values are: 'aws-lambda', 'google-functions', 'electron', 'node', 'browser', 'web-worker', 'deno', 'react-native', 'unknown'.


function detectRuntimeEnvironment() {
    // Implementation of the detectRuntimeEnvironment function
}
    

Example:


console.log(detectRuntimeEnvironment()); // ↦ 'node', 'browser', etc.
    

String Utilities

trimSpaces

The trimSpaces(value) function trims spaces that have been protected by a special escape character.


function trimSpaces(value) {
    // Implementation of the trimSpaces function
}
    

Example:


console.log(trimSpaces(' hello \\ ')); // ↦ 'hello '
console.log(trimSpaces('a\\ b')); // ↦ 'a b'
console.log(trimSpaces('a\\\\ b')); // ↦ 'a\\ b'
    

The current width of the area is too small to display the content correctly.