fix: use util functions in vutils

This commit is contained in:
xile611 2024-08-01 16:52:51 +08:00
parent 52c1d57f88
commit f10b5b287b
4 changed files with 10 additions and 160 deletions

View File

@ -1,8 +1,9 @@
import type { DataView } from '@visactor/vdataset';
import type { Datum } from '../../typings';
import { couldBeValidNumber } from '../../util/type';
import { computeQuadrant, getPercentValue } from '../../util/math';
import { getPercentValue } from '../../util/math';
import { ARC_TRANSFORM_VALUE } from '../../constant/polar';
import { computeQuadrant } from '@visactor/vutils';
export interface IPieOpt {
angleField: string;

View File

@ -18,24 +18,6 @@ import { angleLabelOrientAttribute } from '@visactor/vrender-components';
export const isClose = isNumberClose;
export { isGreater, isLess, normalizeAngle, angleLabelOrientAttribute };
/**
*
*
* @param angle
* @returns
*/
export function computeQuadrant(angle: number): Quadrant {
angle = normalizeAngle(angle);
if (angle > 0 && angle <= Math.PI / 2) {
return 2;
} else if (angle > Math.PI / 2 && angle <= Math.PI) {
return 3;
} else if (angle > Math.PI && angle <= (3 * Math.PI) / 2) {
return 4;
}
return 1;
}
/**
* &
* @param start

View File

@ -1,61 +1,14 @@
import { isArray, isBoolean, isDate, isNumber, isString, isValid, isHTMLElement } from '@visactor/vutils';
import { isHTMLElement, cloneDeep } from '@visactor/vutils';
import { isDataView } from '@visactor/vdataset';
const ignoreWhen = (value: any) => {
return isDataView(value) || isHTMLElement(value);
};
/**
* specDataView
* @param spec spec
*/
export function cloneDeepSpec(spec: any, excludeKeys: string[] = ['data']) {
const value = spec;
let result;
if (!isValid(value) || typeof value !== 'object') {
return value;
}
// 判断是不是不能深拷贝的对象
if (isDataView(value) || isHTMLElement(value)) {
return value;
}
const isArr = isArray(value);
const length = value.length;
// 不考虑特殊数组的额外处理
if (isArr) {
result = new Array(length);
}
// 不考虑 buffer / arguments 类型的处理以及 prototype 的额外处理
else if (typeof value === 'object') {
result = {};
}
// 不建议使用作为 Boolean / Number / String 作为构造器
else if (isBoolean(value) || isNumber(value) || isString(value)) {
result = value;
} else if (isDate(value)) {
result = new Date(+value);
}
// 不考虑 ArrayBuffer / DataView / TypedArray / map / set / regexp / symbol 类型
else {
result = undefined;
}
// 不考虑 map / set / TypedArray 类型的赋值
// 不考虑对象的 symbol 属性
const props = isArr ? undefined : Object.keys(Object(value));
let index = -1;
if (result) {
while (++index < (props || value).length) {
const key = props ? props[index] : index;
const subValue = value[key];
if (excludeKeys?.includes(key.toString())) {
result[key] = subValue;
} else {
result[key] = cloneDeepSpec(subValue, excludeKeys);
}
}
}
return result;
return cloneDeep(spec, ignoreWhen, excludeKeys);
}

View File

@ -1,90 +1,4 @@
import { isArray, isArrayLike, isObject, isPlainObject, isValid } from '@visactor/vutils';
function baseMerge(target: any, source: any, shallowArray = false) {
if (source) {
if (target === source) {
return;
}
if (isValid(source) && typeof source === 'object') {
// baseFor
const iterable = Object(source);
const props = [];
// keysIn
for (const key in iterable) {
props.push(key);
}
let { length } = props;
let propIndex = -1;
while (length--) {
const key = props[++propIndex];
if (
isValid(iterable[key]) &&
typeof iterable[key] === 'object' &&
!isArray(target[key]) // VChart 特有逻辑
) {
baseMergeDeep(target, source, key, shallowArray);
} else {
assignMergeValue(target, key, iterable[key]);
}
}
}
}
}
// 由于目前 VChart 内部对 spec 会先执行一次深拷贝merge 暂时不考虑 source 中有环的问题
function baseMergeDeep(target: object, source: object, key: string, shallowArray = false) {
const objValue = target[key];
const srcValue = source[key];
let newValue = source[key];
let isCommon = true;
// 不考虑 buffer / typedArray 类型
if (isArray(srcValue)) {
if (shallowArray) {
// 依据参数对数组做浅拷贝
newValue = [];
} else if (isArray(objValue)) {
newValue = objValue;
} else if (isArrayLike(objValue)) {
// 如果 source 为数组,则 target 的 arrayLike 对象也视作为数组处理
newValue = new Array(objValue.length);
let index = -1;
const length = objValue.length;
while (++index < length) {
newValue[index] = objValue[index];
}
}
}
// else if (isArray(srcValue) && shallowArray) {
// newValue = [];
// }
// 不考虑 argument 类型
else if (isPlainObject(srcValue)) {
newValue = objValue ?? {};
// 不考虑 prototype 的额外处理
if (typeof objValue === 'function' || typeof objValue !== 'object') {
newValue = {};
}
} else {
isCommon = false;
}
// 对 class 等复杂对象或者浅拷贝的 array 不做拷贝处理
if (isCommon) {
baseMerge(newValue, srcValue, shallowArray);
}
assignMergeValue(target, key, newValue);
}
function assignMergeValue(target: object, key: string, value: any) {
if ((value !== undefined && !eq(target[key], value)) || (value === undefined && !(key in target))) {
// 不考虑 __proto__ 的赋值处理
target[key] = value;
}
}
function eq(value: any, other: any) {
return value === other || (Number.isNaN(value) && Number.isNaN(other));
}
import { baseMerge, isArray, isObject } from '@visactor/vutils';
/* lodash merge source
* padding padding merge
*/
@ -93,7 +7,7 @@ export function mergeSpec(target: any, ...sources: any[]): any {
const length = sources.length;
while (++sourceIndex < length) {
const source = sources[sourceIndex];
baseMerge(target, source, true);
baseMerge(target, source, true, true);
}
return target;
}