1.2.0 • Published 2 years ago

easy-ts-utils v1.2.0

Weekly downloads
-
License
ISC
Repository
bitbucket
Last release
2 years ago

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

Details

addDaysToDate

addDaysToDate(date, days): void

Add a given number of days to a given date.

Parameters

NameTypeDescription
dateDateDate to add days to.
daysnumberNumber of days to add.

Returns

void


addHoursToDate

addHours(date, hours): void

Add a given number of hours to a given date.

Parameters

NameTypeDescription
dateDateDate to add hours to.
hoursnumberNumber of hours to add.

Returns

void


addMonthsToDate

addMonthsToDate(date, months): void

Add a given number of months to a given date.

Parameters

NameTypeDescription
dateDateDate to add months to.
monthsnumberNumber of months to add.

Returns

void


subtractDaysFromDate

subtractDaysFromDate(date, days): void

Subtract a given number of days from a given date.

Parameters

NameTypeDescription
dateDateDate to substract days from.
daysnumberNumber of days to subtract.

Returns

void


subtractMonthsFromDate

subtractMonthsFromDate(date, months): void

Subtract a given number of months from a given date.

Parameters

NameTypeDescription
dateDateDate to substract months from.
monthsnumberNumber of months to subtract.

Returns

void


blobToBase64

blobToBase64(blob): Promise<null | string | ArrayBuffer>

Convert a given blob to a base64 string.

Parameters

NameTypeDescription
blobBlobBlob 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

NameTypeDescription
sourcesstring[]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

NameTypeDescription
dateDateDate 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

NameTypeDescription
textstringString 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

NameTypeDescription
textstringString to copy.

Returns

void


deepCopy

deepCopy(input): object | any[]

Create a deep copy of an object or an array.

Parameters

NameTypeDescription
inputobject | 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

NameTypeDescription
enumToMapanyEnum 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

NameTypeDefault valueDescription
lengthnumberundefinedLength 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

NameTypeDescription
dateDateDay 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

NameTypeDescription
dateDateDay 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

NameTypeDescription
stringDatestringString 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

NameTypeDescription
stringToTeststringString 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

NameTypeDescription
urlstringURL 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

NameTypeDefault valueDescription
nnumberundefinedNumber 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

NameTypeDefault valueDescription
dateDateundefinedDate to parse.
language?"en" | "it""en"Language to use for the month.
printTime?booleantrueIf 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

NameTypeDescription
htmlstringString to strip HTML tags from.

Returns

string

Stripped string.


getDaysNumberBetweenDates

getDaysNumberBetweenDates(firstDate, secondDate): number

Get the number of days between two given dates.

Parameters

NameTypeDescription
firstDateDateFirst date of the interval.
secondDateDateSecond date of the interval.

Returns

number

Number of days between given dates.

1.2.0

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago