new Utils()

Description

A collection of miscellaneous utility methods that goes from object manipulation, text formatting, data validation, environment information, internationalization, URL manipulation, etc. You can require the module by the Utils, example:

define('MyCustomModule', ['Utils'], function (Utils)
			{
			    var translated = Utils.translate('There are $(0) apples in the tree', 55);
			});

Also, all methods in Utils are mixed in underscore, so for example, you is the same to call Utils.translate() and _.translate()

Methods


formatPhone( phone, format ) → {String}

Description

Will try to reformat a phone number for a given phone Format. If no format is given, it will try to use the one in site settings.

Parameters
Name Type Description
phone String
format String
Returns

dateToString( date ) → {String}

Description

Converts a date object to string using international format YYYY-MM-dd. Useful for inputs of type="date"

Parameters
Name Type Description
date Date
Returns

stringToDate( str_date, options )

Description

Parse a string date into a date object.

Parameters
Name Type Description
str_date String
options Object

format is the String format that specify the format of the input string. By Default YYYY-MM-dd. plusMonth: Number that indicate how many month offset should be applied whne creating the date object.


formatCurrency( value, symbol ) → {String}

Description

Formats given number and symbol to a currency like string. Example: Utils.formatCurrency(10, '£')

Parameters
Name Type Description
value String
symbol String
Returns

formatQuantity( number ) → {String}

Description

Formats with commas as thousand separator (e.g. for displaying quantities)

Parameters
Name Type Description
number String
Returns

the formatted quantity.


validateSecurityCode( value )

Parameters
Name Type Description
value String
Returns

a non empty string with a internationalized warning message


validatePhone( phone ) → {String}

Parameters
Name Type Description
phone String
Returns

an error message if the passed phone is invalid or falsy if it is valid


validateState( value, valName, options ) → {String}

Parameters
Name Type Description
value String
valName String
options Object
Returns

an error message if the passed state is invalid or falsy if it is valid


validateZipCode( value, valName, options ) → {String}

Parameters
Name Type Description
value String
valName String
options Object
Returns

an error message if the passed zip code is invalid or falsy if it is valid function


translate( text )

Description

Used on all of the hardcoded texts in the templates. Gets the translated value from SC.Translations object literal.
Please always use the syntax _('string').translate(1, 2) instead of the syntax _.translate('string', 1, 2) if possible.

Example using the Utils object: Utils.translate('There are $(0) apples in the tree', appleCount)

Sample example using the _.translate (underscore mixing): _('There are $(0) apples in the tree').translate(appleCount)

See frontend_internationalization

Parameters
Name Type Description
text String

@return {String}


deepCopy( obj, obj )

Description

Deep Copy of the object taking care of Backbone models

Parameters
Name Type Description
obj any
obj any

getPathFromObject( object, path [, default_value ] )

Description

Gets given object property supporting deep property references by separating names with dots ('.')

Parameters
Name Type Attributes Description
object any

The object to get the property from

path String

a path with values separated by dots

default_value any <optional>

value to return if nothing is found.


setPathFromObject( object, path, value )

Description

Sets given object property supporting deep property references by separating names with dots ('.')

Parameters
Name Type Description
object any

the object to modify

path String

a path with values separated by dots

value any

the value to set


isShoppingDomain() → {Boolean}

Description

Determines if we are in shopping domain (secure or non secure) or single domain

Returns

true if in checkout or in single domain function


isCheckoutDomain() → {Boolean}

Description

Determines if we are in a secure checkout
domain or in a secure single domain environment

Returns

true if in checkout or in single domain


isSingleDomain() → {Boolean}

Description

Determines if we are in a single domain environment

Returns

true if single domain


isInShopping() → {Boolean}

Description

Determines if we are in shopping ssp used when there are frontend features only shown in the shopping domain

Returns

true if in shopping domain, false if in checkout or myaccount


getAbsoluteUrl( path ) → {String}

Description

returns the absolute URL of given relative path

Parameters
Name Type Description
path String
Returns

isInCheckout() → {Boolean}

Description

Determines if we are in checkout or my account ssp

Returns

true if in checkout domain