Vue 3 核心库常用工具函数解析
'use strict';
const _export_sfc = (sfc, props) => {
const target = sfc.__vccOpts || sfc;
for (const [key, val] of props) {
target[key] = val;
}
return target;
};
function makeMap(str, expectsLowerCase) {
const map = /* @PURE / Object.create(null);
const list = str.split(',');
for (let i = 0; i < list.length; i++) {
map[list[i]] = true;
}
return expectsLowerCase ? (val) => !!map[val.toLowerCase()] : (val) => !!map[val];
}
function normalizeStyle(value) {
if (isArray(value)) {
const res = {};
for (let i = 0; i < value.length; i++) {
const item = value[i];
const normalized = isString(item) ? parseStringStyle(item) : normalizeStyle(item);
if (normalized) {
for (const key in normalized) {
res[key] = normalized[key];
}
}
}
return res;
} else if (isString(value)) {
return value;
} else if (isObject(value)) {
return value;
}
}
const listDelimiterRE = /;(?![^(]))/g;
const propertyDelimiterRE = /:([^]+)/;
const styleCommentRE = /*.*?*//gs;
function parseStringStyle(cssText) {
const ret = {};
cssText.replace(styleCommentRE, '').split(listDelimiterRE).forEach((item) => {
if (item) {
const tmp = item.split(propertyDelimiterRE);
tmp.length > 1 && (ret[tmp[0].trim()] = tmp[1].trim());
}
});
return ret;
}
function normalizeClass(value) {
let res = '';
if (isString(value)) {
res = value;
} else if (isArray(value)) {
for (let i = 0; i < value.length; i++) {
const normalized = normalizeClass(value[i]);
if (normalized) {
res += normalized + ' ';
}
}
} else if (isObject(value)) {
for (const name in value) {
if (value[name]) {
res += name + ' ';
}
}
}
return res.trim();
}
const toDisplayString = (val) => {
return isString(val) ? val : val == null ? '' : isArray(val) || isObject(val) && (val.toString === objectToString || !isFunction(val.toString)) ? JSON.stringify(val, replacer, 2) : String(val);
};
const replacer = (_key, val) => {
if (val && val.__v_isRef) {
return replacer(_key, val.value);
} else if (isMap(val)) {
return {
[Map(${val.size})]: [...val.entries()].reduce((entries, [key, val2]) => {
entries[${key} =>] = val2;
return entries;
}, {})
};
} else if (isSet(val)) {
return {
[Set(${val.size})]: [...val.values()]
};
} else if (isObject(val) && !isArray(val) && !isPlainObject(val)) {
return String(val);
}
return val;
};
const EMPTY_OBJ = Object.freeze({});
const EMPTY_ARR = Object.freeze([]);
const NOOP = () => {};
const NO = () => false;
const onRE = /^on[^a-z]/;
const isOn = (key) => onRE.test(key);
const isModelListener = (key) => key.startsWith('onUpdate:');
const extend = Object.assign;
const remove = (arr, el) => {
const i = arr.indexOf(el);
if (i > -1) {
arr.splice(i, 1);
}
};解释内容:此代码段是一些常用的工具函数和常量定义。其中包括:
- _export_sfc:用于导出单文件组件对象,并将一些属性合并到对象中。
- makeMap:用于创建一个包含传入字符串列表的映射表。
- normalizeStyle:用于将样式对象或样式字符串规范化为对象形式。
- parseStringStyle:用于将样式字符串解析为对象形式。
- normalizeClass:用于将类名规范化为字符串形式。
- toDisplayString:用于将值转换为可展示的字符串形式。
- EMPTY_OBJ:空对象常量。
- EMPTY_ARR:空数组常量。
- NOOP:空函数常量。
- NO:返回 false 的函数常量。
- isOn:检查属性名是否以 “on” 开头。
- isModelListener:检查属性名是否以 “onUpdate:” 开头。
- extend:用于将多个对象合并成一个对象。
- remove:从数组中移除指定元素。
原文地址: https://www.cveoy.top/t/topic/lRRV 著作权归作者所有。请勿转载和采集!