easy-ts-utils v1.2.0
easy-ts-utils
A collection of functions that you would normally look for on the internet. This package got you covered if you want to preload a bunch of images before displaying them, manipulate a date, check the validity of a URL, strip all the HTML tags from a string and so much more!
Content
- addDaysToDate
- addHoursToDate
- addMonthsToDate
- subtractDaysFromDate
- subtractMonthsFromDate
- blobToBase64
- cacheImages
- calculateTimeElapsed
- capitalizeFirstCharacter
- copyStringToClipboard
- deepCopy
- enumAsArray
- generateRandomString
- getDaysInMonth
- getDaysInWeek
- isValidStringDate
- isValidURL
- isValidYouTubeURL
- numberWithSeparators
- parseDate
- stripHtml
- getDaysNumberBetweenDates
Details
addDaysToDate
▸ addDaysToDate(date
, days
): void
Add a given number of days to a given date.
Parameters
Name | Type | Description |
---|---|---|
date | Date | Date to add days to. |
days | number | Number of days to add. |
Returns
void
addHoursToDate
▸ addHours(date
, hours
): void
Add a given number of hours to a given date.
Parameters
Name | Type | Description |
---|---|---|
date | Date | Date to add hours to. |
hours | number | Number of hours to add. |
Returns
void
addMonthsToDate
▸ addMonthsToDate(date
, months
): void
Add a given number of months to a given date.
Parameters
Name | Type | Description |
---|---|---|
date | Date | Date to add months to. |
months | number | Number of months to add. |
Returns
void
subtractDaysFromDate
▸ subtractDaysFromDate(date
, days
): void
Subtract a given number of days from a given date.
Parameters
Name | Type | Description |
---|---|---|
date | Date | Date to substract days from. |
days | number | Number of days to subtract. |
Returns
void
subtractMonthsFromDate
▸ subtractMonthsFromDate(date
, months
): void
Subtract a given number of months from a given date.
Parameters
Name | Type | Description |
---|---|---|
date | Date | Date to substract months from. |
months | number | Number of months to subtract. |
Returns
void
blobToBase64
▸ blobToBase64(blob
): Promise<null | string | ArrayBuffer>
Convert a given blob to a base64 string.
Parameters
Name | Type | Description |
---|---|---|
blob | Blob | Blob to convert. |
Returns
Promise<null | string | ArrayBuffer>
Promise to wait for the conversion.
cacheImages
▸ cacheImages(sources
): Promise<void[]>
Preload images starting from an array of sources (images URLs) to avoid images flashing.
Parameters
Name | Type | Description |
---|---|---|
sources | string[] | Array of images sources. |
Returns
Promise<void[]>
A promise resolved when all images have been preloaded.
calculateTimeElapsed
▸ calculateTimeElapsed(date
): string
Calculate the time elapsed since a given date and return it in a readable form.
Throws
Will throw an "Invalid Date" error if the given date is greater than new Date().
Parameters
Name | Type | Description |
---|---|---|
date | Date | Date to calculate the time elapsed from. |
Returns
string
Time elapsed in a readable form.
capitalizeFirstCharacter
▸ capitalizeFirstCharacter(text
): string
Capitalize the first character of a given string.
Parameters
Name | Type | Description |
---|---|---|
text | string | String to capitalize the first character of. |
Returns
string
Given string with first character capitalized.
copyStringToClipboard
▸ copyStringToClipboard(text
): void
Copy the given string to the user clipboard.
Parameters
Name | Type | Description |
---|---|---|
text | string | String to copy. |
Returns
void
deepCopy
▸ deepCopy(input
): object | any[]
Create a deep copy of an object or an array.
Parameters
Name | Type | Description |
---|---|---|
input | object | any[] | Object or array to create a deep copy of. |
Returns
object | any[]
Deep copy of the given object or array.
enumAsArray
▸ enumAsArray(enumToMap
): (string | number | symbol)[]
Return a given enum as an array of strings. Based on this stackoverflow question: https://stackoverflow.com/questions/41308123/map-typescript-enum.
Parameters
Name | Type | Description |
---|---|---|
enumToMap | any | Enum to get the values from. |
Returns
(string | number | symbol)[]
Array that contains all the value of the given enum.
generateRandomString
▸ generateRandomString(length
, set?
): string
Generate a random string based on the set of characters specified.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
length | number | undefined | Length of the generated string. |
set? | "alphanumeric" | "alphanumericUppercaseOnly" | "alphanumericLowercaseOnly" | "numeric" | "alphabetic" | "alphabeticUppercaseOnly" | "alphabeticLowercaseOnly" | "alphanumeric" | Set of characters to use. |
Returns
string
Generated random string.
getDaysInMonth
▸ getDaysInMonth(date
): Date[]
Calculate the month in which a given day is contained and return all the dates of the calculated month.
Parameters
Name | Type | Description |
---|---|---|
date | Date | Day to calculate the month. |
Returns
Date[]
Array that contains all dates of the calculated month.
getDaysInWeek
▸ getDaysInWeek(date
): Date[]
Calculate the week in which a given day is contained and return all the dates of the calculated week (starting from monday).
Parameters
Name | Type | Description |
---|---|---|
date | Date | Day to calculate the week. |
Returns
Date[]
Array that contains all dates of the calculated week (starting from monday).
isValidStringDate
▸ isValidStringDate(stringDate
): boolean
Check if a given string date is in the format yyyy-mm-dd.
Parameters
Name | Type | Description |
---|---|---|
stringDate | string | String date to check. |
Returns
boolean
True if the given string date is in the format yyyy-mm-dd, otherwise false.
isValidURL
▸ isValidURL(stringToTest
): boolean
Check if the given string is a valid URL or not.
Parameters
Name | Type | Description |
---|---|---|
stringToTest | string | String to check. |
Returns
boolean
True if the given string is a valid URL, otherwise false.
isValidYouTubeURL
▸ isValidYouTubeURL(url
): boolean
Check if the given URL is a valid YouTube URL or not.
Parameters
Name | Type | Description |
---|---|---|
url | string | URL to check. |
Returns
boolean
True if the given URL is a valid YouTube URL, otherwise false.
numberWithSeparators
▸ numberWithSeparators(n
, separator?
): string
Return a given number as string with separators (for example 1362958 becomes 1.362.958).
Parameters
Name | Type | Default value | Description |
---|---|---|---|
n | number | undefined | Number to add separators to. |
separator? | "." | "," | "." | Separator to use. |
Returns
string
Number with separators as string.
parseDate
▸ parseDate(date
, language?
, printTime?
): string
Parse a date into a readable string.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
date | Date | undefined | Date to parse. |
language? | "en" | "it" | "en" | Language to use for the month. |
printTime? | boolean | true | If add hours and minutes to the parsed date or not. |
Returns
string
Parsed date.
stripHtml
▸ stripHtml(html
): string
Strip all HTML tags from a given string.
Parameters
Name | Type | Description |
---|---|---|
html | string | String to strip HTML tags from. |
Returns
string
Stripped string.
getDaysNumberBetweenDates
▸ getDaysNumberBetweenDates(firstDate
, secondDate
): number
Get the number of days between two given dates.
Parameters
Name | Type | Description |
---|---|---|
firstDate | Date | First date of the interval. |
secondDate | Date | Second date of the interval. |
Returns
number
Number of days between given dates.
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago