@d-matrix/utils 1.12.0 → 1.13.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/clipboard.js CHANGED
@@ -80,6 +80,7 @@ const legacyWriteText = (text) => {
80
80
  if (process.env.NODE_ENV === 'development') {
81
81
  console.error('复制文本失败', error);
82
82
  }
83
+ throw error;
83
84
  }
84
85
  finally {
85
86
  document.body.removeChild($textarea);
package/dist/date.d.ts CHANGED
@@ -1,7 +1,4 @@
1
- export interface RecentYearOption {
2
- label: string;
3
- value: number;
4
- }
1
+ import { i18n } from './i18n';
5
2
  /**
6
3
  * Generates an array of numbers representing a range of years between the start year and the end year.
7
4
  *
@@ -10,7 +7,15 @@ export interface RecentYearOption {
10
7
  * @return {number[]} an array of numbers representing the range of years
11
8
  */
12
9
  export declare function rangeOfYears(start: number, end?: number): number[];
13
- export declare type GetRecentYearsOptions = {
10
+ export declare enum YearOptionKind {
11
+ Numbers = 0,
12
+ Objects = 1
13
+ }
14
+ export interface YearOption {
15
+ label: string;
16
+ value: number;
17
+ }
18
+ export declare type GetYearsOptions = {
14
19
  startYear?: number;
15
20
  recentYears?: number;
16
21
  endYear?: number;
@@ -20,9 +25,10 @@ export declare type GetRecentYearsOptions = {
20
25
  * 获取n年, 从大到小
21
26
  * @param options
22
27
  */
23
- export declare function getYears(options: GetRecentYearsOptions & {
24
- type: 'number[]';
28
+ export declare function getYears(options: GetYearsOptions & {
29
+ type: YearOptionKind.Numbers;
25
30
  }): number[];
26
- export declare function getYears(options: GetRecentYearsOptions & {
27
- type: 'object[]';
28
- }): RecentYearOption[];
31
+ export declare function getYears(options: GetYearsOptions & {
32
+ type: YearOptionKind.Objects;
33
+ }): YearOption[];
34
+ export declare const dayOfWeek: (num: number, lang?: keyof typeof i18n) => string;
package/dist/date.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { i18n } from './i18n';
1
2
  /**
2
3
  * Generates an array of numbers representing a range of years between the start year and the end year.
3
4
  *
@@ -10,6 +11,11 @@ export function rangeOfYears(start, end = new Date().getFullYear()) {
10
11
  .fill(start)
11
12
  .map((year, index) => year + index);
12
13
  }
14
+ export var YearOptionKind;
15
+ (function (YearOptionKind) {
16
+ YearOptionKind[YearOptionKind["Numbers"] = 0] = "Numbers";
17
+ YearOptionKind[YearOptionKind["Objects"] = 1] = "Objects";
18
+ })(YearOptionKind || (YearOptionKind = {}));
13
19
  export function getYears(options) {
14
20
  const { recentYears = 0, startYear, endYear, suffix = '年', type } = options;
15
21
  const endY = endYear ? endYear : new Date().getFullYear();
@@ -29,14 +35,14 @@ export function getYears(options) {
29
35
  return [];
30
36
  }
31
37
  }
32
- if (type === 'number[]') {
38
+ if (type === YearOptionKind.Numbers) {
33
39
  const result = [];
34
40
  for (let i = 0; i < ranges; i++) {
35
41
  result.push(endY - i);
36
42
  }
37
43
  return result;
38
44
  }
39
- if (type === 'object[]') {
45
+ if (type === YearOptionKind.Objects) {
40
46
  const result = [];
41
47
  for (let i = 0; i < ranges; i++) {
42
48
  result.push({
@@ -46,5 +52,11 @@ export function getYears(options) {
46
52
  }
47
53
  return result;
48
54
  }
49
- throw new Error('type must be "number[]" or "object[]"');
55
+ throw new Error('type must be enum: YearOptionKind.Numbers or YearOptionKind.Objects');
50
56
  }
57
+ export const dayOfWeek = (num, lang = 'zh') => {
58
+ if (!Number.isInteger(num)) {
59
+ throw new Error('请输入一个整数');
60
+ }
61
+ return i18n[lang].dayOfWeek[num % 7];
62
+ };
package/dist/i18n.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export declare const i18n: {
2
+ readonly en: {
3
+ readonly months: readonly ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
4
+ readonly dayOfWeek: readonly ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
5
+ };
6
+ readonly zh: {
7
+ readonly months: readonly ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"];
8
+ readonly dayOfWeek: readonly ["日", "一", "二", "三", "四", "五", "六"];
9
+ };
10
+ };
package/dist/i18n.js ADDED
@@ -0,0 +1,12 @@
1
+ export const i18n = {
2
+ en: {
3
+ // English
4
+ months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
5
+ dayOfWeek: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
6
+ },
7
+ zh: {
8
+ // Simplified Chinese (简体中文)
9
+ months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
10
+ dayOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
11
+ },
12
+ };
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@d-matrix/utils",
3
3
  "sideEffects": false,
4
- "version": "1.12.0",
4
+ "version": "1.13.0",
5
5
  "description": "A dozen of utils for Front-End Development",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
9
  "prebuild": "npm run clean",
10
10
  "postpublish": "echo \"wait for 3 seconds, then sync cnpm\" && npm run wait3s && npm run cnpm:sync",
11
+ "prepublishOnly": "npm run build",
11
12
  "clean": "rimraf dist",
12
13
  "cy:open": "cypress open",
13
14
  "cy:component": "cypress run --component --spec",
package/readme.md CHANGED
@@ -150,7 +150,17 @@ dom.strip('测试<em>高亮</em>测试'); // '测试高亮测试'
150
150
  - `getYears()`
151
151
 
152
152
  ```ts
153
- export type GetRecentYearsOptions = {
153
+ export interface YearOption {
154
+ label: string;
155
+ value: number;
156
+ }
157
+
158
+ export enum YearOptionKind {
159
+ Numbers,
160
+ Objects,
161
+ }
162
+
163
+ export type GetYearsOptions = {
154
164
  // 开始年份
155
165
  startYear?: number;
156
166
  // 最近几年
@@ -161,12 +171,12 @@ export type GetRecentYearsOptions = {
161
171
  suffix?: string;
162
172
  };
163
173
 
164
- export function getYears(options: GetRecentYearsOptions & { type: 'number[]' }): number[];
165
- export function getYears(options: GetRecentYearsOptions & { type: 'object[]' }): RecentYearOption[];
166
- export function getYears(options: GetRecentYearsOptions & { type: 'object[]' | 'number[]' }): number[] | RecentYearOption[]
174
+ export function getYears(options: GetYearsOptions & { type: YearOptionKind.Numbers }): number[];
175
+ export function getYears(options: GetYearsOptions & { type: YearOptionKind.Objects }): YearOption[];
176
+ export function getYears(options: GetYearsOptions & { type: YearOptionKind }): number[] | YearOption[]
167
177
  ```
168
178
 
169
- 获取n年,`type`传`number[]`,返回`[2023, 2022, 2021]`数字数组;`type`传`object[]`,返回如下的对象数组
179
+ 获取n年,`type`传`YearOptionKind.Numbers`,返回`[2023, 2022, 2021]`数字数组;`type`传`YearOptionKind.Objects`,返回如下的对象数组
170
180
 
171
181
  ```sh
172
182
  [
@@ -178,6 +188,14 @@ export function getYears(options: GetRecentYearsOptions & { type: 'object[]' | '
178
188
 
179
189
  更多用法,见[测试用例](./tests/date.cy.ts)
180
190
 
191
+ - `dayOfWeek(num: number, lang: keyof typeof i18n = 'zh'): string`
192
+
193
+ 返回星期几, `lang`仅支持`zh`和`en`, `num`必须为正整数,否则报错
194
+
195
+ ```js
196
+ dayOfWeek(0) // "日"
197
+ ```
198
+
181
199
  ### types
182
200
 
183
201
  - `WithOptional<T, K extends keyof T>`