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 a Set to an array.
parseNumber
  • parseNumber: Parses the given value into a number.
parseBoolean
  • parseBoolean: Parses the given value and returns a boolean representation.
chunkMultiline
  • chunkMultiline: Splits a multiline string into an array of strings, where each string has a maximum length of maxLength.
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 an array to a Set.
getRandomItem
  • getRandomItem: Returns a random item of a given array.
sortAlphabetically
  • sortAlphabetically: Sorts an array of strings alphabetically, in ascending or descending order.
sortNumerically
  • sortNumerically: Sorts an array of numbers 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 a Map to an array.
mapKeysToArray
  • mapKeysToArray: Returns an array containing all the keys from a given Map.
mapEntriesToArray
  • mapEntriesToArray: Converts a Map object to an array of key-value pairs.
mapToObject
  • mapToObject: Converts a Map to an object.
setInMapIfNotExists
  • setInMapIfNotExists: Sets a key-value pair in a Map 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 new Map.
fromMapOfSetsToMapsOfArrays
  • fromMapOfSetsToMapsOfArrays: Converts a Map of Sets to a Map of Arrays.
mapFromArray
  • mapFromArray: Creates a Map from an array of items, using a key handler function to determine the keys.
mapFromArrayWithValues
  • mapFromArrayWithValues: Creates a new Map object from an array by applying a transformation function to each item.
mapOfArraysFromArray
  • mapOfArraysFromArray: Creates a Map 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.
isISODate
  • isISODate: Checks if a given string is in the ISO date format (YYYY-MM-DD)
toISOTime
  • toISOTime: Converts a date to an ISO formatted time string.
isISOTime
  • isISOTime: Checks if the given string is in ISO time format (HH:mm.sss).
isISODateTime
  • isISODateTime: Checks if the given string is in ISO 8601 date-time format.
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.
getWeekNumber
  • getWeekNumber: Calculates the ISO week number for a given date.
getWeekFormat
  • getWeekFormat: Returns a string representing the week format of the given date.
isWeekFormat
  • isWeekFormat: Checks if a given date string is in the "YYYY-Www" week format.C
getMonthFormat
  • getMonthFormat: Returns the month and year in ISO format (YYYY-MM) from a given Date object.
isMonthFormat
  • isMonthFormat: Checks if the given date string is in the format of "YYYY-MM"
parseDateToHTMLFormat
  • parseDateToHTMLFormat: Parses a date string into a specific HTML date format

String processors

capitalize
  • capitalize: Converts first character of string literal type to uppercase.
uncapitalize
  • uncapitalize: Converts first character of string literal type to lowercase.
kebabCase
  • kebabCase: Converts a string to kebab case.
pascalCase
  • pascalCase: Converts a string to pascal case.
snakeCase
  • snakeCase: Converts a string to snake case.
camelCase
  • camelCase: Converts a string to camel case.
upper
  • upper: Converts a string to uppercase.
lower
  • lower: Converts a string to lowercase.
removeAll
  • removeAll: Removes all occurrences of a target string from the input string.
replaceAll
  • replaceAll: Replaces all occurrences of a target string with a replacement string in a given string.
first
  • first: Returns the first element of a string after splitting it by a separator.
last
  • last: Returns the last part of a string after a specified separator.
removeExtension
  • removeExtension: Removes the file extension from a given string.
reverse
  • reverse: Reverses a string.
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 given length using a parser.
createCountArray
  • createCountArray: Creates an array with a given length 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 a string.
isChar
  • isChar: Checks if the provided value is a single character string.
isOneOf
  • isOneOf: Checks if a value is one of the specified values.
isHTTP_URL
  • isHTTP_URL: Checks if a given string is a valid HTTP URL.
isNotEmptyString
  • isNotEmptyString: Checks if a string 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 type string.
isDictOfNumbers
  • isDictOfNumbers: Checks if the given object is a dictionary (object) with values of type number.
isDictOfBooleans
  • isDictOfBooleans: Checks if the given object is a dictionary (object) with values of type boolean.
isDictOfObjects
  • isDictOfObjects: Checks if the given object is a dictionary (object) with values of type object.
isDictOfArrays
  • isDictOfArrays: Checks if the given object is a dictionary (object) with values of type array.
isPrimitive
  • isPrimitive: Checks if a value is a primitive type (string, number, boolean, or null).
isNullish
  • isNullish: Checks if a value is a nullish type (undefined or null).
isNonNullablePrimitive
  • isNonNullablePrimitive: Checks if a value is a primitive type (string, number, or boolean).
isMap
  • isMap: Checks if the provided value is an instance of Map.
isObjectKeyMap
  • isObjectKeyMap: Checks if the given map is a Map where the keys are objects.
isStringKeyMap
  • isStringKeyMap: Checks if the provided value is a Map with string keys.
isNumberKeyMap
  • isNumberKeyMap: Checks if the provided value is a Map with number keys.
isStringOrNumberKeyMap
  • isStringOrNumberKeyMap: Checks if the provided value is a Map with string or number 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 into resolved and rejected 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.