Package -

Akadenia Helpers

A collection of Text, Map, Date helpers and more

Installation

npm install @akadenia/helpers --save

DateHelpers

Import the DateHelpers utility.

import { DateHelpers } from "@akadenia/helpers

DateHelpers.getReadableDateTime(datetime)

Returns the date in yyyy-mm-dd hh:mm:ss format.

Arguments

NameTypeRequiredDescription
datetimeDatefalseThe date to convert to readable date time. Default is current datetime. i.e. new Date()

DateHelpers.getReadableDate(datetime)

Returns the date in yyyy-mm-dd format.

Arguments

NameTypeRequiredDescription
datetimeDatefalseThe date to convert to readable date time. Default is current datetime. i.e. new Date()

DateHelpers.getDateString(date)

Returns a string representing the date in the user's local timezone.

Arguments

NameTypeRequiredDescription
datestring or DatetrueThe date string to convert to the string in the user's local timezone

DateHelpers.parseDate(date)

Returns a date representing the new date object.

Arguments

NameTypeRequiredDescription
datestringtrueThe date string that needs to be returned as a date object

DateHelpers.getShortOrdinalDate(date)

Returns a string representing the date in short ordinal format.

Arguments

NameTypeRequiredDescription
datestringtrueThe date string that needs to be formatted

MapHelpers

Import the MapHelpers utility.

import { MapHelpers } from "@akadenia/helpers

MapHelpers.getDistanceBetweenPoints(point1, point2)

Returns the distance between two points in meters. Returns null if either point is invalid.

Arguments

NameTypeRequiredDescription
point1Array<number>falseThe coordinates of the first point. i.e. [lat, lng]
point2Array<number>falseThe coordinates of the second point. i.e. [lat, lng]

MapHelpers.compareLocations(location1, location2, precision)

Returns a boolean value that indicates whether or not the two locations are the same based on precision provided.

Arguments

NameTypeRequiredDescription
location1Array<number>falseThe coordinates of the first location. i.e. [lat, lng]
location2Array<number>falseThe coordinates of the second location. i.e. [lat, lng]
precisionnumberfalseThe precision to compare the locations to. Default is 6

MapHelpers.getBearingToCoordinate({startCoordinate, endCoordinate})

Returns the bearing to the end coordinate from the start coordinate in degrees.

Arguments

NameTypeRequiredDescription
coordinatesobjecttrueAn object containing the start and end coordinates. i.e. {startCoordinate: [lat, lng], endCoordinate: [lat, lng]}

TextHelpers

Import the TextHelpers utility.

import { TextHelpers } from "@akadenia/helpers

TextHelpers.uuidv4()

Returns a randomly generated string.

Arguments

NameTypeRequiredDescription

TextHelpers.formatPosition(position)

Returns a string position for a provided number. eg. "2nd", "3rd" etc.

Arguments

NameTypeRequiredDescription
positionnumbertrueA position number to add the appropriate suffix to

TextHelpers.truncateText(text, characterLimit)

Returns a string ending in an ellipsis ... or returns the input string if maximum character limit isn't met.

Arguments

NameTypeRequiredDescription
textstringtrueThe text to truncate
characterLimitnumbertrueThe target number of characters to truncate text to

TextHelpers.fileNameFromPath(path)

Returns a file name.

Arguments

NameTypeRequiredDescription
pathstringtrueThe path to extract the file name from

TextHelpers.replaceSpacesWithUnderscore(s)

Returns a string where the spaces have been replaced with underscores.

Arguments

NameTypeRequiredDescription
sstringfalseThe text to replace spaces with underscores in

TextHelpers.replaceUnderscoreWithSpaces(s)

Returns a string where the underscores have been replaced with spaces.

Arguments

NameTypeRequiredDescription
sstringfalseThe text to replace underscores with spaces in

TextHelpers.pluralizeOnCondition(word, condition)

Returns a pluralised version of the input string or returns the same string.

Arguments

NameTypeRequiredDescription
wordstringtrueThe word to pluralise
conditionbooleantrueThe condition that determines whether to plurize the input word

TextHelpers.convertSnakeToCamelCase(data)

Returns the input object or array with all object keys recursively transformed to camelCase. Returns input object if type is invalid.

Arguments

NameTypeRequiredDescription
dataObject or Array<Object>trueThe object whose keys to transform

TextHelpers.convertCamelToSnakeCase(data)

Returns the input object or array with all object keys recursively transformed to snake_case. Returns input object if type is invalid.

Arguments

NameTypeRequiredDescription
dataObject or Array<Object>trueThe object whose keys to transform

TextHelpers.convertCamelToKebabCase(data)

Convert camel case to kebab case The word returned as kebab case

Arguments

NameTypeRequiredDescription
datastringThe word needed to be converted to kebab case from camel case

TextHelpers.convertKebabToCamelCase(data)

Convert kebab case to camel case The word returned as camel case

Arguments

NameTypeRequiredDescription
datastringThe word needed to be converted from kebab case to camel case

TextHelpers.generateAcronym(term)

Generate an acronym from a term an acronym generated from the term

Arguments

NameTypeRequiredDescription
termstringThe term to be converted to an acronym

TextHelpers.isAcronym(data)

