@tanwan/utils 1.0.0
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/array/chunk.cjs +17 -0
- package/dist/array/chunk.d.cts +13 -0
- package/dist/array/chunk.d.cts.map +1 -0
- package/dist/array/chunk.d.ts +14 -0
- package/dist/array/chunk.d.ts.map +1 -0
- package/dist/array/chunk.js +19 -0
- package/dist/array/chunk.js.map +1 -0
- package/dist/array/flatten.cjs +19 -0
- package/dist/array/flatten.d.cts +13 -0
- package/dist/array/flatten.d.cts.map +1 -0
- package/dist/array/flatten.d.ts +14 -0
- package/dist/array/flatten.d.ts.map +1 -0
- package/dist/array/flatten.js +21 -0
- package/dist/array/flatten.js.map +1 -0
- package/dist/array/shuffle.cjs +19 -0
- package/dist/array/shuffle.d.cts +12 -0
- package/dist/array/shuffle.d.cts.map +1 -0
- package/dist/array/shuffle.d.ts +13 -0
- package/dist/array/shuffle.d.ts.map +1 -0
- package/dist/array/shuffle.js +21 -0
- package/dist/array/shuffle.js.map +1 -0
- package/dist/array/unique.cjs +15 -0
- package/dist/array/unique.d.cts +13 -0
- package/dist/array/unique.d.cts.map +1 -0
- package/dist/array/unique.d.ts +14 -0
- package/dist/array/unique.d.ts.map +1 -0
- package/dist/array/unique.js +17 -0
- package/dist/array/unique.js.map +1 -0
- package/dist/base/deepClone.cjs +48 -0
- package/dist/base/deepClone.d.cts +15 -0
- package/dist/base/deepClone.d.cts.map +1 -0
- package/dist/base/deepClone.d.ts +16 -0
- package/dist/base/deepClone.d.ts.map +1 -0
- package/dist/base/deepClone.js +50 -0
- package/dist/base/deepClone.js.map +1 -0
- package/dist/base/isArray.cjs +15 -0
- package/dist/base/isArray.d.cts +13 -0
- package/dist/base/isArray.d.cts.map +1 -0
- package/dist/base/isArray.d.ts +14 -0
- package/dist/base/isArray.d.ts.map +1 -0
- package/dist/base/isArray.js +17 -0
- package/dist/base/isArray.js.map +1 -0
- package/dist/base/isObject.cjs +16 -0
- package/dist/base/isObject.d.cts +14 -0
- package/dist/base/isObject.d.cts.map +1 -0
- package/dist/base/isObject.d.ts +15 -0
- package/dist/base/isObject.d.ts.map +1 -0
- package/dist/base/isObject.js +18 -0
- package/dist/base/isObject.js.map +1 -0
- package/dist/base/isString.cjs +15 -0
- package/dist/base/isString.d.cts +13 -0
- package/dist/base/isString.d.cts.map +1 -0
- package/dist/base/isString.d.ts +14 -0
- package/dist/base/isString.d.ts.map +1 -0
- package/dist/base/isString.js +17 -0
- package/dist/base/isString.js.map +1 -0
- package/dist/date/formatDate.cjs +38 -0
- package/dist/date/formatDate.d.cts +13 -0
- package/dist/date/formatDate.d.cts.map +1 -0
- package/dist/date/formatDate.d.ts +14 -0
- package/dist/date/formatDate.d.ts.map +1 -0
- package/dist/date/formatDate.js +40 -0
- package/dist/date/formatDate.js.map +1 -0
- package/dist/date/getRelativeTime.cjs +29 -0
- package/dist/date/getRelativeTime.d.cts +13 -0
- package/dist/date/getRelativeTime.d.cts.map +1 -0
- package/dist/date/getRelativeTime.d.ts +14 -0
- package/dist/date/getRelativeTime.d.ts.map +1 -0
- package/dist/date/getRelativeTime.js +31 -0
- package/dist/date/getRelativeTime.js.map +1 -0
- package/dist/function/debounce.cjs +29 -0
- package/dist/function/debounce.d.cts +14 -0
- package/dist/function/debounce.d.cts.map +1 -0
- package/dist/function/debounce.d.ts +15 -0
- package/dist/function/debounce.d.ts.map +1 -0
- package/dist/function/debounce.js +31 -0
- package/dist/function/debounce.js.map +1 -0
- package/dist/function/throttle.cjs +28 -0
- package/dist/function/throttle.d.cts +14 -0
- package/dist/function/throttle.d.cts.map +1 -0
- package/dist/function/throttle.d.ts +15 -0
- package/dist/function/throttle.d.ts.map +1 -0
- package/dist/function/throttle.js +30 -0
- package/dist/function/throttle.js.map +1 -0
- package/dist/index.cjs +37 -0
- package/dist/index.d.cts +19 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.iife.js +1 -0
- package/dist/index.js +19 -0
- package/dist/number/clamp.cjs +16 -0
- package/dist/number/clamp.d.cts +14 -0
- package/dist/number/clamp.d.cts.map +1 -0
- package/dist/number/clamp.d.ts +15 -0
- package/dist/number/clamp.d.ts.map +1 -0
- package/dist/number/clamp.js +18 -0
- package/dist/number/clamp.js.map +1 -0
- package/dist/number/formatThousands.cjs +17 -0
- package/dist/number/formatThousands.d.cts +13 -0
- package/dist/number/formatThousands.d.cts.map +1 -0
- package/dist/number/formatThousands.d.ts +14 -0
- package/dist/number/formatThousands.d.ts.map +1 -0
- package/dist/number/formatThousands.js +19 -0
- package/dist/number/formatThousands.js.map +1 -0
- package/dist/number/randomInt.cjs +15 -0
- package/dist/number/randomInt.d.cts +13 -0
- package/dist/number/randomInt.d.cts.map +1 -0
- package/dist/number/randomInt.d.ts +14 -0
- package/dist/number/randomInt.d.ts.map +1 -0
- package/dist/number/randomInt.js +17 -0
- package/dist/number/randomInt.js.map +1 -0
- package/dist/string/camelCase.cjs +12 -0
- package/dist/string/camelCase.d.cts +7 -0
- package/dist/string/camelCase.d.cts.map +1 -0
- package/dist/string/camelCase.d.ts +8 -0
- package/dist/string/camelCase.d.ts.map +1 -0
- package/dist/string/camelCase.js +14 -0
- package/dist/string/camelCase.js.map +1 -0
- package/dist/string/capitalize.cjs +16 -0
- package/dist/string/capitalize.d.cts +13 -0
- package/dist/string/capitalize.d.cts.map +1 -0
- package/dist/string/capitalize.d.ts +14 -0
- package/dist/string/capitalize.d.ts.map +1 -0
- package/dist/string/capitalize.js +18 -0
- package/dist/string/capitalize.js.map +1 -0
- package/dist/string/kebabCase.cjs +9 -0
- package/dist/string/kebabCase.d.cts +7 -0
- package/dist/string/kebabCase.d.cts.map +1 -0
- package/dist/string/kebabCase.d.ts +8 -0
- package/dist/string/kebabCase.d.ts.map +1 -0
- package/dist/string/kebabCase.js +11 -0
- package/dist/string/kebabCase.js.map +1 -0
- package/package.json +104 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/base/isString/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 检测变量是否为字符串
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import isString from '@tw/utils/isString'
|
|
7
|
+
* isString('hello') // true
|
|
8
|
+
* isString(123) // false
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
function isString(v) {
|
|
12
|
+
return typeof v === "string";
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
module.exports = isString;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region src/base/isString/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 检测变量是否为字符串
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import isString from '@tw/utils/isString'
|
|
7
|
+
* isString('hello') // true
|
|
8
|
+
* isString(123) // false
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
declare function isString(v: unknown): v is string;
|
|
12
|
+
export = isString;
|
|
13
|
+
//# sourceMappingURL=isString.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isString.d.cts","names":[],"sources":["../../src/base/isString/index.ts"],"mappings":";;;;;;;;;;iBASwB,QAAA,CAAS,CAAA,YAAa,CAAA;AAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/base/isString/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 检测变量是否为字符串
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import isString from '@tw/utils/isString'
|
|
7
|
+
* isString('hello') // true
|
|
8
|
+
* isString(123) // false
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
declare function isString(v: unknown): v is string;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { isString as default };
|
|
14
|
+
//# sourceMappingURL=isString.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isString.d.ts","names":[],"sources":["../../src/base/isString/index.ts"],"mappings":";;;;;;;;;;iBASwB,QAAA,CAAS,CAAA,YAAa,CAAA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//#region src/base/isString/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 检测变量是否为字符串
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import isString from '@tw/utils/isString'
|
|
7
|
+
* isString('hello') // true
|
|
8
|
+
* isString(123) // false
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
function isString(v) {
|
|
12
|
+
return typeof v === "string";
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { isString as default };
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=isString.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isString.js","names":[],"sources":["../../src/base/isString/index.ts"],"sourcesContent":["/**\n * 检测变量是否为字符串\n * @example\n * ```ts\n * import isString from '@tw/utils/isString'\n * isString('hello') // true\n * isString(123) // false\n * ```\n */\nexport default function isString(v: unknown): v is string {\n return typeof v === 'string';\n}\n"],"mappings":";;;;;;;;;;AASA,SAAwB,SAAS,GAAyB;AACxD,QAAO,OAAO,MAAM"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//#region src/date/formatDate/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 格式化日期
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import formatDate from '@tw/utils/formatDate'
|
|
7
|
+
* formatDate(new Date('2024-01-01'), 'yyyy-MM-dd') // '2024-01-01'
|
|
8
|
+
* formatDate(1609459200000, 'yyyy/MM/dd HH:mm') // '2021/01/01 08:00'
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
function formatDate(date, fmt = "yyyy-MM-dd HH:mm:ss") {
|
|
12
|
+
if (date == null) return "Invalid Date";
|
|
13
|
+
const iOSReg = /-/g;
|
|
14
|
+
let d;
|
|
15
|
+
if (typeof date === "string") d = new Date(iOSReg.test(date) ? date.replace(iOSReg, "/") : date);
|
|
16
|
+
else d = new Date(date);
|
|
17
|
+
if (isNaN(d.getTime())) return "Invalid Date";
|
|
18
|
+
const o = {
|
|
19
|
+
"M+": d.getMonth() + 1,
|
|
20
|
+
"d+": d.getDate(),
|
|
21
|
+
"H+": d.getHours(),
|
|
22
|
+
"h+": d.getHours() % 12 || 12,
|
|
23
|
+
"m+": d.getMinutes(),
|
|
24
|
+
"s+": d.getSeconds()
|
|
25
|
+
};
|
|
26
|
+
const yearMatch = fmt.match(/(y+)/);
|
|
27
|
+
if (yearMatch) fmt = fmt.replace(yearMatch[0], String(d.getFullYear()).slice(4 - yearMatch[1].length));
|
|
28
|
+
for (const k in o) {
|
|
29
|
+
const match = fmt.match(new RegExp(`(${k})`));
|
|
30
|
+
if (match) {
|
|
31
|
+
const str = String(o[k]);
|
|
32
|
+
fmt = fmt.replace(match[0], match[1].length === 1 ? str : str.padStart(2, "0"));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return fmt;
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
module.exports = formatDate;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region src/date/formatDate/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 格式化日期
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import formatDate from '@tw/utils/formatDate'
|
|
7
|
+
* formatDate(new Date('2024-01-01'), 'yyyy-MM-dd') // '2024-01-01'
|
|
8
|
+
* formatDate(1609459200000, 'yyyy/MM/dd HH:mm') // '2021/01/01 08:00'
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
declare function formatDate(date: number | string | Date, fmt?: string): string;
|
|
12
|
+
export = formatDate;
|
|
13
|
+
//# sourceMappingURL=formatDate.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatDate.d.cts","names":[],"sources":["../../src/date/formatDate/index.ts"],"mappings":";;;;;;;;;;iBASwB,UAAA,CAAW,IAAA,oBAAwB,IAAA,EAAM,GAAA;AAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/date/formatDate/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 格式化日期
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import formatDate from '@tw/utils/formatDate'
|
|
7
|
+
* formatDate(new Date('2024-01-01'), 'yyyy-MM-dd') // '2024-01-01'
|
|
8
|
+
* formatDate(1609459200000, 'yyyy/MM/dd HH:mm') // '2021/01/01 08:00'
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
declare function formatDate(date: number | string | Date, fmt?: string): string;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { formatDate as default };
|
|
14
|
+
//# sourceMappingURL=formatDate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatDate.d.ts","names":[],"sources":["../../src/date/formatDate/index.ts"],"mappings":";;;;;;;;;;iBASwB,UAAA,CAAW,IAAA,oBAAwB,IAAA,EAAM,GAAA"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//#region src/date/formatDate/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 格式化日期
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import formatDate from '@tw/utils/formatDate'
|
|
7
|
+
* formatDate(new Date('2024-01-01'), 'yyyy-MM-dd') // '2024-01-01'
|
|
8
|
+
* formatDate(1609459200000, 'yyyy/MM/dd HH:mm') // '2021/01/01 08:00'
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
function formatDate(date, fmt = "yyyy-MM-dd HH:mm:ss") {
|
|
12
|
+
if (date == null) return "Invalid Date";
|
|
13
|
+
const iOSReg = /-/g;
|
|
14
|
+
let d;
|
|
15
|
+
if (typeof date === "string") d = new Date(iOSReg.test(date) ? date.replace(iOSReg, "/") : date);
|
|
16
|
+
else d = new Date(date);
|
|
17
|
+
if (isNaN(d.getTime())) return "Invalid Date";
|
|
18
|
+
const o = {
|
|
19
|
+
"M+": d.getMonth() + 1,
|
|
20
|
+
"d+": d.getDate(),
|
|
21
|
+
"H+": d.getHours(),
|
|
22
|
+
"h+": d.getHours() % 12 || 12,
|
|
23
|
+
"m+": d.getMinutes(),
|
|
24
|
+
"s+": d.getSeconds()
|
|
25
|
+
};
|
|
26
|
+
const yearMatch = fmt.match(/(y+)/);
|
|
27
|
+
if (yearMatch) fmt = fmt.replace(yearMatch[0], String(d.getFullYear()).slice(4 - yearMatch[1].length));
|
|
28
|
+
for (const k in o) {
|
|
29
|
+
const match = fmt.match(new RegExp(`(${k})`));
|
|
30
|
+
if (match) {
|
|
31
|
+
const str = String(o[k]);
|
|
32
|
+
fmt = fmt.replace(match[0], match[1].length === 1 ? str : str.padStart(2, "0"));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return fmt;
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { formatDate as default };
|
|
39
|
+
|
|
40
|
+
//# sourceMappingURL=formatDate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatDate.js","names":[],"sources":["../../src/date/formatDate/index.ts"],"sourcesContent":["/**\n * 格式化日期\n * @example\n * ```ts\n * import formatDate from '@tw/utils/formatDate'\n * formatDate(new Date('2024-01-01'), 'yyyy-MM-dd') // '2024-01-01'\n * formatDate(1609459200000, 'yyyy/MM/dd HH:mm') // '2021/01/01 08:00'\n * ```\n */\nexport default function formatDate(date: number | string | Date, fmt = 'yyyy-MM-dd HH:mm:ss'): string {\n if (date == null) return 'Invalid Date';\n\n const iOSReg = /-/g;\n let d: Date;\n\n if (typeof date === 'string') {\n // iOS 日期字符串中包含 -,需要替换为 /\n d = new Date(iOSReg.test(date) ? date.replace(iOSReg, '/') : date);\n } else {\n d = new Date(date);\n }\n\n if (isNaN(d.getTime())) return 'Invalid Date';\n\n const o: Record<string, number> = {\n 'M+': d.getMonth() + 1,\n 'd+': d.getDate(),\n 'H+': d.getHours(),\n 'h+': d.getHours() % 12 || 12,\n 'm+': d.getMinutes(),\n 's+': d.getSeconds(),\n };\n\n // 处理年份\n const yearMatch = fmt.match(/(y+)/);\n if (yearMatch) {\n fmt = fmt.replace(yearMatch[0], String(d.getFullYear()).slice(4 - yearMatch[1].length));\n }\n\n for (const k in o) {\n const match = fmt.match(new RegExp(`(${k})`));\n if (match) {\n const str = String(o[k]);\n fmt = fmt.replace(match[0], match[1].length === 1 ? str : str.padStart(2, '0'));\n }\n }\n\n return fmt;\n}\n"],"mappings":";;;;;;;;;;AASA,SAAwB,WAAW,MAA8B,MAAM,uBAA+B;AACpG,KAAI,QAAQ,KAAM,QAAO;CAEzB,MAAM,SAAS;CACf,IAAI;AAEJ,KAAI,OAAO,SAAS,SAElB,KAAI,IAAI,KAAK,OAAO,KAAK,KAAK,GAAG,KAAK,QAAQ,QAAQ,IAAI,GAAG,KAAK;KAElE,KAAI,IAAI,KAAK,KAAK;AAGpB,KAAI,MAAM,EAAE,SAAS,CAAC,CAAE,QAAO;CAE/B,MAAM,IAA4B;EAChC,MAAM,EAAE,UAAU,GAAG;EACrB,MAAM,EAAE,SAAS;EACjB,MAAM,EAAE,UAAU;EAClB,MAAM,EAAE,UAAU,GAAG,MAAM;EAC3B,MAAM,EAAE,YAAY;EACpB,MAAM,EAAE,YAAY;EACrB;CAGD,MAAM,YAAY,IAAI,MAAM,OAAO;AACnC,KAAI,UACF,OAAM,IAAI,QAAQ,UAAU,IAAI,OAAO,EAAE,aAAa,CAAC,CAAC,MAAM,IAAI,UAAU,GAAG,OAAO,CAAC;AAGzF,MAAK,MAAM,KAAK,GAAG;EACjB,MAAM,QAAQ,IAAI,MAAM,IAAI,OAAO,IAAI,EAAE,GAAG,CAAC;AAC7C,MAAI,OAAO;GACT,MAAM,MAAM,OAAO,EAAE,GAAG;AACxB,SAAM,IAAI,QAAQ,MAAM,IAAI,MAAM,GAAG,WAAW,IAAI,MAAM,IAAI,SAAS,GAAG,IAAI,CAAC;;;AAInF,QAAO"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region src/date/getRelativeTime/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 获取相对时间描述(如"3分钟前")
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import getRelativeTime from '@tw/utils/getRelativeTime'
|
|
7
|
+
* getRelativeTime(Date.now() - 60000) // '1分钟前'
|
|
8
|
+
* getRelativeTime(Date.now() - 3600000) // '1小时前'
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
function getRelativeTime(date, now = /* @__PURE__ */ new Date()) {
|
|
12
|
+
const d = new Date(date);
|
|
13
|
+
const diff = now.getTime() - d.getTime();
|
|
14
|
+
const seconds = Math.floor(diff / 1e3);
|
|
15
|
+
const minutes = Math.floor(seconds / 60);
|
|
16
|
+
const hours = Math.floor(minutes / 60);
|
|
17
|
+
const days = Math.floor(hours / 24);
|
|
18
|
+
const months = Math.floor(days / 30);
|
|
19
|
+
const years = Math.floor(days / 365);
|
|
20
|
+
if (seconds < 0) return "刚刚";
|
|
21
|
+
if (seconds < 60) return `${seconds}秒前`;
|
|
22
|
+
if (minutes < 60) return `${minutes}分钟前`;
|
|
23
|
+
if (hours < 24) return `${hours}小时前`;
|
|
24
|
+
if (days < 30) return `${days}天前`;
|
|
25
|
+
if (months < 12) return `${months}个月前`;
|
|
26
|
+
return `${years}年前`;
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
module.exports = getRelativeTime;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region src/date/getRelativeTime/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 获取相对时间描述(如"3分钟前")
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import getRelativeTime from '@tw/utils/getRelativeTime'
|
|
7
|
+
* getRelativeTime(Date.now() - 60000) // '1分钟前'
|
|
8
|
+
* getRelativeTime(Date.now() - 3600000) // '1小时前'
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
declare function getRelativeTime(date: number | string | Date, now?: Date): string;
|
|
12
|
+
export = getRelativeTime;
|
|
13
|
+
//# sourceMappingURL=getRelativeTime.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getRelativeTime.d.cts","names":[],"sources":["../../src/date/getRelativeTime/index.ts"],"mappings":";;;;;;;;;;iBASwB,eAAA,CAAgB,IAAA,oBAAwB,IAAA,EAAM,GAAA,GAAK,IAAA;AAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/date/getRelativeTime/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 获取相对时间描述(如"3分钟前")
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import getRelativeTime from '@tw/utils/getRelativeTime'
|
|
7
|
+
* getRelativeTime(Date.now() - 60000) // '1分钟前'
|
|
8
|
+
* getRelativeTime(Date.now() - 3600000) // '1小时前'
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
declare function getRelativeTime(date: number | string | Date, now?: Date): string;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { getRelativeTime as default };
|
|
14
|
+
//# sourceMappingURL=getRelativeTime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getRelativeTime.d.ts","names":[],"sources":["../../src/date/getRelativeTime/index.ts"],"mappings":";;;;;;;;;;iBASwB,eAAA,CAAgB,IAAA,oBAAwB,IAAA,EAAM,GAAA,GAAK,IAAA"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//#region src/date/getRelativeTime/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 获取相对时间描述(如"3分钟前")
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import getRelativeTime from '@tw/utils/getRelativeTime'
|
|
7
|
+
* getRelativeTime(Date.now() - 60000) // '1分钟前'
|
|
8
|
+
* getRelativeTime(Date.now() - 3600000) // '1小时前'
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
function getRelativeTime(date, now = /* @__PURE__ */ new Date()) {
|
|
12
|
+
const d = new Date(date);
|
|
13
|
+
const diff = now.getTime() - d.getTime();
|
|
14
|
+
const seconds = Math.floor(diff / 1e3);
|
|
15
|
+
const minutes = Math.floor(seconds / 60);
|
|
16
|
+
const hours = Math.floor(minutes / 60);
|
|
17
|
+
const days = Math.floor(hours / 24);
|
|
18
|
+
const months = Math.floor(days / 30);
|
|
19
|
+
const years = Math.floor(days / 365);
|
|
20
|
+
if (seconds < 0) return "刚刚";
|
|
21
|
+
if (seconds < 60) return `${seconds}秒前`;
|
|
22
|
+
if (minutes < 60) return `${minutes}分钟前`;
|
|
23
|
+
if (hours < 24) return `${hours}小时前`;
|
|
24
|
+
if (days < 30) return `${days}天前`;
|
|
25
|
+
if (months < 12) return `${months}个月前`;
|
|
26
|
+
return `${years}年前`;
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
export { getRelativeTime as default };
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=getRelativeTime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getRelativeTime.js","names":[],"sources":["../../src/date/getRelativeTime/index.ts"],"sourcesContent":["/**\n * 获取相对时间描述(如\"3分钟前\")\n * @example\n * ```ts\n * import getRelativeTime from '@tw/utils/getRelativeTime'\n * getRelativeTime(Date.now() - 60000) // '1分钟前'\n * getRelativeTime(Date.now() - 3600000) // '1小时前'\n * ```\n */\nexport default function getRelativeTime(date: number | string | Date, now: Date = new Date()): string {\n const d = new Date(date);\n const diff = now.getTime() - d.getTime();\n const seconds = Math.floor(diff / 1000);\n const minutes = Math.floor(seconds / 60);\n const hours = Math.floor(minutes / 60);\n const days = Math.floor(hours / 24);\n const months = Math.floor(days / 30);\n const years = Math.floor(days / 365);\n\n if (seconds < 0) return '刚刚';\n if (seconds < 60) return `${seconds}秒前`;\n if (minutes < 60) return `${minutes}分钟前`;\n if (hours < 24) return `${hours}小时前`;\n if (days < 30) return `${days}天前`;\n if (months < 12) return `${months}个月前`;\n return `${years}年前`;\n}\n"],"mappings":";;;;;;;;;;AASA,SAAwB,gBAAgB,MAA8B,sBAAY,IAAI,MAAM,EAAU;CACpG,MAAM,IAAI,IAAI,KAAK,KAAK;CACxB,MAAM,OAAO,IAAI,SAAS,GAAG,EAAE,SAAS;CACxC,MAAM,UAAU,KAAK,MAAM,OAAO,IAAK;CACvC,MAAM,UAAU,KAAK,MAAM,UAAU,GAAG;CACxC,MAAM,QAAQ,KAAK,MAAM,UAAU,GAAG;CACtC,MAAM,OAAO,KAAK,MAAM,QAAQ,GAAG;CACnC,MAAM,SAAS,KAAK,MAAM,OAAO,GAAG;CACpC,MAAM,QAAQ,KAAK,MAAM,OAAO,IAAI;AAEpC,KAAI,UAAU,EAAG,QAAO;AACxB,KAAI,UAAU,GAAI,QAAO,GAAG,QAAQ;AACpC,KAAI,UAAU,GAAI,QAAO,GAAG,QAAQ;AACpC,KAAI,QAAQ,GAAI,QAAO,GAAG,MAAM;AAChC,KAAI,OAAO,GAAI,QAAO,GAAG,KAAK;AAC9B,KAAI,SAAS,GAAI,QAAO,GAAG,OAAO;AAClC,QAAO,GAAG,MAAM"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region src/function/debounce/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 防抖函数
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import debounce from '@tw/utils/debounce'
|
|
7
|
+
* const search = debounce((keyword: string) => {
|
|
8
|
+
* console.log('搜索:', keyword)
|
|
9
|
+
* }, 300)
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
function debounce(func, wait, immediate = false) {
|
|
13
|
+
let timer = null;
|
|
14
|
+
let canImmediate = immediate;
|
|
15
|
+
return function(...args) {
|
|
16
|
+
if (canImmediate) {
|
|
17
|
+
func.apply(this, args);
|
|
18
|
+
canImmediate = false;
|
|
19
|
+
}
|
|
20
|
+
if (timer) clearTimeout(timer);
|
|
21
|
+
timer = setTimeout(() => {
|
|
22
|
+
if (!immediate) func.apply(this, args);
|
|
23
|
+
timer = null;
|
|
24
|
+
canImmediate = immediate;
|
|
25
|
+
}, wait);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
module.exports = debounce;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/function/debounce/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 防抖函数
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import debounce from '@tw/utils/debounce'
|
|
7
|
+
* const search = debounce((keyword: string) => {
|
|
8
|
+
* console.log('搜索:', keyword)
|
|
9
|
+
* }, 300)
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
declare function debounce<T extends (...args: any[]) => any>(func: T, wait: number, immediate?: boolean): (...args: Parameters<T>) => void;
|
|
13
|
+
export = debounce;
|
|
14
|
+
//# sourceMappingURL=debounce.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debounce.d.cts","names":[],"sources":["../../src/function/debounce/index.ts"],"mappings":";;;;;;;;;;;iBAUwB,QAAA,eAAuB,IAAA,gBAAA,CAC7C,IAAA,EAAM,CAAA,EACN,IAAA,UACA,SAAA,iBACK,IAAA,EAAM,UAAA,CAAW,CAAA;AAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/function/debounce/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 防抖函数
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import debounce from '@tw/utils/debounce'
|
|
7
|
+
* const search = debounce((keyword: string) => {
|
|
8
|
+
* console.log('搜索:', keyword)
|
|
9
|
+
* }, 300)
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
declare function debounce<T extends (...args: any[]) => any>(func: T, wait: number, immediate?: boolean): (...args: Parameters<T>) => void;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { debounce as default };
|
|
15
|
+
//# sourceMappingURL=debounce.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debounce.d.ts","names":[],"sources":["../../src/function/debounce/index.ts"],"mappings":";;;;;;;;;;;iBAUwB,QAAA,eAAuB,IAAA,gBAAA,CAC7C,IAAA,EAAM,CAAA,EACN,IAAA,UACA,SAAA,iBACK,IAAA,EAAM,UAAA,CAAW,CAAA"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//#region src/function/debounce/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 防抖函数
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import debounce from '@tw/utils/debounce'
|
|
7
|
+
* const search = debounce((keyword: string) => {
|
|
8
|
+
* console.log('搜索:', keyword)
|
|
9
|
+
* }, 300)
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
function debounce(func, wait, immediate = false) {
|
|
13
|
+
let timer = null;
|
|
14
|
+
let canImmediate = immediate;
|
|
15
|
+
return function(...args) {
|
|
16
|
+
if (canImmediate) {
|
|
17
|
+
func.apply(this, args);
|
|
18
|
+
canImmediate = false;
|
|
19
|
+
}
|
|
20
|
+
if (timer) clearTimeout(timer);
|
|
21
|
+
timer = setTimeout(() => {
|
|
22
|
+
if (!immediate) func.apply(this, args);
|
|
23
|
+
timer = null;
|
|
24
|
+
canImmediate = immediate;
|
|
25
|
+
}, wait);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
export { debounce as default };
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=debounce.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debounce.js","names":[],"sources":["../../src/function/debounce/index.ts"],"sourcesContent":["/**\n * 防抖函数\n * @example\n * ```ts\n * import debounce from '@tw/utils/debounce'\n * const search = debounce((keyword: string) => {\n * console.log('搜索:', keyword)\n * }, 300)\n * ```\n */\nexport default function debounce<T extends (...args: any[]) => any>(\n func: T,\n wait: number,\n immediate = false,\n): (...args: Parameters<T>) => void {\n let timer: ReturnType<typeof setTimeout> | null = null;\n let canImmediate = immediate;\n\n return function (this: unknown, ...args: Parameters<T>) {\n if (canImmediate) {\n func.apply(this, args);\n canImmediate = false;\n }\n\n if (timer) {\n clearTimeout(timer);\n }\n\n timer = setTimeout(() => {\n if (!immediate) {\n func.apply(this, args);\n }\n timer = null;\n canImmediate = immediate;\n }, wait);\n };\n}\n"],"mappings":";;;;;;;;;;;AAUA,SAAwB,SACtB,MACA,MACA,YAAY,OACsB;CAClC,IAAI,QAA8C;CAClD,IAAI,eAAe;AAEnB,QAAO,SAAyB,GAAG,MAAqB;AACtD,MAAI,cAAc;AAChB,QAAK,MAAM,MAAM,KAAK;AACtB,kBAAe;;AAGjB,MAAI,MACF,cAAa,MAAM;AAGrB,UAAQ,iBAAiB;AACvB,OAAI,CAAC,UACH,MAAK,MAAM,MAAM,KAAK;AAExB,WAAQ;AACR,kBAAe;KACd,KAAK"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//#region src/function/throttle/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 节流函数
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import throttle from '@tw/utils/throttle'
|
|
7
|
+
* const onScroll = throttle(() => {
|
|
8
|
+
* console.log('滚动事件触发')
|
|
9
|
+
* }, 200)
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
function throttle(func, wait) {
|
|
13
|
+
let timer = null;
|
|
14
|
+
let lastArgs = null;
|
|
15
|
+
return function(...args) {
|
|
16
|
+
lastArgs = args;
|
|
17
|
+
if (timer) return;
|
|
18
|
+
timer = setTimeout(() => {
|
|
19
|
+
if (lastArgs) {
|
|
20
|
+
func.apply(this, lastArgs);
|
|
21
|
+
lastArgs = null;
|
|
22
|
+
}
|
|
23
|
+
timer = null;
|
|
24
|
+
}, wait);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
module.exports = throttle;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/function/throttle/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 节流函数
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import throttle from '@tw/utils/throttle'
|
|
7
|
+
* const onScroll = throttle(() => {
|
|
8
|
+
* console.log('滚动事件触发')
|
|
9
|
+
* }, 200)
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
declare function throttle<T extends (...args: any[]) => any>(func: T, wait: number): (...args: Parameters<T>) => void;
|
|
13
|
+
export = throttle;
|
|
14
|
+
//# sourceMappingURL=throttle.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle.d.cts","names":[],"sources":["../../src/function/throttle/index.ts"],"mappings":";;;;;;;;;;;iBAUwB,QAAA,eAAuB,IAAA,gBAAA,CAC7C,IAAA,EAAM,CAAA,EACN,IAAA,eACK,IAAA,EAAM,UAAA,CAAW,CAAA;AAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/function/throttle/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 节流函数
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import throttle from '@tw/utils/throttle'
|
|
7
|
+
* const onScroll = throttle(() => {
|
|
8
|
+
* console.log('滚动事件触发')
|
|
9
|
+
* }, 200)
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
declare function throttle<T extends (...args: any[]) => any>(func: T, wait: number): (...args: Parameters<T>) => void;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { throttle as default };
|
|
15
|
+
//# sourceMappingURL=throttle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle.d.ts","names":[],"sources":["../../src/function/throttle/index.ts"],"mappings":";;;;;;;;;;;iBAUwB,QAAA,eAAuB,IAAA,gBAAA,CAC7C,IAAA,EAAM,CAAA,EACN,IAAA,eACK,IAAA,EAAM,UAAA,CAAW,CAAA"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//#region src/function/throttle/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* 节流函数
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import throttle from '@tw/utils/throttle'
|
|
7
|
+
* const onScroll = throttle(() => {
|
|
8
|
+
* console.log('滚动事件触发')
|
|
9
|
+
* }, 200)
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
function throttle(func, wait) {
|
|
13
|
+
let timer = null;
|
|
14
|
+
let lastArgs = null;
|
|
15
|
+
return function(...args) {
|
|
16
|
+
lastArgs = args;
|
|
17
|
+
if (timer) return;
|
|
18
|
+
timer = setTimeout(() => {
|
|
19
|
+
if (lastArgs) {
|
|
20
|
+
func.apply(this, lastArgs);
|
|
21
|
+
lastArgs = null;
|
|
22
|
+
}
|
|
23
|
+
timer = null;
|
|
24
|
+
}, wait);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
export { throttle as default };
|
|
29
|
+
|
|
30
|
+
//# sourceMappingURL=throttle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle.js","names":[],"sources":["../../src/function/throttle/index.ts"],"sourcesContent":["/**\n * 节流函数\n * @example\n * ```ts\n * import throttle from '@tw/utils/throttle'\n * const onScroll = throttle(() => {\n * console.log('滚动事件触发')\n * }, 200)\n * ```\n */\nexport default function throttle<T extends (...args: any[]) => any>(\n func: T,\n wait: number,\n): (...args: Parameters<T>) => void {\n let timer: ReturnType<typeof setTimeout> | null = null;\n let lastArgs: Parameters<T> | null = null;\n\n return function (this: unknown, ...args: Parameters<T>) {\n lastArgs = args;\n\n if (timer) return;\n\n timer = setTimeout(() => {\n if (lastArgs) {\n func.apply(this, lastArgs);\n lastArgs = null;\n }\n timer = null;\n }, wait);\n };\n}\n"],"mappings":";;;;;;;;;;;AAUA,SAAwB,SACtB,MACA,MACkC;CAClC,IAAI,QAA8C;CAClD,IAAI,WAAiC;AAErC,QAAO,SAAyB,GAAG,MAAqB;AACtD,aAAW;AAEX,MAAI,MAAO;AAEX,UAAQ,iBAAiB;AACvB,OAAI,UAAU;AACZ,SAAK,MAAM,MAAM,SAAS;AAC1B,eAAW;;AAEb,WAAQ;KACP,KAAK"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_base_isArray = require("./base/isArray.cjs");
|
|
3
|
+
const require_base_isString = require("./base/isString.cjs");
|
|
4
|
+
const require_base_isObject = require("./base/isObject.cjs");
|
|
5
|
+
const require_base_deepClone = require("./base/deepClone.cjs");
|
|
6
|
+
const require_function_debounce = require("./function/debounce.cjs");
|
|
7
|
+
const require_function_throttle = require("./function/throttle.cjs");
|
|
8
|
+
const require_array_unique = require("./array/unique.cjs");
|
|
9
|
+
const require_array_flatten = require("./array/flatten.cjs");
|
|
10
|
+
const require_array_chunk = require("./array/chunk.cjs");
|
|
11
|
+
const require_array_shuffle = require("./array/shuffle.cjs");
|
|
12
|
+
const require_number_randomInt = require("./number/randomInt.cjs");
|
|
13
|
+
const require_number_clamp = require("./number/clamp.cjs");
|
|
14
|
+
const require_number_formatThousands = require("./number/formatThousands.cjs");
|
|
15
|
+
const require_date_formatDate = require("./date/formatDate.cjs");
|
|
16
|
+
const require_date_getRelativeTime = require("./date/getRelativeTime.cjs");
|
|
17
|
+
const require_string_camelCase = require("./string/camelCase.cjs");
|
|
18
|
+
const require_string_kebabCase = require("./string/kebabCase.cjs");
|
|
19
|
+
const require_string_capitalize = require("./string/capitalize.cjs");
|
|
20
|
+
exports.camelCase = require_string_camelCase;
|
|
21
|
+
exports.capitalize = require_string_capitalize;
|
|
22
|
+
exports.chunk = require_array_chunk;
|
|
23
|
+
exports.clamp = require_number_clamp;
|
|
24
|
+
exports.debounce = require_function_debounce;
|
|
25
|
+
exports.deepClone = require_base_deepClone;
|
|
26
|
+
exports.flatten = require_array_flatten;
|
|
27
|
+
exports.formatDate = require_date_formatDate;
|
|
28
|
+
exports.formatThousands = require_number_formatThousands;
|
|
29
|
+
exports.getRelativeTime = require_date_getRelativeTime;
|
|
30
|
+
exports.isArray = require_base_isArray;
|
|
31
|
+
exports.isObject = require_base_isObject;
|
|
32
|
+
exports.isString = require_base_isString;
|
|
33
|
+
exports.kebabCase = require_string_kebabCase;
|
|
34
|
+
exports.randomInt = require_number_randomInt;
|
|
35
|
+
exports.shuffle = require_array_shuffle;
|
|
36
|
+
exports.throttle = require_function_throttle;
|
|
37
|
+
exports.unique = require_array_unique;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import chunk from "./array/chunk.cjs";
|
|
2
|
+
import flatten from "./array/flatten.cjs";
|
|
3
|
+
import shuffle from "./array/shuffle.cjs";
|
|
4
|
+
import unique from "./array/unique.cjs";
|
|
5
|
+
import deepClone from "./base/deepClone.cjs";
|
|
6
|
+
import isArray from "./base/isArray.cjs";
|
|
7
|
+
import isObject from "./base/isObject.cjs";
|
|
8
|
+
import isString from "./base/isString.cjs";
|
|
9
|
+
import formatDate from "./date/formatDate.cjs";
|
|
10
|
+
import getRelativeTime from "./date/getRelativeTime.cjs";
|
|
11
|
+
import debounce from "./function/debounce.cjs";
|
|
12
|
+
import throttle from "./function/throttle.cjs";
|
|
13
|
+
import randomInt from "./number/randomInt.cjs";
|
|
14
|
+
import clamp from "./number/clamp.cjs";
|
|
15
|
+
import formatThousands from "./number/formatThousands.cjs";
|
|
16
|
+
import camelCase from "./string/camelCase.cjs";
|
|
17
|
+
import kebabCase from "./string/kebabCase.cjs";
|
|
18
|
+
import capitalize from "./string/capitalize.cjs";
|
|
19
|
+
export { camelCase, capitalize, chunk, clamp, debounce, deepClone, flatten, formatDate, formatThousands, getRelativeTime, isArray, isObject, isString, kebabCase, randomInt, shuffle, throttle, unique };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import chunk from "./array/chunk.js";
|
|
2
|
+
import flatten from "./array/flatten.js";
|
|
3
|
+
import shuffle from "./array/shuffle.js";
|
|
4
|
+
import unique from "./array/unique.js";
|
|
5
|
+
import deepClone from "./base/deepClone.js";
|
|
6
|
+
import isArray from "./base/isArray.js";
|
|
7
|
+
import isObject from "./base/isObject.js";
|
|
8
|
+
import isString from "./base/isString.js";
|
|
9
|
+
import formatDate from "./date/formatDate.js";
|
|
10
|
+
import getRelativeTime from "./date/getRelativeTime.js";
|
|
11
|
+
import debounce from "./function/debounce.js";
|
|
12
|
+
import throttle from "./function/throttle.js";
|
|
13
|
+
import randomInt from "./number/randomInt.js";
|
|
14
|
+
import clamp from "./number/clamp.js";
|
|
15
|
+
import formatThousands from "./number/formatThousands.js";
|
|
16
|
+
import camelCase from "./string/camelCase.js";
|
|
17
|
+
import kebabCase from "./string/kebabCase.js";
|
|
18
|
+
import capitalize from "./string/capitalize.js";
|
|
19
|
+
export { camelCase, capitalize, chunk, clamp, debounce, deepClone, flatten, formatDate, formatThousands, getRelativeTime, isArray, isObject, isString, kebabCase, randomInt, shuffle, throttle, unique };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var TWUtils=(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function t(e){return Array.isArray(e)}function n(e){return typeof e==`string`}function r(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function i(e,t=new WeakMap){if(typeof e!=`object`||!e)return e;if(t.has(e))return t.get(e);if(Array.isArray(e)){let n=[];t.set(e,n);for(let r=0;r<e.length;r++)n[r]=i(e[r],t);return n}if(e instanceof Date)return new Date(e.getTime());if(e instanceof RegExp)return new RegExp(e.source,e.flags);if(e instanceof Map){let n=new Map;t.set(e,n);for(let[r,a]of e)n.set(r,i(a,t));return n}if(e instanceof Set){let n=new Set;t.set(e,n);for(let r of e)n.add(i(r,t));return n}if(e instanceof WeakMap)throw Error(`Cannot clone WeakMap`);if(e instanceof WeakSet)throw Error(`Cannot clone WeakSet`);if(r(e)){let n={};t.set(e,n);for(let r in e)Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=i(e[r],t));return n}return e}function a(e,t,n=!1){let r=null,i=n;return function(...a){i&&=(e.apply(this,a),!1),r&&clearTimeout(r),r=setTimeout(()=>{n||e.apply(this,a),r=null,i=n},t)}}function o(e,t){let n=null,r=null;return function(...i){r=i,!n&&(n=setTimeout(()=>{r&&=(e.apply(this,r),null),n=null},t))}}function s(e){return[...new Set(e)]}function c(e,t=1/0){if(t===0)return e;let n=[];for(let r of e)Array.isArray(r)&&t>0?n.push(...c(r,t-1)):n.push(r);return n}function l(e,t){let n=[];for(let r=0;r<e.length;r+=t)n.push(e.slice(r,r+t));return n}function u(e){let t=[...e];for(let e=t.length-1;e>0;e--){let n=Math.floor(Math.random()*(e+1));[t[e],t[n]]=[t[n],t[e]]}return t}function d(e,t){return Math.floor(Math.random()*(t-e+1))+e}function f(e,t,n){return Math.min(Math.max(e,t),n)}function p(e){let t=String(e).split(`.`);return t[0]=t[0].replace(/\B(?=(\d{3})+(?!\d))/g,`,`),t.join(`.`)}function m(e,t=`yyyy-MM-dd HH:mm:ss`){if(e==null)return`Invalid Date`;let n=/-/g,r;if(r=typeof e==`string`?new Date(n.test(e)?e.replace(n,`/`):e):new Date(e),isNaN(r.getTime()))return`Invalid Date`;let i={"M+":r.getMonth()+1,"d+":r.getDate(),"H+":r.getHours(),"h+":r.getHours()%12||12,"m+":r.getMinutes(),"s+":r.getSeconds()},a=t.match(/(y+)/);a&&(t=t.replace(a[0],String(r.getFullYear()).slice(4-a[1].length)));for(let e in i){let n=t.match(RegExp(`(${e})`));if(n){let r=String(i[e]);t=t.replace(n[0],n[1].length===1?r:r.padStart(2,`0`))}}return t}function h(e,t=new Date){let n=new Date(e),r=t.getTime()-n.getTime(),i=Math.floor(r/1e3),a=Math.floor(i/60),o=Math.floor(a/60),s=Math.floor(o/24),c=Math.floor(s/30),l=Math.floor(s/365);return i<0?`刚刚`:i<60?`${i}秒前`:a<60?`${a}分钟前`:o<24?`${o}小时前`:s<30?`${s}天前`:c<12?`${c}个月前`:`${l}年前`}function g(e,t=!1){let n=e.replace(/[-_.\s]+(\w|$)/g,(e,t)=>t.toUpperCase());return t&&n.length>0&&(n=n[0].toUpperCase()+n.slice(1)),console.log(n),n}function _(e){return e.replace(/([a-z])([A-Z])/g,`$1-$2`).replace(/([A-Z])([A-Z][a-z])/g,`$1-$2`).replace(/[-_.\s]+/g,`-`).toLowerCase()}function v(e){return e&&e.charAt(0).toUpperCase()+e.slice(1)}return e.camelCase=g,e.capitalize=v,e.chunk=l,e.clamp=f,e.debounce=a,e.deepClone=i,e.flatten=c,e.formatDate=m,e.formatThousands=p,e.getRelativeTime=h,e.isArray=t,e.isObject=r,e.isString=n,e.kebabCase=_,e.randomInt=d,e.shuffle=u,e.throttle=o,e.unique=s,e})({});
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import isArray from "./base/isArray.js";
|
|
2
|
+
import isString from "./base/isString.js";
|
|
3
|
+
import isObject from "./base/isObject.js";
|
|
4
|
+
import deepClone from "./base/deepClone.js";
|
|
5
|
+
import debounce from "./function/debounce.js";
|
|
6
|
+
import throttle from "./function/throttle.js";
|
|
7
|
+
import unique from "./array/unique.js";
|
|
8
|
+
import flatten from "./array/flatten.js";
|
|
9
|
+
import chunk from "./array/chunk.js";
|
|
10
|
+
import shuffle from "./array/shuffle.js";
|
|
11
|
+
import randomInt from "./number/randomInt.js";
|
|
12
|
+
import clamp from "./number/clamp.js";
|
|
13
|
+
import formatThousands from "./number/formatThousands.js";
|
|
14
|
+
import formatDate from "./date/formatDate.js";
|
|
15
|
+
import getRelativeTime from "./date/getRelativeTime.js";
|
|
16
|
+
import camelCase from "./string/camelCase.js";
|
|
17
|
+
import kebabCase from "./string/kebabCase.js";
|
|
18
|
+
import capitalize from "./string/capitalize.js";
|
|
19
|
+
export { camelCase, capitalize, chunk, clamp, debounce, deepClone, flatten, formatDate, formatThousands, getRelativeTime, isArray, isObject, isString, kebabCase, randomInt, shuffle, throttle, unique };
|