@etsoo/shared 1.1.49 → 1.1.52

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
@@ -142,6 +142,7 @@ Dates related utilities
142
142
  |Methods||
143
143
  |getDays|Get month's days|
144
144
  |forma|Format dates|
145
+ |formatForInput|Format to 'yyyy-MM-dd' or 'yyyy-MM-ddThh:mm:ss, especially used for date input min/max property|
145
146
  |jsonParser|JSON parser|
146
147
  |parse|Parse string to date|
147
148
  |substract|Date extended method, substract a date|
@@ -1,4 +1,4 @@
1
- import { DataTypes } from '../src/DataTypes';
1
+ import { DataTypes, IdDefaultType, LabelDefaultType } from '../src/DataTypes';
2
2
 
3
3
  test('Tests for DI', () => {
4
4
  const item: DataTypes.DIS<'id', number> & DataTypes.DIS<'label', string> = {
@@ -132,3 +132,25 @@ test('Tests for isSimpleType', () => {
132
132
  expect(DataTypes.isSimpleType(['a', 'b', 'c'])).toBeTruthy();
133
133
  expect(DataTypes.isSimpleType({})).toBeFalsy();
134
134
  });
135
+
136
+ test('Tests for IdDefaultType', () => {
137
+ const test = <T extends object, F extends keyof T = IdDefaultType<T>>(
138
+ obj: T,
139
+ field?: F
140
+ ) => {
141
+ const f = field ?? ('id' as F);
142
+ return obj[f];
143
+ };
144
+
145
+ type D = { label: string; name: string; id: number };
146
+ const data: D = {
147
+ label: 'label',
148
+ name: 'name',
149
+ id: 1
150
+ };
151
+ const v = test<D>(data);
152
+ expect(typeof v).toBe('number');
153
+
154
+ const v1 = test<D, LabelDefaultType<D>>(data, 'label');
155
+ expect(v1).toBe('label');
156
+ });
@@ -56,8 +56,15 @@ test('Tests for formatForInput', () => {
56
56
  const result1 = DateUtils.formatForInput('2021/7/17');
57
57
  expect(result1).toBe('2021-07-17');
58
58
 
59
- const result2 = DateUtils.formatForInput(new Date(2021, 5, 6));
59
+ const d = new Date(2021, 5, 6, 20, 18, 45);
60
+ const result2 = DateUtils.formatForInput(d);
60
61
  expect(result2).toBe('2021-06-06');
62
+
63
+ const result3 = DateUtils.formatForInput(d, false);
64
+ expect(result3).toBe('2021-06-06T20:18');
65
+
66
+ const result4 = DateUtils.formatForInput(d, true);
67
+ expect(result4).toBe('2021-06-06T20:18:45');
61
68
  });
62
69
 
63
70
  test('Tests for substract', () => {
@@ -180,7 +180,7 @@ export declare namespace DataTypes {
180
180
  /**
181
181
  * Get specific type keys
182
182
  */
183
- type Keys<T, R = string | number> = {
183
+ type Keys<T extends object, R = string | number> = {
184
184
  [k in keyof T]: T[k] extends R ? k : never;
185
185
  }[keyof T];
186
186
  /**
@@ -333,3 +333,15 @@ export declare type ListType = DataTypes.IdLabelItem<number>;
333
333
  * List item with string id type
334
334
  */
335
335
  export declare type ListType1 = DataTypes.IdLabelItem<string>;
336
+ /**
337
+ * Id default type
338
+ */
339
+ export declare type IdDefaultType<T extends object> = T extends {
340
+ id: number | string;
341
+ } ? DataTypes.Keys<T> & 'id' : DataTypes.Keys<T>;
342
+ /**
343
+ * Label default type
344
+ */
345
+ export declare type LabelDefaultType<T extends object> = T extends {
346
+ label: string;
347
+ } ? DataTypes.Keys<T, string> & 'label' : DataTypes.Keys<T, string>;
@@ -45,10 +45,11 @@ export declare namespace DateUtils {
45
45
  */
46
46
  function format(input?: Date | string, locale?: string | string[], options?: FormatOptions, timeZone?: string): string | undefined;
47
47
  /**
48
- * Format to 'yyyy-MM-dd', especially used for date input min/max property
48
+ * Format to 'yyyy-MM-dd' or 'yyyy-MM-ddThh:mm:ss, especially used for date input min/max property
49
49
  * @param date Input date
50
+ * @param hasSecond 'undefined' for date only, 'false' for hour:minute only
50
51
  */
51
- function formatForInput(date?: Date | string | null): string;
52
+ function formatForInput(date?: Date | string | null, hasSecond?: boolean): string;
52
53
  /**
53
54
  * Get month's days
54
55
  * @param year Year
@@ -92,10 +92,11 @@ var DateUtils;
92
92
  }
93
93
  DateUtils.format = format;
94
94
  /**
95
- * Format to 'yyyy-MM-dd', especially used for date input min/max property
95
+ * Format to 'yyyy-MM-dd' or 'yyyy-MM-ddThh:mm:ss, especially used for date input min/max property
96
96
  * @param date Input date
97
+ * @param hasSecond 'undefined' for date only, 'false' for hour:minute only
97
98
  */
98
- function formatForInput(date) {
99
+ function formatForInput(date, hasSecond) {
99
100
  // Parse string as date
100
101
  if (typeof date === 'string')
101
102
  date = new Date(date);
@@ -107,8 +108,14 @@ var DateUtils;
107
108
  (date.getMonth() + 1).toString().padStart(2, '0'),
108
109
  date.getDate().toString().padStart(2, '0')
109
110
  ];
110
- // Return
111
- return parts.join('-');
111
+ // Date
112
+ const d = parts.join('-');
113
+ if (hasSecond == null)
114
+ return d;
115
+ const hm = [date.getHours(), date.getMinutes()];
116
+ if (hasSecond)
117
+ hm.push(date.getSeconds());
118
+ return `${d}T${hm.join(':')}`;
112
119
  }
113
120
  DateUtils.formatForInput = formatForInput;
114
121
  /**
@@ -180,7 +180,7 @@ export declare namespace DataTypes {
180
180
  /**
181
181
  * Get specific type keys
182
182
  */
183
- type Keys<T, R = string | number> = {
183
+ type Keys<T extends object, R = string | number> = {
184
184
  [k in keyof T]: T[k] extends R ? k : never;
185
185
  }[keyof T];
186
186
  /**
@@ -333,3 +333,15 @@ export declare type ListType = DataTypes.IdLabelItem<number>;
333
333
  * List item with string id type
334
334
  */
335
335
  export declare type ListType1 = DataTypes.IdLabelItem<string>;
336
+ /**
337
+ * Id default type
338
+ */
339
+ export declare type IdDefaultType<T extends object> = T extends {
340
+ id: number | string;
341
+ } ? DataTypes.Keys<T> & 'id' : DataTypes.Keys<T>;
342
+ /**
343
+ * Label default type
344
+ */
345
+ export declare type LabelDefaultType<T extends object> = T extends {
346
+ label: string;
347
+ } ? DataTypes.Keys<T, string> & 'label' : DataTypes.Keys<T, string>;
@@ -45,10 +45,11 @@ export declare namespace DateUtils {
45
45
  */
46
46
  function format(input?: Date | string, locale?: string | string[], options?: FormatOptions, timeZone?: string): string | undefined;
47
47
  /**
48
- * Format to 'yyyy-MM-dd', especially used for date input min/max property
48
+ * Format to 'yyyy-MM-dd' or 'yyyy-MM-ddThh:mm:ss, especially used for date input min/max property
49
49
  * @param date Input date
50
+ * @param hasSecond 'undefined' for date only, 'false' for hour:minute only
50
51
  */
51
- function formatForInput(date?: Date | string | null): string;
52
+ function formatForInput(date?: Date | string | null, hasSecond?: boolean): string;
52
53
  /**
53
54
  * Get month's days
54
55
  * @param year Year
@@ -89,10 +89,11 @@ export var DateUtils;
89
89
  }
90
90
  DateUtils.format = format;
91
91
  /**
92
- * Format to 'yyyy-MM-dd', especially used for date input min/max property
92
+ * Format to 'yyyy-MM-dd' or 'yyyy-MM-ddThh:mm:ss, especially used for date input min/max property
93
93
  * @param date Input date
94
+ * @param hasSecond 'undefined' for date only, 'false' for hour:minute only
94
95
  */
95
- function formatForInput(date) {
96
+ function formatForInput(date, hasSecond) {
96
97
  // Parse string as date
97
98
  if (typeof date === 'string')
98
99
  date = new Date(date);
@@ -104,8 +105,14 @@ export var DateUtils;
104
105
  (date.getMonth() + 1).toString().padStart(2, '0'),
105
106
  date.getDate().toString().padStart(2, '0')
106
107
  ];
107
- // Return
108
- return parts.join('-');
108
+ // Date
109
+ const d = parts.join('-');
110
+ if (hasSecond == null)
111
+ return d;
112
+ const hm = [date.getHours(), date.getMinutes()];
113
+ if (hasSecond)
114
+ hm.push(date.getSeconds());
115
+ return `${d}T${hm.join(':')}`;
109
116
  }
110
117
  DateUtils.formatForInput = formatForInput;
111
118
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.49",
3
+ "version": "1.1.52",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -53,12 +53,11 @@
53
53
  "url": "https://github.com/ETSOO/Shared/issues"
54
54
  },
55
55
  "homepage": "https://github.com/ETSOO/Shared#readme",
56
- "dependencies": {},
57
56
  "devDependencies": {
58
57
  "@types/jest": "^28.1.8",
59
- "@typescript-eslint/eslint-plugin": "^5.35.1",
60
- "@typescript-eslint/parser": "^5.35.1",
61
- "eslint": "^8.22.0",
58
+ "@typescript-eslint/eslint-plugin": "^5.36.1",
59
+ "@typescript-eslint/parser": "^5.36.1",
60
+ "eslint": "^8.23.0",
62
61
  "eslint-config-airbnb-base": "^15.0.0",
63
62
  "eslint-plugin-import": "^2.26.0",
64
63
  "jest": "^28.1.3",
package/src/DataTypes.ts CHANGED
@@ -231,7 +231,7 @@ export namespace DataTypes {
231
231
  /**
232
232
  * Get specific type keys
233
233
  */
234
- export type Keys<T, R = string | number> = {
234
+ export type Keys<T extends object, R = string | number> = {
235
235
  [k in keyof T]: T[k] extends R ? k : never;
236
236
  }[keyof T];
237
237
 
@@ -656,3 +656,17 @@ export type ListType = DataTypes.IdLabelItem<number>;
656
656
  * List item with string id type
657
657
  */
658
658
  export type ListType1 = DataTypes.IdLabelItem<string>;
659
+
660
+ /**
661
+ * Id default type
662
+ */
663
+ export type IdDefaultType<T extends object> = T extends { id: number | string }
664
+ ? DataTypes.Keys<T> & 'id'
665
+ : DataTypes.Keys<T>;
666
+
667
+ /**
668
+ * Label default type
669
+ */
670
+ export type LabelDefaultType<T extends object> = T extends { label: string }
671
+ ? DataTypes.Keys<T, string> & 'label'
672
+ : DataTypes.Keys<T, string>;
package/src/DateUtils.ts CHANGED
@@ -145,10 +145,14 @@ export namespace DateUtils {
145
145
  }
146
146
 
147
147
  /**
148
- * Format to 'yyyy-MM-dd', especially used for date input min/max property
148
+ * Format to 'yyyy-MM-dd' or 'yyyy-MM-ddThh:mm:ss, especially used for date input min/max property
149
149
  * @param date Input date
150
+ * @param hasSecond 'undefined' for date only, 'false' for hour:minute only
150
151
  */
151
- export function formatForInput(date?: Date | string | null) {
152
+ export function formatForInput(
153
+ date?: Date | string | null,
154
+ hasSecond?: boolean
155
+ ) {
152
156
  // Parse string as date
153
157
  if (typeof date === 'string') date = new Date(date);
154
158
 
@@ -162,8 +166,13 @@ export namespace DateUtils {
162
166
  date.getDate().toString().padStart(2, '0')
163
167
  ];
164
168
 
165
- // Return
166
- return parts.join('-');
169
+ // Date
170
+ const d = parts.join('-');
171
+ if (hasSecond == null) return d;
172
+
173
+ const hm = [date.getHours(), date.getMinutes()];
174
+ if (hasSecond) hm.push(date.getSeconds());
175
+ return `${d}T${hm.join(':')}`;
167
176
  }
168
177
 
169
178
  /**