2015-01-23 13:09:30 +00:00
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
var VnDate = require ('./date');
|
|
|
|
|
2017-04-10 15:17:56 +00:00
|
|
|
/**
|
|
|
|
* Checks if two values are equal, it also checks objects. Basic
|
|
|
|
* values are compared using the non-strict equality operator.
|
|
|
|
*
|
|
|
|
* @param {*} a Value to compare to
|
|
|
|
* @param {*} b Value to compare with
|
|
|
|
* @return {Boolean} %true if they are equal, %false otherwise
|
|
|
|
*/
|
|
|
|
function equals (a, b)
|
|
|
|
{
|
|
|
|
if (a == b)
|
|
|
|
return true;
|
|
|
|
if (a instanceof Date && b instanceof Date)
|
|
|
|
return a.getTime () === b.getTime ();
|
|
|
|
if (a && b && (typeof a === 'object') && (typeof b === 'object'))
|
|
|
|
{
|
|
|
|
for (var key in a)
|
|
|
|
if (!equals (a[key], b[key]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (var key in b)
|
|
|
|
if (a[key] === undefined && b[key] != null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
module.exports =
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
regexpNumber: /%\.([0-9]+)d/g
|
|
|
|
,regexpString: /%s/g
|
|
|
|
|
2017-04-10 15:17:56 +00:00
|
|
|
,equals: equals
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
,compare: function (a, b)
|
|
|
|
{
|
|
|
|
if (a === b)
|
|
|
|
return true;
|
2017-03-23 16:20:51 +00:00
|
|
|
if (a instanceof Date && b instanceof Date)
|
2015-01-23 13:09:30 +00:00
|
|
|
return a.getTime () === b.getTime ();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
,format: function (value, format)
|
|
|
|
{
|
|
|
|
if (value === null || value === undefined)
|
|
|
|
return '';
|
|
|
|
|
|
|
|
if (format)
|
|
|
|
switch (typeof value)
|
|
|
|
{
|
|
|
|
case 'number':
|
|
|
|
return format.replace (this.regexpNumber,
|
|
|
|
this.replaceNumber.bind (null, value));
|
|
|
|
case 'string':
|
|
|
|
return format.replace (this.regexpString,
|
|
|
|
this.replaceString.bind (null, value));
|
|
|
|
case 'object':
|
|
|
|
if (value instanceof Date)
|
2016-09-26 09:28:47 +00:00
|
|
|
return VnDate.strftime (value, format);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
,replaceNumber: function (value, token, digits)
|
|
|
|
{
|
|
|
|
return new Number (value).toFixed (parseInt (digits));
|
|
|
|
}
|
|
|
|
|
|
|
|
,replaceString: function (value)
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
};
|
2015-08-27 14:04:34 +00:00
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
window.sprintf = function (formatString)
|
2015-08-27 14:04:34 +00:00
|
|
|
{
|
|
|
|
var args = arguments;
|
|
|
|
|
|
|
|
if (args.length <= 1)
|
|
|
|
return formatString;
|
|
|
|
|
|
|
|
var i = 1;
|
|
|
|
return formatString.replace (/%[s|d]/g, function ()
|
|
|
|
{
|
|
|
|
return args[i++];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|