Funciones utiles para desarrollar un ecommerce en vtex
npm install --save @fizzmod/utilsKind: 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 :
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
Sets the currency that will be used by helper functions
Kind: static method of Functions
| Param | Type | Description |
|---|---|---|
| currency | string |
The 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; |
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"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.pngset 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.) |
Kind: static method of Functions
Returns: string - - The cookie value
| Param | Type | Description |
|---|---|---|
| cname | string |
The name of the cookie to get |
Remove cookie in docmuent
Kind: static method of Functions
| Param | Type | Description |
|---|---|---|
| cname | string |
The name of the cookie to delete |
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);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...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"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
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%Check if google mao is loadedd
Kind: static method of Functions
Get the VTEX server time
Kind: static method of Functions
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 |
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) ⇒
check if a string is a valid email
Kind: static method of Validations
| Param | Type | Description |
|---|---|---|
string |
string to check |
Check if a string is a valid URL
Kind: static method of Validations
| Param | Type | Description |
|---|---|---|
| URL | string |
string to check |
Check if a string is a valid json
Kind: static method of Validations
| Param | Type | Description |
|---|---|---|
| json | string |
string to check |
Validate RUT (Chile)
Kind: static method of Validations
| Param | Type | Description |
|---|---|---|
| rut | string |
The rut to validate |
Validate RUC (Perú)
Kind: static method of Validations
| Param | Type | Description |
|---|---|---|
| ruc | string |
The ruc to validate |
Validate RFC (Mexico)
Kind: static method of Validations
| Param | Type | Description |
|---|---|---|
| RFC | string |
The RFC to validate |