utilibjs v0.0.3
utilibjs
My collection of most common used function in javascript. Most of them found in stackoverflow.
Table of Contents
- Install
- API
- BrowserFunctions
- isSafari() ⇒ boolean
- isChrome() ⇒ boolean
- isOffline() ⇒ boolean
- openInNewTab(url)
- triggerDownload(fileUrl, [fileName], [target])
- downloadFile(file)
- FileFunctions
- getFileType(fileName) ⇒ string
- getRelativePath(path) ⇒ string
- renameFile(file, name) ⇒ File
- getFileName(url) ⇒ string
- convertMBToBytes(mb) ⇒ number
- convertKBToBytes(kb) ⇒ number
- removeFileExtension(fileName) ⇒ string
- KeyboardFunctions
- isEnterKeyPressed(event) ⇒ boolean
- isSpacebarKeyPressed(event) ⇒ boolean
- isEscKeyPressed(event) ⇒ boolean
- isBackspaceKeyPressed(event) ⇒ boolean
- isTabKeyPressed(event) ⇒ boolean
- isShiftKeyPressed(event) ⇒ boolean
- isUndoKeyPressed(event) ⇒ boolean
- StringFunctions
- isEmail(email) ⇒
- generateRandomString(length) ⇒
- truncateStart(fullStr, strLen, separator) ⇒
- truncateMiddle(fullStr, strLen, separator) ⇒
- truncateEmail(email, strLen, separator) ⇒
- toCapitalize(str) ⇒
- MISC
Install
npm install utilibjs
API
BrowserFunctions
isSafari() ⇒ boolean
Check if the browser is Safari.
Kind: global function
Returns: boolean - true if the browser is Safari, false otherwise.
isChrome() ⇒ boolean
Check if the browser is Chrome.
Kind: global function
Returns: boolean - true if the browser is Chrome, false otherwise.
isOffline() ⇒ boolean
Check if the browser is currently offline.
Kind: global function
Returns: boolean - true if the browser is offline, false otherwise.
openInNewTab(url)
Open the given URL in a new tab.
Kind: global function
Param | Type | Description |
---|---|---|
url | string | The URL to open in a new tab. |
triggerDownload(fileUrl, fileName, target)
Download the file at the given URL with the given filename and target.
Kind: global function
Param | Type | Default | Description |
---|---|---|---|
fileUrl | string | The URL of the file to download. | |
fileName | string | null | null | The name of the file to save as. |
target | string | null | null | The target window or tab to open the file in. |
downloadFile(file)
Download the given file by creating a download link and clicking it.
Kind: global function
Param | Type | Description |
---|---|---|
file | File | The file to download. |
FileFunctions
getFileType(fileName) ⇒ string
Get file type based on the file name extension.
Kind: global function
Returns: string - The file type.
Param | Type | Description |
---|---|---|
fileName | string | The file name. |
getRelativePath(path) ⇒ string
Get the relative path from a full path.
Kind: global function
Returns: string - The relative path.
Param | Type | Description |
---|---|---|
path | string | The full path. |
renameFile(file, name) ⇒ File
Rename a file with a new name.
Kind: global function
Returns: File - A new File instance with the new name.
Param | Type | Description |
---|---|---|
file | File | The file to rename. |
name | string | The new name to assign to the file. |
getFileName(url) ⇒ string
Get the file name from a URL.
Kind: global function
Returns: string - The file name.
Param | Type | Description |
---|---|---|
url | string | The file URL. |
convertMBToBytes(mb) ⇒ number
Convert megabytes to bytes.
Kind: global function
Returns: number - The size in bytes.
Param | Type | Description |
---|---|---|
mb | number | The size in megabytes. |
convertKBToBytes(kb) ⇒ number
Convert kilobytes to bytes.
Kind: global function
Returns: number - The size in bytes.
Param | Type | Description |
---|---|---|
kb | number | The size in kilobytes. |
removeFileExtension(fileName) ⇒ string
Remove the file extension from a file name.
Kind: global function
Returns: string - The file name without the extension.
Param | Type | Description |
---|---|---|
fileName | string | The file name. |
KeyboardFunctions
isEnterKeyPressed(event) ⇒ boolean
Check if the Enter key is pressed in the keyboard event.
Kind: global function
Returns: boolean - - True if Enter key is pressed, false otherwise.
Param | Type | Description |
---|---|---|
event | KeyboardEvent | The keyboard event. |
isSpacebarKeyPressed(event) ⇒ boolean
Check if the Spacebar key is pressed in the keyboard event.
Kind: global function
Returns: boolean - - True if Spacebar key is pressed, false otherwise.
Param | Type | Description |
---|---|---|
event | KeyboardEvent | The keyboard event. |
isEscKeyPressed(event) ⇒ boolean
Check if the Escape key is pressed in the keyboard event.
Kind: global function
Returns: boolean - - True if Escape key is pressed, false otherwise.
Param | Type | Description |
---|---|---|
event | KeyboardEvent | The keyboard event. |
isBackspaceKeyPressed(event) ⇒ boolean
Check if the Backspace key is pressed in the keyboard event.
Kind: global function
Returns: boolean - - True if Backspace key is pressed, false otherwise.
Param | Type | Description |
---|---|---|
event | KeyboardEvent | The keyboard event. |
isTabKeyPressed(event) ⇒ boolean
Check if the Tab key is pressed in the keyboard event.
Kind: global function
Returns: boolean - - True if Tab key is pressed, false otherwise.
Param | Type | Description |
---|---|---|
event | KeyboardEvent | The keyboard event. |
isShiftKeyPressed(event) ⇒ boolean
Check if the Shift key is pressed in the keyboard event.
Kind: global function
Returns: boolean - - True if Shift key is pressed, false otherwise.
Param | Type | Description |
---|---|---|
event | KeyboardEvent | The keyboard event. |
isUndoKeyPressed(event) ⇒ boolean
Check if the Undo (Ctrl + Z) key combination is pressed in the keyboard event.
Kind: global function
Returns: boolean - - True if Undo key combination is pressed, false otherwise.
Param | Type | Description |
---|---|---|
event | KeyboardEvent | The keyboard event. |
StringFunctions
isEmail(email) ⇒
Determines whether the input is a valid email address.
Kind: global function
Returns: true
if the input is a valid email address, false
otherwise.
Param | Description |
---|---|
The email address to check. |
generateRandomString(length) ⇒
Generates a random string.
Kind: global function
Returns: A random string of the specified length.
Param | Description |
---|---|
length | The length of the string to generate. |
truncateStart(fullStr, strLen, separator) ⇒
Truncates a string from the beginning and adds a separator at the end.
Kind: global function
Returns: The truncated string with the separator at the end.
Param | Description |
---|---|
fullStr | The string to truncate. |
strLen | The maximum length of the truncated string. |
separator | The separator to add at the end of the truncated string. Default is "...". |
truncateMiddle(fullStr, strLen, separator) ⇒
Truncates a string from both the beginning and the end and adds a separator in the middle.
Kind: global function
Returns: The truncated string with the separator in the middle.
Param | Description |
---|---|
fullStr | The string to truncate. |
strLen | The maximum length of the truncated string. |
separator | The separator to add in the middle of the truncated string. Default is "...". |
truncateEmail(email, strLen, separator) ⇒
Truncates an email address and adds a separator in the middle.
Kind: global function
Returns: The truncated email address with the separator in the middle.
Param | Description |
---|---|
The email address to truncate. | |
strLen | The maximum length of the truncated email address. |
separator | The separator to add in the middle of the truncated email address. Default is "...". |
toCapitalize(str) ⇒
Converts the first letter of each word in a string to uppercase.
Kind: global function
Returns: The input string with the first letter of each word in uppercase.
Param | Description |
---|---|
str | The string to convert. |
MISC
Happy coding!