hsu-utils 0.0.47 → 0.0.49

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.
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  *
3
- * hsu-utils v0.0.46
3
+ * hsu-utils v0.0.48
4
4
  *
5
5
  * some front-end utils
6
6
  *
@@ -0,0 +1,10 @@
1
+ import { Dayjs } from 'dayjs';
2
+ export type DateRangeType = 'past' | 'future' | 'today' | 'thisWeek' | 'thisMonth' | 'thisQuarter' | 'thisYear';
3
+ export interface GetDateRangeOptions {
4
+ amount?: number;
5
+ type: DateRangeType;
6
+ baseDate?: string | Date | Dayjs;
7
+ unit?: 'day' | 'week' | 'month' | 'year';
8
+ }
9
+ export type DateRangeResult = [string, string];
10
+ export default function getDateRange(options: GetDateRangeOptions): DateRangeResult;
@@ -0,0 +1,75 @@
1
+ import dayjs from 'dayjs';
2
+ import quarterOfYear from 'dayjs/plugin/quarterOfYear';
3
+ import weekOfYear from 'dayjs/plugin/weekOfYear';
4
+ dayjs.extend(quarterOfYear);
5
+ dayjs.extend(weekOfYear);
6
+ function getFormat(type, unit = 'day') {
7
+ switch (type) {
8
+ case 'today':
9
+ return 'YYYY-MM-DD';
10
+ case 'thisWeek':
11
+ return 'YYYY-MM-DD';
12
+ case 'thisMonth':
13
+ return 'YYYY-MM';
14
+ case 'thisQuarter':
15
+ return 'YYYY-[Q]Q';
16
+ case 'thisYear':
17
+ return 'YYYY';
18
+ case 'past':
19
+ case 'future':
20
+ default:
21
+ break;
22
+ }
23
+ switch (unit) {
24
+ case 'year':
25
+ return 'YYYY';
26
+ case 'month':
27
+ return 'YYYY-MM';
28
+ case 'week':
29
+ case 'day':
30
+ default:
31
+ return 'YYYY-MM-DD';
32
+ }
33
+ }
34
+ export default function getDateRange(options) {
35
+ const { type, amount = 0, baseDate, unit = 'day' } = options;
36
+ const base = baseDate ? dayjs(baseDate) : dayjs();
37
+ let minDate;
38
+ let maxDate;
39
+ switch (type) {
40
+ case 'past':
41
+ maxDate = base.endOf(unit);
42
+ minDate = base.subtract(amount, unit).startOf(unit);
43
+ break;
44
+ case 'future':
45
+ minDate = base.startOf(unit);
46
+ maxDate = base.add(amount, unit).endOf(unit);
47
+ break;
48
+ case 'today':
49
+ minDate = base.startOf('day');
50
+ maxDate = base.endOf('day');
51
+ break;
52
+ case 'thisWeek':
53
+ minDate = base.startOf('week');
54
+ maxDate = base.endOf('week');
55
+ break;
56
+ case 'thisMonth':
57
+ minDate = base.startOf('month');
58
+ maxDate = base.endOf('month');
59
+ break;
60
+ case 'thisQuarter':
61
+ minDate = base.startOf('quarter');
62
+ maxDate = base.endOf('quarter');
63
+ break;
64
+ case 'thisYear':
65
+ minDate = base.startOf('year');
66
+ maxDate = base.endOf('year');
67
+ break;
68
+ default:
69
+ throw new Error(`不支持的类型: ${type}`);
70
+ }
71
+ const format = getFormat(type, unit);
72
+ const min = minDate.format(format);
73
+ const max = maxDate.format(format);
74
+ return [min, max];
75
+ }
@@ -2,7 +2,7 @@ interface Font {
2
2
  style?: string;
3
3
  weight?: string;
4
4
  size?: number;
5
- fontFamily?: string;
5
+ family?: string;
6
6
  }
