@dpsys/js-utils
v2.1.1
Published
Set of utilities for JS development
Downloads
48
Maintainers
Readme
Javascript Utilities
Supports CJS, ESM and TypeScript.
Installation
npm i @dpsys/js-utils Example Usage
import { cLog } from '@dpsys/js-utils/misc';
let myVar = 'Hello World';
cLog('myVar', myVar);Complete list
Array
arrayMoveItem(arr, fromIndex, toIndex): Moves an item within an array from one index to another, returning the modified array.
File
blobToBase64(blob): Converts a Blob/File to base64 stringdownloadStream(data, headers): Downloads data stream with proper filename from content-dispositionprint(data, dataType, mimeType): Invokes system print dialog ready to print provided data (base64 or blob)fileExists(url): Checks if file exists at given URL
Callback
debounce(callback, delayMs): Delay function executiondomReady(callback): Executes callback when DOM is ready
Is
isStrictMode(): Detects if code is running in strict modeisEmpty(val): Checks if array/string/object is emptyisArrayStringEmpty(arr): Checks if array or string is emptyisObjectEmpty(obj): Checks if object is emptyisIterable(val): Checks if variable is iterableisString(val): Checks if variable is stringisTouchDevice(): Detects if device has touch capabilities
Misc
emToPx(ems): Converts em units to pixelspxToEm(px): Converts pixels to em unitscLog(valueDescription, value, fn): Enhanced console.log with function name supportcErr(valueDescription, value, fn): Enhanced console.error with function name supportredirect(url, afterMs): Redirects to URL after specified millisecondspause(ms): Creates a promise that resolves after specified millisecondsgetTimestamp(format): Gets current timestamp in seconds or milliseconds
String
stringTruncate(str, length, ellipsis): Truncates string to specified length with ellipsisstringToBool(str): Converts string to boolean
Form
setDisabledStateChildInputs(parent, state): Sets disabled state for all form inputs within parent elementgetFileNames(fileInp): Gets array of file names from file input element
Element
getOffset(el): Gets element's offset relative to documentelCreate(tagName, attrs, innerHTML): Creates HTML element with attributes and inner HTMLisOverflown(el, heightTolerance, widthTolerance): Checks if element content is overflowingisScrollableX(el): Checks if element is scrollable horizontallyisScrollableY(el): Checks if element is scrollable verticallyisScrollableXY(el): Checks if element is scrollable horizontally and verticallygetPositionAmongSiblings(element): Gets element's position among its siblingshtmlToElements(html, elementNodesOnly): Converts HTML string to elements/nodesinsertNodes(nodes, referenceElement, after): Inserts nodes (or nodes created from provided string) before/after the reference elementswitchElements(element1, element2): Swaps positions of two DOM elements
