zmdms-utils 0.0.25 → 0.0.27
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/dist/es/common.d.ts +5 -1
- package/dist/es/parseTime.d.ts +5 -1
- package/dist/es/parseTime.js +19 -1
- package/dist/es/src/parseTimeTest.js +34 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/package.json +1 -1
- package/src/index.ts +2 -1
- package/src/parseTime.ts +25 -1
- package/src/parseTimeTest.js +33 -0
package/dist/es/common.d.ts
CHANGED
|
@@ -63,5 +63,9 @@ declare function isImageExtension(extension: string): boolean;
|
|
|
63
63
|
* @returns
|
|
64
64
|
*/
|
|
65
65
|
declare function isNonEmpty(number: any): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* 遍历一个对象,将值是数组的项,用逗号拼接起来
|
|
68
|
+
*/
|
|
69
|
+
declare function parseJoin(params?: any): any;
|
|
66
70
|
|
|
67
|
-
export { addThousedSeparator, copyToClipboard, dangerouslyXss, delay, emitter, getFileExtension, isImageExtension, isNonEmpty, parseQueryParams, specialString, unescapeString };
|
|
71
|
+
export { addThousedSeparator, copyToClipboard, dangerouslyXss, delay, emitter, getFileExtension, isImageExtension, isNonEmpty, parseJoin, parseQueryParams, specialString, unescapeString };
|
package/dist/es/parseTime.d.ts
CHANGED
|
@@ -21,5 +21,9 @@ interface IConfig {
|
|
|
21
21
|
* { key1: "20xx-xx-xx", key1: "20xx-xx-xx" } => { mergeKey: [Dayjs, Dayjs] }
|
|
22
22
|
*/
|
|
23
23
|
declare function parseTime(config: IConfig): any;
|
|
24
|
+
/**
|
|
25
|
+
* 计算年龄
|
|
26
|
+
*/
|
|
27
|
+
declare function calculateAge(birthday: any): number;
|
|
24
28
|
|
|
25
|
-
export { parseTime
|
|
29
|
+
export { calculateAge, parseTime };
|
package/dist/es/parseTime.js
CHANGED
|
@@ -167,5 +167,23 @@ function parseByResult(option) {
|
|
|
167
167
|
}
|
|
168
168
|
};
|
|
169
169
|
}
|
|
170
|
+
/**
|
|
171
|
+
* 计算年龄
|
|
172
|
+
*/
|
|
173
|
+
function calculateAge(birthday) {
|
|
174
|
+
if (!birthday) {
|
|
175
|
+
console.error("请传入一个生日信息");
|
|
176
|
+
return -1;
|
|
177
|
+
}
|
|
178
|
+
var birthDate = dayjs(birthday);
|
|
179
|
+
var today = dayjs();
|
|
180
|
+
var age = today.year() - birthDate.year();
|
|
181
|
+
var monthDifference = today.month() - birthDate.month();
|
|
182
|
+
if (monthDifference < 0 ||
|
|
183
|
+
(monthDifference === 0 && today.date() < birthDate.date())) {
|
|
184
|
+
age -= 1;
|
|
185
|
+
}
|
|
186
|
+
return age;
|
|
187
|
+
}
|
|
170
188
|
|
|
171
|
-
export { parseTime
|
|
189
|
+
export { calculateAge, parseTime };
|
|
@@ -252,4 +252,37 @@ console.log(
|
|
|
252
252
|
toKeys: [["time1", "time2"], "timea"],
|
|
253
253
|
format: ["YYYY-MM-DD", "YYYY-MM-DD HH:mm:ss"],
|
|
254
254
|
})
|
|
255
|
-
);
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
function calculateAge(birthday) {
|
|
258
|
+
if (!birthday) {
|
|
259
|
+
console.error("请传入一个生日信息");
|
|
260
|
+
return -1;
|
|
261
|
+
}
|
|
262
|
+
const birthDate = dayjs(birthday);
|
|
263
|
+
const today = dayjs();
|
|
264
|
+
|
|
265
|
+
let age = today.year() - birthDate.year();
|
|
266
|
+
const monthDifference = today.month() - birthDate.month();
|
|
267
|
+
|
|
268
|
+
if (
|
|
269
|
+
monthDifference < 0 ||
|
|
270
|
+
(monthDifference === 0 && today.date() < birthDate.date())
|
|
271
|
+
) {
|
|
272
|
+
age -= 1;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return age;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
console.log(calculateAge()); // -1
|
|
279
|
+
console.log(calculateAge("1994-04-02")); // 29
|
|
280
|
+
console.log(calculateAge("1994-10-12")); // 28
|
|
281
|
+
console.log(calculateAge("2023-10-11")); // 0
|
|
282
|
+
console.log(calculateAge("2023-10-12")); // -1
|
|
283
|
+
console.log(calculateAge("2023-10-10")); // 0
|
|
284
|
+
console.log(calculateAge("2022-10-11")); // 1
|
|
285
|
+
console.log(calculateAge("2022-10-10")); // 1
|
|
286
|
+
console.log(calculateAge("2022-10-12")); // 0
|
|
287
|
+
console.log(calculateAge(dayjs()));
|
|
288
|
+
console.log(calculateAge(dayjs("1994-04-02")));
|
package/dist/index.d.ts
CHANGED
|
@@ -5,10 +5,10 @@ export { getToken, removeToken, setToken } from './es/auth.js';
|
|
|
5
5
|
export { AxiosRequest, IOptions, IOtherOptions } from './es/request.js';
|
|
6
6
|
export { getBaseUrl } from './es/baseUrl.js';
|
|
7
7
|
export { Crypto, md5 } from './es/crypto.js';
|
|
8
|
-
export { addThousedSeparator, copyToClipboard, dangerouslyXss, delay, emitter, getFileExtension, isImageExtension, isNonEmpty, parseQueryParams, specialString, unescapeString } from './es/common.js';
|
|
8
|
+
export { addThousedSeparator, copyToClipboard, dangerouslyXss, delay, emitter, getFileExtension, isImageExtension, isNonEmpty, parseJoin, parseQueryParams, specialString, unescapeString } from './es/common.js';
|
|
9
9
|
export { divide, exactRound, formatUnit, minus, plus, times } from './es/math.js';
|
|
10
10
|
export { validatePassword } from './es/passwordValidate.js';
|
|
11
11
|
export { emailValidate, idCardValidate, morePhoneValidate, phoneValidate, strLenValidate } from './es/validate.js';
|
|
12
12
|
export { batchDownloadFiles, createDeleteFileLink, createDownloadLink, createOriginalLink, createThumbnailLink, createUploadFileLink, downloadFile, exportBolb, previewFile } from './es/file.js';
|
|
13
|
-
export {
|
|
13
|
+
export { calculateAge, parseTime } from './es/parseTime.js';
|
|
14
14
|
export { EMITTER_CCP_KEY, EMITTER_PCC_KEY, EMITTER_TYPE } from './es/constants.js';
|
package/dist/index.js
CHANGED
|
@@ -5,10 +5,10 @@ export { getToken, removeToken, setToken } from './es/auth.js';
|
|
|
5
5
|
export { AxiosRequest } from './es/request.js';
|
|
6
6
|
export { getBaseUrl } from './es/baseUrl.js';
|
|
7
7
|
export { Crypto, md5 } from './es/crypto.js';
|
|
8
|
-
export { addThousedSeparator, copyToClipboard, dangerouslyXss, delay, emitter, getFileExtension, isImageExtension, isNonEmpty, parseQueryParams, specialString, unescapeString } from './es/common.js';
|
|
8
|
+
export { addThousedSeparator, copyToClipboard, dangerouslyXss, delay, emitter, getFileExtension, isImageExtension, isNonEmpty, parseJoin, parseQueryParams, specialString, unescapeString } from './es/common.js';
|
|
9
9
|
export { divide, exactRound, formatUnit, minus, plus, times } from './es/math.js';
|
|
10
10
|
export { validatePassword } from './es/passwordValidate.js';
|
|
11
11
|
export { emailValidate, idCardValidate, morePhoneValidate, phoneValidate, strLenValidate } from './es/validate.js';
|
|
12
12
|
export { batchDownloadFiles, createDeleteFileLink, createDownloadLink, createOriginalLink, createThumbnailLink, createUploadFileLink, downloadFile, exportBolb, previewFile } from './es/file.js';
|
|
13
|
-
export {
|
|
13
|
+
export { calculateAge, parseTime } from './es/parseTime.js';
|
|
14
14
|
export { EMITTER_CCP_KEY, EMITTER_PCC_KEY, EMITTER_TYPE } from './es/constants.js';
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -17,6 +17,7 @@ export {
|
|
|
17
17
|
dangerouslyXss,
|
|
18
18
|
specialString,
|
|
19
19
|
isNonEmpty,
|
|
20
|
+
parseJoin,
|
|
20
21
|
} from "./common";
|
|
21
22
|
export { plus, minus, times, divide, exactRound, formatUnit } from "./math";
|
|
22
23
|
export { validatePassword } from "./passwordValidate";
|
|
@@ -42,7 +43,7 @@ export {
|
|
|
42
43
|
/**
|
|
43
44
|
* 时间处理函数
|
|
44
45
|
*/
|
|
45
|
-
export {
|
|
46
|
+
export { parseTime, calculateAge } from "./parseTime";
|
|
46
47
|
|
|
47
48
|
/**
|
|
48
49
|
* 常量相关
|
package/src/parseTime.ts
CHANGED
|
@@ -31,7 +31,7 @@ interface IConfig {
|
|
|
31
31
|
* { key: "20xx-xx-xx" } => { key: Dayjs }
|
|
32
32
|
* { key1: "20xx-xx-xx", key1: "20xx-xx-xx" } => { mergeKey: [Dayjs, Dayjs] }
|
|
33
33
|
*/
|
|
34
|
-
export
|
|
34
|
+
export function parseTime(config: IConfig) {
|
|
35
35
|
const {
|
|
36
36
|
params,
|
|
37
37
|
format = defaultFormat,
|
|
@@ -207,3 +207,27 @@ function parseByResult(option: IProps) {
|
|
|
207
207
|
}
|
|
208
208
|
};
|
|
209
209
|
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* 计算年龄
|
|
213
|
+
*/
|
|
214
|
+
export function calculateAge(birthday: any) {
|
|
215
|
+
if (!birthday) {
|
|
216
|
+
console.error("请传入一个生日信息");
|
|
217
|
+
return -1;
|
|
218
|
+
}
|
|
219
|
+
const birthDate = dayjs(birthday);
|
|
220
|
+
const today = dayjs();
|
|
221
|
+
|
|
222
|
+
let age = today.year() - birthDate.year();
|
|
223
|
+
const monthDifference = today.month() - birthDate.month();
|
|
224
|
+
|
|
225
|
+
if (
|
|
226
|
+
monthDifference < 0 ||
|
|
227
|
+
(monthDifference === 0 && today.date() < birthDate.date())
|
|
228
|
+
) {
|
|
229
|
+
age -= 1;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return age;
|
|
233
|
+
}
|
package/src/parseTimeTest.js
CHANGED
|
@@ -251,3 +251,36 @@ console.log(
|
|
|
251
251
|
format: ["YYYY-MM-DD", "YYYY-MM-DD HH:mm:ss"],
|
|
252
252
|
})
|
|
253
253
|
);
|
|
254
|
+
|
|
255
|
+
function calculateAge(birthday) {
|
|
256
|
+
if (!birthday) {
|
|
257
|
+
console.error("请传入一个生日信息");
|
|
258
|
+
return -1;
|
|
259
|
+
}
|
|
260
|
+
const birthDate = dayjs(birthday);
|
|
261
|
+
const today = dayjs();
|
|
262
|
+
|
|
263
|
+
let age = today.year() - birthDate.year();
|
|
264
|
+
const monthDifference = today.month() - birthDate.month();
|
|
265
|
+
|
|
266
|
+
if (
|
|
267
|
+
monthDifference < 0 ||
|
|
268
|
+
(monthDifference === 0 && today.date() < birthDate.date())
|
|
269
|
+
) {
|
|
270
|
+
age -= 1;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return age;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
console.log(calculateAge()); // -1
|
|
277
|
+
console.log(calculateAge("1994-04-02")); // 29
|
|
278
|
+
console.log(calculateAge("1994-10-12")); // 28
|
|
279
|
+
console.log(calculateAge("2023-10-11")); // 0
|
|
280
|
+
console.log(calculateAge("2023-10-12")); // -1
|
|
281
|
+
console.log(calculateAge("2023-10-10")); // 0
|
|
282
|
+
console.log(calculateAge("2022-10-11")); // 1
|
|
283
|
+
console.log(calculateAge("2022-10-10")); // 1
|
|
284
|
+
console.log(calculateAge("2022-10-12")); // 0
|
|
285
|
+
console.log(calculateAge(dayjs()));
|
|
286
|
+
console.log(calculateAge(dayjs("1994-04-02")));
|