/
get.js
100 lines (91 loc) · 2.52 KB
/
get.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import Vue from 'vue';
import { DEFAULT_COMPONENT_VALUE } from '../constants';
import { isSimilarNum, isStr, isArr, isFunc } from './is';
export function getEmptyArray() {
return [];
}
export function getEmptyObject() {
return {};
}
export function getValidator(arr = []) {
return function(val) {
return arr.includes(val);
};
}
export function getSizePx(size) {
const sizeIsNum = isSimilarNum(size);
if (sizeIsNum || isStr(size)) {
return sizeIsNum ? `${size}px` : size;
}
return size;
}
export function getCamelCase(str) {
const splitArr = str.split('-');
if (str.length <= 1) {
return str;
}
const pascalCaseArr = [];
const camelCaseArr = splitArr.map((splitItem, index) => {
const result = splitItem.slice(0, 1).toUpperCase() + splitItem.slice(1);
pascalCaseArr.push(index === 0 ? splitItem : result);
return result;
});
return {
camelCase: camelCaseArr.join(''),
pascalCase: pascalCaseArr.join(''),
};
}
export function getParamCase(str) {
return str.replace(/([A-Z])/g, (all, letter, index) => {
if (index === 0) {
return letter;
}
return `-${letter}`;
}).toLowerCase();
}
function getPropDefaultValue(prop) {
if (isFunc(prop)) {
return prop();
}
if (isArr(prop)) {
return prop?.[0]?.();
}
if (typeof prop.default !== 'undefined') {
return isFunc(prop.default) ? prop.default() : prop.default;
}
if (prop?.type) {
return getPropDefaultValue(prop.type);
}
return DEFAULT_COMPONENT_VALUE;
}
export function getDefaultValueByComponentType(component) {
if (isStr(component)) {
const components = Vue?.options?.components;
if (components[component]) {
return getDefaultValueByComponentType(components[component]?.options);
}
// 需要进行组件名转换
if (component.indexOf('-') !== -1) {
const { camelCase, pascalCase } = getCamelCase(component);
if (components[camelCase]) {
return getDefaultValueByComponentType(components[camelCase]?.options);
}
if (components[pascalCase]) {
return getDefaultValueByComponentType(components[pascalCase]?.options);
}
} else {
const paramCase = getParamCase(component);
if (components[paramCase]) {
return getDefaultValueByComponentType(components[paramCase]?.options);
}
}
} else {
const props = component?.props;
if (isArr(props)) {
return DEFAULT_COMPONENT_VALUE;
}
const value = props?.value;
return getPropDefaultValue(value);
}
return DEFAULT_COMPONENT_VALUE;
}