@flowlist/js-core 2.3.0 → 3.0.0

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/utils.ts DELETED
@@ -1,325 +0,0 @@
1
- import ENUM from './enum'
2
- import type {
3
- objectKey,
4
- keyMap,
5
- morphArray,
6
- fieldResult,
7
- defaultField,
8
- fetchTypes,
9
- fieldKeys,
10
- generateFieldProps,
11
- generateParamsType,
12
- generateParamsResp
13
- } from './types'
14
-
15
- export const isObjectResult = (data: Record<string, any>): boolean => {
16
- return data.result === undefined
17
- }
18
-
19
- export const generateDefaultField = (opts = {}): defaultField => ({
20
- ...{
21
- result: [],
22
- noMore: false,
23
- nothing: false,
24
- loading: false,
25
- error: null,
26
- extra: null,
27
- fetched: false,
28
- page: 0,
29
- total: 0
30
- },
31
- ...opts
32
- })
33
-
34
- /**
35
- * 根据参数生成 field 的 namespace
36
- * @param {string} func
37
- * @param {string} type
38
- * @param {object} query
39
- * @return {string}
40
- */
41
- export const generateFieldName = ({
42
- func,
43
- type,
44
- query = {}
45
- }: generateFieldProps): string => {
46
- func =
47
- typeof func === 'string'
48
- ? func
49
- : `api-${Math.random().toString(36).substring(2)}`
50
- type = type || 'auto'
51
- let result = `${func}-${type}`
52
- Object.keys(query)
53
- .filter(
54
- (_) =>
55
- !~['undefined', 'object', 'function'].indexOf(typeof query[_]) &&
56
- !~[
57
- 'page',
58
- 'is_up',
59
- 'since_id',
60
- 'seen_ids',
61
- '__refresh__',
62
- '__reload__'
63
- ].indexOf(_)
64
- )
65
- .sort()
66
- .forEach((key) => {
67
- result += `-${key}-${query[key]}`
68
- })
69
-
70
- return result
71
- }
72
-
73
- /**
74
- * 根据 key 从 object 里拿 value
75
- * @param {object} field
76
- * @param {string} keys
77
- * @return {*}
78
- */
79
- export const getObjectDeepValue = (
80
- field: keyMap,
81
- keys: string | string[]
82
- ): any => {
83
- if (!keys) {
84
- return field || ''
85
- }
86
-
87
- let result = field || ''
88
- const keysArr = isArray(keys)
89
- ? (keys as string[])
90
- : (keys as string).split('.')
91
-
92
- keysArr.forEach((key: string) => {
93
- result = (result as any)[key]
94
- })
95
-
96
- return result || ''
97
- }
98
-
99
- export const updateObjectDeepValue = (
100
- field: keyMap,
101
- changeKey: string,
102
- value: any
103
- ): void => {
104
- if (/\./.test(changeKey)) {
105
- const keys = changeKey.split('.')
106
- const prefix = keys.pop()
107
- let result = field
108
-
109
- keys.forEach((key) => {
110
- result = result[key]
111
- })
112
- result[prefix as string] = value
113
- } else {
114
- field[changeKey] = value
115
- }
116
- }
117
-
118
- export const searchValueByKey = (
119
- result: fieldResult,
120
- id: objectKey,
121
- key: objectKey
122
- ): any => {
123
- if (isArray(result)) {
124
- const index = computeMatchedItemIndex(id, result as morphArray, key)
125
- if (index < 0) {
126
- return undefined
127
- }
128
- return (result as morphArray)[index]
129
- }
130
- return (result as keyMap)[id]
131
- }
132
-
133
- /**
134
- * 通过 id 匹配返回数组中某个对象的 index
135
- * @param {int|string} itemId
136
- * @param {array} fieldArr
137
- * @param {int|string} changingKey
138
- * @return {number}
139
- */
140
- export const computeMatchedItemIndex = (
141
- itemId: objectKey,
142
- fieldArr: morphArray,
143
- changingKey: objectKey
144
- ): number => {
145
- let s = -1
146
-
147
- for (let i = 0; i < fieldArr.length; i++) {
148
- if (
149
- getObjectDeepValue(fieldArr[i], changingKey.toString()).toString() ===
150
- (itemId || '').toString()
151
- ) {
152
- s = i
153
- break
154
- }
155
- }
156
- return s
157
- }
158
-
159
- export const combineArrayData = (
160
- fieldArray: any[],
161
- value: fieldResult,
162
- changingKey: string
163
- ): void => {
164
- if (isArray(value)) {
165
- value.forEach((col: keyMap) => {
166
- const stringifyId = getObjectDeepValue(col, changingKey).toString()
167
- fieldArray.forEach((item, index) => {
168
- if (getObjectDeepValue(item, changingKey).toString() === stringifyId) {
169
- fieldArray[index] = {
170
- ...item,
171
- ...col
172
- }
173
- }
174
- })
175
- })
176
- } else {
177
- Object.keys(value).forEach((uniqueId) => {
178
- const stringifyId = (uniqueId || '').toString()
179
- fieldArray.forEach((item, index) => {
180
- if (getObjectDeepValue(item, changingKey).toString() === stringifyId) {
181
- fieldArray[index] = {
182
- ...item,
183
- ...(value as keyMap)[uniqueId]
184
- }
185
- }
186
- })
187
- })
188
- }
189
- }
190
-
191
- /**
192
- * 判断参数是否为数组
193
- * @param {object|array} data
194
- * @return {boolean}
195
- */
196
- export const isArray = (data: any): boolean =>
197
- Object.prototype.toString.call(data) === '[object Array]'
198
-
199
- /**
200
- * 设置一个响应式的数据到对象上
201
- * @param {object} field
202
- * @param {string} key
203
- * @param {array|object} value
204
- * @param {string} type
205
- * @param {boolean} insertBefore
206
- */
207
- export const setReactivityField = (
208
- field: defaultField,
209
- key: fieldKeys,
210
- value: any,
211
- type: fetchTypes,
212
- insertBefore: boolean
213
- ) => {
214
- if (type === ENUM.FETCH_TYPE.PAGINATION) {
215
- ;(field[key] as any) = value
216
- return
217
- }
218
-
219
- if (isArray(value)) {
220
- ;(field[key] as morphArray) = insertBefore
221
- ? value.concat(field[key] || [])
222
- : (field[key] || []).concat(value)
223
- return
224
- }
225
-
226
- if (key !== ENUM.FIELD_DATA.RESULT_KEY) {
227
- ;(field[key] as any) = value
228
- return
229
- }
230
-
231
- if (isArray(field[key])) {
232
- ;(field[key] as keyMap) = {}
233
- }
234
-
235
- Object.keys(value).forEach((subKey) => {
236
- field[key][subKey] = field[key][subKey]
237
- ? insertBefore
238
- ? value[subKey].concat(field[key][subKey])
239
- : field[key][subKey].concat(value[subKey])
240
- : value[subKey]
241
- })
242
- }
243
-
244
- /**
245
- * 计算一个数据列的长度
246
- * @param {array|object} data
247
- * @return {number}
248
- */
249
- export const computeResultLength = (data: fieldResult): number => {
250
- let result = 0
251
- if (isArray(data)) {
252
- result = data.length
253
- } else {
254
- Object.keys(data).forEach((key) => {
255
- result += (data as keyMap)[key].length
256
- })
257
- }
258
- return result
259
- }
260
-
261
- /**
262
- * 拼接请求的参数
263
- * @param {object} field
264
- * @param {string} uniqueKey
265
- * @param {object} query
266
- * @param {string} type
267
- * @return {object}
268
- */
269
- export const generateRequestParams = ({
270
- field,
271
- uniqueKey,
272
- query = {},
273
- type
274
- }: generateParamsType): generateParamsResp => {
275
- const result: generateParamsResp = {}
276
- if (field.fetched) {
277
- const changing = uniqueKey || ENUM.DEFAULT_UNIQUE_KEY_NAME
278
- if (type === ENUM.FETCH_TYPE.AUTO) {
279
- result.seen_ids = field.result
280
- .map((_: keyMap) => getObjectDeepValue(_, changing))
281
- .join(',')
282
- result.since_id = getObjectDeepValue(
283
- (field.result as morphArray)[query.is_up ? 0 : field.result.length - 1],
284
- changing
285
- )
286
- result.is_up = query.is_up ? 1 : 0
287
- result.page = query.page || field.page + 1
288
- } else if (type === ENUM.FETCH_TYPE.HAS_LOADED_IDS) {
289
- result.seen_ids = field.result
290
- .map((_: keyMap) => getObjectDeepValue(_, changing))
291
- .join(',')
292
- } else if (type === ENUM.FETCH_TYPE.SINCE_FIRST_OR_END_ID) {
293
- result.since_id = getObjectDeepValue(
294
- (field.result as morphArray)[query.is_up ? 0 : field.result.length - 1],
295
- changing
296
- )
297
- result.is_up = query.is_up ? 1 : 0
298
- } else if (type === ENUM.FETCH_TYPE.PAGINATION) {
299
- result.page = query.page
300
- } else if (type === ENUM.FETCH_TYPE.SCROLL_LOAD_MORE) {
301
- result.page = field.page + 1
302
- }
303
- } else {
304
- if (type === ENUM.FETCH_TYPE.AUTO) {
305
- result.seen_ids = ''
306
- result.since_id = query.sinceId || (query.is_up ? 999999999 : 0)
307
- result.is_up = query.is_up ? 1 : 0
308
- result.page = query.page || field.page || 1
309
- } else if (type === ENUM.FETCH_TYPE.HAS_LOADED_IDS) {
310
- result.seen_ids = ''
311
- } else if (type === ENUM.FETCH_TYPE.SINCE_FIRST_OR_END_ID) {
312
- result.since_id = query.sinceId || (query.is_up ? 999999999 : 0)
313
- result.is_up = query.is_up ? 1 : 0
314
- } else if (type === ENUM.FETCH_TYPE.PAGINATION) {
315
- result.page = query.page || field.page
316
- } else if (type === ENUM.FETCH_TYPE.SCROLL_LOAD_MORE) {
317
- result.page = 1
318
- }
319
- }
320
-
321
- return {
322
- ...query,
323
- ...result
324
- }
325
- }