dlsjs 1.0.5 → 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/src/ES/QRW.js DELETED
@@ -1,287 +0,0 @@
1
- import {isNil} from "ramda";
2
- import {findLeaf} from "./Tree";
3
- /**
4
- * 读写扩展,将扩展getter,setter方法,更易于双向绑定的使用
5
- */
6
- const ReservedFunction = {
7
- /**
8
- * 取值时解析JSON字符串为JS对象,赋值后将JS对象序列化回JSON字符串
9
- */
10
- "JSON": {
11
- get: (v) => {
12
- try {
13
- return JSON.parse(v)
14
- }catch (e) {
15
- // console.warn(e)
16
- return v
17
- }
18
- },
19
- set: (tempSchema, tempKey, tempValue) => {
20
- try {
21
- tempSchema[tempKey] = JSON.stringify(tempValue)
22
- return true
23
- }catch (e) {
24
- // console.warn(e)
25
- tempSchema[tempKey] = '{}'
26
- return true
27
- }
28
- }
29
- },
30
- /**
31
- * 取值时如果为空则创建空数组容器,赋值前如果没有数组容器,则创建数组容器
32
- */
33
- "Array": {
34
- get: (v, tempSchema, tempKey) => {
35
- if(!v) tempSchema[tempKey] = []
36
- return tempSchema[tempKey]
37
- },
38
- preSet: (tempSchema, tempKey, tempValue) => {
39
- if(!tempSchema) tempSchema = []
40
- for(let i = 0; i < Number(tempKey); i++){
41
- if(isNil(tempSchema[i])) {
42
- tempSchema[i] = null
43
- }
44
- }
45
- return tempSchema
46
- }
47
- },
48
- /**
49
- * 取值时如果上级是对象数组,则调用Array.property.map(item => item[index])
50
- * 赋值时则调用Array.property.forEach(item => item[index] = tempValue)
51
- */
52
- "Item": {
53
- get: (v, tempSchema, tempKey) => {
54
- if(Array.isArray(tempSchema)) {
55
- return tempSchema.map(item => item[tempKey])
56
- }else {
57
- return tempSchema[tempKey]
58
- }
59
- },
60
- set: (tempSchema, tempKey, tempValue) => {
61
- for(let i = 0; i < tempValue.length; i++){
62
- if(!isNil(tempValue[i])) {
63
- if(!tempSchema[i]) tempSchema[i] = {}
64
- tempSchema[i][tempKey] = tempValue[i]
65
- }
66
- }
67
- return tempSchema
68
- }
69
- },
70
- /*针对数字与数组 todo 切换A2S*/
71
- "ItemString": {
72
- get: (v, tempSchema, tempKey) => {
73
- if(Array.isArray(tempSchema)) {
74
- return tempSchema.map(item => item[tempKey]).toString()
75
- }else {
76
- return tempSchema[tempKey]
77
- }
78
- },
79
- set: (tempSchema, tempKey, tempValue) => {
80
- tempValue = tempValue.split(',')
81
- for(let i = 0; i < tempValue.length; i++){
82
- if(!tempSchema[i]) tempSchema[i] = {}
83
- if(!isNil(tempValue[i])) {
84
- if(!tempSchema[i]) tempSchema[i] = {}
85
- tempSchema[i][tempKey] = tempValue[i]
86
- }
87
- }
88
- return tempSchema
89
- }
90
- },
91
- /*针对数字与数组 todo 切换N2S*/
92
- "String": {
93
- get: (v) => {
94
- return v.toString()
95
- },
96
- set: (tempSchema, tempKey, tempValue) => {
97
- if(typeof tempValue !== 'string') {
98
- tempSchema[tempKey] = tempValue.toString()
99
- }else {
100
- tempSchema[tempKey] = tempValue
101
- }
102
- return tempSchema
103
- }
104
- },
105
- "Number": {
106
- get: (v) => {
107
- return Number(v || 0)
108
- },
109
- set: (tempSchema, tempKey, tempValue) => {
110
- tempSchema[tempKey] = Number(tempValue || 0)
111
- return tempSchema
112
- }
113
- },
114
- "Boolean": {
115
- get: (v) => {
116
- if(v === 'true') return true
117
- if(v === 'false') return false
118
- return !!v
119
- },
120
- set: (tempSchema, tempKey, tempValue) => {
121
- if(tempValue === 'true') tempSchema[tempKey] = true
122
- else if(tempValue === 'false') tempSchema[tempKey] = false
123
- else tempSchema[tempKey] = !!tempValue
124
- return tempSchema
125
- }
126
- },
127
- /**
128
- * 取一叉树叶子 todo 赋值未完成
129
- */
130
- "Leaf": {
131
- get: (v, tempSchema, tempKey) => {
132
- return findLeaf(tempSchema, tempKey)
133
- }
134
- }
135
- }
136
-
137
- /**
138
- * 获取符合openApi中$Ref格式的内容
139
- * @param {Object} target
140
- * @param {String} refName
141
- * @param {String} splitString
142
- */
143
- export function queryDefinitionByRef(target, refName, splitString = '/') {
144
- let refArray = refName?.split(splitString)
145
- refArray.shift() // 抛出"#"
146
- let tempSchema = target // 临时参数(每次循环体执行后会改变),当前内容,随着逐步深入解析改变
147
- while(refArray.length > 0) {
148
- // todo 是否对Map与Set提供支持,待决定
149
- if(!tempSchema) return tempSchema // 开始时做非空判断,防止出现空指针
150
- let tempKey = refArray.shift() || '' // 临时参数,取当前key或index
151
- if(tempKey === '') return tempSchema // 应对refName为#/的情况
152
- let regexRes = /\$([a-zA-Z0-9]+)_(.+)/.exec(tempKey) // 当识别到 $ 开始 _ 结束的正则,开始匹配保留字,做对应处理
153
- if(regexRes) {
154
- const [matchString, reservedWord, newTempKey] = regexRes
155
- tempKey = newTempKey
156
- let reversed = ReservedFunction[reservedWord]
157
- if(reversed && reversed.get) {
158
- tempSchema = reversed.get(tempSchema[tempKey], tempSchema, tempKey)
159
- continue
160
- }
161
- }
162
- tempSchema = tempSchema[tempKey]
163
- }
164
- return tempSchema
165
- }
166
-
167
- /**
168
- * 设置符合openApi中$Ref格式的内容
169
- * @param {Object} target
170
- * @param {String} refName
171
- * @param {any} value
172
- * @param {String} splitString
173
- */
174
- export function setDefinitionByRef(target, refName, value, splitString = '/') {
175
- let refArray = refName?.split(splitString)
176
- // refArray.shift() // 不抛出"#"
177
- let tempSchema;
178
- let tempValue = value;
179
- let tempKey = refArray.pop() || '';
180
- let preKey;
181
- while (refArray.length > 0) {
182
- let tempRef = refArray.join('/')
183
- tempSchema = queryDefinitionByRef(target, tempRef) // 取当前要赋值的区域
184
- preKey = refArray[refArray.length-1] // 赋值区域的键名
185
- let regexResPre = /\$([a-zA-Z0-9]+)_(.+)/.exec(preKey) // 当识别到 $ 开始 _ 结束的正则,开始匹配保留字,做对应处理
186
- let regexResTemp = /\$([a-zA-Z0-9]+)_(.+)/.exec(tempKey) // 当识别到 $ 开始 _ 结束的正则,开始匹配保留字,做对应处理
187
- if(regexResPre) {
188
- const [ms, $1, $2] = regexResPre
189
- let reversed = ReservedFunction[$1]
190
- if(reversed && reversed.preSet) {
191
- tempSchema = reversed.preSet(tempSchema, tempKey, tempValue)
192
- }
193
- }
194
- if(regexResTemp) {
195
- const [ms, $1, $2] = regexResTemp
196
- let reversed = ReservedFunction[$1]
197
- if(reversed && reversed.set) {
198
- tempValue = reversed.set(tempSchema, $2, tempValue)
199
- }else {
200
- if(!tempSchema) tempSchema = {}
201
- tempSchema[$2] = tempValue
202
- tempValue = tempSchema
203
- }
204
- }else {
205
- if(!tempSchema) tempSchema = {}
206
- tempSchema[tempKey] = tempValue
207
- tempValue = tempSchema
208
- }
209
- tempKey = preKey;
210
- refArray.pop();
211
- }
212
- return true
213
- }
214
-
215
- /**
216
- * 读写代理内容
217
- */
218
- const handler = {
219
- get: (target, key) => {
220
- let result;
221
- if(typeof key === 'string' && key.includes('#/')) {
222
- result = queryDefinitionByRef(target, key)
223
- }else {
224
- result = target[key]
225
- }
226
- return result
227
- },
228
- set: (target, key, value) => {
229
- if(typeof key === 'string') {
230
- if(key.includes('#/')) {
231
- setDefinitionByRef(target, key, value)
232
- return true
233
- }
234
- }
235
- target[key] = value
236
- return true
237
- }
238
- }
239
-
240
- /**
241
- * 读写代理内容
242
- */
243
- const deepHandler = {
244
- get: (target, key) => {
245
- let result;
246
- if(typeof key === 'string' && key.includes('#/')) {
247
- result = queryDefinitionByRef(target, key)
248
- }else {
249
- result = target[key]
250
- }
251
- if(['[object Object]', '[object Array]'].includes(Object.prototype.toString.call(result))) {
252
- return new Proxy(result, deepHandler)
253
- }else {
254
- return result
255
- }
256
- },
257
- set: (target, key, value) => {
258
- if(typeof key === 'string') {
259
- if(key.includes('#/')) {
260
- setDefinitionByRef(target, key, value)
261
- return true
262
- }
263
- }
264
- target[key] = value
265
- return true
266
- }
267
- }
268
-
269
-
270
- /**
271
- * 构造函数,创建一个智能读写对象
272
- * @constructor
273
- */
274
- export function shallowRW(data) {
275
- return new Proxy(data, handler)
276
- }
277
-
278
- export function RW(data) {
279
- if(isNil(data)) {
280
- return {}
281
- }
282
- return new Proxy(data, deepHandler)
283
- }
284
-
285
- export default {
286
- RW, shallowRW
287
- }
@@ -1,73 +0,0 @@
1
- import {isNil} from 'ramda'
2
-
3
- class SingletonManage {
4
- constructor(option) {
5
- // todo 检查option结构
6
- this.data = {}
7
- this.promiseInstance = {}
8
- this.option = option
9
- }
10
- async get(name) {
11
- // 内存有值返回值
12
- if(this.data[name]) return this.data[name]
13
- // 已发起请求需等待
14
- if(this.promiseInstance[name]) {
15
- await this.promiseInstance[name]
16
- return this.data[name]
17
- }
18
- if(!this.promiseInstance[name]) {
19
- if(!this.option[name]) return null
20
- this.promiseInstance[name] = this.option[name]()
21
- this.data[name] = await this.promiseInstance[name]
22
- this.promiseInstance[name] = null
23
- return this.data[name]
24
- }
25
- }
26
- getSync(name) {
27
- // 内存有值返回值
28
- if(this.data[name]) return this.data[name]
29
- else {
30
- this.data[name] = this.option[name]()
31
- return this.data[name]
32
- }
33
- }
34
- async set(name, func) {
35
- // todo 命名冲突校验
36
- if(Object.prototype.toString.call(func) === '[object Function]') {
37
- this.option[name] = func
38
- }else if(Object.prototype.toString.call(func) === '[object AsyncFunction]'){
39
- if(!this.promiseInstance[name]) {
40
- this.promiseInstance[name] = func()
41
- this.data[name] = await this.promiseInstance[name]
42
- this.promiseInstance[name] = null
43
- }
44
- }
45
- }
46
- async refresh(name) {
47
- this.data[name] = null
48
- return await this.get(name)
49
- }
50
- }
51
-
52
- /**
53
- * 创建单例仓库
54
- * @param option 键值是getter函数,可以是async函数
55
- */
56
- export function createSM(option) {
57
- const instance = new SingletonManage(option)
58
- const handler = {
59
- get: (target, key) => {
60
- if(!isNil(target[key])) {
61
- return target[key]
62
- }else {
63
- return target.getSync(key)
64
- }
65
- }
66
- }
67
- // 支持直接取值
68
- return new Proxy(instance, handler)
69
- }
70
-
71
- export default {
72
- createSM
73
- }