dlsjs 1.0.4 → 1.0.6
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/dist/dlsjs.cjs.js +2 -0
- package/dist/dlsjs.cjs.js.map +1 -0
- package/dist/dlsjs.es.js +8332 -0
- package/dist/dlsjs.es.js.map +1 -0
- package/dist/dlsjs.umd.js +2 -0
- package/dist/dlsjs.umd.js.map +1 -0
- package/package.json +7 -3
- package/src/Browser/Dom.js +0 -127
- package/src/Browser/File.js +0 -174
- package/src/Browser/Local.js +0 -61
- package/src/Browser/Nav.js +0 -208
- package/src/Browser/Screen.js +0 -175
- package/src/Browser/Timer.js +0 -99
- package/src/ES/Chain.js +0 -62
- package/src/ES/Const.js +0 -24
- package/src/ES/Judgment.js +0 -184
- package/src/ES/QRW.js +0 -287
- package/src/ES/Singleton.js +0 -73
- package/src/ES/Trans.js +0 -390
- package/src/ES/Tree.js +0 -916
- package/src/Instance/transCore.js +0 -108
- package/src/Instance/transFunc.js +0 -84
- package/src/Instance/transRule.js +0 -32
- package/src/index.js +0 -19
package/src/ES/Trans.js
DELETED
|
@@ -1,390 +0,0 @@
|
|
|
1
|
-
import {clone, equals, isNil, mergeDeepWith, type} from 'ramda'
|
|
2
|
-
/**
|
|
3
|
-
* 默认携带当前值的key与parent,便于delete操作等
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 获得翻译转换后的配置,当前版本仅翻译匹配成功的字符串
|
|
8
|
-
* @param data
|
|
9
|
-
* @param scope
|
|
10
|
-
* @param convertFunc
|
|
11
|
-
* @param isTerminator
|
|
12
|
-
*/
|
|
13
|
-
export function deepConvert(data, scope, convertFunc, isTerminator) {
|
|
14
|
-
// todo 是否应区分scope中的临时可变数据(key,parent),与常驻不变数据(dataScope等其他外部传入的数据)
|
|
15
|
-
if(isTerminator(data, scope) === 'deep') {
|
|
16
|
-
return deepConvert(convertFunc(data, scope), scope, convertFunc, isTerminator)
|
|
17
|
-
}
|
|
18
|
-
if(isTerminator(data, scope)) return convertFunc(data, scope) // 最优先判断,是否为终止标记
|
|
19
|
-
if(['[object Object]','[object Array]'].includes(Object.prototype.toString.call(data))) {
|
|
20
|
-
// 初始化容器类型
|
|
21
|
-
let container = null
|
|
22
|
-
if(Object.prototype.toString.call(data) === '[object Array]') {
|
|
23
|
-
container = []
|
|
24
|
-
}
|
|
25
|
-
if(Object.prototype.toString.call(data) === '[object Object]') {
|
|
26
|
-
container = {}
|
|
27
|
-
}
|
|
28
|
-
for(let key in data) {
|
|
29
|
-
scope.key = key
|
|
30
|
-
scope.parent = container
|
|
31
|
-
if(Object.prototype.toString.call(container) === '[object Object]') {
|
|
32
|
-
container[key] = deepConvert(data[key], scope, convertFunc, isTerminator)
|
|
33
|
-
}else {
|
|
34
|
-
let tempResult = deepConvert(data[key], scope, convertFunc, isTerminator)
|
|
35
|
-
if(tempResult !== undefined) container.push(tempResult)
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return container
|
|
39
|
-
}
|
|
40
|
-
return data
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* 代替直接赋值方式,合并两个对象,尽可能的不改变指针,键名以obj2为主,obj1中存在,obj2中不存在的键名,将会删除obj1中的内容
|
|
45
|
-
* @param obj1
|
|
46
|
-
* @param obj2
|
|
47
|
-
*/
|
|
48
|
-
export function deepMerge(obj1, obj2) {
|
|
49
|
-
if(Object.prototype.toString.call(obj1) === '[object Array]') {
|
|
50
|
-
obj1.length = 0
|
|
51
|
-
obj1.splice(0,0,...obj2)
|
|
52
|
-
return
|
|
53
|
-
}
|
|
54
|
-
for(let key in obj1) {
|
|
55
|
-
if(!obj2.hasOwnProperty(key)) {
|
|
56
|
-
delete obj1[key]
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
for(let key in obj1) {
|
|
60
|
-
if(!equals(obj1[key], obj2[key])) { // todo 使用equals将增加时空复杂度,待优化
|
|
61
|
-
if(Object.prototype.toString.call(obj1[key]) === '[object Object]') {
|
|
62
|
-
deepMerge(obj1[key], obj2[key])
|
|
63
|
-
}else if(Object.prototype.toString.call(obj1[key]) === '[object Array]') {
|
|
64
|
-
obj1[key].length = 0
|
|
65
|
-
obj1[key].splice(0,0,...obj2[key])
|
|
66
|
-
}else {
|
|
67
|
-
obj1[key] = obj2[key]
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
for(let key in obj2) {
|
|
72
|
-
if(!obj1.hasOwnProperty(key)) obj1[key] = obj2[key]
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* 代替object.assign,合并两个对象,尽可能的不改变指针
|
|
78
|
-
* @param obj1
|
|
79
|
-
* @param obj2
|
|
80
|
-
* @param [matchFunc]
|
|
81
|
-
*/
|
|
82
|
-
export function deepAssign(obj1 = {}, obj2 = {}, matchFunc = null, notUseMatch = null) {
|
|
83
|
-
for(let key in obj2) {
|
|
84
|
-
// 属性判断类型
|
|
85
|
-
if(obj1.hasOwnProperty(key) && type(obj2[key]) === 'Object' && type(obj1[key]) === 'Object') {
|
|
86
|
-
deepAssign(obj1[key], obj2[key], matchFunc, notUseMatch)
|
|
87
|
-
}else if(obj1.hasOwnProperty(key) && type(obj2[key]) === 'Array' && type(obj1[key]) === 'Array') {
|
|
88
|
-
deepAssignArr(obj1[key], obj2[key], matchFunc, notUseMatch)
|
|
89
|
-
}else {
|
|
90
|
-
obj1[key] = obj2[key]
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
return obj1
|
|
94
|
-
}
|
|
95
|
-
export function shallowAssign(obj1 = {}, obj2 = {}, matchFunc = null) {
|
|
96
|
-
for(let key in obj2) {
|
|
97
|
-
// 属性判断类型
|
|
98
|
-
if(obj1.hasOwnProperty(key) && type(obj2[key]) === 'Object' && type(obj1[key]) === 'Object') {
|
|
99
|
-
deepAssign(obj1[key], obj2[key], matchFunc)
|
|
100
|
-
}else {
|
|
101
|
-
obj1[key] = obj2[key]
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return obj1
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* 合并数组
|
|
109
|
-
* @param arr1
|
|
110
|
-
* @param arr2
|
|
111
|
-
* @param [matchFunc] 例:(a,b) => a.id === b.id
|
|
112
|
-
* @param [notUseMatch] 例:(arr1,arr2) => !a?.[0]?.id || b?.[0]?.id 为true时,不适用标识配对情况,仍以顺序覆盖
|
|
113
|
-
*/
|
|
114
|
-
export function deepAssignArr(arr1, arr2, matchFunc = null, notUseMatch = null) {
|
|
115
|
-
if(arr2.filter(item => typeof item === 'object')?.length === 0) {
|
|
116
|
-
arr1.splice(0, arr1.length)
|
|
117
|
-
}
|
|
118
|
-
for (let index in arr2) {
|
|
119
|
-
if(type(arr2[index]) === 'Object') {
|
|
120
|
-
// 没有自定义匹配规则时使用,顺位赋值
|
|
121
|
-
if((notUseMatch instanceof Function && notUseMatch(arr1, arr2)) || !(matchFunc instanceof Function)) {
|
|
122
|
-
if(!isNil(arr1[index])) {
|
|
123
|
-
deepAssign(arr1[index], arr2[index], matchFunc, notUseMatch);
|
|
124
|
-
}else {
|
|
125
|
-
arr1.push(arr2[index])
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
// 匹配规则赋值
|
|
129
|
-
else if(matchFunc instanceof Function) {
|
|
130
|
-
let matchRes;
|
|
131
|
-
for(let i1 = 0;i1 < arr1.length;i1++) {
|
|
132
|
-
matchRes = matchFunc(arr1[i1], arr2[index])
|
|
133
|
-
if(matchRes) {
|
|
134
|
-
deepAssign(arr1[i1], arr2[index], matchFunc, notUseMatch);
|
|
135
|
-
break;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
if(!matchRes) {
|
|
139
|
-
arr1.push(arr2[index])
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
}else if(type(arr2[index]) === 'Array' && type(arr1[index]) === 'Array') {
|
|
144
|
-
// 多位数组合并情况,仅通过index合并
|
|
145
|
-
deepAssignArr(arr1[index], arr2[index], matchFunc, notUseMatch)
|
|
146
|
-
}else {
|
|
147
|
-
// 非数组对象仅通过赋值合并
|
|
148
|
-
arr1[index] = arr2[index]
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export function shallowAssignArr(arr1, arr2, matchFunc = null) {
|
|
154
|
-
for (let index in arr2) {
|
|
155
|
-
if(type(arr2[index]) === 'Object') {
|
|
156
|
-
// 匹配规则赋值
|
|
157
|
-
if(matchFunc instanceof Function) {
|
|
158
|
-
let matchRes;
|
|
159
|
-
for(let i1 = 0;i1 < arr1.length;i1++) {
|
|
160
|
-
matchRes = matchFunc(arr1[i1], arr2[index])
|
|
161
|
-
if(matchRes) {
|
|
162
|
-
shallowAssign(arr1[i1], arr2[index], matchFunc);
|
|
163
|
-
break;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
if(!matchRes) {
|
|
167
|
-
arr1.push(arr2[index])
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
// 没有自定义匹配规则时使用,顺位赋值
|
|
171
|
-
else {
|
|
172
|
-
if(!isNil(arr1[index])) {
|
|
173
|
-
shallowAssign(arr1[index], arr2[index], matchFunc);
|
|
174
|
-
}else {
|
|
175
|
-
arr1.push(arr2[index])
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}else if(type(arr2[index]) === 'Array' && type(arr1[index]) === 'Array') {
|
|
179
|
-
// 多位数组合并情况,仅通过index合并
|
|
180
|
-
shallowAssignArr(arr1[index], arr2[index], matchFunc)
|
|
181
|
-
}else {
|
|
182
|
-
// 非数组对象仅通过赋值合并
|
|
183
|
-
arr1[index] = arr2[index]
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* 查询不同值
|
|
191
|
-
* @param json1
|
|
192
|
-
* @param json2
|
|
193
|
-
*/
|
|
194
|
-
export function deepDiff(json1, json2) {
|
|
195
|
-
let differences = {};
|
|
196
|
-
|
|
197
|
-
function compare(obj1, obj2, path) {
|
|
198
|
-
for (let key in obj1) {
|
|
199
|
-
if (!(key in obj2)) {
|
|
200
|
-
differences[path + key] = { left: obj1[key], right: undefined };
|
|
201
|
-
} else {
|
|
202
|
-
if (type(obj1[key]) === 'Object' && type(obj2[key]) === 'Object' || type(obj1[key]) === 'Array' && type(obj2[key]) === 'Array') {
|
|
203
|
-
let newPath = ''
|
|
204
|
-
if(path === '') newPath = `#/${key}/`
|
|
205
|
-
else newPath = path + `${key}/`
|
|
206
|
-
compare(obj1[key], obj2[key], newPath);
|
|
207
|
-
} else if (obj1[key] !== obj2[key]) {
|
|
208
|
-
differences[path + key] = { left: obj1[key], right: obj2[key] };
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
for (let key in obj2) {
|
|
214
|
-
if (!(key in obj1)) {
|
|
215
|
-
differences[path + key] = { left: undefined, right: obj2[key] };
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
compare(json1, json2, '');
|
|
221
|
-
return differences;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
class Convert {
|
|
225
|
-
constructor(config) {
|
|
226
|
-
this.reservedProperty = config.reservedProperty || {}
|
|
227
|
-
this.reservedWord = config.reservedWord || {}
|
|
228
|
-
}
|
|
229
|
-
createConverter() {
|
|
230
|
-
return (data) => deepConvert(data, {key: null, parent: null}, this.transValue , this.isTerminator)
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* 可以把符合条件的字符或者对象转换为目标格式
|
|
234
|
-
* @param data
|
|
235
|
-
* @param params
|
|
236
|
-
* @return {(function(): void)|*}
|
|
237
|
-
*/
|
|
238
|
-
transValue(data, params) {
|
|
239
|
-
if(Object.prototype.toString.call(data) === '[object Object]') {
|
|
240
|
-
for(let key in data) {
|
|
241
|
-
if(this.reservedProperty.hasOwnProperty(key)) {
|
|
242
|
-
return this.reservedProperty[key](data, params)
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
if(typeof data === 'string') {
|
|
247
|
-
let regexRes = /^(@[a-zA-Z0-9]+)\/(.+)/.exec(data)
|
|
248
|
-
if(regexRes) {
|
|
249
|
-
const [matchString, $1, $2] = regexRes;
|
|
250
|
-
if(this.reservedWord.hasOwnProperty($1)) {
|
|
251
|
-
return this.reservedWord[$1](data, params, $2)
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
return data
|
|
256
|
-
}
|
|
257
|
-
/**
|
|
258
|
-
* 终止符判断
|
|
259
|
-
* @param data
|
|
260
|
-
* @returns {boolean}
|
|
261
|
-
*/
|
|
262
|
-
isTerminator(data) {
|
|
263
|
-
if(Object.prototype.toString.call(data) === '[object Object]') {
|
|
264
|
-
for(let key in data) {
|
|
265
|
-
if(this.reservedProperty.hasOwnProperty(key)) {
|
|
266
|
-
return true
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
if(typeof data === 'string') {
|
|
271
|
-
let regexRes = /^(@[a-zA-Z0-9]+)\/(.+)/.exec(data)
|
|
272
|
-
if(regexRes) {
|
|
273
|
-
const [matchString, $1, $2] = regexRes;
|
|
274
|
-
if(this.reservedWord.hasOwnProperty($1)) {
|
|
275
|
-
return true
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
return false
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* 多长度数组组合长度可能性
|
|
285
|
-
* @param sizeArr
|
|
286
|
-
*/
|
|
287
|
-
export function combine(sizeArr) {
|
|
288
|
-
if(sizeArr.length === 1) return combineSingle(sizeArr[0])
|
|
289
|
-
let res = combineNum(sizeArr[0], sizeArr[1])
|
|
290
|
-
if(sizeArr.length === 2) return res
|
|
291
|
-
for(let i = 2;i < sizeArr.length;i++) {
|
|
292
|
-
res = combineArrSize(res, sizeArr[i])
|
|
293
|
-
}
|
|
294
|
-
return res
|
|
295
|
-
|
|
296
|
-
function combineSingle(size) {
|
|
297
|
-
const arr = []
|
|
298
|
-
for(let i = 0;i < size;i++) {
|
|
299
|
-
arr.push([i])
|
|
300
|
-
}
|
|
301
|
-
return arr
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
function combineNum(size1, size2) {
|
|
305
|
-
const arr = []
|
|
306
|
-
for(let i = 0;i < size1;i++) {
|
|
307
|
-
for(let j = 0;j < size2;j++) {
|
|
308
|
-
arr.push([i, j])
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
return arr
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
function combineArrSize(lastArr, size) {
|
|
315
|
-
const arr = []
|
|
316
|
-
for(let item of lastArr) {
|
|
317
|
-
for(let j = 0;j < size;j++) {
|
|
318
|
-
const iArr = clone(item)
|
|
319
|
-
arr.push(iArr.concat(j))
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
return arr
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* 多组数组组合排列
|
|
328
|
-
* @param groupArr
|
|
329
|
-
*/
|
|
330
|
-
export function combineArr(groupArr) {
|
|
331
|
-
const sizeArr = []
|
|
332
|
-
for(let item of groupArr) {
|
|
333
|
-
sizeArr.push(item.length)
|
|
334
|
-
}
|
|
335
|
-
const indexList = combine(sizeArr)
|
|
336
|
-
|
|
337
|
-
const res = []
|
|
338
|
-
for(let indexGroup of indexList) {
|
|
339
|
-
const item = []
|
|
340
|
-
for(let i in indexGroup) {
|
|
341
|
-
let index = indexGroup[i]
|
|
342
|
-
item.push(clone(groupArr[i][index]))
|
|
343
|
-
}
|
|
344
|
-
res.push(item)
|
|
345
|
-
}
|
|
346
|
-
return res
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
// 运行模板字符串
|
|
350
|
-
export function runTemplateString(template, variableMap) {
|
|
351
|
-
let keys = [...new Set([...Object.keys(variableMap), ...extractPlaceholders(template)])]
|
|
352
|
-
let values = Object.values(variableMap)
|
|
353
|
-
for(let i = 1;keys.length >= values.length;i++) {
|
|
354
|
-
values.push('')
|
|
355
|
-
}
|
|
356
|
-
let func
|
|
357
|
-
try {
|
|
358
|
-
func = new Function(...keys, `return \`${template}\``)
|
|
359
|
-
}catch (e) {
|
|
360
|
-
console.warn(e)
|
|
361
|
-
return ''
|
|
362
|
-
}
|
|
363
|
-
let res = ''
|
|
364
|
-
try {
|
|
365
|
-
res = func(...values)
|
|
366
|
-
}catch (e) {
|
|
367
|
-
console.warn(e)
|
|
368
|
-
return ''
|
|
369
|
-
}
|
|
370
|
-
return res
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
function extractPlaceholders(str) {
|
|
374
|
-
// 使用正则表达式来匹配 ${} 中的内容
|
|
375
|
-
const regex = /\$\{([a-zA-Z0-9_]+)\}/g;
|
|
376
|
-
const matches = [];
|
|
377
|
-
let match;
|
|
378
|
-
|
|
379
|
-
// 使用 while 循环来提取所有匹配项
|
|
380
|
-
while ((match = regex.exec(str)) !== null) {
|
|
381
|
-
// 第一个匹配的分组是 `([a-zA-Z0-9_]+)` 中的内容
|
|
382
|
-
matches.push(match[1]);
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
return matches;
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
export default {
|
|
389
|
-
deepConvert, deepMerge, deepAssign, deepDiff, combine, combineArr, shallowAssign, shallowAssignArr
|
|
390
|
-
}
|