@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 +1 -0
- package/__tests__/ArrayUtils.ts +13 -0
- package/lib/cjs/ArrayUtils.d.ts +6 -0
- package/lib/cjs/ArrayUtils.js +6 -0
- package/lib/mjs/ArrayUtils.d.ts +6 -0
- package/lib/mjs/ArrayUtils.js +6 -0
- package/package.json +1 -1
- package/src/ArrayUtils.ts +24 -0
package/README.md
CHANGED
package/__tests__/ArrayUtils.ts
CHANGED
|
@@ -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
|
+
});
|
package/lib/cjs/ArrayUtils.d.ts
CHANGED
|
@@ -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
|
package/lib/cjs/ArrayUtils.js
CHANGED
|
@@ -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
|
*/
|
package/lib/mjs/ArrayUtils.d.ts
CHANGED
|
@@ -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
|
package/lib/mjs/ArrayUtils.js
CHANGED
|
@@ -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
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
|
*/
|