@oipage/core.js 1.0.0 → 1.1.0-alpha.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.
Files changed (84) hide show
  1. package/CHANGELOG +52 -6
  2. package/LICENSE +20 -20
  3. package/README.md +170 -51
  4. package/dist/core.esm.js +2711 -0
  5. package/dist/core.js +2709 -208
  6. package/dist/core.min.js +2 -6
  7. package/package.json +63 -34
  8. package/src/Array/group.js +35 -0
  9. package/src/Array/index.js +21 -0
  10. package/src/Array/list.js +79 -0
  11. package/src/Array/sort.js +40 -0
  12. package/src/Array/tool.js +20 -0
  13. package/src/Array/unique.js +30 -0
  14. package/src/Cache/driver.js +86 -0
  15. package/src/Cache/expire.js +21 -0
  16. package/src/Cache/index.js +33 -0
  17. package/src/Cache/store.js +113 -0
  18. package/src/Color/adjust.js +67 -0
  19. package/src/Color/convert.js +100 -0
  20. package/src/Color/index.js +19 -0
  21. package/src/Color/tool.js +39 -0
  22. package/src/Digits/compare.js +33 -0
  23. package/src/Digits/format.js +55 -0
  24. package/src/Digits/index.js +32 -9
  25. package/src/Digits/math.js +75 -0
  26. package/src/Digits/precise.js +230 -0
  27. package/src/Digits/tool.js +29 -0
  28. package/src/Id/index.js +17 -0
  29. package/src/Id/nanoid.js +24 -0
  30. package/src/Id/random.js +40 -0
  31. package/src/Id/tool.js +38 -0
  32. package/src/Id/uuid.js +21 -0
  33. package/src/Object/check.js +15 -0
  34. package/src/Object/clone.js +66 -0
  35. package/src/Object/equal.js +63 -0
  36. package/src/Object/index.js +25 -0
  37. package/src/Object/merge.js +35 -0
  38. package/src/Object/path.js +58 -0
  39. package/src/Object/pick.js +34 -0
  40. package/src/Query/index.js +14 -0
  41. package/src/Query/parse.js +40 -0
  42. package/src/Query/stringify.js +45 -0
  43. package/src/Query/tool.js +58 -0
  44. package/src/Query/url.js +63 -0
  45. package/src/String/case.js +35 -0
  46. package/src/String/escape.js +40 -0
  47. package/src/String/index.js +28 -0
  48. package/src/String/mask.js +70 -0
  49. package/src/String/pad.js +23 -0
  50. package/src/String/trim.js +10 -0
  51. package/src/String/truncate.js +36 -0
  52. package/src/String/width.js +27 -0
  53. package/src/Time/arithmetic.js +61 -0
  54. package/src/Time/calendar.js +37 -0
  55. package/src/Time/compare.js +28 -0
  56. package/src/Time/field.js +37 -0
  57. package/src/Time/format.js +28 -0
  58. package/src/Time/index.js +97 -74
  59. package/src/Time/tool.js +20 -16
  60. package/src/Validate/contact.js +14 -0
  61. package/src/Validate/identity.js +57 -0
  62. package/src/Validate/index.js +24 -0
  63. package/src/Validate/network.js +59 -0
  64. package/src/Validate/type.js +28 -0
  65. package/src/index.js +10 -2
  66. package/src/shared/date.js +123 -0
  67. package/src/shared/is.js +63 -0
  68. package/src/tool.js +2 -2
  69. package/types/Array.d.ts +33 -0
  70. package/types/Cache.d.ts +51 -0
  71. package/types/Color.d.ts +43 -0
  72. package/types/Digits.d.ts +92 -0
  73. package/types/Id.d.ts +25 -0
  74. package/types/Object.d.ts +45 -0
  75. package/types/Query.d.ts +35 -0
  76. package/types/String.d.ts +62 -0
  77. package/types/Time.d.ts +136 -0
  78. package/types/Validate.d.ts +39 -0
  79. package/types/index.d.ts +30 -5
  80. package/.github/FUNDING.yml +0 -12
  81. package/.mailmap +0 -1
  82. package/AUTHORS.txt +0 -6
  83. package/types/Digits.ts +0 -13
  84. package/types/Time.ts +0 -52
