@gotrip/utils
v2.2.0
Published
JavaScript utilities library
Downloads
7
Readme
Broxus JavaScript utilities library
How to install
npm install --save @gotrip/js-utils
Contents
Browser utils
Provides a set of utility functions that help check the browser environment.
getUserAgent
getUserAgent(): string
Retrieves and returns the UserAgent string. Used only on the client side.
import { getUserAgent } from '@gotrip/js-utils'
isBot
isBot(): boolean
Checks UserAgent
string for the Search Engine Bot attribute.
import { isBot } from '@gotrip/js-utils'
isBrowser
isBrowser(): boolean
Checks if a browser is being used.
import { isBrowser } from '@gotrip/js-utils'
isChrome
isChrome(ua: string): boolean
Checks if the given UserAgent
string is the UserAgent
of the Chrome browser.
import { isChrome } from '@gotrip/js-utils'
isFirefox
isFirefox(ua: string): boolean
Checks if the given UserAgent
string is the UserAgent
of the Mozilla Firefox browser.
import { isFirefox } from '@gotrip/js-utils'
isSafari
isSafari(ua: string): boolean
Checks if the given UserAgent
string is the UserAgent
of the Safari browser.
import { isSafari } from '@gotrip/js-utils'
Device utils
Provides a set of utility functions that help check the device environment.
getAndroidVersion
getAndroidVersion(ua: string): string | undefined
Retrieves and returns Android version from the passed UserAgent
string. Otherwise, return undefined
.
import { getAndroidVersion } from '@gotrip/js-utils'
getIosVersion
getIosVersion(ua: string): string | undefined
Retrieves and returns iOS version from the passed UserAgent
string. Otherwise, return undefined
.
import { getIosVersion } from '@gotrip/js-utils'
isAndroid
isAndroid(ua: string): boolean
Checks if the given UserAgent
string is the UserAgent
of the Android device.
import { isAndroid } from '@gotrip/js-utils'
isIos
isIos(ua: string): boolean
Checks if the given UserAgent
string is the UserAgent
of the iOS device.
import { isIos } from '@gotrip/js-utils'
isMobile
isMobile(ua: string): boolean
Checks if the given UserAgent
string is the UserAgent
of the mobile device (e.g. any smartphone).
import { isMobile } from '@gotrip/js-utils'
isTablet
isTablet(ua: string): boolean
Checks if the given UserAgent
string is the UserAgent
of the tablet device (e.g. any tablet).
import { isTablet } from '@gotrip/js-utils'
DOM
Provides a set of utility functions that helps interact with DOM.
canUseDom
canUseDom(): boolean
Checks if DOM is available.
import { canUseDom } from '@gotrip/js-utils'
canUseDocElement
canUseDocElement(): boolean
Checks if Document Element is available.
import { canUseDocElement } from '@gotrip/js-utils'
addClass
addClass(node: Element, ...args: any[]): void
Adds class name(s) to the given DOM node.
import { addClass } from '@gotrip/js-utils'
removeClass
removeClass(node: Element, ...args: any[]): void
Removes class name(s) from the given DOM node.
import { removeClass } from '@gotrip/js-utils'
hasClass
hasClass(node: Element, ...args: any[]): boolean
Checks if class name(s) contains in the given DOM node.
import { hasClass } from '@gotrip/js-utils'
toggleClass
toggleClass(node: Element, ...args: any[]): void
Toggle class name(s) in the given DOM node.
import { toggleClass } from '@gotrip/js-utils'
getScrollWidth
getScrollWidth(): number
Get system scrollbar width.
import { getScrollWidth } from '@gotrip/js-utils'
retrieveGlobalAttributes
retrieveGlobalAttributes(props: Record<string, any>): Record<string, string>
Retrieve global attributes (like aria-
, data-
or role
) from the given props hash.
import { retrieveGlobalAttributes } from '@gotrip/js-utils'
Formatters
abbrNumber
abbrNumber(value: number | string, decimals: number = 2): string
Round and abbreviation of the given number by digits and truncate
floating part by the given decimals
. Default decimals is 2.
import { abbrNumber } from '@gotrip/js-utils'
abbrNumber(10600) // => 10.6K
abbrNumber(123456789) // => 123.45M
abbrNumber(123456789879, 4) // => 123.4567B
formattedAmount
formattedAmount(value?: string | number, decimals?: number, options?: FormattedAmountOptions): string
Format amount value to display prettified value in a UI.
If integer part greater or equal roundOn
value - truncate is 0
If fractional part value less than or equal 1e-2 - truncate is 3
If fractional part value less than or equal 1e-3 - truncate is 4
If fractional part value less than or equal 1e-4 - precision is 4
import { formattedAmount } from '@gotrip/js-utils'
formattedAmount(10600) // => 10 600
formattedAmount(123456789) // => 123 456 789
formattedTokenAmount
formattedTokenAmount(value?: string | number, decimals?: number, options?: FormattedAmountOptions): string
Format token amount value to display prettified value in a UI. It's like a formattedAmount
, but it has different truncating system;
By default, truncate is 0
If integer part less than 1e3 - truncate is 4
If integer part less than 1 - truncate is 8
If integer part less than or equal 1e-8 - precision is 4
import { formattedTokenAmount } from '@gotrip/js-utils'
formattedTokenAmount(10600) // => 10 600
formattedTokenAmount('123456789.853234') // => 123 456 789
sliceAddress
sliceAddress(address?: string): string
Returns sliced address string (e.g. 0:00...0000)
import { sliceAddress } from '@gotrip/js-utils'
Generators
makeArray
makeArray(length: number, fill?: ArrayFiller): Array
Generate array with the given length and fill with a given value.
import { makeArray } from '@gotrip/js-utils'
uniqueKey
uniqueKey(length: number = 7, prefix: string = ''): string
Generate unique key with the given length and prefix.
import { uniqueKey } from '@gotrip/js-utils'
Validation
isGoodNumber
isGoodNumber(value: BigNumber | number | string, nonZeroCheck = true): boolean
Checks if the given value is valid number.
You can pass value as BigNumber instance, number or string.
nonZeroCheck
- enable check for zero. If true
- zero being as invalid value.
import { isGoodNumber } from '@gotrip/js-utils'
validateMaxValue
validateMaxValue(maxValue?: string, value?: string): boolean
Checks if the given value less than or equal the given maxValue.
import { validateMaxValue } from '@gotrip/js-utils'
validateMinValue
validateMinValue(minValue?: string, value?: string): boolean
Checks if the given value greater than or equal the given minValue.
import { validateMinValue } from '@gotrip/js-utils'
Parse
cleanObject
cleanObject(obj: Record<string, any>): Record<string, any>
Clean object - removes all null
or empty string keys in object
import { cleanObject } from '@gotrip/js-utils'
parseObject
parseObject(value: any): Record<string, string>[] | Record<string, string> | boolean
Attempting to parse the given value into a JS object. Returns false
if parsing finished with error.
import { parseObject } from '@gotrip/js-utils'
str2json
str2json(string: string, notEvil?: boolean): Record<string, string>[] | Record<string, string> | boolean
Parse JSON-like string to the JS object. Returns false
if parsing finished with error.
import { str2json } from '@gotrip/js-utils'
stripHtmlTags
stripHtmlTags(html: string): string
Remove all HTML tags in the given value.
import { stripHtmlTags } from '@gotrip/js-utils'
Support
animation
animation(): { end: string } | undefined
Checks if animation is supported.
import { animation } from '@gotrip/js-utils'
transition
transition(): { end: string } | undefined
Checks if transition is supported.
import { transition } from '@gotrip/js-utils'
touch
touch(): boolean
Checks if touch is supported.
import { touch } from '@gotrip/js-utils'
isHighDensity
isHighDensity(): boolean
Checks if device is supported high density display.
import { isHighDensity } from '@gotrip/js-utils'
isRetina
isRetina(): boolean
Checks if device is supported Retina display.
import { isRetina } from '@gotrip/js-utils'
Event
cancelEvent
cancelEvent(event: T): void
Cancel passed event.
import { cancelEvent } from '@gotrip/js-utils'
preventEvent
preventEvent(event: T): void
Prevent default behavior in the given event.
import { preventEvent } from '@gotrip/js-utils'
stopEventPropagate
stopEventPropagate(event: T): void
Stop propagation of the given event.
import { stopEventPropagate } from '@gotrip/js-utils'
triggerEvent
triggerEvent(eventName: string): void
Trigger event by the given name.
import { triggerEvent } from '@gotrip/js-utils'
Casts
toBoolean
toBoolean(value: any): boolean
Casts the passed value to a boolean
value.
import { toBoolean } from '@gotrip/js-utils'
toNull
toNull(value: any): null | any
Casts the passed value to null
.
import { toNull } from '@gotrip/js-utils'
toNumber
toNumber(value: any): number | undefined
Casts the passed value to number
. Otherwise, returns undefined
.
import { toNumber } from '@gotrip/js-utils'