@qxs-bns/utils 0.0.15 → 0.0.16
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/es/index.d.ts +1 -0
- package/es/index.mjs +30 -28
- package/es/index.mjs.map +1 -1
- package/es/src/date-transfer.d.ts +20 -0
- package/es/src/date-transfer.mjs +62 -0
- package/es/src/date-transfer.mjs.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -1
- package/lib/src/date-transfer.d.ts +20 -0
- package/lib/src/date-transfer.js +2 -0
- package/lib/src/date-transfer.js.map +1 -0
- package/package.json +1 -1
package/es/index.d.ts
CHANGED
package/es/index.mjs
CHANGED
|
@@ -1,33 +1,35 @@
|
|
|
1
1
|
import { ArgoLog as r } from "./src/argo-log.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
2
|
+
import { formatTime as t } from "./src/date-transfer.mjs";
|
|
3
|
+
import { isMobile as p } from "./src/device.mjs";
|
|
4
|
+
import { createVideoUploader as a, downloadFile as l } from "./src/file-operations.mjs";
|
|
5
|
+
import { JsonToExcel as n, excelToJson as x } from "./src/json.mjs";
|
|
6
|
+
import { OssError as g, OssUploader as c, generateSignedUrl as S, uploadFile as O } from "./src/oss-uploader.mjs";
|
|
7
|
+
import { Storage as U } from "./src/storage.mjs";
|
|
8
|
+
import { isBoolean as w, isElement as E, isImageByMimeType as J, isJSONString as u, isNumber as y, isPlainObject as A, isUndefined as B, isWindow as F } from "./src/types.mjs";
|
|
9
|
+
import { ApiService as N } from "./src/use-api.mjs";
|
|
10
|
+
import { watermark as k } from "./src/watermark.mjs";
|
|
10
11
|
export {
|
|
11
|
-
|
|
12
|
+
N as ApiService,
|
|
12
13
|
r as ArgoLog,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
w as
|
|
23
|
-
E as
|
|
24
|
-
J as
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
14
|
+
n as JsonToExcel,
|
|
15
|
+
g as OssError,
|
|
16
|
+
c as OssUploader,
|
|
17
|
+
U as Storage,
|
|
18
|
+
a as createVideoUploader,
|
|
19
|
+
l as downloadFile,
|
|
20
|
+
x as excelToJson,
|
|
21
|
+
t as formatTime,
|
|
22
|
+
S as generateSignedUrl,
|
|
23
|
+
w as isBoolean,
|
|
24
|
+
E as isElement,
|
|
25
|
+
J as isImageByMimeType,
|
|
26
|
+
u as isJSONString,
|
|
27
|
+
p as isMobile,
|
|
28
|
+
y as isNumber,
|
|
29
|
+
A as isPlainObject,
|
|
30
|
+
B as isUndefined,
|
|
31
|
+
F as isWindow,
|
|
32
|
+
O as uploadFile,
|
|
33
|
+
k as watermark
|
|
32
34
|
};
|
|
33
35
|
//# sourceMappingURL=index.mjs.map
|
package/es/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 统一的时间转换函数
|
|
3
|
+
* @param {number|string|Date} time - 输入时间(时间戳、日期字符串或Date对象)
|
|
4
|
+
* @param {object} options - 配置选项
|
|
5
|
+
* @param {boolean} options.isMs - 输入是否为毫秒,默认false(秒)
|
|
6
|
+
* @param {string} options.format - 输出格式:'duration'(持续时间)| 'relative'(相对时间),默认'duration'
|
|
7
|
+
* @param {string} options.separator - 时间分隔符,默认':'
|
|
8
|
+
* @param {boolean} options.complete - 是否显示完整时间格式,默认false
|
|
9
|
+
* @param {boolean} options.showDate - 是否显示日期部分,默认true
|
|
10
|
+
* @param {boolean} options.showTime - 是否显示时间部分,默认true
|
|
11
|
+
* @returns {string} 格式化后的时间字符串
|
|
12
|
+
*/
|
|
13
|
+
export declare function formatTime(time: number | string | Date, { isMs, format, separator, complete, showDate, showTime, }?: {
|
|
14
|
+
isMs?: boolean | undefined;
|
|
15
|
+
format?: string | undefined;
|
|
16
|
+
separator?: string | undefined;
|
|
17
|
+
complete?: boolean | undefined;
|
|
18
|
+
showDate?: boolean | undefined;
|
|
19
|
+
showTime?: boolean | undefined;
|
|
20
|
+
}): string;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const c = {
|
|
2
|
+
thresholds: {
|
|
3
|
+
second: 1e3,
|
|
4
|
+
minute: 6e4,
|
|
5
|
+
hour: 36e5,
|
|
6
|
+
day: 864e5,
|
|
7
|
+
month: 2592e6,
|
|
8
|
+
year: 31536e6
|
|
9
|
+
},
|
|
10
|
+
units: {
|
|
11
|
+
year: 31536e6,
|
|
12
|
+
month: 2592e6,
|
|
13
|
+
day: 864e5,
|
|
14
|
+
hour: 36e5,
|
|
15
|
+
minute: 6e4,
|
|
16
|
+
second: 1e3
|
|
17
|
+
},
|
|
18
|
+
labels: {
|
|
19
|
+
relative: {
|
|
20
|
+
justNow: "刚刚",
|
|
21
|
+
seconds: "几秒前",
|
|
22
|
+
minutes: "分钟前",
|
|
23
|
+
hours: "小时前",
|
|
24
|
+
days: "天前",
|
|
25
|
+
months: "个月前",
|
|
26
|
+
years: "年前"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
function b(u, {
|
|
31
|
+
isMs: m = !1,
|
|
32
|
+
format: $ = "duration",
|
|
33
|
+
separator: d = ":",
|
|
34
|
+
complete: y = !1,
|
|
35
|
+
showDate: M = !0,
|
|
36
|
+
showTime: w = !0
|
|
37
|
+
} = {}) {
|
|
38
|
+
let e;
|
|
39
|
+
if (typeof u == "number")
|
|
40
|
+
e = m ? u : u * 1e3;
|
|
41
|
+
else {
|
|
42
|
+
const r = Date.now(), t = new Date(u).getTime();
|
|
43
|
+
e = $ === "relative" ? r - t : t;
|
|
44
|
+
}
|
|
45
|
+
if (Number.isNaN(e) || e < 0)
|
|
46
|
+
throw new Error("无效的时间输入");
|
|
47
|
+
if ($ === "relative") {
|
|
48
|
+
const { justNow: r, seconds: t, minutes: f, hours: N, days: p, months: S, years: T } = c.labels.relative, o = c.thresholds;
|
|
49
|
+
return e < o.second ? r : e < o.minute ? t : e < o.hour ? `${Math.floor(e / o.minute)}${f}` : e < o.day ? `${Math.floor(e / o.hour)}${N}` : e < o.month ? `${Math.floor(e / o.day)}${p}` : e < o.year ? `${Math.floor(e / o.month)}${S}` : `${Math.floor(e / o.year)}${T}`;
|
|
50
|
+
}
|
|
51
|
+
const h = [], i = Math.floor(e / 1e3), l = Math.floor(i / (24 * 3600)), a = Math.floor(i % (24 * 3600) / 3600), s = Math.floor(i % 3600 / 60), n = i % 60;
|
|
52
|
+
if (M && l > 0 && h.push(`${l}天`), w) {
|
|
53
|
+
const r = (f) => String(f).padStart(2, "0");
|
|
54
|
+
let t = "";
|
|
55
|
+
y ? t = `${r(a)}${d}${r(s)}${d}${r(n)}` : l > 0 ? (t = a > 0 ? `${a}小时` : "", t += s > 0 ? `${s}分钟` : "") : a > 0 ? (t = `${a}小时`, t += s > 0 ? `${s}分钟` : "", t += n > 0 ? `${n}秒` : "") : s > 0 ? (t = `${s}分钟`, t += n > 0 ? `${n}秒` : "") : t = `${n}秒`, t && h.push(t);
|
|
56
|
+
}
|
|
57
|
+
return h.join(" ");
|
|
58
|
+
}
|
|
59
|
+
export {
|
|
60
|
+
b as formatTime
|
|
61
|
+
};
|
|
62
|
+
//# sourceMappingURL=date-transfer.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date-transfer.mjs","sources":["../../../../packages/utils/src/date-transfer.ts"],"sourcesContent":["// 将阈值和时间单位合并为一个常量对象\nconst TIME_CONSTANTS = {\n thresholds: {\n second: 1000,\n minute: 60000,\n hour: 3600000,\n day: 86400000,\n month: 2592000000,\n year: 31536000000,\n },\n units: {\n year: 31536000000,\n month: 2592000000,\n day: 86400000,\n hour: 3600000,\n minute: 60000,\n second: 1000,\n },\n labels: {\n relative: {\n justNow: '刚刚',\n seconds: '几秒前',\n minutes: '分钟前',\n hours: '小时前',\n days: '天前',\n months: '个月前',\n years: '年前',\n },\n },\n}\n\n/**\n * 统一的时间转换函数\n * @param {number|string|Date} time - 输入时间(时间戳、日期字符串或Date对象)\n * @param {object} options - 配置选项\n * @param {boolean} options.isMs - 输入是否为毫秒,默认false(秒)\n * @param {string} options.format - 输出格式:'duration'(持续时间)| 'relative'(相对时间),默认'duration'\n * @param {string} options.separator - 时间分隔符,默认':'\n * @param {boolean} options.complete - 是否显示完整时间格式,默认false\n * @param {boolean} options.showDate - 是否显示日期部分,默认true\n * @param {boolean} options.showTime - 是否显示时间部分,默认true\n * @returns {string} 格式化后的时间字符串\n */\nexport function formatTime(\n time: number | string | Date,\n {\n isMs = false,\n format = 'duration',\n separator = ':',\n complete = false,\n showDate = true,\n showTime = true,\n } = {},\n) {\n // 处理输入时间\n let ms: number\n\n if (typeof time === 'number') {\n ms = isMs ? time : time * 1000\n }\n else {\n const now = Date.now()\n const timestamp = new Date(time).getTime()\n ms = format === 'relative' ? now - timestamp : timestamp\n }\n\n // 验证输入\n if (Number.isNaN(ms) || ms < 0) {\n throw new Error('无效的时间输入')\n }\n\n // 相对时间格式\n if (format === 'relative') {\n const { justNow, seconds, minutes, hours, days, months, years }\n = TIME_CONSTANTS.labels.relative\n const thresholds = TIME_CONSTANTS.thresholds\n\n if (ms < thresholds.second) {\n return justNow\n }\n if (ms < thresholds.minute) {\n return seconds\n }\n if (ms < thresholds.hour) {\n return `${Math.floor(ms / thresholds.minute)}${minutes}`\n }\n if (ms < thresholds.day) {\n return `${Math.floor(ms / thresholds.hour)}${hours}`\n }\n if (ms < thresholds.month) {\n return `${Math.floor(ms / thresholds.day)}${days}`\n }\n if (ms < thresholds.year) {\n return `${Math.floor(ms / thresholds.month)}${months}`\n }\n return `${Math.floor(ms / thresholds.year)}${years}`\n }\n\n // 持续时间格式\n const result = []\n const totalSeconds = Math.floor(ms / 1000)\n const days = Math.floor(totalSeconds / (24 * 3600))\n const hours = Math.floor((totalSeconds % (24 * 3600)) / 3600)\n const minutes = Math.floor((totalSeconds % 3600) / 60)\n const seconds = totalSeconds % 60\n\n // 处理日期部分\n if (showDate && days > 0) {\n result.push(`${days}天`)\n }\n\n // 处理时间部分\n if (showTime) {\n const pad = (num: number): string => String(num).padStart(2, '0')\n let timeStr = ''\n\n if (complete) {\n timeStr = `${pad(hours)}${separator}${pad(minutes)}${separator}${pad(seconds)}`\n }\n else {\n if (days > 0) {\n timeStr = hours > 0 ? `${hours}小时` : ''\n timeStr += minutes > 0 ? `${minutes}分钟` : ''\n }\n else if (hours > 0) {\n timeStr = `${hours}小时`\n timeStr += minutes > 0 ? `${minutes}分钟` : ''\n timeStr += seconds > 0 ? `${seconds}秒` : ''\n }\n else if (minutes > 0) {\n timeStr = `${minutes}分钟`\n timeStr += seconds > 0 ? `${seconds}秒` : ''\n }\n else {\n timeStr = `${seconds}秒`\n }\n }\n if (timeStr) {\n result.push(timeStr)\n }\n }\n\n return result.join(' ')\n}\n"],"names":["TIME_CONSTANTS","formatTime","time","isMs","format","separator","complete","showDate","showTime","ms","now","timestamp","justNow","seconds","minutes","hours","days","months","years","thresholds","result","totalSeconds","pad","num","timeStr"],"mappings":"AACA,MAAMA,IAAiB;AAAA,EACrB,YAAY;AAAA,IACV,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,MAAM;AAAA,IACN,KAAK;AAAA,IACL,OAAO;AAAA,IACP,MAAM;AAAA,EACR;AAAA,EACA,OAAO;AAAA,IACL,MAAM;AAAA,IACN,OAAO;AAAA,IACP,KAAK;AAAA,IACL,MAAM;AAAA,IACN,QAAQ;AA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argo-log.js"),n=require("./src/device.js"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./src/argo-log.js"),n=require("./src/date-transfer.js"),t=require("./src/device.js"),i=require("./src/file-operations.js"),o=require("./src/json.js"),r=require("./src/oss-uploader.js"),a=require("./src/storage.js"),e=require("./src/types.js"),l=require("./src/use-api.js"),d=require("./src/watermark.js");exports.ArgoLog=s.ArgoLog;exports.formatTime=n.formatTime;exports.isMobile=t.isMobile;exports.createVideoUploader=i.createVideoUploader;exports.downloadFile=i.downloadFile;exports.JsonToExcel=o.JsonToExcel;exports.excelToJson=o.excelToJson;exports.OssError=r.OssError;exports.OssUploader=r.OssUploader;exports.generateSignedUrl=r.generateSignedUrl;exports.uploadFile=r.uploadFile;exports.Storage=a.Storage;exports.isBoolean=e.isBoolean;exports.isElement=e.isElement;exports.isImageByMimeType=e.isImageByMimeType;exports.isJSONString=e.isJSONString;exports.isNumber=e.isNumber;exports.isPlainObject=e.isPlainObject;exports.isUndefined=e.isUndefined;exports.isWindow=e.isWindow;exports.ApiService=l.ApiService;exports.watermark=d.watermark;
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 统一的时间转换函数
|
|
3
|
+
* @param {number|string|Date} time - 输入时间(时间戳、日期字符串或Date对象)
|
|
4
|
+
* @param {object} options - 配置选项
|
|
5
|
+
* @param {boolean} options.isMs - 输入是否为毫秒,默认false(秒)
|
|
6
|
+
* @param {string} options.format - 输出格式:'duration'(持续时间)| 'relative'(相对时间),默认'duration'
|
|
7
|
+
* @param {string} options.separator - 时间分隔符,默认':'
|
|
8
|
+
* @param {boolean} options.complete - 是否显示完整时间格式,默认false
|
|
9
|
+
* @param {boolean} options.showDate - 是否显示日期部分,默认true
|
|
10
|
+
* @param {boolean} options.showTime - 是否显示时间部分,默认true
|
|
11
|
+
* @returns {string} 格式化后的时间字符串
|
|
12
|
+
*/
|
|
13
|
+
export declare function formatTime(time: number | string | Date, { isMs, format, separator, complete, showDate, showTime, }?: {
|
|
14
|
+
isMs?: boolean | undefined;
|
|
15
|
+
format?: string | undefined;
|
|
16
|
+
separator?: string | undefined;
|
|
17
|
+
complete?: boolean | undefined;
|
|
18
|
+
showDate?: boolean | undefined;
|
|
19
|
+
showTime?: boolean | undefined;
|
|
20
|
+
}): string;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c={thresholds:{second:1e3,minute:6e4,hour:36e5,day:864e5,month:2592e6,year:31536e6},units:{year:31536e6,month:2592e6,day:864e5,hour:36e5,minute:6e4,second:1e3},labels:{relative:{justNow:"刚刚",seconds:"几秒前",minutes:"分钟前",hours:"小时前",days:"天前",months:"个月前",years:"年前"}}};function p(i,{isMs:m=!1,format:d="duration",separator:$=":",complete:y=!1,showDate:M=!0,showTime:S=!0}={}){let e;if(typeof i=="number")e=m?i:i*1e3;else{const r=Date.now(),t=new Date(i).getTime();e=d==="relative"?r-t:t}if(Number.isNaN(e)||e<0)throw new Error("无效的时间输入");if(d==="relative"){const{justNow:r,seconds:t,minutes:f,hours:w,days:N,months:T,years:b}=c.labels.relative,o=c.thresholds;return e<o.second?r:e<o.minute?t:e<o.hour?`${Math.floor(e/o.minute)}${f}`:e<o.day?`${Math.floor(e/o.hour)}${w}`:e<o.month?`${Math.floor(e/o.day)}${N}`:e<o.year?`${Math.floor(e/o.month)}${T}`:`${Math.floor(e/o.year)}${b}`}const l=[],u=Math.floor(e/1e3),h=Math.floor(u/(24*3600)),a=Math.floor(u%(24*3600)/3600),s=Math.floor(u%3600/60),n=u%60;if(M&&h>0&&l.push(`${h}天`),S){const r=f=>String(f).padStart(2,"0");let t="";y?t=`${r(a)}${$}${r(s)}${$}${r(n)}`:h>0?(t=a>0?`${a}小时`:"",t+=s>0?`${s}分钟`:""):a>0?(t=`${a}小时`,t+=s>0?`${s}分钟`:"",t+=n>0?`${n}秒`:""):s>0?(t=`${s}分钟`,t+=n>0?`${n}秒`:""):t=`${n}秒`,t&&l.push(t)}return l.join(" ")}exports.formatTime=p;
|
|
2
|
+
//# sourceMappingURL=date-transfer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date-transfer.js","sources":["../../../../packages/utils/src/date-transfer.ts"],"sourcesContent":["// 将阈值和时间单位合并为一个常量对象\nconst TIME_CONSTANTS = {\n thresholds: {\n second: 1000,\n minute: 60000,\n hour: 3600000,\n day: 86400000,\n month: 2592000000,\n year: 31536000000,\n },\n units: {\n year: 31536000000,\n month: 2592000000,\n day: 86400000,\n hour: 3600000,\n minute: 60000,\n second: 1000,\n },\n labels: {\n relative: {\n justNow: '刚刚',\n seconds: '几秒前',\n minutes: '分钟前',\n hours: '小时前',\n days: '天前',\n months: '个月前',\n years: '年前',\n },\n },\n}\n\n/**\n * 统一的时间转换函数\n * @param {number|string|Date} time - 输入时间(时间戳、日期字符串或Date对象)\n * @param {object} options - 配置选项\n * @param {boolean} options.isMs - 输入是否为毫秒,默认false(秒)\n * @param {string} options.format - 输出格式:'duration'(持续时间)| 'relative'(相对时间),默认'duration'\n * @param {string} options.separator - 时间分隔符,默认':'\n * @param {boolean} options.complete - 是否显示完整时间格式,默认false\n * @param {boolean} options.showDate - 是否显示日期部分,默认true\n * @param {boolean} options.showTime - 是否显示时间部分,默认true\n * @returns {string} 格式化后的时间字符串\n */\nexport function formatTime(\n time: number | string | Date,\n {\n isMs = false,\n format = 'duration',\n separator = ':',\n complete = false,\n showDate = true,\n showTime = true,\n } = {},\n) {\n // 处理输入时间\n let ms: number\n\n if (typeof time === 'number') {\n ms = isMs ? time : time * 1000\n }\n else {\n const now = Date.now()\n const timestamp = new Date(time).getTime()\n ms = format === 'relative' ? now - timestamp : timestamp\n }\n\n // 验证输入\n if (Number.isNaN(ms) || ms < 0) {\n throw new Error('无效的时间输入')\n }\n\n // 相对时间格式\n if (format === 'relative') {\n const { justNow, seconds, minutes, hours, days, months, years }\n = TIME_CONSTANTS.labels.relative\n const thresholds = TIME_CONSTANTS.thresholds\n\n if (ms < thresholds.second) {\n return justNow\n }\n if (ms < thresholds.minute) {\n return seconds\n }\n if (ms < thresholds.hour) {\n return `${Math.floor(ms / thresholds.minute)}${minutes}`\n }\n if (ms < thresholds.day) {\n return `${Math.floor(ms / thresholds.hour)}${hours}`\n }\n if (ms < thresholds.month) {\n return `${Math.floor(ms / thresholds.day)}${days}`\n }\n if (ms < thresholds.year) {\n return `${Math.floor(ms / thresholds.month)}${months}`\n }\n return `${Math.floor(ms / thresholds.year)}${years}`\n }\n\n // 持续时间格式\n const result = []\n const totalSeconds = Math.floor(ms / 1000)\n const days = Math.floor(totalSeconds / (24 * 3600))\n const hours = Math.floor((totalSeconds % (24 * 3600)) / 3600)\n const minutes = Math.floor((totalSeconds % 3600) / 60)\n const seconds = totalSeconds % 60\n\n // 处理日期部分\n if (showDate && days > 0) {\n result.push(`${days}天`)\n }\n\n // 处理时间部分\n if (showTime) {\n const pad = (num: number): string => String(num).padStart(2, '0')\n let timeStr = ''\n\n if (complete) {\n timeStr = `${pad(hours)}${separator}${pad(minutes)}${separator}${pad(seconds)}`\n }\n else {\n if (days > 0) {\n timeStr = hours > 0 ? `${hours}小时` : ''\n timeStr += minutes > 0 ? `${minutes}分钟` : ''\n }\n else if (hours > 0) {\n timeStr = `${hours}小时`\n timeStr += minutes > 0 ? `${minutes}分钟` : ''\n timeStr += seconds > 0 ? `${seconds}秒` : ''\n }\n else if (minutes > 0) {\n timeStr = `${minutes}分钟`\n timeStr += seconds > 0 ? `${seconds}秒` : ''\n }\n else {\n timeStr = `${seconds}秒`\n }\n }\n if (timeStr) {\n result.push(timeStr)\n }\n }\n\n return result.join(' ')\n}\n"],"names":["TIME_CONSTANTS","formatTime","time","isMs","format","separator","complete","showDate","showTime","ms","now","timestamp","justNow","seconds","minutes","hours","days","months","years","thresholds","result","totalSeconds","pad","num","timeStr"],"mappings":"gFACA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