@d-matrix/utils 1.6.0 → 1.7.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/date.d.ts +12 -0
- package/dist/date.js +24 -1
- package/dist/dom.d.ts +7 -0
- package/dist/dom.js +12 -1
- package/dist/react/useCopyToClipboard.js +1 -1
- package/package.json +1 -1
- package/readme.md +35 -1
package/dist/date.d.ts
CHANGED
|
@@ -6,3 +6,15 @@
|
|
|
6
6
|
* @return {number[]} an array of numbers representing the range of years
|
|
7
7
|
*/
|
|
8
8
|
export declare const rangeOfYears: (start: number, end?: number) => number[];
|
|
9
|
+
export interface RecentYearOption {
|
|
10
|
+
label: string;
|
|
11
|
+
value: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 获取最近n年, 从大到小
|
|
15
|
+
* @param recentYears 最近几年
|
|
16
|
+
* @param type 控制返回值类型
|
|
17
|
+
* @param suffix 后缀,默认为'年'
|
|
18
|
+
*/
|
|
19
|
+
export declare function getRecentYears(recentYears: number, type: 'number[]'): number[];
|
|
20
|
+
export declare function getRecentYears(recentYears: number, type: 'object[]'): RecentYearOption[];
|
package/dist/date.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.rangeOfYears = void 0;
|
|
3
|
+
exports.getRecentYears = exports.rangeOfYears = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Generates an array of numbers representing a range of years between the start year and the end year.
|
|
6
6
|
*
|
|
@@ -15,3 +15,26 @@ var rangeOfYears = function (start, end) {
|
|
|
15
15
|
.map(function (year, index) { return year + index; });
|
|
16
16
|
};
|
|
17
17
|
exports.rangeOfYears = rangeOfYears;
|
|
18
|
+
function getRecentYears(recentYears, type, suffix) {
|
|
19
|
+
if (suffix === void 0) { suffix = '年'; }
|
|
20
|
+
var thisYear = new Date().getFullYear();
|
|
21
|
+
if (type === 'number[]') {
|
|
22
|
+
var result = [];
|
|
23
|
+
for (var i = 0; i < recentYears; i++) {
|
|
24
|
+
result.push(thisYear - i);
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
if (type === 'object[]') {
|
|
29
|
+
var result = [];
|
|
30
|
+
for (var i = 0; i < recentYears; i++) {
|
|
31
|
+
result.push({
|
|
32
|
+
value: thisYear - i,
|
|
33
|
+
label: "" + (thisYear - i) + suffix,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
throw new Error('type must be "number[]" or "object[]"');
|
|
39
|
+
}
|
|
40
|
+
exports.getRecentYears = getRecentYears;
|
package/dist/dom.d.ts
CHANGED
|
@@ -2,3 +2,10 @@
|
|
|
2
2
|
* 元素滚动条滚动到顶部
|
|
3
3
|
*/
|
|
4
4
|
export declare function scrollToTop(element: Element | null | undefined): void;
|
|
5
|
+
/**
|
|
6
|
+
* Strips HTML tags from a given string and returns the plain text content.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} html - The HTML string to strip tags from.
|
|
9
|
+
* @return {string} The plain text content of the HTML string.
|
|
10
|
+
*/
|
|
11
|
+
export declare function strip(html: string): string;
|
package/dist/dom.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.scrollToTop = void 0;
|
|
3
|
+
exports.strip = exports.scrollToTop = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* 元素滚动条滚动到顶部
|
|
6
6
|
*/
|
|
@@ -19,3 +19,14 @@ function scrollToTop(element) {
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
exports.scrollToTop = scrollToTop;
|
|
22
|
+
/**
|
|
23
|
+
* Strips HTML tags from a given string and returns the plain text content.
|
|
24
|
+
*
|
|
25
|
+
* @param {string} html - The HTML string to strip tags from.
|
|
26
|
+
* @return {string} The plain text content of the HTML string.
|
|
27
|
+
*/
|
|
28
|
+
function strip(html) {
|
|
29
|
+
var doc = new DOMParser().parseFromString(html !== null && html !== void 0 ? html : '', 'text/html');
|
|
30
|
+
return doc.body.textContent || '';
|
|
31
|
+
}
|
|
32
|
+
exports.strip = strip;
|
|
@@ -42,7 +42,7 @@ function useCopyToClipboard(props) {
|
|
|
42
42
|
}, timeout);
|
|
43
43
|
})
|
|
44
44
|
.catch(function (error) { return onError === null || onError === void 0 ? void 0 : onError(error); });
|
|
45
|
-
}, []);
|
|
45
|
+
}, [onCopy, onError, timeout]);
|
|
46
46
|
return { isCopied: isCopied, copyToClipboard: copyToClipboard };
|
|
47
47
|
}
|
|
48
48
|
exports.useCopyToClipboard = useCopyToClipboard;
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -86,7 +86,7 @@ const Test = () => {
|
|
|
86
86
|
|
|
87
87
|
- `useCopyToClipboard(props?: UseCopyToClipboardProps)`
|
|
88
88
|
|
|
89
|
-
复制文本到剪切板, 用法见[测试](
|
|
89
|
+
复制文本到剪切板, 用法见[测试](./tests/react.cy.tsx)
|
|
90
90
|
|
|
91
91
|
### dom
|
|
92
92
|
|
|
@@ -94,12 +94,46 @@ const Test = () => {
|
|
|
94
94
|
|
|
95
95
|
元素滚动条滚动到顶部,对老旧浏览器做了兼容,见[浏览器兼容性](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop#browser_compatibility)。
|
|
96
96
|
|
|
97
|
+
- `strip(html: string): string`
|
|
98
|
+
|
|
99
|
+
从字符串中去除 HTML 标签并返回纯文本内容。
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
import { dom } from '@d-matrix/utils';
|
|
103
|
+
|
|
104
|
+
dom.strip('测试<em>高亮</em>测试'); // '测试高亮测试'
|
|
105
|
+
```
|
|
106
|
+
|
|
97
107
|
### date
|
|
98
108
|
|
|
99
109
|
- `rangeOfYears(start: number, end: number = new Date().getFullYear()): number[]`
|
|
100
110
|
|
|
101
111
|
创建`start`和`end`之间的年份数组。
|
|
102
112
|
|
|
113
|
+
- `getRecentYears()`
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
export function getRecentYears(recentYears: number, type: 'number[]'): number[];
|
|
117
|
+
export function getRecentYears(recentYears: number, type: 'object[]'): RecentYearOption[];
|
|
118
|
+
export function getRecentYears(recentYears: number, type: 'number[]' | 'object[]', suffix = '年'): number[] | RecentYearOption[]
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
获取最近n年
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
import { date } from '@d-matrix/utils';
|
|
125
|
+
|
|
126
|
+
getRecentYears(5, 'number[]'); // [2024, 2023, 2022, 2021, 2020]
|
|
127
|
+
getRecentYears(5, 'object[]');
|
|
128
|
+
// [
|
|
129
|
+
// { value: 2024, label: '2024年' },
|
|
130
|
+
// { value: 2023, label: '2023年' },
|
|
131
|
+
// { value: 2022, label: '2022年' },
|
|
132
|
+
// { value: 2021, label: '2021年' },
|
|
133
|
+
// { value: 2020, label: '2020年' },
|
|
134
|
+
// ]
|
|
135
|
+
```
|
|
136
|
+
|
|
103
137
|
### types
|
|
104
138
|
|
|
105
139
|
- `WithOptional<T, K extends keyof T>`
|