Files
utils/docs/modules.md

9.5 KiB

@resultium/utils - v1.0.0 / Exports

@resultium/utils - v1.0.0

Table of contents

Functions

Functions

capitalizeFirstChar

capitalizeFirstChar(string): string

Capitalizes first character of the given string

Parameters

Name Type Description
string string string to modify

Returns

string

new string

Example

// Prints "Lorem ipsum"
console.log(capitalizeFirstChar("lorem ipsum"))

Defined in

functions/capitalizeFirstChar.ts:32


classNames

classNames(...classes): string

Conditionally joins given class strings

Parameters

Name Type Description
...classes string[] class strings to join

Returns

string

new string

Example

let isVisible = false

// prints "bg-blue-300 hidden"
console.log(classNames("bg-blue-300", isVisible ? "block" : "hidden"))

Defined in

functions/classNames.ts:34


conditionalJoin

conditionalJoin(array, joinChar): string

Conditionally joins given strings

Parameters

Name Type Description
array string[] strings to join
joinChar string character used to join strings

Returns

string

new string

Example

let isAdmin = true

// prints "Hello, Administrator!"
console.log(conditionalJoin(["Hello,", isAdmin ? "Administrator!" : "User!"], " "))

Defined in

functions/conditionalJoin.ts:35


extractTextFromHTML

extractTextFromHTML(html, domParser?): null | string

Extracts text content from an HTML string

Parameters

Name Type Description
html string HTML to extract text from
domParser DOMParser DOMParser to use

Returns

null | string

Remarks

By default this function uses new window.DOMParser() to parse HTML, however on the server you might need to use alternative DOM Parser such as JSDOM, puppeteer or cheerio as window object is not defined in NodeJS

See

Why doesn't Node.js have a native DOM? for more information on why this function is made the way it is

Example

Here's how you would use this function in browser

let HTML = '<p><a>Lorem ipsum</a> dolor sit</p>'

// prints "Lorem ipsum dolor sit"
console.log(extractTextFromHTML(HTML))

Example

Here's how you would use this function on server

import { JSDOM } from "jsdom";
const domParser = new new JSDOM().window.DOMParser();

let HTML = '<p><a>Lorem ipsum</a> dolor sit</p>'

// prints "Lorem ipsum dolor sit"
console.log(extractTextFromHTML(HTML, domParser))

Defined in

functions/extractTextFromHTML.ts:56


isDeepWeakEqual

isDeepWeakEqual(obj1, obj2): boolean

Weakly compares two objects

Parameters

Name Type Description
obj1 object first object for comparison
obj2 object second object for comparison

Returns

boolean

true if equal

Remarks

uses JSON.stringify()

Example

// prints false
console.log(isDeepWeakEqual({ a: 1 }, ["lorem"]))

// prints true
console.log(isDeepWeakEqual({ a: 1 }, { a: 1 }))

// prints true
// snippet displaying the weak equality of this function
console.log(
  isDeepWeakEqual(
    { date: "2024-03-12T18:19:50.548Z" },
    { date: new Date(1710267590548) },
  ),
);

Defined in

functions/isDeepWeakEqual.ts:47


randomInRange

randomInRange(min, max): number

Returns a pseudo-random number in given range

Parameters

Name Type Description
min number minimum acceptable value
max number maximum acceptable value

Returns

number

number [min; max]

Remarks

Uses Math.random(), which is not cryptographically secure Returns only integers

Example

// prints 66
console.log(randomInRange(1, 100))

Defined in

functions/randomInRange.ts:37


removeFromArrayByKeyValue

removeFromArrayByKeyValue<T>(array, keyValue): T[]

Removes an object from an array by key value object.

Type parameters

Name
T

Parameters

Name Type Description
array T[] array which shall be manipulated with
keyValue Partial<{ [K in string | number | symbol]: T[K] }> key-value object which defines which object shall be removed from the array

Returns

T[]

modified array

Remarks

This method doesn't support deep comparements. Vulnerable to generic object injection sink

Example

// prints [{ a: "1", b: "0" }, { a: "2", b: "0" }]
console.log(removeFromArrayByKeyValue([{ a: "1", b: "0" }, { a: "2", b: "0" }, { a: "3", b: "0" }], { a: "3" }))

// prints []
console.log(removeFromArrayByKeyValue([{ a: "1", b: "0" }, { a: "2", b: "0" }, { a: "3", b: "0" }], { b: "0" }))

Defined in

functions/removeFromArrayByKeyValue.ts:40


removeObjectProperty

removeObjectProperty<T, K>(object, key): Omit<T, K>

Removes an object property by provided key

Type parameters

Name Type
T T
K extends string | number | symbol

Parameters

Name Type Description
object T object which shall be manipulated
key K key which should be used to remove a property from object

Returns

Omit<T, K>

new object

Example

// print { a: "1", b: "2" }
console.log(removeObjectProperty({ a: "1", b: "2", c: "3" }, "c"))

Defined in

functions/removeObjectProperty.ts:33


replaceFromArrayByKeyValue

replaceFromArrayByKeyValue<T>(array, keyValue, newObject): T[]

Replaces an object from an array with key value object search

Type parameters

Name
T

Parameters

Name Type Description
array T[] array which shall be manipulated with
keyValue Partial<{ [K in string | number | symbol]: T[K] }> key-value object which defines which object shall be removed from the array
newObject Partial<{ [K in string | number | symbol]: T[K] }> object or values to replace within the searched object

Returns

T[]

modified array

Remarks

This method doesn't support deep comparements. Vulnerable to generic object injection sink

Example

// prints [{ a: "1", b: "0" }, { a: "2", b: "0" }, { a: "3", b: "lorem" }]
console.log(replaceFromArrayByKeyValue([{ a: "1", b: "0" }, { a: "2", b: "0" }, { a: "3", b: "0" }], { a: "3" }, { b: "lorem" }))

// prints [{ a: "lorem", b: "0" }, { a: "2", b: "0" }, { a: "3", b: "0" }]
// replaces only the first occurance
console.log(replaceFromArrayByKeyValue([{ a: "1", b: "0" }, { a: "2", b: "0" }, { a: "3", b: "0" }], { b: "0" }, { a: "lorem" }))

Defined in

functions/replaceFromArrayByKeyValue.ts:42


splitIntoChunks

splitIntoChunks<T>(array, chunkSize): T[][]

Splits an array into chunks

Type parameters

Name
T

Parameters

Name Type Description
array T[] array which shall be split into chunks
chunkSize number size of a single chunk

Returns

T[][]

array of chunks

Example

// prints [["a", "b", "c"], ["d", "e", "f"]]
console.log(splitIntoChunks(["a", "b", "c", "d", "e", "f"], 3));

Defined in

functions/splitIntoChunks.ts:33