139 lines
3.7 KiB
JavaScript
139 lines
3.7 KiB
JavaScript
import isString from 'lodash/isString';
|
|
import isNan from 'lodash/isNaN';
|
|
import get from 'lodash/get';
|
|
import lodashIsNumber from 'lodash/isNumber';
|
|
export var mathSign = function mathSign(value) {
|
|
if (value === 0) {
|
|
return 0;
|
|
}
|
|
if (value > 0) {
|
|
return 1;
|
|
}
|
|
return -1;
|
|
};
|
|
export var isPercent = function isPercent(value) {
|
|
return isString(value) && value.indexOf('%') === value.length - 1;
|
|
};
|
|
export var isNumber = function isNumber(value) {
|
|
return lodashIsNumber(value) && !isNan(value);
|
|
};
|
|
export var isNumOrStr = function isNumOrStr(value) {
|
|
return isNumber(value) || isString(value);
|
|
};
|
|
var idCounter = 0;
|
|
export var uniqueId = function uniqueId(prefix) {
|
|
var id = ++idCounter;
|
|
return "".concat(prefix || '').concat(id);
|
|
};
|
|
|
|
/**
|
|
* Get percent value of a total value
|
|
* @param {number|string} percent A percent
|
|
* @param {number} totalValue Total value
|
|
* @param {number} defaultValue The value returned when percent is undefined or invalid
|
|
* @param {boolean} validate If set to be true, the result will be validated
|
|
* @return {number} value
|
|
*/
|
|
export var getPercentValue = function getPercentValue(percent, totalValue) {
|
|
var defaultValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
|
|
var validate = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
if (!isNumber(percent) && !isString(percent)) {
|
|
return defaultValue;
|
|
}
|
|
var value;
|
|
if (isPercent(percent)) {
|
|
var index = percent.indexOf('%');
|
|
value = totalValue * parseFloat(percent.slice(0, index)) / 100;
|
|
} else {
|
|
value = +percent;
|
|
}
|
|
if (isNan(value)) {
|
|
value = defaultValue;
|
|
}
|
|
if (validate && value > totalValue) {
|
|
value = totalValue;
|
|
}
|
|
return value;
|
|
};
|
|
export var getAnyElementOfObject = function getAnyElementOfObject(obj) {
|
|
if (!obj) {
|
|
return null;
|
|
}
|
|
var keys = Object.keys(obj);
|
|
if (keys && keys.length) {
|
|
return obj[keys[0]];
|
|
}
|
|
return null;
|
|
};
|
|
export var hasDuplicate = function hasDuplicate(ary) {
|
|
if (!Array.isArray(ary)) {
|
|
return false;
|
|
}
|
|
var len = ary.length;
|
|
var cache = {};
|
|
for (var i = 0; i < len; i++) {
|
|
if (!cache[ary[i]]) {
|
|
cache[ary[i]] = true;
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
};
|
|
|
|
/* @todo consider to rename this function into `getInterpolator` */
|
|
export var interpolateNumber = function interpolateNumber(numberA, numberB) {
|
|
if (isNumber(numberA) && isNumber(numberB)) {
|
|
return function (t) {
|
|
return numberA + t * (numberB - numberA);
|
|
};
|
|
}
|
|
return function () {
|
|
return numberB;
|
|
};
|
|
};
|
|
export function findEntryInArray(ary, specifiedKey, specifiedValue) {
|
|
if (!ary || !ary.length) {
|
|
return null;
|
|
}
|
|
return ary.find(function (entry) {
|
|
return entry && (typeof specifiedKey === 'function' ? specifiedKey(entry) : get(entry, specifiedKey)) === specifiedValue;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* The least square linear regression
|
|
* @param {Array} data The array of points
|
|
* @returns {Object} The domain of x, and the parameter of linear function
|
|
*/
|
|
export var getLinearRegression = function getLinearRegression(data) {
|
|
if (!data || !data.length) {
|
|
return null;
|
|
}
|
|
var len = data.length;
|
|
var xsum = 0;
|
|
var ysum = 0;
|
|
var xysum = 0;
|
|
var xxsum = 0;
|
|
var xmin = Infinity;
|
|
var xmax = -Infinity;
|
|
var xcurrent = 0;
|
|
var ycurrent = 0;
|
|
for (var i = 0; i < len; i++) {
|
|
xcurrent = data[i].cx || 0;
|
|
ycurrent = data[i].cy || 0;
|
|
xsum += xcurrent;
|
|
ysum += ycurrent;
|
|
xysum += xcurrent * ycurrent;
|
|
xxsum += xcurrent * xcurrent;
|
|
xmin = Math.min(xmin, xcurrent);
|
|
xmax = Math.max(xmax, xcurrent);
|
|
}
|
|
var a = len * xxsum !== xsum * xsum ? (len * xysum - xsum * ysum) / (len * xxsum - xsum * xsum) : 0;
|
|
return {
|
|
xmin: xmin,
|
|
xmax: xmax,
|
|
a: a,
|
|
b: (ysum - a * xsum) / len
|
|
};
|
|
}; |