xy-tool-ui
Version:
XiaoYi tool compenent library
51 lines (48 loc) • 1.63 kB
JavaScript
let setting = 'setting';
let that = null;
export function Config(thi, configKey) {
that = thi;
if (configKey) {
setting = configKey;
}
}
/**
* 如果obj为字符串 则实际为值,如果为obj 则通过key转换值
* createKey 修改、创建对象key
* obj 对应值 或 对象[key] 的值
* prams 数据数组
* [0] boolean 判断、regex 正则表达式
* [1] boolean 值、function 函数
* defaultVal String 默认匹配值 、function 执行方法
*/
Config.prototype.set = function (createKey, obj, prams, defaultVal) {
let val = typeof obj == 'string' ? obj : obj[createKey];
if (createKey && val) {
if (prams && typeof prams == 'object' && prams.length > 0) {
for (let i = 0; i < prams.length; i++) {
let pram = prams[i];
let isPram = typeof pram[0];
if (
(isPram == 'boolean' && pram[0]) ||
(isPram == 'object' && pram[0].test(val.toLowerCase()))
) {
if (typeof pram[1] == 'function') {
pram[1]();
} else {
that[setting][createKey] = pram[1];
}
}
}
} else {
// 使用obj 对象中的值
that[setting][createKey] = val;
}
} else {
// 使用默认的值
if (typeof defaultVal == 'function') {
defaultVal();
} else {
that[setting][createKey] = defaultVal;
}
}
};