forked from opentiny/tiny-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
179 lines (151 loc) · 4.66 KB
/
Copy pathindex.js
File metadata and controls
179 lines (151 loc) · 4.66 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
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
177
178
179
/**
* 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 fun_ctor = Function
/**
* 解析表达式字符串
* @param {string} rawCode 表达式字符串
* @param {object} context 调用的上下文
* @returns any
*/
export function parseExpression(rawCode, context = {}) {
try {
return fun_ctor(`return (${rawCode})`).call(context)
} catch (error) {
// eslint-disable-next-line no-console
console.error(`parseExpression error: ${error}`)
return undefined
}
}
/**
* 解析函数字符串成函数
* @param {string} rawCode 字符串函数
* @param {object} context 需要绑定的函数上下文
* @returns Function
*/
export function parseFunction(rawCode, context = {}) {
try {
return fun_ctor(`return (${rawCode})`).call(context).bind(context)
} catch (error) {
// eslint-disable-next-line no-console
console.error(`parseFunction error: ${JSON.Stringify(error)}`)
return null
}
}
/**
* 将字符串包含的特殊正则字符逃逸出来 (加上 \\)
* 适用于 new Regexp(`${str}`)的情形,防止变量 str 具有一些特殊的正则字符串导致挂掉或者不符合期望
* @param {string} value 字符串
* @returns escape 之后的字符串
*/
export const escapeRegExp = (value) => {
const reg = /[\\^$.*+?()[\]{}|]/g
const str = String(value)
return str.replace(reg, '\\$&')
}
// prefer old unicode hacks for backward compatibility
// https://base64.guru/developers/javascript/examples/unicode-strings
export const utoa = (string) => btoa(unescape(encodeURIComponent(string)))
export const atou = (base64) => decodeURIComponent(escape(atob(base64)))
/**
* 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)
})
export const hyphenateRE = /\B([A-Z])/g
export const hyphenate = cached((str) => {
return str.replace(hyphenateRE, '-$1').toLowerCase()
})
/**
* get random id
*/
export const guid = () => {
return 'xxxxxxxx'.replace(/[x]/g, (c) => {
const random = parseFloat('0.' + crypto.getRandomValues(new Uint32Array(1))[0])
const r = (random * 16) | 0
const v = c === 'x' ? r : (r & 0x3) | 0x8
return v.toString(16)
})
}
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 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
}
export function generateRandomLetters(length = 1) {
let result = ''
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
for (let i = 0; i < length; i++) {
const random = parseFloat('0.' + crypto.getRandomValues(new Uint32Array(1))[0])
result += chars.charAt(Math.floor(random * chars.length))
}
return result
}