Validate if a word is acronym The boolean value when the condition is met

Arguments

NameTypeRequiredDescription
datastringThe word that is being validated as acronym

TextHelpers.acronymToKebabCase(data)

Convert acronym to kebab case The word returned as camel case

Arguments

NameTypeRequiredDescription
datastringThe acronym to be converted to kebab case

TextHelpers.handleNullDisplay(value, defaultValue="N/A")

Returns the input string or the default string if the input value is null or undefined. Arguments

NameTypeRequiredDescription
valuestringtrueThe string to display in the ui
defaultValuestringfalseThe default string to display if the input value is null or undefined defaults to N/A

TextHelpers.enforceCharacterLimit({text, characterLimit, onCharacterLimit})

Returns the input string if the character limit is not exceeded or returns the truncated string when the character limit is exceeded. Calls the onCharacterLimit callback when the character limit is exceeded. Arguments

NameTypeRequiredDescription
textstringtrueThe string to truncate
characterLimitnumbertrueThe target number of characters to truncate text to
onCharacterLimitfunctiontrueThe callback to call when the character limit is exceeded

TextHelpers.isValidEmail(email)

Returns true when the email is a proper email and returns false when the email is not a proper email Arguments

NameTypeRequiredDescription
emailstringtrueThe email to be validated

TextHelpers.generateIdFromWord(word)

Generate ID from word The generated ID from the word

Arguments

NameTypeRequiredDescription
wordstringtrueThe word needed to generate ID from

TextHelpers.generateWordFromId(word)

Get the word from the generated ID The word that is got from the id

Arguments

NameTypeRequiredDescription
wordstringtrueThe id that is needed to get the word from
customListRecord<string, string>falseThe custom lists that has the id with the custom list of words

ObjectHelpers

Import the ObjectHelpers utility.

import { ObjectHelpers } from "@akadenia/helpers

ObjectHelpers.isPureObject(any)

Returns a boolean indicating whether or not the input is a pure object.

Arguments

NameTypeRequiredDescription
objectanytrueThe object to test for pureness

ObjectHelpers.parseCookie(str)

Returns an object containing the key-value pairs of the parsed cookies.

Arguments

NameTypeRequiredDescription
strstringtrueThe string of cookies to parse

ObjectHelpers.filterObjectsByProperty ( array, propertyName, propertyValue)

Filter an array of objects based on a specific property and its corresponding value. Returns an array containing all objects that have the specified property with the given value.

Template T extends Record<string, any>

Arguments

NameTypeRequiredDescription
arrayT[]trueAn array of objects to be searched
propertyNamekeyof TtrueThe name of the property to match against
propertyValueT[keyof T]trueThe value that the property should have to be considered

ObjectHelpers.containsSubObject(mainObject, subObject)

It compares the key-value pairs of the sub-object with the corresponding key-value pairs in the main object. Returns true If all key-value pairs in the sub-object are found in the main object; otherwise, it returns false.

Template T extends Record<string, any>

Arguments

NameTypeRequiredDescription
mainObjectTtrueThe main object to be checked
subObjectPartial<T>trueThe sub-object to be searched for

ObjectHelpers.findObjectBySubObject(array, subObject)

Searches an array of objects for an object that contains a specific sub-object. Returns the first object found in the array that contains the sub-object or null if no match is found.

Template T extends Record<string, any>

Arguments

NameTypeRequiredDescription
arrayT[]trueAn array of objects to be searched
arrayPartial<T>trueThe sub-object to be searched for in the array

ObjectHelpers.filterObjectsBySubObject(array, subObject)

Filters an array of objects based on the presence of a specific sub-object within each object. Returns an array containing all objects from the input array that contain the specified sub-object.

Template T extends Record<string, any>

Arguments

NameTypeRequiredDescription
arrayT[]trueAn array of objects to be filtered
arrayPartial<T>trueThe sub-object to be searched for in the array

ObjectHelpers.objectPropHasValue({object, key, value)}

Checks if the object has the key with the value.

Arguments

NameTypeRequiredDescription
objectObjecttrueThe object to check
keystringtrueThe key to check
valueanytrueThe value to check

ObjectHelpers.findEntry(array, predicate)

Finds the first entry in the array that satisfies the predicate.

Arguments

NameTypeRequiredDescription
arrayArray<any>trueThe array of entries to search
predicate(item: any) => booleantrueThe predicate function to use to search the array

GenericHelpers

Import the GenericHelpers utility.

import { GenericHelpers } from "@akadenia/helpers

GenericHelpers.delay(ms)

A "modern" sleep statement.

Arguments

NameTypeRequiredDescription
msnumbertrueThe number of milliseconds to wait

GenericHelpers.getPreferredUriScheme(host)

Get the preferred scheme for a host

Arguments

NameTypeRequiredDescription
hoststringtrueAn IP address or a domain name

FileHelpers

FileHelpers.checkFileExtension(filePath, validExtensions)

Check if a file path has a valid extension

Arguments

NameTypeRequiredDescription
filePathstringtrueThe file path to check
validExtensionsstring[]trueThe valid extensions

License

MIT

More packages

Akadenia API

An opinionated wrapper around axios library

View package

Akadenia Azure Storage

A wrapper around Azure Storage SDK to make it easier to use

View package