@plasma-platform/helpers
v0.12.1
Published
helpers functions for storefront project
Downloads
130
Readme
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
locale
string
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
locale
string
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
location
Object
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
locale
string
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
sKey
string (required) key to get cookie
Examples
const locale = Cookie.getItem('country_code');
Returns string cookie value
setItem
getItem
Parameters
sKey
string (required) for set cookiesValue
string (required) value for set cookievEnd
string date to endsPath
string cookie pathsDomain
string cookie domainbSecure
boolean is securesSameSite
string 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
sKey
string (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
name
string Event namedetail
any? Additional data (read only)
Examples
dispatchCustomEvent('eventName', { data: 'myData' })
Returns boolean
getElement
Get element by selector
Parameters
selector
string
Returns any
getAll
Get all elements by selector
Parameters
selector
string
Returns any
data
Get data attributes
Parameters
element
Returns {}
addClass
Add class
Parameters
element
classList
(optional, default[]
)
Returns any
attr
Attributes
Parameters
element
key
value
Returns any
removeClass
Remove class
Parameters
element
classList
Returns any
hasClass
Has class
Parameters
element
className
Returns boolean
removeAttribute
Remove Attribute
Parameters
element
name
Returns any
parseHtmlString
parses html string to child node
Parameters
htmlString
string
Returns ChildNode
prepend
inserts element as first child of its parent
Parameters
parent
element
Returns (null | any)
append
inserts element as last child of its parent
Parameters
parent
element
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
sKey
string (required) key to get cookie
Examples
const locale = IsomorphicCookie.getItem('country_code');
Returns string cookie value
setItem
setItem
Parameters
sKey
string (required) for set cookiecookieValue
(string | object) (required) value for set cookievEnd
string date to endsPath
string cookie pathsDomain
string cookie domainbSecure
boolean is secure
Examples
IsomorphicCookie.setItem('country_code', 'ua', 36000000, '/', '.templatemonster.me', false);
removeItem
removeItem
Parameters
sKey
string (required) key remove cookiesPath
string (required) cookie pathsDomain
string (required) cookie domain
Examples
IsomorphicCookie.removeItem('country_code', '/', '.templatemonster.com);
ClientCookie
Client-side utility for cookies
getItem
getItem
Parameters
sKey
string (required) key to get cookie
Examples
const locale = ClientCookie.getItem('country_code');
Returns string cookie value
setItem
setItem
Parameters
sKey
string (required) for set cookiesValue
string (required) value for set cookievEnd
string date to endsPath
string cookie pathsDomain
string cookie domainbSecure
boolean is secure *
Examples
ClientCookie.setItem('country_code', 'ua', 36000000, '/', '.templatemonster.me', false);
removeItem
removeItem
Parameters
sKey
string (required) key remove cookiesPath
string (required) cookie pathsDomain
string (required) cookie domain
Examples
ClientCookie.removeItem('country_code', '/', '.templatemonster.com);
ServerCookie
Creates server-side utility for cookies
Parameters
oReq
object (required) request object from ExpressoRes
object (required) response object from Express
getItem
getItem
Parameters
sKey
string (required) key to get cookie
Examples
const locale = IsomorphicCookie.getItem('country_code');
Returns string cookie value
setItem
setItem
Parameters
sKey
string (required) for set cookiesValue
string (required) value for set cookievEnd
string date to endsPath
string cookie pathsDomain
string cookie domainbSecure
boolean is secure
Examples
ServerCookie.setItem('country_code', 'ua', 36000000, '/', '.templatemonster.me', false);
removeItem
removeItem
Parameters
sKey
string (required) key remove cookiesPath
string (required) cookie pathsDomain
string (required) cookie domain
Examples
ServerCookie.removeItem('country_code', '/', '.templatemonster.com);
Observer
Observer lib
getObservers
Get observers
Parameters
name
string (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
name
string (required) Event namedata
any? Data to be passed as argument to callback function
Examples
const observer = new Observer();
observer.subscribe('click');
observer.broadcast('click', 'Hello!');