7
7
  export interface LoadFontOptions {
8
8
  ctx?: CanvasRenderingContext2D;
@@ -10,10 +10,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  export default function loadFont(options) {
11
11
  return __awaiter(this, void 0, void 0, function* () {
12
12
  const { ctx, font = {}, text } = options;
13
- const { style = 'normal', weight = 'normal', size = 10, fontFamily = 'sans-serif' } = font;
14
- yield document.fonts.load(`${style} ${weight} ${size}px ${fontFamily}`);
13
+ const { style = 'normal', weight = 'normal', size = 10, family = 'sans-serif' } = font;
14
+ yield document.fonts.load(`${style} ${weight} ${size}px ${family}`);
15
15
  const _ctx = ctx || document.createElement('canvas').getContext('2d');
16
- _ctx.font = `${style} ${weight} ${size}px ${fontFamily}`;
16
+ _ctx.font = `${style} ${weight} ${size}px ${family}`;
17
17
  _ctx.fillText(text || '', -999, -999);
18
18
  yield new Promise(requestAnimationFrame);
19
19
  });
package/es/index.d.ts CHANGED
@@ -11,6 +11,9 @@ import array_is_includes from './ArrayIsIncludes';
11
11
  import generateRandomStr from './GenerateRandomStr';
12
12
  import getTimeDifference from './GetTimeDifference';
13
13
  import loadFont from './LoadFont';
14
- export { console_table, deepCopy, Equal, Typeof, get_string_size, get_string_size_async, loadFont, ConvertNumbers, loadImage, RenderPDF, downloadFile, array_is_includes, generateRandomStr, getTimeDifference };
14
+ import getDateRange from './GetDateRange';
15
+ export { console_table, deepCopy, Equal, Typeof, get_string_size, get_string_size_async, loadFont, ConvertNumbers, loadImage, RenderPDF, downloadFile, array_is_includes, generateRandomStr, getTimeDifference, getDateRange };
15
16
  import { ConsoleData } from './ConsoleTable';
16
17
  export type { ConsoleData };
18
+ import type { DateRangeType, GetDateRangeOptions, DateRangeResult } from './GetDateRange';
19
+ export type { DateRangeType, GetDateRangeOptions, DateRangeResult };
package/es/index.js CHANGED
@@ -11,4 +11,5 @@ import array_is_includes from './ArrayIsIncludes';
11
11
  import generateRandomStr from './GenerateRandomStr';
12
12
  import getTimeDifference from './GetTimeDifference';
13
13
  import loadFont from './LoadFont';
14
- export { console_table, deepCopy, Equal, Typeof, get_string_size, get_string_size_async, loadFont, ConvertNumbers, loadImage, RenderPDF, downloadFile, array_is_includes, generateRandomStr, getTimeDifference };
14
+ import getDateRange from './GetDateRange';
15
+ export { console_table, deepCopy, Equal, Typeof, get_string_size, get_string_size_async, loadFont, ConvertNumbers, loadImage, RenderPDF, downloadFile, array_is_includes, generateRandomStr, getTimeDifference, getDateRange };
@@ -0,0 +1,10 @@
1
+ import { Dayjs } from 'dayjs';
2
+ export type DateRangeType = 'past' | 'future' | 'today' | 'thisWeek' | 'thisMonth' | 'thisQuarter' | 'thisYear';
3
+ export interface GetDateRangeOptions {
4
+ amount?: number;
5
+ type: DateRangeType;
6
+ baseDate?: string | Date | Dayjs;
7
+ unit?: 'day' | 'week' | 'month' | 'year';
8
+ }
9
+ export type DateRangeResult = [string, string];
10
+ export default function getDateRange(options: GetDateRangeOptions): DateRangeResult;
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var dayjs_1 = __importDefault(require("dayjs"));
7
+ var quarterOfYear_1 = __importDefault(require("dayjs/plugin/quarterOfYear"));
8
+ var weekOfYear_1 = __importDefault(require("dayjs/plugin/weekOfYear"));
9
+ dayjs_1.default.extend(quarterOfYear_1.default);
10
+ dayjs_1.default.extend(weekOfYear_1.default);
11
+ function getFormat(type, unit) {
12
+ if (unit === void 0) { unit = 'day'; }
13
+ switch (type) {
14
+ case 'today':
15
+ return 'YYYY-MM-DD';
16
+ case 'thisWeek':
17
+ return 'YYYY-MM-DD';
18
+ case 'thisMonth':
19
+ return 'YYYY-MM';
20
+ case 'thisQuarter':
21
+ return 'YYYY-[Q]Q';
22
+ case 'thisYear':
23
+ return 'YYYY';
24
+ case 'past':
25
+ case 'future':
26
+ default:
27
+ break;
28
+ }
29
+ switch (unit) {
30
+ case 'year':
31
+ return 'YYYY';
32
+ case 'month':
33
+ return 'YYYY-MM';
34
+ case 'week':
35
+ case 'day':
36
+ default:
37
+ return 'YYYY-MM-DD';
38
+ }
39
+ }
40
+ function getDateRange(options) {
41
+ var type = options.type, _a = options.amount, amount = _a === void 0 ? 0 : _a, baseDate = options.baseDate, _b = options.unit, unit = _b === void 0 ? 'day' : _b;
42
+ var base = baseDate ? (0, dayjs_1.default)(baseDate) : (0, dayjs_1.default)();
43
+ var minDate;
44
+ var maxDate;
45
+ switch (type) {
46
+ case 'past':
47
+ maxDate = base.endOf(unit);
48
+ minDate = base.subtract(amount, unit).startOf(unit);
49
+ break;
50
+ case 'future':
51
+ minDate = base.startOf(unit);
52
+ maxDate = base.add(amount, unit).endOf(unit);
53
+ break;
54
+ case 'today':
55
+ minDate = base.startOf('day');
56
+ maxDate = base.endOf('day');
57
+ break;
58
+ case 'thisWeek':
59
+ minDate = base.startOf('week');
60
+ maxDate = base.endOf('week');
61
+ break;
62
+ case 'thisMonth':
63
+ minDate = base.startOf('month');
64
+ maxDate = base.endOf('month');
65
+ break;
66
+ case 'thisQuarter':
67
+ minDate = base.startOf('quarter');
68
+ maxDate = base.endOf('quarter');
69
+ break;
70
+ case 'thisYear':
71
+ minDate = base.startOf('year');
72
+ maxDate = base.endOf('year');
73
+ break;
74
+ default:
75
+ throw new Error("\u4E0D\u652F\u6301\u7684\u7C7B\u578B: ".concat(type));
76
+ }
77
+ var format = getFormat(type, unit);
78
+ var min = minDate.format(format);
79
+ var max = maxDate.format(format);
80
+ return [min, max];
81
+ }
82
+ exports.default = getDateRange;
@@ -2,7 +2,7 @@ interface Font {
2
2
  style?: string;
3
3
  weight?: string;
4
4
  size?: number;
5
- fontFamily?: string;
5
+ family?: string;
6
6
  }
7
7
  export interface LoadFontOptions {
8
8
  ctx?: CanvasRenderingContext2D;
@@ -38,17 +38,17 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  function loadFont(options) {
40
40
  return __awaiter(this, void 0, void 0, function () {
41
- var ctx, _a, font, text, _b, style, _c, weight, _d, size, _e, fontFamily, _ctx;
41
+ var ctx, _a, font, text, _b, style, _c, weight, _d, size, _e, family, _ctx;
42
42
  return __generator(this, function (_f) {
43
43
  switch (_f.label) {
44
44
  case 0:
45
45
  ctx = options.ctx, _a = options.font, font = _a === void 0 ? {} : _a, text = options.text;
46
- _b = font.style, style = _b === void 0 ? 'normal' : _b, _c = font.weight, weight = _c === void 0 ? 'normal' : _c, _d = font.size, size = _d === void 0 ? 10 : _d, _e = font.fontFamily, fontFamily = _e === void 0 ? 'sans-serif' : _e;
47
- return [4, document.fonts.load("".concat(style, " ").concat(weight, " ").concat(size, "px ").concat(fontFamily))];
46
+ _b = font.style, style = _b === void 0 ? 'normal' : _b, _c = font.weight, weight = _c === void 0 ? 'normal' : _c, _d = font.size, size = _d === void 0 ? 10 : _d, _e = font.family, family = _e === void 0 ? 'sans-serif' : _e;
47
+ return [4, document.fonts.load("".concat(style, " ").concat(weight, " ").concat(size, "px ").concat(family))];
48
48
  case 1:
49
49
  _f.sent();
50
50
  _ctx = ctx || document.createElement('canvas').getContext('2d');
51
- _ctx.font = "".concat(style, " ").concat(weight, " ").concat(size, "px ").concat(fontFamily);
51
+ _ctx.font = "".concat(style, " ").concat(weight, " ").concat(size, "px ").concat(family);
52
52
  _ctx.fillText(text || '', -999, -999);
53
53
  return [4, new Promise(requestAnimationFrame)];
54
54
  case 2:
package/lib/index.d.ts CHANGED
@@ -11,6 +11,9 @@ import array_is_includes from './ArrayIsIncludes';
11
11
  import generateRandomStr from './GenerateRandomStr';
12
12
  import getTimeDifference from './GetTimeDifference';
13
13
  import loadFont from './LoadFont';
14
- export { console_table, deepCopy, Equal, Typeof, get_string_size, get_string_size_async, loadFont, ConvertNumbers, loadImage, RenderPDF, downloadFile, array_is_includes, generateRandomStr, getTimeDifference };
14
+ import getDateRange from './GetDateRange';
15
+ export { console_table, deepCopy, Equal, Typeof, get_string_size, get_string_size_async, loadFont, ConvertNumbers, loadImage, RenderPDF, downloadFile, array_is_includes, generateRandomStr, getTimeDifference, getDateRange };
15
16
  import { ConsoleData } from './ConsoleTable';
16
17
  export type { ConsoleData };
18
+ import type { DateRangeType, GetDateRangeOptions, DateRangeResult } from './GetDateRange';
19
+ export type { DateRangeType, GetDateRangeOptions, DateRangeResult };
package/lib/index.js CHANGED
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.getTimeDifference = exports.generateRandomStr = exports.array_is_includes = exports.downloadFile = exports.RenderPDF = exports.loadImage = exports.ConvertNumbers = exports.loadFont = exports.get_string_size_async = exports.get_string_size = exports.Typeof = exports.Equal = exports.deepCopy = exports.console_table = void 0;
29
+ exports.getDateRange = exports.getTimeDifference = exports.generateRandomStr = exports.array_is_includes = exports.downloadFile = exports.RenderPDF = exports.loadImage = exports.ConvertNumbers = exports.loadFont = exports.get_string_size_async = exports.get_string_size = exports.Typeof = exports.Equal = exports.deepCopy = exports.console_table = void 0;
30
30
  var ConsoleTable_1 = __importDefault(require("./ConsoleTable"));
31
31
  exports.console_table = ConsoleTable_1.default;
32
32
  var DeepCopy_1 = __importDefault(require("./DeepCopy"));
@@ -54,3 +54,5 @@ var GetTimeDifference_1 = __importDefault(require("./GetTimeDifference"));
54
54
  exports.getTimeDifference = GetTimeDifference_1.default;
55
55
  var LoadFont_1 = __importDefault(require("./LoadFont"));
56
56
  exports.loadFont = LoadFont_1.default;
57
+ var GetDateRange_1 = __importDefault(require("./GetDateRange"));
58
+ exports.getDateRange = GetDateRange_1.default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hsu-utils",
3
- "version": "0.0.47",
3
+ "version": "0.0.49",
4
4
  "description": "some front-end utils",
5
5
  "repository": "git@github.com:VitaTsui/hsu-utils.git",
6
6
  "author": "VitaHsu <vitahsu7@gmail.com>",
@@ -54,6 +54,7 @@
54
54
  "webpack-cli": "^4.9.1"
55
55
  },
56
56
  "dependencies": {
57
+ "dayjs": "^1.11.19",
57
58
  "pdfjs-dist": "2.13.216"
58
59
  }
59
60
  }