@plasma-platform/helpers v0.12.1
Full documentation
install:npm i @plasma-platform/helpers -S
Short documentation
Table of Contents
- convertToAppFormat
- convertToDBFormat
- getCurrent
- getLocaleForUrl
- Cookie
- dispatchCustomEvent
- getElement
- getAll
- data
- addClass
- attr
- removeClass
- hasClass
- removeAttribute
- parseHtmlString
- prepend
- append
- renderHtml
- IsomorphicCookie
- ClientCookie
- ServerCookie
- Observer
convertToAppFormat
Function for convert locale bd format (pt or etc) to app format (pt-br or etc)
Parameters
localestring
Examples
import { convertToAppFormat } from '@plasma-platform/helpers/lib/locales';
// or
import convertToAppFormat from '@plasma-platform/helpers/lib/locales/convertToAppFormat';
const locale = convertToAppFormat(uk);
// locale === 'ua'Returns string
convertToDBFormat
Function for convert locale app format (pt-br or etc) to db format (pt or etc)
Parameters
localestring
Examples
import { convertToDBFormat } from '@plasma-platform/helpers/lib/locales';
// or
import convertToDBFormat from '@plasma-platform/helpers/lib/locales/convertToDBFormat';
const locale = convertToDBFormat(ua);
// locale === 'uk'Returns string
getCurrent
Function for get current locale form location or from cookie
Parameters
locationObject
Examples
import { getCurrent } from '@plasma-platform/helpers/lib/locales';
// or
import getCurrent from '@plasma-platform/helpers/lib/locales/getCurrent';
const locale = getCurrent(location);
// locale === { label: 'uk', value: 'ua' }Returns object { label: 'uk', value: 'ua' }
getLocaleForUrl
Function for get locale for url
Parameters
localestring
Examples
import { getLocaleForUrl } from '@plasma-platform/helpers/lib/locales';
// or
import getLocaleForUrl from '@plasma-platform/helpers/lib/locales/getLocaleForUrl';
const locale = getLocaleForUrl('ua');
// locale === '/en'Returns string /uk
Cookie
Cookie API
getItem
getItem
Parameters
sKeystring (required) key to get cookie
Examples
const locale = Cookie.getItem('country_code');Returns string cookie value
setItem
getItem
Parameters
sKeystring (required) for set cookiesValuestring (required) value for set cookievEndstring date to endsPathstring cookie pathsDomainstring cookie domainbSecureboolean is securesSameSitestring SameSite
Examples
const locale = Cookie.setItem('country_code', 'ua');Returns boolean result
removeItem
getItem
Parameters
Examples
const locale = Cookie.removeItem('country_code');Returns boolean result
hasItem
getItem
Parameters
sKeystring (required) key for check cookie
Examples
const locale = Cookie.hasItem('country_code');Returns boolean result
dispatchCustomEvent
Create and dispatch custom event with optional 'detail' parameter
Parameters
namestring Event namedetailany? Additional data (read only)
Examples
dispatchCustomEvent('eventName', { data: 'myData' })Returns boolean
getElement
Get element by selector
Parameters
selectorstring
Returns any
getAll
Get all elements by selector
Parameters
selectorstring
Returns any
data
Get data attributes
Parameters
element
Returns {}
addClass
Add class
Parameters
elementclassList(optional, default[])
Returns any
attr
Attributes
Parameters
elementkeyvalue
Returns any
removeClass
Remove class
Parameters
elementclassList
Returns any
hasClass
Has class
Parameters
elementclassName
Returns boolean
removeAttribute
Remove Attribute
Parameters
elementname
Returns any
parseHtmlString
parses html string to child node
Parameters
htmlStringstring
Returns ChildNode
prepend
inserts element as first child of its parent
Parameters
parentelement
Returns (null | any)
append
inserts element as last child of its parent
Parameters
parentelement
Returns (null | any)
renderHtml
Appends timer to html page
IsomorphicCookie
Utility for interacting with cookies in isomorphic way
switchToServerMode
switchToServerMode
Parameters
Examples
IsomorphicCookie.switchToServerMode(req, res);getItem
getItem
Parameters
sKeystring (required) key to get cookie
Examples
const locale = IsomorphicCookie.getItem('country_code');Returns string cookie value
setItem
setItem
Parameters
sKeystring (required) for set cookiecookieValue(string | object) (required) value for set cookievEndstring date to endsPathstring cookie pathsDomainstring cookie domainbSecureboolean is secure
Examples
IsomorphicCookie.setItem('country_code', 'ua', 36000000, '/', '.templatemonster.me', false);removeItem
removeItem
Parameters
sKeystring (required) key remove cookiesPathstring (required) cookie pathsDomainstring (required) cookie domain
Examples
IsomorphicCookie.removeItem('country_code', '/', '.templatemonster.com);ClientCookie
Client-side utility for cookies
getItem
getItem
Parameters
sKeystring (required) key to get cookie
Examples
const locale = ClientCookie.getItem('country_code');Returns string cookie value
setItem
setItem
Parameters
sKeystring (required) for set cookiesValuestring (required) value for set cookievEndstring date to endsPathstring cookie pathsDomainstring cookie domainbSecureboolean is secure *
Examples
ClientCookie.setItem('country_code', 'ua', 36000000, '/', '.templatemonster.me', false);removeItem
removeItem
Parameters
sKeystring (required) key remove cookiesPathstring (required) cookie pathsDomainstring (required) cookie domain
Examples
ClientCookie.removeItem('country_code', '/', '.templatemonster.com);ServerCookie
Creates server-side utility for cookies
Parameters
oReqobject (required) request object from ExpressoResobject (required) response object from Express
getItem
getItem
Parameters
sKeystring (required) key to get cookie
Examples
const locale = IsomorphicCookie.getItem('country_code');Returns string cookie value
setItem
setItem
Parameters
sKeystring (required) for set cookiesValuestring (required) value for set cookievEndstring date to endsPathstring cookie pathsDomainstring cookie domainbSecureboolean is secure
Examples
ServerCookie.setItem('country_code', 'ua', 36000000, '/', '.templatemonster.me', false);removeItem
removeItem
Parameters
sKeystring (required) key remove cookiesPathstring (required) cookie pathsDomainstring (required) cookie domain
Examples
ServerCookie.removeItem('country_code', '/', '.templatemonster.com);Observer
Observer lib
getObservers
Get observers
Parameters
namestring (required) Event name
Examples
const observer = new Observer();
const observers = observer.getObservers('click');Returns Array<function> Array of callback functions or empty array
subscribe
Subscribe for event and pass callback function to be called
Parameters
Examples
const observer = new Observer();
observer.subscribe('click');Returns function Callback function
unsubscribe
Unsubscribe from event
Parameters
Examples
const observer = new Observer();
observer.subscribe('click');
observer.unsubscribe('click');broadcast
Broadcast event
Parameters
namestring (required) Event namedataany? Data to be passed as argument to callback function
Examples
const observer = new Observer();
observer.subscribe('click');
observer.broadcast('click', 'Hello!');5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago