@etsoo/shared 1.1.96 → 1.1.97

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
@@ -102,6 +102,7 @@ Array related utilities
102
102
  |Name|Description|
103
103
  |---:|---|
104
104
  |differences|Array 1 items do not exist in Array 2 or reverse match|
105
+ |sum|Sum number items or number item properties|
105
106
  |toUnique|Make all items are unique|
106
107
 
107
108
  ## DataTypes
@@ -20,3 +20,16 @@ test('Tests for differences', () => {
20
20
  expect(ArrayUtils.differences(a1, a2)).toEqual(['b', 'e']);
21
21
  expect(ArrayUtils.differences(a1, a2, true)).toEqual(['b', 'e', 'd']);
22
22
  });
23
+
24
+ test('Tests for sum numbers', () => {
25
+ const items = [12, 8, 22];
26
+ expect(items.sum()).toBe(42);
27
+ });
28
+
29
+ test('Tests for sum fields', () => {
30
+ const items = [
31
+ { id: 1, label: 'a', amount: 3.14 },
32
+ { id: 2, label: 'b', amount: 4.54 }
33
+ ];
34
+ expect(items.sum('amount')).toBe(7.68);
35
+ });
@@ -1,3 +1,4 @@
1
+ import { DataTypes } from './DataTypes';
1
2
  declare global {
2
3
  interface Array<T> {
3
4
  /**
@@ -6,6 +7,11 @@ declare global {
6
7
  * @param round A round for both matches
7
8
  */
8
9
  different(target: Array<T>, round?: boolean): Array<T>;
10
+ /**
11
+ * Sum number items or number item properties
12
+ * @param field Object field to calculate
13
+ */
14
+ sum(...field: T extends number ? [undefined?] : T extends object ? [DataTypes.Keys<T, number>] : [never]): number;
9
15
  /**
10
16
  * Make all items are unique
11
17
  * @param this Input array
@@ -19,6 +19,12 @@ Array.prototype.toUnique = function () {
19
19
  });
20
20
  return newArray;
21
21
  };
22
+ Array.prototype.sum = function (field) {
23
+ if (field == null) {
24
+ return this.reduce((total, num) => total + num, 0);
25
+ }
26
+ return this.reduce((total, item) => total + item[field], 0);
27
+ };
22
28
  /**
23
29
  * Array Utilities
24
30
  */
@@ -1,3 +1,4 @@
1
+ import { DataTypes } from './DataTypes';
1
2
  declare global {
2
3
  interface Array<T> {
3
4
  /**
@@ -6,6 +7,11 @@ declare global {
6
7
  * @param round A round for both matches
7
8
  */
8
9
  different(target: Array<T>, round?: boolean): Array<T>;
10
+ /**
11
+ * Sum number items or number item properties
12
+ * @param field Object field to calculate
13
+ */
14
+ sum(...field: T extends number ? [undefined?] : T extends object ? [DataTypes.Keys<T, number>] : [never]): number;
9
15
  /**
10
16
  * Make all items are unique
11
17
  * @param this Input array
@@ -13,6 +13,12 @@ Array.prototype.toUnique = function () {
13
13
  });
14
14
  return newArray;
15
15
  };
16
+ Array.prototype.sum = function (field) {
17
+ if (field == null) {
18
+ return this.reduce((total, num) => total + num, 0);
19
+ }
20
+ return this.reduce((total, item) => total + item[field], 0);
21
+ };
16
22
  /**
17
23
  * Array Utilities
18
24
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.96",
3
+ "version": "1.1.97",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
package/src/ArrayUtils.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import isEqual from 'lodash.isequal';
2
+ import { DataTypes } from './DataTypes';
2
3
 
3
4
  declare global {
4
5
  interface Array<T> {
@@ -9,6 +10,18 @@ declare global {
9
10
  */
10
11
  different(target: Array<T>, round?: boolean): Array<T>;
11
12
 
13
+ /**
14
+ * Sum number items or number item properties
15
+ * @param field Object field to calculate
16
+ */
17
+ sum(
18
+ ...field: T extends number
19
+ ? [undefined?]
20
+ : T extends object
21
+ ? [DataTypes.Keys<T, number>]
22
+ : [never]
23
+ ): number;
24
+
12
25
  /**
13
26
  * Make all items are unique
14
27
  * @param this Input array
@@ -37,6 +50,17 @@ Array.prototype.toUnique = function <T>(this: Array<T>) {
37
50
  return newArray;
38
51
  };
39
52
 
53
+ Array.prototype.sum = function <T>(
54
+ this: Array<T>,
55
+ field: T extends object ? DataTypes.Keys<T, number> : undefined
56
+ ) {
57
+ if (field == null) {
58
+ return this.reduce((total, num) => total + (num as number), 0);
59
+ }
60
+
61
+ return this.reduce((total, item) => total + (item[field] as number), 0);
62
+ };
63
+
40
64
  /**
41
65
  * Array Utilities
42
66
  */