Skip to content
Helpers โ
Import any of these function from @zurich/dev-utils
.
ts
import { sum } from '@zurich/dev-utils/helpers';
const sum_of_values = sum([1, 2, 3]); // 6
Transformers โ
General โ
getLoremPicsum โ
getLoremPicsum
: Generates a random image URL from Picsum with the specified size.
setToArray โ
setToArray
: Converts aSet
to anarray
.
parseNumber โ
parseNumber
: Parses the given value into a number.
parseBoolean โ
parseBoolean
: Parses the given value and returns a boolean representation.
chunkMultiline โ
chunkMultiline
: Splits a multilinestring
into an array of strings, where eachstring
has a maximum length ofmaxLength
.
findShortestWord โ
findShortestWord
: Finds the shortest word in a given string.
findLongestWord โ
findLongestWord
: Finds the longest word in a given string.
For Boolean โ
We provide also some common boolean operators as functions:
not โ
not
: Toggles a boolean value.
nor โ
nor
: Performs the NOR operation on two boolean values.
nand โ
nand
: Performs the NAND operation on two boolean values.
xor โ
xor
: Performs the XOR operation on two boolean values.
For Numbers โ
Some common operations with collections of numbers:
sum โ
sum
: Calculates the sum of all numbers in an array.
sumIf โ
sumIf
: Sums up the numbers in an array that satisfy a given condition.
avg โ
avg
: Calculates the average of an array of numbers
avgIf โ
avgIf
: Calculates the average of the numbers in an array that satisfy a given condition.
For Arrays โ
scrutinize โ
scrutinize
: Filters an array based on a given predicate function.
mapValue โ
mapValue
: Maps the values of a specific key from an array of objects.
doubleLoop โ
doubleLoop
: Executes a callback function for each item in an array of objects, where each object has an array property.
tripleLoop โ
tripleLoop
: Executes a triple loop over an array of objects and invokes a callback function for each iteration.
arrayToSet โ
arrayToSet
: Converts anarray
to aSet
.
getRandomItem โ
getRandomItem
: Returns a random item of a given array.
sortAlphabetically โ
sortAlphabetically
: Sorts an array ofstrings
alphabetically, in ascending or descending order.
sortNumerically โ
sortNumerically
: Sorts an array ofnumbers
in ascending or descending order.
sortObjectsAlphabetically โ
sortObjectsAlphabetically
: Sorts an array of objects alphabetically based on a specified key.
sortObjectsNumerically โ
sortObjectsNumerically
: Sorts an array of objects numerically based on a specified key.
sortByLength โ
sortByLength
:
insertElement โ
insertElement
: Inserts an element at a certain position in an array without modifying the original array.
shuffle โ
shuffle
: Shuffles the elements of an array randomly.
For Objects โ
objectKeys โ
objectKeys
: Returns an array of keys from the given object.
objectValues โ
objectValues
: Returns an array of values from the given object.
objectEntries โ
objectEntries
: Returns an array of entries from the given object.
objectToMap โ
objectToMap
: Converts an object to a Map.
For Maps โ
mapValuesToArray โ
mapValuesToArray
: Converts the values of aMap
to an array.
mapKeysToArray โ
mapKeysToArray
: Returns an array containing all the keys from a givenMap
.
mapEntriesToArray โ
mapEntriesToArray
: Converts aMap
object to an array of key-value pairs.
mapToObject โ
mapToObject
: Converts aMap
to an object.
setInMapIfNotExists โ
setInMapIfNotExists
: Sets a key-value pair in aMap
if the key does not already exist.
returnAMergeWith โ
returnAMergeWith
: Merges two maps and returns a new map.
putArrayItemsInMap โ
putArrayItemsInMap
: Puts array items in a Map.
mergeMaps โ
mergeMaps
: Merges two maps into a newMap
.
fromMapOfSetsToMapsOfArrays โ
fromMapOfSetsToMapsOfArrays
: Converts aMap
ofSets
to aMap
ofArrays
.
mapFromArray โ
mapFromArray
: Creates aMap
from an array of items, using a key handler function to determine the keys.
mapFromArrayWithValues โ
mapFromArrayWithValues
: Creates a newMap
object from an array by applying a transformation function to each item.
mapOfArraysFromArray โ
mapOfArraysFromArray
: Creates aMap
of arrays from an input array, using a key handler function.
mergeMapsOfArrays โ
mergeMapsOfArrays
: Merges two maps of arrays into a single map of arrays. The resulting map contains unique values from both input maps.
For Date โ
isWeekend โ
isWeekend
: Checks if a given date falls on a weekend.
daysDiff โ
daysDiff
: Calculates the difference in days between two dates.
today โ
today
: Returns the current date as an ISO formatted string.
timeStamp โ
timeStamp
: Returns the current time as a clock time string.
toISOStringDate โ
toISOStringDate
: Converts a date to an ISO formatted date string.
toISODate โ
toISODate
: Converts a date to an ISO formatted string.
toISOTime โ
toISOTime
: Converts a date to an ISO formatted time string.
parseUSDateToISODate โ
parseUSDateToISODate
: Converts a date string from US format (MM/DD/YYYY) to ISO format (YYYY-MM-DD)
toClockTime โ
toClockTime
: Converts a date to a clock time string.
getDayName โ
getDayName
: Gets the name of the day of the week for a given date.
getMonthName โ
getMonthName
: Gets the name of the month for a given date.
String processors โ
capitalize โ
capitalize
: Converts first character ofstring
literal type to uppercase.
uncapitalize โ
uncapitalize
: Converts first character ofstring
literal type to lowercase.
kebabCase โ
kebabCase
: Converts astring
to kebab case.
pascalCase โ
pascalCase
: Converts astring
to pascal case.
snakeCase โ
snakeCase
: Converts astring
to snake case.
camelCase โ
camelCase
: Converts astring
to camel case.
upper โ
upper
: Converts astring
to uppercase.
lower โ
lower
: Converts astring
to lowercase.
removeAll โ
removeAll
: Removes all occurrences of a targetstring
from the inputstring
.
replaceAll โ
replaceAll
: Replaces all occurrences of a targetstring
with a replacementstring
in a givenstring
.
first โ
first
: Returns the first element of astring
after splitting it by a separator.
last โ
last
: Returns the last part of astring
after a specified separator.
removeExtension โ
removeExtension
: Removes the file extension from a givenstring
.
reverse โ
reverse
: Reverses astring
.
underscorePerSpace โ
underscorePerSpace
: Replaces all underscores (_
) in the given text with spaces.
spacePerUnderScore โ
spacePerUnderScore
: Replaces all spaces in the given text with underscores (_
).
hyphenPerSpace โ
hyphenPerSpace
: Replaces all hyphens (-
) in the given text with spaces.
spacePerHyphen โ
spacePerHyphen
: Replaces all spaces in the given text with hyphens (-
).
truncateString โ
truncateString
: Truncates a string to a specified length and appends ellipsis if necessary.
truncateStringMiddle โ
truncateStringMiddle
: Truncates a string by replacing the middle portion with ellipsis.
DOM manipulators โ
toggleAttribute โ
toggleAttribute
: Toggles the value of an attribute on an HTML element.
bindInputChange โ
bindInputChange
: Binds an input element's change event to a callback function.
Generators โ
createArray โ
createArray
: Creates an array with a givenlength
using a parser.
createCountArray โ
createCountArray
: Creates an array with a givenlength
and each item indicating its position.
randomNumber โ
randomNumber
: Generates a random number between the specified minimum and maximum values (inclusive).
randomString โ
randomString
: Generates a random string of the specified length.
randomColor โ
randomColor
: Generates a random color in hexadecimal format.
randomBoolean โ
randomBoolean
: Generates a random boolean value..
getLoremIpsumWords โ
getLoremIpsumWords
: Returns the given amount of words from the Lorem ipsum text with a maximum of 200 words.
Comparison โ
objectsAreEqual โ
objectsAreEqual
: Checks if two objects are equal by comparing their stringified representations.
arraysAreEqual โ
arraysAreEqual
: Checks if two arrays are equal by comparing their stringified representations.
Casters โ
trueTypeOf โ
trueTypeOf
: Determines the true type of the given object.
isBoolean โ
isBoolean
: Checks if a value is a boolean.
isString โ
isString
: Checks if the provided value is astring
.
isChar โ
isChar
: Checks if the provided value is a single characterstring
.
isOneOf โ
isOneOf
: Checks if a value is one of the specified values.
isHTTP_URL โ
isHTTP_URL
: Checks if a givenstring
is a valid HTTP URL.
isNotEmptyString โ
isNotEmptyString
: Checks if astring
is not empty.
isNumber โ
isNumber
: Checks if the provided value is a number.
isEven โ
isEven
: Checks if a number is even.
isObject โ
isObject
: Checks if the given value is an object.
isEmptyObject โ
isEmptyObject
: Checks if the given object is an empty object.
isDictOfStrings โ
isDictOfStrings
: Checks if the given object is a dictionary (object
) where all values of typestring
.
isDictOfNumbers โ
isDictOfNumbers
: Checks if the given object is a dictionary (object
) with values of typenumber
.
isDictOfBooleans โ
isDictOfBooleans
: Checks if the given object is a dictionary (object
) with values of typeboolean
.
isDictOfObjects โ
isDictOfObjects
: Checks if the given object is a dictionary (object
) with values of typeobject
.
isDictOfArrays โ
isDictOfArrays
: Checks if the given object is a dictionary (object
) with values of typearray
.
isPrimitive โ
isPrimitive
: Checks if a value is a primitive type (string
,number
,boolean
, ornull
).
isNullish โ
isNullish
: Checks if a value is a nullish type (undefined
ornull
).
isNonNullablePrimitive โ
isNonNullablePrimitive
: Checks if a value is a primitive type (string
,number
, orboolean
).
isMap โ
isMap
: Checks if the provided value is an instance ofMap
.
isObjectKeyMap โ
isObjectKeyMap
: Checks if the given map is aMap
where the keys are objects.
isStringKeyMap โ
isStringKeyMap
: Checks if the provided value is aMap
withstring
keys.
isNumberKeyMap โ
isNumberKeyMap
: Checks if the provided value is aMap
withnumber
keys.
isStringOrNumberKeyMap โ
isStringOrNumberKeyMap
: Checks if the provided value is aMap
withstring
ornumber
keys.
isArray โ
isArray
: Checks if the provided value is an array.
isEmptyArray โ
isEmptyArray
: Checks if the provided value is an array and empty.
isArrayOfNumbers โ
isArrayOfNumbers
: Checks if the given array is an array of numbers.
isArrayOfStrings โ
isArrayOfStrings
: Checks if the given value is an array of strings.
isArrayOfObjects โ
isArrayOfObjects
: Checks if the given array is an array of objects.
isArrayOfBooleans โ
isArrayOfBooleans
: Checks if the given array is an array of booleans.
isMatrix โ
isMatrix
: Checks if the given array is a matrix. A matrix is defined as a two-dimensional array where each element is an array.
Promises โ
reducePromisedItems โ
reducePromisedItems
: This function reduces promised items.
mergePromises โ
mergePromises
: Merges an array of promises into a single promise that resolves to an array of values.
countPromises โ
countPromises
: Counts the resolved values of an array of promises and returns the sum.
awaitResolved โ
awaitResolved
: Waits for multiple promises to resolve and returns an array of resolved values.
awaitRejected โ
awaitRejected
: Returns an array of rejected reasons from an array of promises.
awaitClassified โ
awaitClassified
: Waits for multiple promises to settle and classifies the results intoresolved
andrejected
arrays.
Other โ
inferTypeOfArray โ
inferTypeOfArray
: Infers the types of elements in an array, returning a list with the types.
sleep โ
sleep
: Asynchronously pauses the execution for the specified amount of time.