@@ -0,0 +1,35 @@
1
+ /**
2
+ * String 命名与反转
3
+ */
4
+ import { toChars } from "./width.js"
5
+
6
+ /**
7
+ * 转为小驼峰:foo-bar、foo_bar、foo bar 都转成 fooBar
8
+ */
9
+ export function camelCase(str) {
10
+ let words = String(str).split(/[\s\-_./]+/).filter(Boolean)
11
+ return words.map(function (word, index) {
12
+ let lower = word.toLowerCase()
13
+ if (index === 0) return lower
14
+ return lower.charAt(0).toUpperCase() + lower.substring(1)
15
+ }).join("")
16
+ }
17
+
18
+ /**
19
+ * 转为短横线命名:fooBar 转成 foo-bar
20
+ */
21
+ export function kebabCase(str) {
22
+ return String(str)
23
+ .replace(/([a-z0-9])([A-Z])/g, "$1-$2")
24
+ .replace(/[\s_./]+/g, "-")
25
+ .replace(/-+/g, "-")
26
+ .replace(/^-|-$/g, "")
27
+ .toLowerCase()
28
+ }
29
+
30
+ /**
31
+ * 反向字符串(按 Unicode 码位切分,不会拆坏 emoji 等代理对)
32
+ */
33
+ export function reverse(str) {
34
+ return toChars(str).reverse().join("")
35
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * String HTML 转义
3
+ */
4
+
5
+ const ESCAPE_MAP = {
6
+ "&": "&",
7
+ "<": "&lt;",
8
+ ">": "&gt;",
9
+ '"': "&quot;",
10
+ "'": "&#39;"
11
+ }
12
+
13
+ const UNESCAPE_MAP = {
14
+ "&amp;": "&",
15
+ "&lt;": "<",
16
+ "&gt;": ">",
17
+ "&quot;": '"',
18
+ "&#39;": "'",
19
+ "&#x27;": "'"
20
+ }
21
+
22
+ /**
23
+ * 转义 HTML 特殊字符,用于把用户输入拼进页面
24
+ */
25
+ export function escapeHTML(str) {
26
+ return String(str).replace(/[&<>"']/g, function (char) {
27
+ return ESCAPE_MAP[char]
28
+ })
29
+ }
30
+
31
+ /**
32
+ * 还原 HTML 实体
33
+ */
34
+ export function unescapeHTML(str) {
35
+ return String(str).replace(/&(amp|lt|gt|quot|#39|#x27);/g, function (entity) {
36
+ return UNESCAPE_MAP[entity] || entity
37
+ })
38
+ }
39
+
40
+ // 用命名空间对象导出,避免在本文件内遮蔽原生 String
@@ -0,0 +1,28 @@
1
+ /**
2
+ * String 对外入口
3
+ */
4
+ import { camelCase, kebabCase, reverse } from "./case.js"
5
+ import { escapeHTML, unescapeHTML } from "./escape.js"
6
+ import { mask, maskMiddle } from "./mask.js"
7
+ import { pad } from "./pad.js"
8
+ import { trim } from "./trim.js"
9
+ import { cutByWidth, truncate } from "./truncate.js"
10
+ import { width, widthOf } from "./width.js"
11
+
12
+ const StringUtil = {
13
+ trim: trim,
14
+ width: width,
15
+ widthOf: widthOf,
16
+ truncate: truncate,
17
+ cutByWidth: cutByWidth,
18
+ pad: pad,
19
+ camelCase: camelCase,
20
+ kebabCase: kebabCase,
21
+ reverse: reverse,
22
+ mask: mask,
23
+ maskMiddle: maskMiddle,
24
+ escapeHTML: escapeHTML,
25
+ unescapeHTML: unescapeHTML
26
+ }
27
+
28
+ export { StringUtil as String }
@@ -0,0 +1,70 @@
1
+ /**
2
+ * String 脱敏
3
+ */
4
+ import { toChars } from "./width.js"
5
+
6
+ /**
7
+ * 按 head/tail 保留首尾,中间用 char 替代
8
+ */
9
+ export function maskMiddle(str, head, tail, char) {
10
+ char = char === undefined ? "*" : String(char)
11
+ let chars = toChars(str)
12
+ head = Math.max(0, head || 0)
13
+ tail = Math.max(0, tail || 0)
14
+
15
+ if (chars.length <= head + tail) {
16
+ return char.repeat(chars.length)
17
+ }
18
+
19
+ return chars.slice(0, head).join("")
20
+ + char.repeat(chars.length - head - tail)
21
+ + chars.slice(chars.length - tail).join("")
22
+ }
23
+
24
+ /**
25
+ * 常用脱敏
26
+ *
27
+ * @param {string} type mobile、idcard、bankcard、email、name,或 custom
28
+ * @param {object} option custom 时的配置:head、tail、char
29
+ */
30
+ export function mask(str, type, option) {
31
+ str = String(str === undefined || str === null ? "" : str)
32
+ if (!str) return ""
33
+
34
+ option = option || {}
35
+ let char = option.char || "*"
36
+
37
+ switch (type) {
38
+ case "mobile":
39
+ return maskMiddle(str, 3, 4, char)
40
+
41
+ case "idcard":
42
+ return maskMiddle(str, 6, 4, char)
43
+
44
+ case "bankcard": {
45
+ let last = toChars(str).slice(-4).join("")
46
+ let groups = []
47
+ for (let i = 0; i < 3; i++) groups.push(char.repeat(4))
48
+ return groups.join(" ") + " " + last
49
+ }
50
+
51
+ case "email": {
52
+ let atIndex = str.indexOf("@")
53
+ if (atIndex <= 0) return maskMiddle(str, 2, 2, char)
54
+ let local = str.substring(0, atIndex)
55
+ let host = str.substring(atIndex)
56
+ if (local.length <= 2) return char.repeat(local.length) + host
57
+ return local.substring(0, 2) + char.repeat(3) + host
58
+ }
59
+
60
+ case "name": {
61
+ let chars = toChars(str)
62
+ if (chars.length === 1) return char
63
+ if (chars.length === 2) return chars[0] + char
64
+ return chars[0] + char.repeat(chars.length - 2) + chars[chars.length - 1]
65
+ }
66
+
67
+ default:
68
+ return maskMiddle(str, option.head || 0, option.tail || 0, char)
69
+ }
70
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * String 补位
3
+ */
4
+ import { width } from "./width.js"
5
+
6
+ /**
7
+ * 补齐到指定显示宽度
8
+ *
9
+ * @param {string} position 补在左边用 "start",补在右边用 "end",默认 "end"
10
+ */
11
+ export function pad(str, length, char, position) {
12
+ str = String(str)
13
+ char = char === undefined ? " " : String(char)
14
+ let current = width(str)
15
+ if (current >= length) return str
16
+
17
+ let fill = ""
18
+ while (current + width(fill) + width(char) <= length) {
19
+ fill += char
20
+ }
21
+
22
+ return position === "start" ? fill + str : str + fill
23
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * String 去空白
3
+ */
4
+
5
+ /**
6
+ * 去掉首尾空白,包含全角空格与不换行空格
7
+ */
8
+ export function trim(str) {
9
+ return String(str).replace(/^[\s\u00A0\u3000]+|[\s\u00A0\u3000]+$/g, "")
10
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * String 截断
3
+ */
4
+ import { toChars, width, widthOf } from "./width.js"
5
+
6
+ /**
7
+ * 按字符数截断,超出部分用 suffix 替代
8
+ */
9
+ export function truncate(str, maxLength, suffix) {
10
+ suffix = suffix === undefined ? "..." : suffix
11
+ let chars = toChars(str)
12
+ if (chars.length <= maxLength) return String(str)
13
+ return chars.slice(0, Math.max(0, maxLength - Array.from(String(suffix)).length)).join("") + suffix
14
+ }
15
+
16
+ /**
17
+ * 按显示宽度截断(中文按 2 个宽度),超出部分用 suffix 替代
18
+ */
19
+ export function cutByWidth(str, maxWidth, suffix) {
20
+ suffix = suffix === undefined ? "..." : suffix
21
+ let suffixWidth = width(suffix)
22
+ if (width(str) <= maxWidth) return String(str)
23
+
24
+ let total = 0
25
+ let result = []
26
+ let chars = toChars(str)
27
+
28
+ for (let i = 0; i < chars.length; i++) {
29
+ let current = widthOf(chars[i])
30
+ if (total + current > maxWidth - suffixWidth) break
31
+ total += current
32
+ result.push(chars[i])
33
+ }
34
+
35
+ return result.join("") + suffix
36
+ }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * String 宽度与字符
3
+ */
4
+
5
+ const WIDE_RE = /[\u1100-\u115F\u2E80-\u303E\u3041-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uA000-\uA4CF\uAC00-\uD7A3\uF900-\uFAFF\uFE10-\uFE19\uFE30-\uFE6F\uFF00-\uFF60\uFFE0-\uFFE6]/
6
+
7
+ export function toChars(str) {
8
+ return Array.from(String(str))
9
+ }
10
+
11
+ /**
12
+ * 单个字符的显示宽度:中文等宽字符算 2,其余算 1
13
+ */
14
+ export function widthOf(char) {
15
+ return WIDE_RE.test(char) ? 2 : 1
16
+ }
17
+
18
+ /**
19
+ * 字符串的显示宽度
20
+ */
21
+ export function width(str) {
22
+ let total = 0
23
+ toChars(str).forEach(function (char) {
24
+ total += widthOf(char)
25
+ })
26
+ return total
27
+ }
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Time 日期加减
3
+ */
4
+ import { daysInMonth as getDaysInMonth } from "../shared/date"
5
+ import { isInteger } from "../shared/is"
6
+
7
+ export function useArithmetic(Time) {
8
+ /**
9
+ * 新增 year 年(负数即减少),闰日会自动夹取到当月最后一天
10
+ */
11
+ Time.prototype.addYear = function (year) {
12
+ if (!isInteger(year)) throw new TypeError("addYear 需要整数,收到:" + year)
13
+ this.year += year
14
+
15
+ let totalDay = getDaysInMonth(this.year, this.month)
16
+ if (totalDay < this.day) this.day = totalDay
17
+
18
+ return this
19
+ }
20
+
21
+ /**
22
+ * 新增 month 月(负数即减少),月末日期会自动夹取到当月最后一天
23
+ */
24
+ Time.prototype.addMonth = function (month) {
25
+ if (!isInteger(month)) throw new TypeError("addMonth 需要整数,收到:" + month)
26
+
27
+ let total = (this.year * 12 + this.month - 1) + month
28
+ this.year = Math.floor(total / 12)
29
+ this.month = total - this.year * 12 + 1
30
+
31
+ let totalDay = getDaysInMonth(this.year, this.month)
32
+ if (totalDay < this.day) this.day = totalDay
33
+
34
+ return this
35
+ }
36
+
37
+ /**
38
+ * 新增 day 天(负数即减少)
39
+ * 使用本地日历做加减,跨月、跨年、跨夏令时都安全
40
+ */
41
+ Time.prototype.addDay = function (day) {
42
+ if (!isInteger(day)) throw new TypeError("addDay 需要整数,收到:" + day)
43
+
44
+ let dateObj = this.toDate()
45
+ dateObj.setDate(dateObj.getDate() + day)
46
+
47
+ this.year = dateObj.getFullYear()
48
+ this.month = dateObj.getMonth() + 1
49
+ this.day = dateObj.getDate()
50
+
51
+ return this
52
+ }
53
+
54
+ /**
55
+ * 新增 week 周(负数即减少)
56
+ */
57
+ Time.prototype.addWeek = function (week) {
58
+ if (!isInteger(week)) throw new TypeError("addWeek 需要整数,收到:" + week)
59
+ return this.addDay(week * 7)
60
+ }
61
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Time 月历信息
3
+ */
4
+ import { daysInMonth as getDaysInMonth, isLeapYear as checkLeapYear } from "../shared/date"
5
+
6
+ export function useCalendar(Time) {
7
+ /**
8
+ * 把日期调整到当月第一天
9
+ */
10
+ Time.prototype.startOfMonth = function () {
11
+ this.day = 1
12
+ return this
13
+ }
14
+
15
+ /**
16
+ * 把日期调整到当月最后一天
17
+ */
18
+ Time.prototype.endOfMonth = function () {
19
+ this.day = getDaysInMonth(this.year, this.month)
20
+ return this
21
+ }
22
+
23
+ /**
24
+ * 星期几:0 表示周日,1-6 表示周一至周六
25
+ */
26
+ Time.prototype.getWeekDay = function () {
27
+ return this.toDate().getDay()
28
+ }
29
+
30
+ Time.prototype.isLeapYear = function () {
31
+ return checkLeapYear(this.year)
32
+ }
33
+
34
+ Time.prototype.daysInMonth = function () {
35
+ return getDaysInMonth(this.year, this.month)
36
+ }
37
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Time 比较
3
+ */
4
+ import { utcStamp, toTime } from "./tool"
5
+
6
+ export function useCompare(Time) {
7
+ /**
8
+ * 与另一个日期相差的天数(this 比 other 晚则为正数)
9
+ */
10
+ Time.prototype.diffDays = function (other) {
11
+ let target = toTime(Time, other, this.inputFormat)
12
+ let selfStamp = utcStamp(this.year, this.month, this.day)
13
+ let targetStamp = utcStamp(target.year, target.month, target.day)
14
+ return Math.round((selfStamp - targetStamp) / 86400000)
15
+ }
16
+
17
+ Time.prototype.isBefore = function (other) {
18
+ return this.diffDays(other) < 0
19
+ }
20
+
21
+ Time.prototype.isAfter = function (other) {
22
+ return this.diffDays(other) > 0
23
+ }
24
+
25
+ Time.prototype.isSame = function (other) {
26
+ return this.diffDays(other) === 0
27
+ }
28
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Time 字段与转换
3
+ */
4
+ import { toLocalDate, isValidDate } from "../shared/date"
5
+
6
+ export function useFields(Time) {
7
+ /**
8
+ * 当前维护的日期是否仍然合法(直接修改 year/month/day 后可能非法)
9
+ */
10
+ Time.prototype.isValid = function () {
11
+ return isValidDate(this.year, this.month, this.day)
12
+ }
13
+
14
+ /**
15
+ * 复制一个内容相同、格式相同的新对象
16
+ */
17
+ Time.prototype.clone = function () {
18
+ return new Time(this.getTime(), {
19
+ input: this.inputFormat,
20
+ output: this.outputFormat
21
+ })
22
+ }
23
+
24
+ /**
25
+ * 本地时间零点对应的时间戳
26
+ */
27
+ Time.prototype.getTime = function () {
28
+ return toLocalDate(this.year, this.month, this.day).getTime()
29
+ }
30
+
31
+ /**
32
+ * 转成可以直接使用的 Date 对象(本地时间零点)
33
+ */
34
+ Time.prototype.toDate = function () {
35
+ return toLocalDate(this.year, this.month, this.day)
36
+ }
37
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Time 格式化输出
3
+ */
4
+ import { formatDate } from "../shared/date"
5
+
6
+ export function useFormat(Time) {
7
+ /**
8
+ * 按指定格式返回当前维护的日期,不传时用 outputFormat
9
+ */
10
+ Time.prototype.format = function (format) {
11
+ return formatDate(this.year, this.month, this.day, format || this.outputFormat)
12
+ }
13
+
14
+ /**
15
+ * 按照 outputFormat 格式返回当前维护的日期
16
+ */
17
+ Time.prototype.valueOf = function () {
18
+ return this.format()
19
+ }
20
+
21
+ Time.prototype.toString = function () {
22
+ return this.format()
23
+ }
24
+
25
+ Time.prototype.toJSON = function () {
26
+ return this.format()
27
+ }
28
+ }
package/src/Time/index.js CHANGED
@@ -1,74 +1,97 @@
1
- import { dateFormat } from "oipage/web/format/index"
2
- import { is } from "../tool"
3
- import { calcMonthDays } from "./tool"
4
-
5
- export function Time(date, format = {}) {
6
- this.inputFormat = format.input || "yyyy/MM/dd"
7
- this.outputFormat = format.output || "yyyy/MM/dd"
8
-
9
- // 字符串
10
- if (date && typeof date === "string") {
11
- let temp = dateFormat(date, {
12
- inputFormat: this.inputFormat
13
- }).split("/")
14
-
15
- this.year = +temp[0]
16
- this.month = +temp[1]
17
- this.day = +temp[2]
18
- }
19
-
20
- else {
21
- // 不是Date对象
22
- if (!is(date, "Date")) date = new Date()
23
-
24
- this.year = date.getFullYear()
25
- this.month = date.getMonth() + 1
26
- this.day = date.getDate()
27
- }
28
- }
29
-
30
- Time.prototype.valueOf = function () {
31
- return dateFormat(`${this.year}/${this.month}/${this.day}`, {
32
- format: this.outputFormat
33
- })
34
- }
35
-
36
- Time.prototype.addYear = function (year) {
37
- this.year += year
38
- return this
39
- }
40
-
41
- Time.prototype.addMonth = function (month) {
42
- let month1 = month % 12 // 零碎的月份
43
- this.year += (month - month1) / 12 // 整齐的年份
44
-
45
- if (month1 > 0) {
46
- let month2 = 12 - this.month // 新增月份剩余空间
47
- if (month2 >= month1) {
48
- this.month += month1
49
- } else {
50
- this.year += 1
51
- this.month = month1 - month2
52
- }
53
- } else if (month1 < 0) {
54
- if (this.month + month1 > 0) {
55
- this.month += month1
56
- } else {
57
- this.year -= 1
58
- this.month += 12 + month1
59
- }
60
- }
61
-
62
- let totalDay = calcMonthDays(this.year, this.month)
63
- if (totalDay < this.day) this.day = totalDay
64
-
65
- return this
66
- }
67
-
68
- Time.prototype.addDay = function (day) {
69
- let dateObj = new Date(+new Date(`${this.year}/${this.month}/${this.day}`) + (day * 86400000)) // 86400000 = 24 * 60 * 60 * 1000
70
- this.year = dateObj.getFullYear()
71
- this.month = dateObj.getMonth() + 1
72
- this.day = dateObj.getDate()
73
- return this
74
- }
1
+ import { parseDate, isValidDate } from "../shared/date"
2
+ import { isDate } from "../shared/is"
3
+ import { useFields } from "./field"
4
+ import { useFormat } from "./format"
5
+ import { useArithmetic } from "./arithmetic"
6
+ import { useCompare } from "./compare"
7
+ import { useCalendar } from "./calendar"
8
+
9
+ /**
10
+ * 日期对象:只维护年、月、日
11
+ *
12
+ * @param {string|Date|number} date 日期,可以是字符串、Date 对象或时间戳,默认今天
13
+ * @param {object} format 格式配置
14
+ * @param {string} format.input 字符串输入格式,默认 yyyy/MM/dd
15
+ * @param {string} format.output 输出格式,默认 yyyy/MM/dd
16
+ */
17
+ export function Time(date, format) {
18
+ format = format || {}
19
+
20
+ Object.defineProperty(this, "inputFormat", {
21
+ value: format.input || "yyyy/MM/dd",
22
+ writable: true,
23
+ enumerable: false
24
+ })
25
+ Object.defineProperty(this, "outputFormat", {
26
+ value: format.output || "yyyy/MM/dd",
27
+ writable: true,
28
+ enumerable: false
29
+ })
30
+
31
+ let year, month, day
32
+
33
+ // 空值兜底为今天
34
+ if (date === undefined || date === null || date === "") {
35
+ let now = new Date()
36
+ year = now.getFullYear()
37
+ month = now.getMonth() + 1
38
+ day = now.getDate()
39
+ }
40
+
41
+ // 字符串
42
+ else if (typeof date === "string") {
43
+ let temp = parseDate(date, this.inputFormat)
44
+ year = temp.year
45
+ month = temp.month
46
+ day = temp.day
47
+ }
48
+
49
+ // 时间戳
50
+ else if (typeof date === "number") {
51
+ if (!isFinite(date)) throw new TypeError("非法的日期时间戳:" + date)
52
+ let dateObj = new Date(date)
53
+ year = dateObj.getFullYear()
54
+ month = dateObj.getMonth() + 1
55
+ day = dateObj.getDate()
56
+ }
57
+
58
+ // Date 对象
59
+ else if (isDate(date)) {
60
+ if (isNaN(date.getTime())) throw new TypeError("非法的 Date 对象")
61
+ year = date.getFullYear()
62
+ month = date.getMonth() + 1
63
+ day = date.getDate()
64
+ }
65
+
66
+ else {
67
+ throw new TypeError("Time 只接受字符串、Date 对象或时间戳,收到:" + date)
68
+ }
69
+
70
+ if (!isValidDate(year, month, day)) {
71
+ throw new TypeError("非法的日期:" + date)
72
+ }
73
+
74
+ this.year = year
75
+ this.month = month
76
+ this.day = day
77
+ }
78
+
79
+ useFields(Time)
80
+ useFormat(Time)
81
+ useArithmetic(Time)
82
+ useCompare(Time)
83
+ useCalendar(Time)
84
+
85
+ /**
86
+ * 今天
87
+ */
88
+ Time.today = function (format) {
89
+ return new Time(undefined, format)
90
+ }
91
+
92
+ /**
93
+ * 按指定格式解析日期字符串
94
+ */
95
+ Time.parse = function (input, format) {
96
+ return new Time(input, { input: format, output: format })
97
+ }