@vef-framework/core 1.0.62 → 1.0.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/api/api-client.js +543 -2
- package/es/api/api-context.js +94 -2
- package/es/api/index.js +4 -2
- package/es/api/query-client.js +30 -2
- package/es/api/request-client.js +243 -2
- package/es/auth/auth-context.js +35 -2
- package/es/auth/index.js +2 -2
- package/es/expr/compiler.js +299 -2
- package/es/expr/helpers.js +86 -2
- package/es/index.js +8 -2
- package/es/middleware/dispatcher.js +28 -2
- package/lib/api/api-client.cjs +548 -2
- package/lib/api/api-context.cjs +99 -2
- package/lib/api/index.cjs +19 -2
- package/lib/api/query-client.cjs +34 -2
- package/lib/api/request-client.cjs +248 -2
- package/lib/auth/auth-context.cjs +40 -2
- package/lib/auth/index.cjs +11 -2
- package/lib/expr/compiler.cjs +303 -2
- package/lib/expr/helpers.cjs +94 -2
- package/lib/index.cjs +26 -2
- package/lib/middleware/dispatcher.cjs +32 -2
- package/package.json +2 -2
package/es/api/request-client.js
CHANGED
|
@@ -1,3 +1,244 @@
|
|
|
1
|
-
/*! VefFramework version: 1.0.
|
|
2
|
-
import{isNullish
|
|
1
|
+
/*! VefFramework version: 1.0.64, build time: 2025-01-10T01:26:01.352Z, made by Venus. */
|
|
2
|
+
import { isNullish, showInfoMessage, showWarningMessage, VefError, isString, showErrorMessage, isFunction } from '@vef-framework/shared';
|
|
3
|
+
import axios, { CanceledError } from 'axios';
|
|
4
|
+
import qs from 'qs';
|
|
5
|
+
|
|
6
|
+
var __typeError = (msg) => {
|
|
7
|
+
throw TypeError(msg);
|
|
8
|
+
};
|
|
9
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
10
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
11
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
12
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
13
|
+
var _axiosInstance;
|
|
14
|
+
const pathParamRegex = /\{\w+\}/;
|
|
15
|
+
class RequestClient {
|
|
16
|
+
constructor(options) {
|
|
17
|
+
this.options = options;
|
|
18
|
+
/**
|
|
19
|
+
* The axios instance.
|
|
20
|
+
*/
|
|
21
|
+
__privateAdd(this, _axiosInstance);
|
|
22
|
+
const {
|
|
23
|
+
baseUrl,
|
|
24
|
+
timeout,
|
|
25
|
+
getAccessToken,
|
|
26
|
+
successCode = 0,
|
|
27
|
+
unauthorizedCode = 1e3,
|
|
28
|
+
accessDeniedCode = 1002,
|
|
29
|
+
tokenExpiredCode = 1001,
|
|
30
|
+
onUnauthorized,
|
|
31
|
+
onAccessDenied
|
|
32
|
+
} = options;
|
|
33
|
+
__privateSet(this, _axiosInstance, axios.create({
|
|
34
|
+
baseURL: baseUrl,
|
|
35
|
+
timeout: timeout ?? 1e3 * 30,
|
|
36
|
+
headers: {
|
|
37
|
+
"Content-Type": "application/json"
|
|
38
|
+
},
|
|
39
|
+
paramsSerializer: (params) => qs.stringify(
|
|
40
|
+
params,
|
|
41
|
+
{
|
|
42
|
+
arrayFormat: "repeat",
|
|
43
|
+
skipNulls: true,
|
|
44
|
+
charset: "utf-8"
|
|
45
|
+
}
|
|
46
|
+
),
|
|
47
|
+
responseType: "json",
|
|
48
|
+
validateStatus: () => true
|
|
49
|
+
}));
|
|
50
|
+
__privateGet(this, _axiosInstance).interceptors.request.use((config) => {
|
|
51
|
+
const token = getAccessToken?.();
|
|
52
|
+
if (token) {
|
|
53
|
+
config.headers.Authorization = `Bearer ${token}`;
|
|
54
|
+
}
|
|
55
|
+
const { url } = config;
|
|
56
|
+
if (url && pathParamRegex.test(url)) {
|
|
57
|
+
config.url = url.replace(pathParamRegex, (_, key) => {
|
|
58
|
+
const value = config.params?.[key];
|
|
59
|
+
if (isNullish(value)) {
|
|
60
|
+
throw new Error(`Path parameter ${key} is nil`);
|
|
61
|
+
}
|
|
62
|
+
return value;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return config;
|
|
66
|
+
});
|
|
67
|
+
__privateGet(this, _axiosInstance).interceptors.response.use(
|
|
68
|
+
async (response) => {
|
|
69
|
+
const { code, message } = response.data;
|
|
70
|
+
if (code === successCode) {
|
|
71
|
+
return response;
|
|
72
|
+
} else if (code === unauthorizedCode) {
|
|
73
|
+
await onUnauthorized?.();
|
|
74
|
+
} else if (code === tokenExpiredCode) {
|
|
75
|
+
showInfoMessage("\u767B\u5F55\u5DF2\u5931\u6548");
|
|
76
|
+
await onUnauthorized?.();
|
|
77
|
+
} else if (code === accessDeniedCode) {
|
|
78
|
+
showWarningMessage(`${message}\uFF0C\u8BF7\u8054\u7CFB\u7BA1\u7406\u5458\u4E3A\u60A8\u5F00\u901A`);
|
|
79
|
+
await onAccessDenied?.();
|
|
80
|
+
} else {
|
|
81
|
+
showWarningMessage(message);
|
|
82
|
+
}
|
|
83
|
+
throw new VefError(code, message);
|
|
84
|
+
},
|
|
85
|
+
(error) => {
|
|
86
|
+
if (error instanceof CanceledError) {
|
|
87
|
+
return Promise.reject(error);
|
|
88
|
+
}
|
|
89
|
+
const { response, message } = error;
|
|
90
|
+
if (response?.data) {
|
|
91
|
+
const errorMessage = `\u5904\u7406\u8BF7\u6C42\u54CD\u5E94\u4FE1\u606F\u5F02\u5E38\uFF1A${message ?? "\u65E0"}`;
|
|
92
|
+
return Promise.reject(new VefError(response.status, errorMessage));
|
|
93
|
+
} else {
|
|
94
|
+
let errorMessage = message ?? (isString(error) ? error : "");
|
|
95
|
+
if (errorMessage === "Network Error") {
|
|
96
|
+
errorMessage = "\u63A5\u53E3\u8FDE\u63A5\u5F02\u5E38\u53EF\u80FD\u539F\u56E0\uFF1A\u7F51\u7EDC\u4E0D\u901A\u6216\u5B58\u5728\u8DE8\u57DF\u95EE\u9898\u3002";
|
|
97
|
+
} else if (errorMessage.includes("timeout")) {
|
|
98
|
+
errorMessage = "\u63A5\u53E3\u8BF7\u6C42\u8D85\u65F6";
|
|
99
|
+
} else if (errorMessage.includes("Request failed with status code")) {
|
|
100
|
+
const code = message.substring(message.length - 3);
|
|
101
|
+
errorMessage = `\u63A5\u53E3\u72B6\u6001\u7801\u5F02\u5E38\uFF1A${code}`;
|
|
102
|
+
} else {
|
|
103
|
+
errorMessage = "\u63A5\u53E3\u672A\u77E5\u5F02\u5E38";
|
|
104
|
+
}
|
|
105
|
+
showErrorMessage(errorMessage);
|
|
106
|
+
return Promise.reject(new VefError(-1, errorMessage));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Perform a GET request.
|
|
113
|
+
*
|
|
114
|
+
* @param url - The url.
|
|
115
|
+
* @param options - The request options.
|
|
116
|
+
* @returns The response data promise.
|
|
117
|
+
*/
|
|
118
|
+
async get(url, options) {
|
|
119
|
+
const response = await __privateGet(this, _axiosInstance).get(url, {
|
|
120
|
+
params: options?.params,
|
|
121
|
+
signal: options?.signal
|
|
122
|
+
});
|
|
123
|
+
return response.data;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Perform a POST request.
|
|
127
|
+
*
|
|
128
|
+
* @param url - The url.
|
|
129
|
+
* @param data - The request data.
|
|
130
|
+
* @param options - The request options.
|
|
131
|
+
* @returns The response data promise.
|
|
132
|
+
*/
|
|
133
|
+
async post(url, data, options) {
|
|
134
|
+
const response = await __privateGet(this, _axiosInstance).post(url, data, {
|
|
135
|
+
params: options?.params,
|
|
136
|
+
signal: options?.signal
|
|
137
|
+
});
|
|
138
|
+
return response.data;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Perform a PUT request.
|
|
142
|
+
*
|
|
143
|
+
* @param url - The url.
|
|
144
|
+
* @param data - The request data.
|
|
145
|
+
* @param options - The request options.
|
|
146
|
+
* @returns The response data promise.
|
|
147
|
+
*/
|
|
148
|
+
async put(url, data, options) {
|
|
149
|
+
const response = await __privateGet(this, _axiosInstance).put(url, data, {
|
|
150
|
+
params: options?.params,
|
|
151
|
+
signal: options?.signal
|
|
152
|
+
});
|
|
153
|
+
return response.data;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Perform a DELETE request.
|
|
157
|
+
*
|
|
158
|
+
* @param url - The url.
|
|
159
|
+
* @param options - The request options.
|
|
160
|
+
* @returns The response data promise.
|
|
161
|
+
*/
|
|
162
|
+
async delete(url, options) {
|
|
163
|
+
const response = await __privateGet(this, _axiosInstance).delete(url, {
|
|
164
|
+
params: options?.params,
|
|
165
|
+
signal: options?.signal
|
|
166
|
+
});
|
|
167
|
+
return response.data;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Perform a file upload request.
|
|
171
|
+
*
|
|
172
|
+
* @param url - The url.
|
|
173
|
+
* @param data - The file data.
|
|
174
|
+
* @param options - The request options.
|
|
175
|
+
* @returns The response data promise.
|
|
176
|
+
*/
|
|
177
|
+
async upload(url, data, options) {
|
|
178
|
+
const response = await __privateGet(this, _axiosInstance).postForm(
|
|
179
|
+
url,
|
|
180
|
+
data,
|
|
181
|
+
{
|
|
182
|
+
params: options?.params,
|
|
183
|
+
signal: options?.signal,
|
|
184
|
+
onUploadProgress: isFunction(options?.onProgress) ? (progressEvent) => {
|
|
185
|
+
const total = progressEvent.lengthComputable ? progressEvent.total ?? 0 : 0;
|
|
186
|
+
const { loaded } = progressEvent;
|
|
187
|
+
const percent = Math.round(loaded / total * 100);
|
|
188
|
+
options.onProgress?.(percent);
|
|
189
|
+
} : void 0
|
|
190
|
+
}
|
|
191
|
+
);
|
|
192
|
+
return response.data;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Perform a file download request.
|
|
196
|
+
*
|
|
197
|
+
* @param url - The url.
|
|
198
|
+
* @param options - The request options.
|
|
199
|
+
*/
|
|
200
|
+
async download(url, options) {
|
|
201
|
+
const { data, headers } = await __privateGet(this, _axiosInstance).get(
|
|
202
|
+
url,
|
|
203
|
+
{
|
|
204
|
+
params: options?.params,
|
|
205
|
+
responseType: "blob",
|
|
206
|
+
onDownloadProgress: isFunction(options?.onProgress) ? (progressEvent) => {
|
|
207
|
+
const total = progressEvent.lengthComputable ? progressEvent.total ?? 0 : 0;
|
|
208
|
+
const { loaded } = progressEvent;
|
|
209
|
+
const percent = Math.round(loaded / total * 100);
|
|
210
|
+
options.onProgress?.(percent);
|
|
211
|
+
} : void 0
|
|
212
|
+
}
|
|
213
|
+
);
|
|
214
|
+
let response = null;
|
|
215
|
+
try {
|
|
216
|
+
response = JSON.parse(await data.text());
|
|
217
|
+
} catch {
|
|
218
|
+
}
|
|
219
|
+
if (response && response.code !== 0) {
|
|
220
|
+
throw new VefError(response.code, response.message);
|
|
221
|
+
}
|
|
222
|
+
const contentDisposition = headers.get("content-disposition");
|
|
223
|
+
if (isString(contentDisposition)) {
|
|
224
|
+
const fileNameRegex = /filename[^;=\n]*=(?<name>(?<quote>['"]).*?\2|[^;\n]*)/;
|
|
225
|
+
const matches = fileNameRegex.exec(contentDisposition);
|
|
226
|
+
if (matches && matches.groups) {
|
|
227
|
+
const { name } = matches.groups;
|
|
228
|
+
const fileName = decodeURIComponent(name);
|
|
229
|
+
const a = document.createElement("a");
|
|
230
|
+
a.href = URL.createObjectURL(data);
|
|
231
|
+
a.download = isFunction(options?.fileName) ? options.fileName(fileName) : options?.fileName ?? fileName;
|
|
232
|
+
a.click();
|
|
233
|
+
URL.revokeObjectURL(a.href);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
_axiosInstance = new WeakMap();
|
|
239
|
+
function createRequestClient(options) {
|
|
240
|
+
return Object.freeze(new RequestClient(options));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export { RequestClient, createRequestClient };
|
|
3
244
|
/*! VefFramework is a blazingly high-level, modern, flexible, easy-to-use UI framework made by Venus. Follow me on Github: https://github.com/ilxqx! @ilxqx */
|
package/es/auth/auth-context.js
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
|
-
/*! VefFramework version: 1.0.
|
|
2
|
-
import{isString
|
|
1
|
+
/*! VefFramework version: 1.0.64, build time: 2025-01-10T01:26:01.352Z, made by Venus. */
|
|
2
|
+
import { isString, VefError } from '@vef-framework/shared';
|
|
3
|
+
import { createContext, useMemo, createElement, useContext } from 'react';
|
|
4
|
+
|
|
5
|
+
const Context = createContext(null);
|
|
6
|
+
const AuthContextProvider = ({
|
|
7
|
+
permissionChecker,
|
|
8
|
+
children
|
|
9
|
+
}) => {
|
|
10
|
+
const authContext = useMemo(() => ({
|
|
11
|
+
checkPermission: (token) => {
|
|
12
|
+
if (!permissionChecker) {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
if (isString(token)) {
|
|
16
|
+
return permissionChecker(token);
|
|
17
|
+
}
|
|
18
|
+
return permissionChecker(token.key);
|
|
19
|
+
}
|
|
20
|
+
}), [permissionChecker]);
|
|
21
|
+
return createElement(
|
|
22
|
+
Context.Provider,
|
|
23
|
+
{ value: authContext },
|
|
24
|
+
children
|
|
25
|
+
);
|
|
26
|
+
};
|
|
27
|
+
function useAuthContext() {
|
|
28
|
+
const context = useContext(Context);
|
|
29
|
+
if (context === null) {
|
|
30
|
+
throw new VefError(-1, "useAuthContext must be used within a AuthContextProvider");
|
|
31
|
+
}
|
|
32
|
+
return context;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { AuthContextProvider, useAuthContext };
|
|
3
36
|
/*! VefFramework is a blazingly high-level, modern, flexible, easy-to-use UI framework made by Venus. Follow me on Github: https://github.com/ilxqx! @ilxqx */
|
package/es/auth/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/*! VefFramework version: 1.0.
|
|
2
|
-
export{AuthContextProvider,useAuthContext}from
|
|
1
|
+
/*! VefFramework version: 1.0.64, build time: 2025-01-10T01:26:01.352Z, made by Venus. */
|
|
2
|
+
export { AuthContextProvider, useAuthContext } from './auth-context.js';
|
|
3
3
|
/*! VefFramework is a blazingly high-level, modern, flexible, easy-to-use UI framework made by Venus. Follow me on Github: https://github.com/ilxqx! @ilxqx */
|
package/es/expr/compiler.js
CHANGED
|
@@ -1,3 +1,300 @@
|
|
|
1
|
-
/*! VefFramework version: 1.0.
|
|
2
|
-
import{parseArgs
|
|
1
|
+
/*! VefFramework version: 1.0.64, build time: 2025-01-10T01:26:01.352Z, made by Venus. */
|
|
2
|
+
import { parseArgs, getAt, isQuotedString, rmEscapes } from './helpers.js';
|
|
3
|
+
|
|
4
|
+
function compile(expr) {
|
|
5
|
+
let provideTokens;
|
|
6
|
+
const requirements = /* @__PURE__ */ new Set();
|
|
7
|
+
const x = function expand(operand, tokens) {
|
|
8
|
+
return typeof operand === "function" ? operand(tokens) : operand;
|
|
9
|
+
};
|
|
10
|
+
const operatorRegistry = [
|
|
11
|
+
{
|
|
12
|
+
"&&": (l, r, t) => x(l, t) && x(r, t),
|
|
13
|
+
"||": (l, r, t) => x(l, t) || x(r, t)
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"===": (l, r, t) => !!(x(l, t) === x(r, t)),
|
|
17
|
+
"!==": (l, r, t) => !!(x(l, t) !== x(r, t)),
|
|
18
|
+
// eslint-disable-next-line eqeqeq
|
|
19
|
+
"==": (l, r, t) => !!(x(l, t) == x(r, t)),
|
|
20
|
+
// eslint-disable-next-line eqeqeq
|
|
21
|
+
"!=": (l, r, t) => !!(x(l, t) != x(r, t)),
|
|
22
|
+
">=": (l, r, t) => !!(x(l, t) >= x(r, t)),
|
|
23
|
+
"<=": (l, r, t) => !!(x(l, t) <= x(r, t)),
|
|
24
|
+
">": (l, r, t) => !!(x(l, t) > x(r, t)),
|
|
25
|
+
"<": (l, r, t) => !!(x(l, t) < x(r, t))
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"+": (l, r, t) => x(l, t) + x(r, t),
|
|
29
|
+
"-": (l, r, t) => x(l, t) - x(r, t)
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
"*": (l, r, t) => x(l, t) * x(r, t),
|
|
33
|
+
"/": (l, r, t) => x(l, t) / x(r, t),
|
|
34
|
+
"%": (l, r, t) => x(l, t) % x(r, t)
|
|
35
|
+
}
|
|
36
|
+
];
|
|
37
|
+
const operatorSymbols = operatorRegistry.reduce((s, g) => s.concat(Object.keys(g)), []);
|
|
38
|
+
const operatorChars = new Set(operatorSymbols.map((key) => key.charAt(0)));
|
|
39
|
+
function getOp(symbols, char, p, expression) {
|
|
40
|
+
const candidates = symbols.filter((s) => s.startsWith(char));
|
|
41
|
+
if (!candidates.length) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
return candidates.find((symbol) => {
|
|
45
|
+
if (expression.length >= p + symbol.length) {
|
|
46
|
+
const nextChars = expression.substring(p, p + symbol.length);
|
|
47
|
+
if (nextChars === symbol) {
|
|
48
|
+
return symbol;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
function getStep(p, expression, direction = 1) {
|
|
55
|
+
let next = direction ? expression.substring(p + 1).trim() : expression.substring(0, p).trim();
|
|
56
|
+
if (!next.length) {
|
|
57
|
+
return -1;
|
|
58
|
+
}
|
|
59
|
+
if (!direction) {
|
|
60
|
+
const reversed = next.split("").reverse();
|
|
61
|
+
const start = reversed.findIndex((char2) => operatorChars.has(char2));
|
|
62
|
+
next = reversed.slice(start).join("");
|
|
63
|
+
}
|
|
64
|
+
const [char] = next;
|
|
65
|
+
return operatorRegistry.findIndex((operators) => {
|
|
66
|
+
const symbols = Object.keys(operators);
|
|
67
|
+
return !!getOp(symbols, char, 0, next);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
function getTail(pos, expression) {
|
|
71
|
+
let tail = "";
|
|
72
|
+
const { length } = expression;
|
|
73
|
+
let depth = 0;
|
|
74
|
+
for (let p = pos; p < length; p++) {
|
|
75
|
+
const char = expression.charAt(p);
|
|
76
|
+
if (char === "(") {
|
|
77
|
+
depth++;
|
|
78
|
+
} else if (char === ")") {
|
|
79
|
+
depth--;
|
|
80
|
+
} else if (depth === 0 && char === " ") {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (depth === 0 && getOp(operatorSymbols, char, p, expression)) {
|
|
84
|
+
return [tail, p - 1];
|
|
85
|
+
} else {
|
|
86
|
+
tail += char;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return [tail, expression.length - 1];
|
|
90
|
+
}
|
|
91
|
+
function compileExpr(expression, step = 0) {
|
|
92
|
+
const operators = operatorRegistry[step];
|
|
93
|
+
const { length } = expression;
|
|
94
|
+
const symbols = Object.keys(operators);
|
|
95
|
+
let depth = 0;
|
|
96
|
+
let quote = false;
|
|
97
|
+
let op = null;
|
|
98
|
+
let operand = "";
|
|
99
|
+
let left = null;
|
|
100
|
+
let operation;
|
|
101
|
+
let lastChar;
|
|
102
|
+
let char = "";
|
|
103
|
+
let parenthetical = "";
|
|
104
|
+
let parenQuote = "";
|
|
105
|
+
let startP = 0;
|
|
106
|
+
const addTo = (depth2, char2) => {
|
|
107
|
+
if (depth2) {
|
|
108
|
+
parenthetical += char2;
|
|
109
|
+
} else {
|
|
110
|
+
operand += char2;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
for (let p = 0; p < length; p++) {
|
|
114
|
+
lastChar = char;
|
|
115
|
+
char = expression.charAt(p);
|
|
116
|
+
if ((char === "'" || char === '"') && lastChar !== "\\" && (depth === 0 && !quote || depth && !parenQuote)) {
|
|
117
|
+
if (depth) {
|
|
118
|
+
parenQuote = char;
|
|
119
|
+
} else {
|
|
120
|
+
quote = char;
|
|
121
|
+
}
|
|
122
|
+
addTo(depth, char);
|
|
123
|
+
continue;
|
|
124
|
+
} else if (quote && (char !== quote || lastChar === "\\") || parenQuote && (char !== parenQuote || lastChar === "\\")) {
|
|
125
|
+
addTo(depth, char);
|
|
126
|
+
continue;
|
|
127
|
+
} else if (quote === char) {
|
|
128
|
+
quote = false;
|
|
129
|
+
addTo(depth, char);
|
|
130
|
+
continue;
|
|
131
|
+
} else if (parenQuote === char) {
|
|
132
|
+
parenQuote = false;
|
|
133
|
+
addTo(depth, char);
|
|
134
|
+
continue;
|
|
135
|
+
} else if (char === " ") {
|
|
136
|
+
continue;
|
|
137
|
+
} else if (char === "(") {
|
|
138
|
+
if (depth === 0) {
|
|
139
|
+
startP = p;
|
|
140
|
+
} else {
|
|
141
|
+
parenthetical += char;
|
|
142
|
+
}
|
|
143
|
+
depth++;
|
|
144
|
+
} else if (char === ")") {
|
|
145
|
+
depth--;
|
|
146
|
+
if (depth === 0) {
|
|
147
|
+
const fn = typeof operand === "string" && operand.startsWith("$") ? operand : void 0;
|
|
148
|
+
const hasTail = fn && expression.charAt(p + 1) === ".";
|
|
149
|
+
let tail = "";
|
|
150
|
+
if (hasTail) {
|
|
151
|
+
[tail, p] = getTail(p + 2, expression);
|
|
152
|
+
}
|
|
153
|
+
const lStep = op ? step : getStep(startP, expression, 0);
|
|
154
|
+
const rStep = getStep(p, expression);
|
|
155
|
+
if (lStep === -1 && rStep === -1) {
|
|
156
|
+
operand = evaluate(parenthetical, -1, fn, tail);
|
|
157
|
+
if (typeof operand === "string") {
|
|
158
|
+
operand = parenthetical;
|
|
159
|
+
}
|
|
160
|
+
} else if (op && (lStep >= rStep || rStep === -1) && step === lStep) {
|
|
161
|
+
left = op.bind(null, evaluate(parenthetical, -1, fn, tail));
|
|
162
|
+
op = null;
|
|
163
|
+
operand = "";
|
|
164
|
+
} else if (rStep > lStep && step === rStep) {
|
|
165
|
+
operand = evaluate(parenthetical, -1, fn, tail);
|
|
166
|
+
} else {
|
|
167
|
+
operand += `(${parenthetical})${hasTail ? `.${tail}` : ""}`;
|
|
168
|
+
}
|
|
169
|
+
parenthetical = "";
|
|
170
|
+
} else {
|
|
171
|
+
parenthetical += char;
|
|
172
|
+
}
|
|
173
|
+
} else if (depth === 0 && (operation = getOp(symbols, char, p, expression))) {
|
|
174
|
+
if (p === 0) {
|
|
175
|
+
console.error(103, [operation, expression]);
|
|
176
|
+
}
|
|
177
|
+
p += operation.length - 1;
|
|
178
|
+
if (p === expression.length - 1) {
|
|
179
|
+
console.error(104, [operation, expression]);
|
|
180
|
+
}
|
|
181
|
+
if (!op) {
|
|
182
|
+
if (left) {
|
|
183
|
+
op = operators[operation].bind(null, evaluate(left, step));
|
|
184
|
+
left = null;
|
|
185
|
+
} else {
|
|
186
|
+
op = operators[operation].bind(null, evaluate(operand, step));
|
|
187
|
+
operand = "";
|
|
188
|
+
}
|
|
189
|
+
} else if (operand) {
|
|
190
|
+
left = op.bind(null, evaluate(operand, step));
|
|
191
|
+
op = operators[operation].bind(null, left);
|
|
192
|
+
operand = "";
|
|
193
|
+
}
|
|
194
|
+
continue;
|
|
195
|
+
} else {
|
|
196
|
+
addTo(depth, char);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (operand && op) {
|
|
200
|
+
op = op.bind(null, evaluate(operand, step));
|
|
201
|
+
}
|
|
202
|
+
op = !op && left ? left : op;
|
|
203
|
+
if (!op && operand) {
|
|
204
|
+
op = (v, t) => typeof v === "function" ? v(t) : v;
|
|
205
|
+
op = op.bind(null, evaluate(operand, step));
|
|
206
|
+
}
|
|
207
|
+
if (!op && !operand) {
|
|
208
|
+
console.error(105, expression);
|
|
209
|
+
}
|
|
210
|
+
return op;
|
|
211
|
+
}
|
|
212
|
+
function evaluate(operand, step, fnToken, tail) {
|
|
213
|
+
if (fnToken) {
|
|
214
|
+
const fn = evaluate(fnToken, operatorRegistry.length);
|
|
215
|
+
let userFuncReturn;
|
|
216
|
+
let tailCall = tail ? compile(`$${tail}`) : false;
|
|
217
|
+
if (typeof fn === "function") {
|
|
218
|
+
const args = parseArgs(String(operand)).map(
|
|
219
|
+
(arg) => evaluate(arg, -1)
|
|
220
|
+
);
|
|
221
|
+
return (tokens) => {
|
|
222
|
+
const userFunc = fn(tokens);
|
|
223
|
+
if (typeof userFunc !== "function") {
|
|
224
|
+
console.warn(150, fnToken);
|
|
225
|
+
return userFunc;
|
|
226
|
+
}
|
|
227
|
+
userFuncReturn = userFunc(
|
|
228
|
+
...args.map(
|
|
229
|
+
(arg) => typeof arg === "function" ? arg(tokens) : arg
|
|
230
|
+
)
|
|
231
|
+
);
|
|
232
|
+
if (tailCall) {
|
|
233
|
+
tailCall = tailCall.provide((subTokens) => {
|
|
234
|
+
const rootTokens = provideTokens(subTokens);
|
|
235
|
+
const t = subTokens.reduce(
|
|
236
|
+
(tokenSet, token) => {
|
|
237
|
+
const isTail = token === tail || tail?.startsWith(`${token}(`);
|
|
238
|
+
if (isTail) {
|
|
239
|
+
const value = getAt(userFuncReturn, token);
|
|
240
|
+
tokenSet[token] = () => value;
|
|
241
|
+
} else {
|
|
242
|
+
tokenSet[token] = rootTokens[token];
|
|
243
|
+
}
|
|
244
|
+
return tokenSet;
|
|
245
|
+
},
|
|
246
|
+
{}
|
|
247
|
+
);
|
|
248
|
+
return t;
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
return tailCall ? tailCall() : userFuncReturn;
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
} else if (typeof operand === "string") {
|
|
255
|
+
if (operand === "true") {
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
if (operand === "false") {
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
if (operand === "undefined") {
|
|
262
|
+
return void 0;
|
|
263
|
+
}
|
|
264
|
+
if (isQuotedString(operand)) {
|
|
265
|
+
return rmEscapes(operand.substring(1, operand.length - 1));
|
|
266
|
+
}
|
|
267
|
+
if (!Number.isNaN(+operand)) {
|
|
268
|
+
return Number(operand);
|
|
269
|
+
}
|
|
270
|
+
if (step < operatorRegistry.length - 1) {
|
|
271
|
+
return compileExpr(operand, step + 1);
|
|
272
|
+
} else {
|
|
273
|
+
if (operand.startsWith("$")) {
|
|
274
|
+
const cleaned = operand.substring(1);
|
|
275
|
+
requirements.add(cleaned);
|
|
276
|
+
return function getToken(tokens) {
|
|
277
|
+
return cleaned in tokens ? tokens[cleaned]() : void 0;
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
return operand;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return operand;
|
|
284
|
+
}
|
|
285
|
+
const compiled = compileExpr(expr);
|
|
286
|
+
const identifiers = Array.from(requirements);
|
|
287
|
+
function provide(callback) {
|
|
288
|
+
provideTokens = callback;
|
|
289
|
+
return Object.assign(
|
|
290
|
+
compiled.bind(null, callback(identifiers)),
|
|
291
|
+
{ provide }
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
return Object.assign(compiled, {
|
|
295
|
+
provide
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export { compile };
|
|
3
300
|
/*! VefFramework is a blazingly high-level, modern, flexible, easy-to-use UI framework made by Venus. Follow me on Github: https://github.com/ilxqx! @ilxqx */
|