0
1
Fork 0
hedera-web-mindshore/js/vn/value.js

169 lines
3.4 KiB
JavaScript
Raw Normal View History

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
2017-04-21 10:53:15 +00:00
* values are compared using the strict equality operator.
2017-04-10 15:17:56 +00:00
*
* @param {*} a Value to compare to
* @param {*} b Value to compare with
2017-04-21 10:53:15 +00:00
* @return {boolean} %true if they are equal, %false otherwise
2017-04-10 15:17:56 +00:00
*/
function equals (a, b)
{
2017-04-21 10:53:15 +00:00
if (a === b)
2017-04-10 15:17:56 +00:00
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)
2017-04-21 10:53:15 +00:00
if (a[key] === undefined && b[key] !== undefined)
2017-04-10 15:17:56 +00:00
return false;
return true;
}
return false;
}
2017-04-19 06:16:37 +00:00
/**
2017-04-21 10:53:15 +00:00
* Calculates differences of two key-value objects.
2017-04-19 06:16:37 +00:00
*
* @param {Object} orgObject Value to compare to
* @param {Object} newObject Value to compare with
* @return {Object} The differences or %null if there are no differences
*/
function diff (orgObject, newObject)
{
var key;
var diff = {};
var keys = Object.keys (orgObject);
for (var i = keys.length; --i; key = keys[i])
2017-04-21 10:53:15 +00:00
if (!simpleEquals (orgObject[key], newObject[key]))
diff[key] = simpleClone (newObject[key]);
2017-04-19 06:16:37 +00:00
var keys = Object.keys (newObject);
for (var i = keys.length; --i; key = keys[i])
2017-04-21 10:53:15 +00:00
if (orgObject[key] === undefined && newObject[key] !== undefined)
diff[key] = simpleClone (newObject[key]);
2017-04-19 06:16:37 +00:00
if (Object.keys (diff).length > 0)
return diff;
return null;
}
2017-04-21 10:53:15 +00:00
/**
* Copies a simple value.
*
* @param {*} value The value to be copied
* @return {*} The value copy
*/
function simpleClone (value)
{
if (value instanceof Date)
return value.clone ();
return value;
}
/**
* Checks if two simple values are equal using the 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 simpleEquals (a, b)
{
if (a === b)
return true;
if (a instanceof Date && b instanceof Date)
return a.getTime () === b.getTime ();
return false;
}
2017-04-19 06:16:37 +00:00
/**
* Returns a formated string.
*
* @param {Object} formatString The base string template
* @param {...} arguments Format parameters
2017-04-21 10:53:15 +00:00
* @return {string} The formated string
2017-04-19 06:16:37 +00:00
*/
function sprintf (formatString)
{
var args = arguments;
if (args.length <= 1)
return formatString;
var i = 1;
return formatString.replace (/%[s|d]/g, function ()
{
return args[i++];
});
}
2016-09-26 09:28:47 +00:00
module.exports =
{
regexpNumber: /%\.([0-9]+)d/g
,regexpString: /%s/g
2017-04-10 15:17:56 +00:00
,equals: equals
2017-04-19 06:16:37 +00:00
,diff: diff
2017-04-21 10:53:15 +00:00
,simpleClone: simpleClone
,simpleEquals: simpleEquals
2017-04-19 06:16:37 +00:00
,sprintf: sprintf
2017-04-10 15:17:56 +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)
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);
}
return value;
}
,replaceNumber: function (value, token, digits)
{
return new Number (value).toFixed (parseInt (digits));
}
,replaceString: function (value)
{
return value;
}
};