@etsoo/shared 1.1.95 → 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/__tests__/DateUtils.ts +6 -0
- package/lib/cjs/ArrayUtils.d.ts +6 -0
- package/lib/cjs/ArrayUtils.js +6 -0
- package/lib/cjs/DateUtils.d.ts +1 -1
- package/lib/cjs/DateUtils.js +3 -2
- package/lib/mjs/ArrayUtils.d.ts +6 -0
- package/lib/mjs/ArrayUtils.js +6 -0
- package/lib/mjs/DateUtils.d.ts +1 -1
- package/lib/mjs/DateUtils.js +3 -2
- package/package.json +3 -3
- package/src/ArrayUtils.ts +24 -0
- package/src/DateUtils.ts +3 -3
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/__tests__/DateUtils.ts
CHANGED
|
@@ -71,6 +71,12 @@ test('Tests for formatForInput', () => {
|
|
|
71
71
|
|
|
72
72
|
const result41 = DateUtils.formatForInput(d, 'datetime-local');
|
|
73
73
|
expect(result41).toBe('2021-06-06T20:08:45');
|
|
74
|
+
|
|
75
|
+
const result5 = DateUtils.formatForInput('');
|
|
76
|
+
expect(result5).toBeUndefined();
|
|
77
|
+
|
|
78
|
+
const result51 = DateUtils.formatForInput(null);
|
|
79
|
+
expect(result51).toBeUndefined();
|
|
74
80
|
});
|
|
75
81
|
|
|
76
82
|
test('Tests for isExpired', () => {
|
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/cjs/DateUtils.d.ts
CHANGED
|
@@ -49,7 +49,7 @@ export declare namespace DateUtils {
|
|
|
49
49
|
* @param date Input date
|
|
50
50
|
* @param hasSecondOrType 'undefined' for date only, 'false' for hour:minute only, 'true' for all, or input field type
|
|
51
51
|
*/
|
|
52
|
-
function formatForInput(date?: Date | string | null, hasSecondOrType?: boolean | string): string;
|
|
52
|
+
function formatForInput(date?: Date | string | null, hasSecondOrType?: boolean | string): string | undefined;
|
|
53
53
|
/**
|
|
54
54
|
* Get month's days
|
|
55
55
|
* @param year Year
|
package/lib/cjs/DateUtils.js
CHANGED
|
@@ -97,11 +97,12 @@ var DateUtils;
|
|
|
97
97
|
* @param hasSecondOrType 'undefined' for date only, 'false' for hour:minute only, 'true' for all, or input field type
|
|
98
98
|
*/
|
|
99
99
|
function formatForInput(date, hasSecondOrType) {
|
|
100
|
+
// Return when null
|
|
101
|
+
if (date == null || date === '')
|
|
102
|
+
return undefined;
|
|
100
103
|
// Parse string as date
|
|
101
104
|
if (typeof date === 'string')
|
|
102
105
|
date = new Date(date);
|
|
103
|
-
// Default is now
|
|
104
|
-
date !== null && date !== void 0 ? date : (date = new Date());
|
|
105
106
|
const hasSecond = typeof hasSecondOrType === 'string'
|
|
106
107
|
? hasSecondOrType === 'date'
|
|
107
108
|
? undefined
|
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/lib/mjs/DateUtils.d.ts
CHANGED
|
@@ -49,7 +49,7 @@ export declare namespace DateUtils {
|
|
|
49
49
|
* @param date Input date
|
|
50
50
|
* @param hasSecondOrType 'undefined' for date only, 'false' for hour:minute only, 'true' for all, or input field type
|
|
51
51
|
*/
|
|
52
|
-
function formatForInput(date?: Date | string | null, hasSecondOrType?: boolean | string): string;
|
|
52
|
+
function formatForInput(date?: Date | string | null, hasSecondOrType?: boolean | string): string | undefined;
|
|
53
53
|
/**
|
|
54
54
|
* Get month's days
|
|
55
55
|
* @param year Year
|
package/lib/mjs/DateUtils.js
CHANGED
|
@@ -94,11 +94,12 @@ export var DateUtils;
|
|
|
94
94
|
* @param hasSecondOrType 'undefined' for date only, 'false' for hour:minute only, 'true' for all, or input field type
|
|
95
95
|
*/
|
|
96
96
|
function formatForInput(date, hasSecondOrType) {
|
|
97
|
+
// Return when null
|
|
98
|
+
if (date == null || date === '')
|
|
99
|
+
return undefined;
|
|
97
100
|
// Parse string as date
|
|
98
101
|
if (typeof date === 'string')
|
|
99
102
|
date = new Date(date);
|
|
100
|
-
// Default is now
|
|
101
|
-
date !== null && date !== void 0 ? date : (date = new Date());
|
|
102
103
|
const hasSecond = typeof hasSecondOrType === 'string'
|
|
103
104
|
? hasSecondOrType === 'date'
|
|
104
105
|
? undefined
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/shared",
|
|
3
|
-
"version": "1.1.
|
|
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",
|
|
@@ -58,8 +58,8 @@
|
|
|
58
58
|
"@types/lodash.isequal": "^4.5.6",
|
|
59
59
|
"jest": "^29.5.0",
|
|
60
60
|
"jest-environment-jsdom": "^29.5.0",
|
|
61
|
-
"ts-jest": "^29.0
|
|
62
|
-
"typescript": "^5.0.
|
|
61
|
+
"ts-jest": "^29.1.0",
|
|
62
|
+
"typescript": "^5.0.3"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"lodash.isequal": "^4.5.0"
|
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
|
*/
|
package/src/DateUtils.ts
CHANGED
|
@@ -153,12 +153,12 @@ export namespace DateUtils {
|
|
|
153
153
|
date?: Date | string | null,
|
|
154
154
|
hasSecondOrType?: boolean | string
|
|
155
155
|
) {
|
|
156
|
+
// Return when null
|
|
157
|
+
if (date == null || date === '') return undefined;
|
|
158
|
+
|
|
156
159
|
// Parse string as date
|
|
157
160
|
if (typeof date === 'string') date = new Date(date);
|
|
158
161
|
|
|
159
|
-
// Default is now
|
|
160
|
-
date ??= new Date();
|
|
161
|
-
|
|
162
162
|
const hasSecond =
|
|
163
163
|
typeof hasSecondOrType === 'string'
|
|
164
164
|
? hasSecondOrType === 'date'
|