tools-min-ns 1.13.0 → 1.14.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/README.md CHANGED
@@ -5,6 +5,8 @@ cnpm i tools-min-ns --save
5
5
  ```
6
6
 
7
7
  # updates
8
+ ## v1.14.0
9
+ > ArrayUtil 新增三个分组方法
8
10
  ## v1.13.0
9
11
  > RequestUtil
10
12
  - handleRequests 中onUploadProgress新增两个参数nowResponse: T; endResponses: T[]
@@ -135,5 +135,43 @@ console.log(universalSort(objArray, obj => obj.name.length)); // [{ name: 'Jim',
135
135
  console.log(universalSort([5, 3, 8, 1, 2], null, 'desc')); // [8, 5, 3, 2, 1]
136
136
  */
137
137
  function universalSort<T>(array: T[], key?: keyof T | ((item: T) => any), order?: 'asc' | 'desc'): T[];
138
+ /**
139
+ * 根据对象的属性值进行分组
140
+ * @param array 对象数组
141
+ * @param key 对象的key
142
+ * @example
143
+ const data = [
144
+ { name: 'Alice', age: 21 },
145
+ { name: 'Bob', age: 25 },
146
+ { name: 'Charlie', age: 21 },
147
+ { name: 'David', age: 25 }
148
+ ];
149
+
150
+ const groupedByAge = groupBy(data, 'age');
151
+ console.log(groupedByAge); // {21: Array(2), 25: Array(2)}
152
+ */
153
+ function groupBy<T>(array: T[], key: keyof T): Record<keyof T, T[]>;
154
+ /**
155
+ * 按条件分组
156
+ * @param array 对象数组
157
+ * @param condition filter
158
+ * @example
159
+ const numbers: number[] = [1, 2, 3, 4, 5, 6];
160
+
161
+ const groupedByOddEven = groupByCondition(numbers, num => num % 2 === 0);
162
+ console.log(groupedByOddEven); // {false: Array(3), true: Array(3)}
163
+ */
164
+ function groupByCondition<T>(array: T[], condition: (value: T) => boolean): Record<'true' | 'false', T[]>;
165
+ /**
166
+ * 按固定大小分组
167
+ * @param array 对象数组
168
+ * @param size 数量
169
+ * @example
170
+ const data: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
171
+
172
+ const chunkedArray = chunkArray(data, 3);
173
+ console.log(chunkedArray);// [Array(3), Array(3), Array(3)]
174
+ */
175
+ function chunkArray<T>(array: T[], size: number): T[][];
138
176
  }
139
177
  export default ArrayUtil;
@@ -309,5 +309,67 @@ var ArrayUtil;
309
309
  return array.sort(compareFunction);
310
310
  }
311
311
  ArrayUtil.universalSort = universalSort;
312
+ /**
313
+ * 根据对象的属性值进行分组
314
+ * @param array 对象数组
315
+ * @param key 对象的key
316
+ * @example
317
+ const data = [
318
+ { name: 'Alice', age: 21 },
319
+ { name: 'Bob', age: 25 },
320
+ { name: 'Charlie', age: 21 },
321
+ { name: 'David', age: 25 }
322
+ ];
323
+ const groupedByAge = groupBy(data, 'age');
324
+ console.log(groupedByAge); // {21: Array(2), 25: Array(2)}
325
+ */
326
+ function groupBy(array, key) {
327
+ return array.reduce(function (result, currentValue) {
328
+ var groupKey = currentValue[key];
329
+ if (!result[groupKey]) {
330
+ result[groupKey] = [];
331
+ }
332
+ result[groupKey].push(currentValue);
333
+ return result;
334
+ }, {});
335
+ }
336
+ ArrayUtil.groupBy = groupBy;
337
+ /**
338
+ * 按条件分组
339
+ * @param array 对象数组
340
+ * @param condition filter
341
+ * @example
342
+ const numbers: number[] = [1, 2, 3, 4, 5, 6];
343
+ const groupedByOddEven = groupByCondition(numbers, num => num % 2 === 0);
344
+ console.log(groupedByOddEven); // {false: Array(3), true: Array(3)}
345
+ */
346
+ function groupByCondition(array, condition) {
347
+ return array.reduce(function (result, currentValue) {
348
+ var groupKey = condition(currentValue) ? 'true' : 'false';
349
+ if (!result[groupKey]) {
350
+ result[groupKey] = [];
351
+ }
352
+ result[groupKey].push(currentValue);
353
+ return result;
354
+ }, {});
355
+ }
356
+ ArrayUtil.groupByCondition = groupByCondition;
357
+ /**
358
+ * 按固定大小分组
359
+ * @param array 对象数组
360
+ * @param size 数量
361
+ * @example
362
+ const data: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
363
+ const chunkedArray = chunkArray(data, 3);
364
+ console.log(chunkedArray);// [Array(3), Array(3), Array(3)]
365
+ */
366
+ function chunkArray(array, size) {
367
+ var result = [];
368
+ for (var i = 0; i < array.length; i += size) {
369
+ result.push(array.slice(i, i + size));
370
+ }
371
+ return result;
372
+ }
373
+ ArrayUtil.chunkArray = chunkArray;
312
374
  })(ArrayUtil || (ArrayUtil = {}));
313
375
  export default ArrayUtil;
@@ -135,5 +135,43 @@ console.log(universalSort(objArray, obj => obj.name.length)); // [{ name: 'Jim',
135
135
  console.log(universalSort([5, 3, 8, 1, 2], null, 'desc')); // [8, 5, 3, 2, 1]
136
136
  */
137
137
  function universalSort<T>(array: T[], key?: keyof T | ((item: T) => any), order?: 'asc' | 'desc'): T[];
138
+ /**
139
+ * 根据对象的属性值进行分组
140
+ * @param array 对象数组
141
+ * @param key 对象的key
142
+ * @example
143
+ const data = [
144
+ { name: 'Alice', age: 21 },
145
+ { name: 'Bob', age: 25 },
146
+ { name: 'Charlie', age: 21 },
147
+ { name: 'David', age: 25 }
148
+ ];
149
+
150
+ const groupedByAge = groupBy(data, 'age');
151
+ console.log(groupedByAge); // {21: Array(2), 25: Array(2)}
152
+ */
153
+ function groupBy<T>(array: T[], key: keyof T): Record<keyof T, T[]>;
154
+ /**
155
+ * 按条件分组
156
+ * @param array 对象数组
157
+ * @param condition filter
158
+ * @example
159
+ const numbers: number[] = [1, 2, 3, 4, 5, 6];
160
+
161
+ const groupedByOddEven = groupByCondition(numbers, num => num % 2 === 0);
162
+ console.log(groupedByOddEven); // {false: Array(3), true: Array(3)}
163
+ */
164
+ function groupByCondition<T>(array: T[], condition: (value: T) => boolean): Record<'true' | 'false', T[]>;
165
+ /**
166
+ * 按固定大小分组
167
+ * @param array 对象数组
168
+ * @param size 数量
169
+ * @example
170
+ const data: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
171
+
172
+ const chunkedArray = chunkArray(data, 3);
173
+ console.log(chunkedArray);// [Array(3), Array(3), Array(3)]
174
+ */
175
+ function chunkArray<T>(array: T[], size: number): T[][];
138
176
  }
139
177
  export default ArrayUtil;
@@ -319,5 +319,67 @@ var ArrayUtil;
319
319
  return array.sort(compareFunction);
320
320
  }
321
321
  ArrayUtil.universalSort = universalSort;
322
+ /**
323
+ * 根据对象的属性值进行分组
324
+ * @param array 对象数组
325
+ * @param key 对象的key
326
+ * @example
327
+ const data = [
328
+ { name: 'Alice', age: 21 },
329
+ { name: 'Bob', age: 25 },
330
+ { name: 'Charlie', age: 21 },
331
+ { name: 'David', age: 25 }
332
+ ];
333
+ const groupedByAge = groupBy(data, 'age');
334
+ console.log(groupedByAge); // {21: Array(2), 25: Array(2)}
335
+ */
336
+ function groupBy(array, key) {
337
+ return array.reduce(function (result, currentValue) {
338
+ var groupKey = currentValue[key];
339
+ if (!result[groupKey]) {
340
+ result[groupKey] = [];
341
+ }
342
+ result[groupKey].push(currentValue);
343
+ return result;
344
+ }, {});
345
+ }
346
+ ArrayUtil.groupBy = groupBy;
347
+ /**
348
+ * 按条件分组
349
+ * @param array 对象数组
350
+ * @param condition filter
351
+ * @example
352
+ const numbers: number[] = [1, 2, 3, 4, 5, 6];
353
+ const groupedByOddEven = groupByCondition(numbers, num => num % 2 === 0);
354
+ console.log(groupedByOddEven); // {false: Array(3), true: Array(3)}
355
+ */
356
+ function groupByCondition(array, condition) {
357
+ return array.reduce(function (result, currentValue) {
358
+ var groupKey = condition(currentValue) ? 'true' : 'false';
359
+ if (!result[groupKey]) {
360
+ result[groupKey] = [];
361
+ }
362
+ result[groupKey].push(currentValue);
363
+ return result;
364
+ }, {});
365
+ }
366
+ ArrayUtil.groupByCondition = groupByCondition;
367
+ /**
368
+ * 按固定大小分组
369
+ * @param array 对象数组
370
+ * @param size 数量
371
+ * @example
372
+ const data: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
373
+ const chunkedArray = chunkArray(data, 3);
374
+ console.log(chunkedArray);// [Array(3), Array(3), Array(3)]
375
+ */
376
+ function chunkArray(array, size) {
377
+ var result = [];
378
+ for (var i = 0; i < array.length; i += size) {
379
+ result.push(array.slice(i, i + size));
380
+ }
381
+ return result;
382
+ }
383
+ ArrayUtil.chunkArray = chunkArray;
322
384
  })(ArrayUtil || (ArrayUtil = {}));
323
385
  exports.default = ArrayUtil;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tools-min-ns",
3
3
  "description": "工具包适用于前端以及node",
4
- "version": "1.13.0",
4
+ "version": "1.14.0",
5
5
  "main": "lib/index.js",
6
6
  "license": "MIT",
7
7
  "author": "nanshen",