A collection of JS utilities.
npm install @fabioquarantini/js-utils
import { setCookie } from 'js-utils';
setCookie("examplename", "123")
Sets a cookie with a name, value, and number of days until it expires.
name
(string): The name of the cookie.value
(string): The value of the cookie.days
(number): The number of days until the cookie expires.
Retrieves the value of a cookie given its name.
name
(string): The name of the cookie.
- (string|null): The value of the cookie if it exists, otherwise null.
Deletes a cookie given its name.
name
(string): The name of the cookie.
Match CSS media queries and JavaScript window width.
- {width: number, height: number}: An object containing the width and height of the viewport.
Checks if the current device is a touch-enabled device.
- {boolean}: True if the device supports touch, false otherwise.
Determines the orientation of the device.
- {string}: 'portrait' if the device is in portrait mode, 'landscape' otherwise.
Calculates the mouse position relative to the document.
event
(Event): The mouse event from which to extract the position.
- {x: number, y: number}: An object containing the x and y coordinates of the mouse.
Retrieves the user's preferred language setting from the browser.
- (string): The user's preferred language as a string (e.g., "en-US").
Gets the language attribute of the current document.
- (string): The language of the current document as defined in its tag (e.g., "en").
Generates a random number within a specified range, optionally formatted to the specified decimal precision.
min
(number): The minimum value of the range.max
(number): The maximum value of the range.decimal
(number, optional): The number of decimal places to include in the result.
- (number): A random number within the specified range, optionally formatted to the specified decimal precision.
Generates a random integer within a specified range.
min
(number): The minimum value of the range.max
(number): The maximum value of the range.
- (number): A random integer within the specified range.
Normalizes a given value from one range to another.
value
(number): The value to normalize.sourceMin
(number): The minimum value of the source range.sourceMax
(number): The maximum value of the source range.targetMin
(number): The minimum value of the target range.targetMax
(number): The maximum value of the target range.
- (number): The value normalized to the target range.
Performs linear interpolation between two values based on an amount.
start
(number): The start value.end
(number): The end value.amount
(number): The interpolation amount between the start and end values.
- (number): The interpolated value.
Checks if an element with the specified selector exists in the DOM.
selector
(string): The CSS selector of the element to check.
- (boolean): True if the element exists, false otherwise.
Observes DOM for specified selector's element additions or removals, triggering callbacks.
selector
(string): CSS selector for matching elements.onAdded
(function): Callback for element additions matching the selector.onRemoved
(function): Callback for element removals matching the selector.observeConfig
(Object): Optional config for the observer (childList, subtree, attributes, characterData).
- (function): Function to disconnect the observer.
Throttles a function call, ensuring that it is only invoked at most once every specified limit in milliseconds.
func
(Function): The function to be throttled.limit
(number): The time limit in milliseconds to throttle the function calls.
- (Function): A throttled version of the input function.
Returns a function, that, as long as it continues to be invoked, will not be triggered. The function will be called after it stops being called for N milliseconds. If 'immediate' is passed, trigger the function on the leading edge, instead of the trailing.
func
(Function): The function to debounce.wait
(number): The number of milliseconds to delay.immediate
(boolean, optional): Specify invoking on the leading edge of the timeout.
- (Function): Returns the new debounced function.
Dispatches a custom event.
eventName
(string): The name of the custom event to dispatch.detail
(*): The data to pass with the event.debug
(boolean, optional, default: false): Enables logging for debugging purposes.
- Will throw an error if the event name is not a string or is empty.
Checks if a given value is a valid email address.
value
(string): The value to be checked.
- (boolean): True if the value is a valid email address, false otherwise.
Checks if a given value is a valid URL.
value
(string): The value to be checked.
- (boolean): True if the value is a valid URL, false otherwise.
Checks if a given value is an integer.
value
(string): The value to be checked.
- (boolean): True if the value is an integer, false otherwise.
Checks if a given value is numeric.
value
(string): The value to be checked.
- (boolean): True if the value is numeric, false otherwise.
Checks if a given value is a floating-point number.
value
(string): The value to be checked.
- (boolean): True if the value is a float, false otherwise.
Checks if a given value is empty. Can optionally ignore white space.
value
(string): The value to be checked.ignoreWhiteSpace
(boolean): If true, white space is ignored.
- (boolean): True if the value is empty, false otherwise.
Get the base URL of the current page. Combines protocol, host, and port information.
- (string): The site URL.
Get the full URL of the current page. Includes protocol, host, path, and query string.
- (string): The current page URL.
Get template URL from the data attributes 'data-template-url' of the body tag.
- (string|null): The template URL if set, or null if not found.