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 โ€‹

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.
toISOTime โ€‹
  • toISOTime: Converts a date to an ISO formatted time string.
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 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.