baja-lite 1.0.7 → 1.0.16

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/fn.ts CHANGED
@@ -219,49 +219,57 @@ export function excuteSplit<T = any, R = any, E = any>(
219
219
  const list = arraySplit(datas, ps);
220
220
  if (sync === ExcuteSplitMode.AsyncTrust) {
221
221
  return new Promise<R[]>(async (resolve, reject) => {
222
- const reasons: R[] = [];
223
- if (settled) {
224
- const result = await Promise.allSettled(list.map((list, i) => fn(list, i, list.length, extendParams[i])));
225
- for (const item of result) {
226
- if (item.status === 'rejected') {
227
- reject(item.reason);
228
- } else {
229
- reasons.push(item.value);
222
+ try {
223
+ const reasons: R[] = [];
224
+ if (settled) {
225
+ const result = await Promise.allSettled(list.map((list, i) => fn(list, i, list.length, extendParams[i])));
226
+ for (const item of result) {
227
+ if (item.status === 'rejected') {
228
+ reject(item.reason);
229
+ } else {
230
+ reasons.push(item.value);
231
+ }
232
+ }
233
+ } else {
234
+ for (let i = 0; i < list.length; i++) {
235
+ const startIndex = (i - 1) * ps.everyLength;
236
+ const endIndex = startIndex + list[i]!.length - 1;
237
+ reasons.push(await fn(list[i]!, i, list.length, extendParams[i], startIndex, endIndex));
230
238
  }
231
239
  }
232
- } else {
233
- for (let i = 0; i < list.length; i++) {
234
- const startIndex = (i - 1) * ps.everyLength;
235
- const endIndex = startIndex + list[i]!.length - 1;
236
- reasons.push(await fn(list[i]!, i, list.length, extendParams[i], startIndex, endIndex));
237
- }
240
+ resolve(reasons);
241
+ } catch (error) {
242
+ reject(error);
238
243
  }
239
- resolve(reasons);
240
244
  });
241
245
  } else if (sync === ExcuteSplitMode.AsyncNoTrust) {
242
246
  return new Promise<{ result: R[]; error: string[]; }>(async (resolve, reject) => {
243
- const reasons: { result: R[]; error: string[]; } = { result: [], error: [] };
244
- if (settled) {
245
- const result = await Promise.allSettled(list.map((list, i) => fn(list, i, list.length, extendParams[i])));
246
- for (const item of result) {
247
- if (item.status === 'rejected') {
248
- reasons.error.push(item.reason);
249
- } else {
250
- reasons.result.push(item.value);
247
+ try {
248
+ const reasons: { result: R[]; error: string[]; } = { result: [], error: [] };
249
+ if (settled) {
250
+ const result = await Promise.allSettled(list.map((list, i) => fn(list, i, list.length, extendParams[i])));
251
+ for (const item of result) {
252
+ if (item.status === 'rejected') {
253
+ reasons.error.push(item.reason);
254
+ } else {
255
+ reasons.result.push(item.value);
256
+ }
251
257
  }
252
- }
253
- } else {
254
- for (let i = 0; i < list.length; i++) {
255
- const startIndex = (i - 1) * ps.everyLength;
256
- const endIndex = startIndex + list[i]!.length - 1;
257
- try {
258
- reasons.result.push(await fn(list[i]!, i, list.length, extendParams[i], startIndex, endIndex));
259
- } catch (error) {
260
- reasons.error.push(error as string);
258
+ } else {
259
+ for (let i = 0; i < list.length; i++) {
260
+ const startIndex = (i - 1) * ps.everyLength;
261
+ const endIndex = startIndex + list[i]!.length - 1;
262
+ try {
263
+ reasons.result.push(await fn(list[i]!, i, list.length, extendParams[i], startIndex, endIndex));
264
+ } catch (error) {
265
+ reasons.error.push(error as string);
266
+ }
261
267
  }
262
268
  }
269
+ resolve(reasons);
270
+ } catch (error) {
271
+ reject(error);
263
272
  }
264
- resolve(reasons);
265
273
  });
266
274
  } else if (sync === ExcuteSplitMode.SyncTrust) {
267
275
  const reasons: R[] = [];
package/src/list.ts CHANGED
@@ -1,3 +1,4 @@
1
+
1
2
  export class ArrayList<T> extends Array<T> {
2
3
  constructor(array?: Array<T> | T | undefined) {
3
4
  super();
@@ -28,4 +29,29 @@ export class ArrayList<T> extends Array<T> {
28
29
  remove(index: number) {
29
30
  this.splice(index, 1);
30
31
  }
31
- }
32
+ }
33
+
34
+ // export class ArrayMap<T> extends Array<T> {
35
+ // private _map: Map<string, T> = new Map();
36
+ // private key: keyof T;
37
+ // constructor(key: keyof T, array?: Array<T> | T | undefined) {
38
+ // super();
39
+ // this.key = key;
40
+ // if (array instanceof Array) {
41
+ // super.push(...array);
42
+ // } else if (typeof array !== 'undefined') {
43
+ // super.push(array);
44
+ // }
45
+ // }
46
+ // override push(...items: T[]): number {
47
+ // for (const item of items) {
48
+ // const key = item[this.key] as string;
49
+ // if (!this._map.has(key)) {
50
+ // super.push(item);
51
+ // this._map.set(key, item);
52
+ // }
53
+ // }
54
+ // return this.length;
55
+ // }
56
+ // override
57
+ // }
package/src/math.ts CHANGED
@@ -134,7 +134,7 @@ export const round = (number: any, numDigits: number, upOrDown = 0): number => {
134
134
  /** =value.xx,其中xx=number,如number=99,表示修正数字为value.99 */
135
135
  export const merge = function (value: any, number: any) {
136
136
  if (isNum(value) && isNum(number)) {
137
- return new Decimal(value).floor().add(`0.${ number }`).toNumber();
137
+ return new Decimal(value).floor().add(`0.${number}`).toNumber();
138
138
  } else if (isNum(value)) {
139
139
  return value;
140
140
  } else {
@@ -181,7 +181,7 @@ const IF = function () {
181
181
  export class Bus {
182
182
  private result: number;
183
183
  private ifit = true;
184
- constructor (result: any) {
184
+ constructor(result: any) {
185
185
  this.result = num(result);
186
186
  }
187
187
  @IF()
@@ -364,4 +364,42 @@ export const getGeo = (p1: Point, p2: Point) => {
364
364
  Math.cos(p1.lat), Math.cos(p2.lat), Math.pow(Math.sin(div(sub(p1.long, p2.long), 2)), 2)
365
365
  ))
366
366
  )), 2, 6378.137, 10000))).div(10000).round(2).over();
367
- };
367
+ };
368
+
369
+ const ZM = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
370
+ /**
371
+ * 十进制转换自定义进制
372
+ * @param from 数字
373
+ * @param to 自定义进制的字符
374
+ * @returns
375
+ */
376
+ export function ten2Any(from: number, to = ZM) {
377
+ let result = '';
378
+ const length = to.length;
379
+ while (from > 0) {
380
+ from--;
381
+ let remainder = from % length;
382
+ result = to.charAt(remainder) + result;
383
+ from = Math.floor(from / length);
384
+ }
385
+ return result;
386
+ }
387
+ /**
388
+ * 自定义进制转换十进制
389
+ * @param from
390
+ * @param to
391
+ * @returns
392
+ */
393
+ export function any2Ten(from: string, to = ZM) {
394
+ let decimal = 0;
395
+ from = from.toUpperCase();
396
+ for (let i = 0; i < from.length; i++) {
397
+ const char = from[from.length - 1 - i]!.toUpperCase();
398
+ const index = to.indexOf(char);
399
+ if (index > -1) {
400
+ const value = to.indexOf(char) + 1;
401
+ decimal += value * Math.pow(26, i);
402
+ }
403
+ }
404
+ return decimal;
405
+ }
package/src/set-ex.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import iterate from "iterare";
2
+
1
3
  export class SetEx<T> extends Set {
2
4
  private _key: keyof T;
3
5
  private _onExist1?: (oldData: T, newData: T) => void | null;
@@ -6,6 +8,7 @@ export class SetEx<T> extends Set {
6
8
  private _onExist2?: (oldData: T, newData: T) => void | null;
7
9
  private _onNotExist2?: (newData: T) => void | null;
8
10
  private _replaceIfExits2: boolean;
11
+ private _map: Map<string, T> = new Map();
9
12
  /**
10
13
  * @param key 识别是否存在的对象的属性名
11
14
  * @param onExist 当存在时作何操作? oldData/newData 哪个将添加到set,由replaceItemWhenExits决定,默认是oldData生效
@@ -52,21 +55,22 @@ export class SetEx<T> extends Set {
52
55
  */
53
56
  override add(value: T): this {
54
57
  let flag = false;
55
- this.forEach((item: T): any => {
56
- if (item[this._key] === value[this._key]) {
57
- flag = true;
58
- if (this._onExist1) {
59
- this._onExist1(item, value);
60
- }
61
- if (this._replaceIfExits1 === true) {
62
- super.delete(item);
63
- flag = false;
64
- }
65
- return false;
58
+ const key = value[this._key] as string;
59
+ const item = this._map.get(key);
60
+ if (item) {
61
+ flag = true;
62
+ if (this._onExist1) {
63
+ this._onExist1(item, value);
66
64
  }
67
- });
65
+ if (this._replaceIfExits1 === true) {
66
+ super.delete(item);
67
+ this._map.delete(key);
68
+ flag = false;
69
+ }
70
+ }
68
71
  if (flag === false) {
69
72
  super.add(value);
73
+ this._map.set(key, value);
70
74
  if (this._onNotExist1) {
71
75
  this._onNotExist1(value);
72
76
  }
@@ -92,24 +96,25 @@ export class SetEx<T> extends Set {
92
96
  */
93
97
  add2(value: T): T {
94
98
  let flag = false;
99
+ const key = value[this._key] as string;
100
+ const item = this._map.get(key);
95
101
  let tmp = value;
96
- this.forEach((item: T): any => {
97
- if (item[this._key] === value[this._key]) {
98
- flag = true;
99
- if (this._onExist2) {
100
- this._onExist2(item, value);
101
- }
102
- if (this._replaceIfExits2 === true) {
103
- super.delete(value);
104
- flag = false;
105
- } else {
106
- tmp = item;
107
- }
108
- return false;
102
+ if (item) {
103
+ flag = true;
104
+ if (this._onExist2) {
105
+ this._onExist2(item, value);
109
106
  }
110
- });
107
+ if (this._replaceIfExits2 === true) {
108
+ super.delete(value);
109
+ this._map.delete(key);
110
+ flag = false;
111
+ } else {
112
+ tmp = item;
113
+ }
114
+ }
111
115
  if (flag === false) {
112
116
  super.add(value);
117
+ this._map.set(key, value);
113
118
  if (this._onNotExist2) {
114
119
  this._onNotExist2(value);
115
120
  }
@@ -131,30 +136,19 @@ export class SetEx<T> extends Set {
131
136
  }
132
137
  /**
133
138
  * 用key找到匹配的第一个对象
134
- * @param {*} value 这是对象的关键属性,而非对象
139
+ * @param {*} key 这是对象的关键属性,而非对象
135
140
  * @returns {(T | null)}
136
141
  */
137
- find(value: T[keyof T]): T | null {
138
- for (const item of this) {
139
- if (item[this._key] === value) {
140
- return item;
141
- }
142
- }
143
- return null;
142
+ find(key: T[keyof T]): T | null {
143
+ return this._map.get(key as string) ?? null;
144
144
  }
145
145
  /**
146
146
  * 用key找到匹配的所有对象
147
- * @param {*} value 这是对象的关键属性,而非对象
147
+ * @param {*} key 这是对象的关键属性,而非对象
148
148
  * @returns {T[]}
149
149
  */
150
- findAll(value: T[keyof T]): T[] {
151
- const res = new Array<T>();
152
- this.forEach((item) => {
153
- if (item[this._key] === value) {
154
- res.push(item);
155
- }
156
- });
157
- return res;
150
+ findAll(key: T[keyof T]): T[] {
151
+ return iterate(key as string[]).map(k => this._map.get(k)).filter(v => v !== undefined).toArray() as T[];
158
152
  }
159
153
  /**
160
154
  *
@@ -191,13 +185,8 @@ export class SetEx<T> extends Set {
191
185
  * @param {*} value 这是对象的关键属性,而非对象
192
186
  * @returns {boolean}
193
187
  */
194
- override has(value: T[keyof T] ): boolean {
195
- for (const item of this) {
196
- if (item[this._key] === value) {
197
- return true;
198
- }
199
- }
200
- return false;
188
+ override has(key: T[keyof T]): boolean {
189
+ return this._map.has(key as string);
201
190
  }
202
191
  /**
203
192
  * 转为数组
@@ -235,15 +224,16 @@ export class SetEx<T> extends Set {
235
224
  /**
236
225
  *
237
226
  * 删除key对应的对象
238
- * @param {*} value 这是对象的关键属性,而非对象
227
+ * @param {*} _key 这是对象的关键属性,而非对象
239
228
  * @returns {boolean}
240
229
  */
241
- override delete(value: T[keyof T]): boolean {
242
- for (const item of this) {
243
- if (item[this._key] === value) {
244
- super.delete(item);
245
- return true;
246
- }
230
+ override delete(_key: T[keyof T]): boolean {
231
+ const key = _key as string;
232
+ const item = this._map.get(key);
233
+ if (item) {
234
+ super.delete(item);
235
+ this._map.delete(key);
236
+ return true;
247
237
  }
248
238
  return false;
249
239
  }
@@ -271,6 +261,7 @@ export class SetEx<T> extends Set {
271
261
  values?: ReadonlyArray<T> | null;
272
262
  }): this {
273
263
  this.clear();
264
+ this._map.clear();
274
265
  if (option.key) { this._key = option.key; }
275
266
  if (option.onExist1) { this._onExist1 = option.onExist1; }
276
267
  if (option.onNotExist1) { this._onNotExist1 = option.onNotExist1; }
@@ -380,4 +371,4 @@ export class SetEx<T> extends Set {
380
371
  set key(key: keyof T) {
381
372
  this._key = key;
382
373
  }
383
- }
374
+ }