@formant/ava
Version:
A framework for automated visual analytics.
97 lines (96 loc) • 4.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDisplayValue = exports.getFormattedNumberValue = exports.getAssessment = exports.getScopeVariableArray = exports.formattedAsArray = exports.templateStr2Structure = exports.getByPath = void 0;
var lodash_1 = require("lodash");
var utils_1 = require("../../utils");
/**
* simple path system
* if starts with '.', it's relative path
* // TODO 支持相对路径找到上级
* if starts with '..', it's can get prev level variable
* otherwise, it's absolute path
*/
function getByPath(globalVar, scopeVar, path) {
if (!path)
return scopeVar;
if ((0, lodash_1.startsWith)(path, '.'))
return (0, lodash_1.get)(scopeVar, path.slice(1));
return (0, lodash_1.get)(globalVar, path);
}
exports.getByPath = getByPath;
/**
* parse template string
* Use $ for variable
* Use & for template
* @example 'aaa ${key} &{temp1}'
* => [{ type: 'text', value: 'aaa ' }, { type: 'variable', value: 'key' }, { type: 'template', value: 'temp1' }]
*/
function templateStr2Structure(templateStr) {
// eslint-disable-next-line no-useless-escape
var splitReg = /([\$|&]{.*?})/;
var varReg = /\${(.*?)}/;
var tempReg = /&{(.*?)}/;
return templateStr
.split(splitReg)
.filter(function (str) { return str; })
.map(function (str) {
var _a, _b;
var templateId = (_a = tempReg.exec(str)) === null || _a === void 0 ? void 0 : _a[1];
if (templateId)
return { value: templateId, type: 'template' };
var varName = (_b = varReg.exec(str)) === null || _b === void 0 ? void 0 : _b[1];
if (varName)
return { value: varName, type: 'variable' };
return { value: str, type: 'text' };
});
}
exports.templateStr2Structure = templateStr2Structure;
/** format any to array, and filter nil */
function formattedAsArray(variable) {
if ((0, lodash_1.isUndefined)(variable))
return [];
return (0, lodash_1.isArray)(variable) ? variable : [variable];
}
exports.formattedAsArray = formattedAsArray;
function getScopeVariableArray(globalVar, scopeVar, path, limit) {
return (0, lodash_1.slice)(formattedAsArray(getByPath(globalVar, scopeVar, path)), 0, limit);
}
exports.getScopeVariableArray = getScopeVariableArray;
function getAssessment(entityType, value) {
if (!(0, utils_1.isNumberLike)(value))
return undefined;
if (entityType === 'delta_value' || entityType === 'ratio_value') {
if (value > 0)
return 'positive';
if (value < 0)
return 'negative';
}
return undefined;
}
exports.getAssessment = getAssessment;
function getFormattedNumberValue(varType, value, formatter) {
if (!(0, utils_1.isNumberLike)(value))
return value;
if (varType === 'delta_value' || varType === 'ratio_value') {
return formatter ? formatter(Math.abs(value)) : (0, utils_1.dataFormat)(Math.abs(value));
}
if (varType === 'proportion') {
return formatter ? formatter(Math.abs(value)) : "".concat((0, utils_1.dataFormat)(value * 100), "%");
}
return formatter ? formatter(value) : (0, utils_1.dataFormat)(value);
}
exports.getFormattedNumberValue = getFormattedNumberValue;
/** get phrase text */
function getDisplayValue(getDisplayValuePattern, globalVar, scopeVar) {
if ((0, lodash_1.isString)(getDisplayValuePattern)) {
// 用字符串表示使用变量拼接短语文字内容,这里进行变量替换,eg '${city} = ${value}' => '北京 = 1000'
// Use a string to represent the text content of the phrase using variables to splice,eg '${city} = ${value}' => 'Beijing = 1000'
return (0, lodash_1.replace)(getDisplayValuePattern, /\${(.*?)}/g, function (match) {
var _a;
var varName = (_a = /\${(.*?)}/.exec(match)) === null || _a === void 0 ? void 0 : _a[1];
return varName ? getByPath(globalVar, scopeVar, varName) : '';
});
}
return getDisplayValuePattern(globalVar, scopeVar);
}
exports.getDisplayValue = getDisplayValue;