-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
176 lines (148 loc) · 3.6 KB
/
utils.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const FALLBACK = 0;
const OVERWRITE = 1;
const MERGE = 2;
const MODIFIERS = {
'!': OVERWRITE,
'&': MERGE,
};
const hyphenateRE = /\B([A-Z])/g;
const camelizeRE = /-(\w)/g;
function hyphenate(string) {
return string.replace(hyphenateRE, '-$1').toLowerCase();
}
function camelize(string) {
return string.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '');
}
export function isEmpty(object) {
// eslint-disable-next-line no-unreachable-loop
for (const key in object) {
return false;
}
return true;
}
export function set(dest, key, valueData) {
if (!valueData) {
return;
}
const {value, modifier} = valueData;
const destValue = dest[key];
if (
destValue === undefined ||
destValue === null ||
modifier === OVERWRITE
) {
dest[key] = value;
} else if (modifier === MERGE) {
if (Array.isArray(destValue)) {
if (Array.isArray(value)) {
destValue.push(...value);
} else {
destValue.push(value);
}
} else if (typeof destValue === 'object' && typeof value === 'object') {
Object.assign(destValue, value);
} else if (typeof destValue === 'function' && typeof value === 'function') {
dest[key] = function () {
Reflect.apply(destValue, this, arguments);
Reflect.apply(value, this, arguments);
};
} else {
dest[key] += value;
}
}
}
export function merge(destObject, property, src) {
if (!destObject[property]) {
destObject[property] = {};
}
const dest = destObject[property];
for (const key in src) {
set(dest, key, src[key]);
}
}
function findProp(attrs, prop) {
if (prop in attrs) {
return prop;
}
const kebab = hyphenate(prop);
if (kebab in attrs) {
return kebab;
}
return false;
}
export function getPropsData(componentOptions, attrs) {
const {props} = componentOptions.Ctor.options;
const propsData = {};
if (props) {
// Iterate over props to find attrs that are props
// By iterating over props instead of attrs, we can leverage Vue's camelization
for (const prop in props) {
const isProp = findProp(attrs, prop);
if (isProp) {
propsData[prop] = attrs[isProp];
delete attrs[isProp];
}
}
}
return propsData;
}
export function parseModifiers(object) {
const normalized = {};
for (let key in object) {
const value = object[key];
let modifier = MODIFIERS[key.slice(-1)];
if (modifier) {
key = key.slice(0, -1);
} else {
modifier = FALLBACK;
}
normalized[key] = {value, modifier};
}
return normalized;
}
export function getStaticPair(object, name) {
const staticProp = camelize('static-' + name);
const pair = [object[staticProp], object[name]].filter(Boolean).flat(Infinity);
if (pair.length === 0) {
return undefined;
}
delete object[staticProp];
return pair;
}
export function getAndRemoveAttr(attrs, attrName) {
const value = attrs[attrName];
if (value) {
delete attrs[attrName];
return value;
}
}
export function createFallback(value) {
return value && {
value,
modifier: FALLBACK,
};
}
export function parseStyles(styleString) {
return Object.fromEntries(
styleString
.split(';')
.map(pair => {
const [property, value] = pair.split(':');
return (property && value) && [camelize(property.trim()), value.trim()];
})
.filter(Boolean),
);
}
let VNode;
export function cloneVNode(vnode) {
if (!VNode) {
VNode = Object.getPrototypeOf(vnode).constructor;
}
const clonedVNode = new VNode();
Object.assign(clonedVNode, vnode);
if (clonedVNode.componentOptions) {
clonedVNode.componentOptions = Object.assign({}, clonedVNode.componentOptions);
clonedVNode.componentOptions.propsData = Object.assign({}, clonedVNode.componentOptions.propsData);
}
return clonedVNode;
}