@fizzmod/utils
Funciones utiles para desarrollar un ecommerce en vtex
Install
npm install --save @fizzmod/utils
Peer Dependecies
object
Utils : Kind: global namespace
-
Utils :
object
-
.Functions :
object
- .setCurrency(currency)
-
.formatPrice(number, [thousands], [decimals], [length], [currency]) ⇒
string
-
.sanitizeString(str, [replace]) ⇒
string
-
.getResizedImage(src, width, height) ⇒
string
-
.setCookie(cname, cvalue, [exdays], [isdomain]) ⇒
void
-
.getCookie(cname) ⇒
string
-
.deleteCookie(cname) ⇒
void
- .addAnimation(name, callback)
- .setStrLength(str, maxLength) ⇒
-
.stripHost(URL) ⇒
string
-
.detectIE() ⇒
string
|false
-
.calculatePercentDiscount(listprice, bestprice) ⇒
string
-
.isGoogleMapLoaded() ⇒
boolean
-
.getServerTime() ⇒
Promise
-
.getCustomDataInfo(customData, appName, fieldsToSearch) ⇒
object
|null
-
.Validations :
object
-
.isEmail(email) ⇒
boolean
-
.isURL(URL) ⇒
boolean
-
.isJSON(json) ⇒
boolean
-
.isRUT(rut) ⇒
boolean
-
.isRUC(ruc) ⇒
boolean
-
.isRFC(RFC) ⇒
boolean
-
.isEmail(email) ⇒
-
.Functions :
object
Utils.Functions : Kind: static namespace of Utils
-
.Functions :
object
- .setCurrency(currency)
-
.formatPrice(number, [thousands], [decimals], [length], [currency]) ⇒
string
-
.sanitizeString(str, [replace]) ⇒
string
-
.getResizedImage(src, width, height) ⇒
string
-
.setCookie(cname, cvalue, [exdays], [isdomain]) ⇒
void
-
.getCookie(cname) ⇒
string
-
.deleteCookie(cname) ⇒
void
- .addAnimation(name, callback)
- .setStrLength(str, maxLength) ⇒
-
.stripHost(URL) ⇒
string
-
.detectIE() ⇒
string
|false
-
.calculatePercentDiscount(listprice, bestprice) ⇒
string
-
.isGoogleMapLoaded() ⇒
boolean
-
.getServerTime() ⇒
Promise
-
.getCustomDataInfo(customData, appName, fieldsToSearch) ⇒
object
|null
Functions.setCurrency(currency)
Sets the currency that will be used by helper functions
Kind: static method of Functions
Param | Type | Description |
---|---|---|
currency | string |
The currency |
string
Functions.formatPrice(number, [thousands], [decimals], [length], [currency]) ⇒ Formats a number
Kind: static method of Functions
Returns: string
- The formatted price
Param | Type | Default | Description |
---|---|---|---|
number |
number | string
|
The number to format | |
[thousands] | string |
""."" |
thousands delimiter |
[decimals] | string |
"","" |
decimal delimiter |
[length] | integer |
2 |
length of decimal |
[currency] | string |
If true, the currency setted with Utils.setCurrency("$") will be added, if a currency (string) is passed it will use that instead; |
string
Functions.sanitizeString(str, [replace]) ⇒ Sanitize a string, removing/replacing all special characters and spaces with underscore
Kind: static method of Functions
Returns: string
- The modified string
Param | Type | Default | Description |
---|---|---|---|
str | string |
The string to sanitize | |
[replace] | string |
""-"" |
The string to replace white spaces with, default "-" |
Example
Utils.sanitizeString("hóla múndo"); //Output "hola-mundo"
string
Functions.getResizedImage(src, width, height) ⇒ Change the width & height from a given VTEX image source
Kind: static method of Functions
Returns: string
- The resized image source
Param | Type | Description |
---|---|---|
src | string |
The source of the image |
width |
int | string
|
The new image with |
height |
int | string
|
The new image height |
Example
//Given an image thumb source
Fizzmod.Utils.getResizedImage('http://fizzmod.vteximg.com.br/arquivos/ids/155242-292-292/image.png', 500, 600);
//Output: http://fizzmod.vteximg.com.br/arquivos/ids/155242-500-600/image.png
//Given a full image source
Fizzmod.Utils.getResizedImage('http://fizzmod.vteximg.com.br/arquivos/ids/155242/image.png', 100, 100);
//Output: http://fizzmod.vteximg.com.br/arquivos/ids/155242-100-100/image.png
void
Functions.setCookie(cname, cvalue, [exdays], [isdomain]) ⇒ set a cookie
Kind: static method of Functions
Param | Type | Description |
---|---|---|
cname | string |
The name of the cookie |
cvalue | mixed |
The value of the cookie, if the value is an object, it will be JSON encoded |
[exdays] | int |
Expiration days, if not set the cookie will last through the session only |
[isdomain] | bool |
Set as domain cookie. (Default false, adding "." before the url.) |
string
Functions.getCookie(cname) ⇒ Kind: static method of Functions
Returns: string
- - The cookie value
Param | Type | Description |
---|---|---|
cname | string |
The name of the cookie to get |
void
Functions.deleteCookie(cname) ⇒ Remove cookie in docmuent
Kind: static method of Functions
Param | Type | Description |
---|---|---|
cname | string |
The name of the cookie to delete |
Functions.addAnimation(name, callback)
Add an animation listener for the given animation name
Kind: static method of Functions
Param | Type | Description |
---|---|---|
name | string |
The animation name |
callback | function |
The animation callback |
Example
Fizzmod.Utils.addAnimation('nodeInserted', myFunction);
Functions.setStrLength(str, maxLength) ⇒
slice string if string is greater than maxLength
Kind: static method of Functions
Returns: new string with three dots
Param | Type | Description |
---|---|---|
str | string |
|
maxLength | number |
default 27 |
Example
Utils.setStrLength('Fizzmod', 3) // Fizz...
string
Functions.stripHost(URL) ⇒ Removes the host from an URL
Kind: static method of Functions
Returns: string
- The modified string
Param | Type | Description |
---|---|---|
URL | string |
The URL |
Example
Utils.stripHost("http://test.vtexcommercestable.com.br/contacto/test"); // "/contacto/test"
string
| false
Functions.detectIE() ⇒ Check whether the browser is IE and return the version if so.
Kind: static method of Functions
Returns: string
| false
- The IE version or false if other browser
string
Functions.calculatePercentDiscount(listprice, bestprice) ⇒ calculates discount percentage between two prices.
Kind: static method of Functions
Returns: string
- - Return percent discunt rounded in Price
Param | Type | Description |
---|---|---|
listprice | number |
Number price of list |
bestprice | number |
Number price for selling |
Example
Utils.calculatePercentDiscount(100, 50) // 50%
boolean
Functions.isGoogleMapLoaded() ⇒ Check if google mao is loadedd
Kind: static method of Functions
Promise
Functions.getServerTime() ⇒ Get the VTEX server time
Kind: static method of Functions
object
| null
Functions.getCustomDataInfo(customData, appName, fieldsToSearch) ⇒ Function for get info into especific fields in app into customData
Kind: static method of Functions
Param | Type | Description |
---|---|---|
customData | object |
customData object into orderForm |
appName | string |
CustomData App name |
fieldsToSearch | array |
Array of string with CustomData app fields names |
object
Utils.Validations : Kind: static namespace of Utils
-
.Validations :
object
-
.isEmail(email) ⇒
boolean
-
.isURL(URL) ⇒
boolean
-
.isJSON(json) ⇒
boolean
-
.isRUT(rut) ⇒
boolean
-
.isRUC(ruc) ⇒
boolean
-
.isRFC(RFC) ⇒
boolean
-
.isEmail(email) ⇒
boolean
Validations.isEmail(email) ⇒ check if a string is a valid email
Kind: static method of Validations
Param | Type | Description |
---|---|---|
string |
string to check |
boolean
Validations.isURL(URL) ⇒ Check if a string is a valid URL
Kind: static method of Validations
Param | Type | Description |
---|---|---|
URL | string |
string to check |
boolean
Validations.isJSON(json) ⇒ Check if a string is a valid json
Kind: static method of Validations
Param | Type | Description |
---|---|---|
json | string |
string to check |
boolean
Validations.isRUT(rut) ⇒ Validate RUT (Chile)
Kind: static method of Validations
Param | Type | Description |
---|---|---|
rut | string |
The rut to validate |
boolean
Validations.isRUC(ruc) ⇒ Validate RUC (Perú)
Kind: static method of Validations
Param | Type | Description |
---|---|---|
ruc | string |
The ruc to validate |
boolean
Validations.isRFC(RFC) ⇒ Validate RFC (Mexico)
Kind: static method of Validations
Param | Type | Description |
---|---|---|
RFC | string |
The RFC to validate |