Skip to content

Utils (client/server side)

This package contains a set of utilities that can be used on both the client and server side.

Usage

All functions can be imported from the package like this for example:

typescript
import { isValidIp } from '@keload/node-red-dxp/utils';

And you benefit from tree-shaking in the final bundle 🚀

INFO

This documentation is automatically generated from the source code.

Functions

isUrl()

isUrl(string, options): boolean

Validates if the given string is a valid URL.

A valid URL includes a protocol (e.g., http, https) and conforms to the URL standard. If the lenient option is enabled, it will attempt to prepend https:// to the input if it fails the initial check.

Parameters

string

string

The string to validate as a URL.

options

Validation options.

lenient

boolean = false

If true, tries to prepend https:// to the input for lenient validation (default: false).

Returns

boolean

true if the string is a valid URL, otherwise false.

Throws

TypeError if the input is not a string.

Example

typescript
isUrl("https://example.com"); // true
isUrl("example.com", { lenient: true }); // true
isUrl("invalid url"); // false

isValidIP()

isValidIP(ip): boolean

Validates if the given string is a valid IPv4 or IPv6 address.

Combines the validation checks for IPv4 and IPv6 addresses.

Parameters

ip

string

The string to validate as an IP address.

Returns

boolean

true if the string is a valid IPv4 or IPv6 address, otherwise false.

Example

typescript
isValidIP("192.168.1.1"); // true
isValidIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); // true
isValidIP("localhost"); // false

isValidIPv4()

isValidIPv4(ip): boolean

Validates if the given string is a valid IPv4 address.

An IPv4 address consists of four octets separated by dots, where each octet is a number between 0 and 255.

Parameters

ip

string

The string to validate as an IPv4 address.

Returns

boolean

true if the string is a valid IPv4 address, otherwise false.

Example

typescript
isValidIPv4("192.168.1.1"); // true
isValidIPv4("256.256.256.256"); // false
isValidIPv4("localhost"); // false

isValidIPv6()

isValidIPv6(ip): boolean

Validates if the given string is a valid IPv6 address.

An IPv6 address is a 128-bit address represented as eight groups of four hexadecimal digits, separated by colons. Shortened notation and mixed IPv4/IPv6 formats are also supported.

Parameters

ip

string

The string to validate as an IPv6 address.

Returns

boolean

true if the string is a valid IPv6 address, otherwise false.

Example

typescript
isValidIPv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); // true
isValidIPv6("1234::abcd"); // true
isValidIPv6("localhost"); // false

merge()

merge<T>(root, others, matcher): readonly T[]

Given two lists of the same type, iterate the first list and replace items matched by the matcher func in the first place.

Type Parameters

T

Parameters

root

readonly T[]

others

readonly T[]

matcher

(item) => any

Returns

readonly T[]


tryit()

tryit<Args, Return>(func): (...args) => Return extends Promise<any> ? Promise<[Error, undefined] | [undefined, Awaited<Return<Return>>]> : [Error, undefined] | [undefined, Return]

A helper to try an async function without forking the control flow. Returns an error first callback like array response as [Error, result]

Type Parameters

Args extends any[]

Return

Parameters

func

(...args) => Return

Returns

Function

Parameters
args

...Args

Returns

Return extends Promise<any> ? Promise<[Error, undefined] | [undefined, Awaited<Return<Return>>]> : [Error, undefined] | [undefined, Return]