forked from opentiny/tiny-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
133 lines (111 loc) · 3.01 KB
/
Copy pathutils.js
File metadata and controls
133 lines (111 loc) · 3.01 KB
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
/**
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
import { isObject, isArray } from '@opentiny/vue-renderless/grid/static'
export const MATERIAL_TYPE = {
Component: 'component',
Block: 'block'
}
export const NODE_INSERT_TYPE = {
Inside: 'inside',
After: 'after',
Before: 'before',
Replace: 'replace'
}
export const PROP_DATA_TYPE = {
I18N: 'i18n',
VARIABLE: 'variable',
JSEXPRESSION: 'JSExpression',
JSRESOURCE: 'JSResource',
JSSLOT: 'JSSlot'
}
/**
* Create a cached version of a pure function.
*/
function cached(fn) {
const cache = Object.create(null)
return function cachedFn(str) {
if (!cache[str]) {
cache[str] = fn(str)
}
return cache[str]
}
}
/**
* Camelize a hyphen-delimited string.
*/
const camelizeRE = /-(\w)/g
export const camelize = cached((str) => {
return str.replace(camelizeRE, (_, c) => {
return c ? c.toUpperCase() : ''
})
})
/**
* Capitalize a string.
*/
export const capitalize = cached((str) => {
return str.charAt(0).toUpperCase() + str.slice(1)
})
const hyphenateRE = /\B([A-Z])/g
export const hyphenate = cached((str) => {
return str.replace(hyphenateRE, '-$1').toLowerCase()
})
export const getEnumData = (item) => {
if (item.enum && item.enumNames) {
return item.enum.map((value, index) => ({ value, text: item.enumNames[index] }))
}
return undefined
}
export const mapTree = (obj = {}, handler, childName = 'children') => {
const children = obj[childName]
const node = handler(obj)
if (Array.isArray(children)) {
node[childName] = children.map((child) => mapTree(child, handler))
}
return node
}
export const mapObj = (source, handler, rootKey) => {
const caller = (obj, key) => {
const { item, deep } = handler(obj, key)
return deep ? mapObj(item, handler, key) : item
}
if (isArray(source)) {
return source.map((obj) => caller(obj, rootKey))
}
if (source && isObject(source)) {
return Object.keys(source).reduce((output, key) => {
output[key] = caller(source[key], rootKey || key)
return output
}, {})
}
return source
}
export const generateFunction = (body, context) => {
const Func = Function
try {
return new Func(`return ${body}`).call(context).bind(context)
} catch (error) {
// do nothing
}
return undefined
}
export const getDefaultProps = (properties = []) => {
const props = {}
properties.forEach(({ content = [] }) => {
content.forEach(({ defaultValue, schema, property }) => {
const value = Array.isArray(schema) ? getDefaultProps(schema) : defaultValue
if (value) {
props[property] = value
}
})
})
return props
}