@d-matrix/utils 1.6.1 → 1.8.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 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;
@@ -0,0 +1,12 @@
1
+ import React from 'react';
2
+ /**
3
+ * react component 的增强类
4
+ */
5
+ export declare class EnhancedComponent<P, S> extends React.Component<P, S> {
6
+ /**
7
+ * setState 方法的同步版本
8
+ * @param state
9
+ * @returns Promise
10
+ */
11
+ protected setStateAsync<K extends keyof S>(state: ((prevState: Readonly<S>, props: Readonly<P>) => Pick<S, K> | S | null) | (Pick<S, K> | S | null)): Promise<void>;
12
+ }
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __importDefault = (this && this.__importDefault) || function (mod) {
18
+ return (mod && mod.__esModule) ? mod : { "default": mod };
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.EnhancedComponent = void 0;
22
+ var react_1 = __importDefault(require("react"));
23
+ /**
24
+ * react component 的增强类
25
+ */
26
+ var EnhancedComponent = /** @class */ (function (_super) {
27
+ __extends(EnhancedComponent, _super);
28
+ function EnhancedComponent() {
29
+ return _super !== null && _super.apply(this, arguments) || this;
30
+ }
31
+ /**
32
+ * setState 方法的同步版本
33
+ * @param state
34
+ * @returns Promise
35
+ */
36
+ EnhancedComponent.prototype.setStateAsync = function (state) {
37
+ var _this = this;
38
+ return new Promise(function (resolve) {
39
+ _this.setState(state, resolve);
40
+ });
41
+ };
42
+ return EnhancedComponent;
43
+ }(react_1.default.Component));
44
+ exports.EnhancedComponent = EnhancedComponent;
@@ -3,3 +3,4 @@ export * from './useStateCallback';
3
3
  export * from './renderToString';
4
4
  export * from './useIsMounted';
5
5
  export * from './useCopyToClipboard';
6
+ export * from './enhancedComponent';
@@ -15,3 +15,4 @@ __exportStar(require("./useStateCallback"), exports);
15
15
  __exportStar(require("./renderToString"), exports);
16
16
  __exportStar(require("./useIsMounted"), exports);
17
17
  __exportStar(require("./useCopyToClipboard"), exports);
18
+ __exportStar(require("./enhancedComponent"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-matrix/utils",
3
- "version": "1.6.1",
3
+ "version": "1.8.0",
4
4
  "description": "A dozen of utils for Front-End Development",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -11,7 +11,8 @@
11
11
  "cy:component": "cypress run --component --spec",
12
12
  "cy:component:all": "cypress run --component",
13
13
  "test:tsd": "tsd",
14
- "postversion": "git push && git push --tags"
14
+ "postversion": "git push && git push --tags",
15
+ "cnpm:sync": "cnpm sync %npm_package_name%"
15
16
  },
16
17
  "engines": {
17
18
  "node": ">=16.20.0"
@@ -42,6 +43,7 @@
42
43
  },
43
44
  "devDependencies": {
44
45
  "@vitejs/plugin-react": "^4.2.1",
46
+ "cnpm": "^9.4.0",
45
47
  "cypress": "^13.6.6",
46
48
  "expect-type": "^0.19.0",
47
49
  "rimraf": "^5.0.5",
package/readme.md CHANGED
@@ -88,18 +88,79 @@ const Test = () => {
88
88
 
89
89
  复制文本到剪切板, 用法见[测试](./tests/react.cy.tsx)
90
90
 
91
+ - `EnhancedComponent.prototype.setStateAsync(state)`
92
+
93
+ setState 方法的同步版本
94
+
95
+ ```ts
96
+ import { react } from '@d-matrix/utils';
97
+
98
+ class TestComponent extends EnhancedComponent<unknown, { pageIndex: number }> {
99
+ state = {
100
+ pageIndex: 1,
101
+ };
102
+
103
+ async onClick() {
104
+ await this.setStateAsync({ pageIndex: 2 });
105
+ console.log(this.state.pageIndex); // 2
106
+ }
107
+
108
+ render() {
109
+ return (
110
+ <button data-cy="test-button" onClick={() => this.onClick()}>
111
+ click
112
+ </button>
113
+ );
114
+ }
115
+ }
116
+ ```
117
+
91
118
  ### dom
92
119
 
93
120
  - `scrollToTop(element: Element | null | undefined): void`
94
121
 
95
122
  元素滚动条滚动到顶部,对老旧浏览器做了兼容,见[浏览器兼容性](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop#browser_compatibility)。
96
123
 
124
+ - `strip(html: string): string`
125
+
126
+ 从字符串中去除 HTML 标签并返回纯文本内容。
127
+
128
+ ```ts
129
+ import { dom } from '@d-matrix/utils';
130
+
131
+ dom.strip('测试<em>高亮</em>测试'); // '测试高亮测试'
132
+ ```
133
+
97
134
  ### date
98
135
 
99
136
  - `rangeOfYears(start: number, end: number = new Date().getFullYear()): number[]`
100
137
 
101
138
  创建`start`和`end`之间的年份数组。
102
139
 
140
+ - `getRecentYears()`
141
+
142
+ ```ts
143
+ export function getRecentYears(recentYears: number, type: 'number[]'): number[];
144
+ export function getRecentYears(recentYears: number, type: 'object[]'): RecentYearOption[];
145
+ export function getRecentYears(recentYears: number, type: 'number[]' | 'object[]', suffix = '年'): number[] | RecentYearOption[]
146
+ ```
147
+
148
+ 获取最近n年
149
+
150
+ ```ts
151
+ import { date } from '@d-matrix/utils';
152
+
153
+ getRecentYears(5, 'number[]'); // [2024, 2023, 2022, 2021, 2020]
154
+ getRecentYears(5, 'object[]');
155
+ // [
156
+ // { value: 2024, label: '2024年' },
157
+ // { value: 2023, label: '2023年' },
158
+ // { value: 2022, label: '2022年' },
159
+ // { value: 2021, label: '2021年' },
160
+ // { value: 2020, label: '2020年' },
161
+ // ]
162
+ ```
163
+
103
164
  ### types
104
165
 
105
166
  - `WithOptional<T, K extends keyof T>`