@oipage/core.js 1.0.0-alpha.0 → 1.0.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.
@@ -1,12 +1,12 @@
1
- # These are supported funding model platforms
2
-
3
- github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4
- patreon: # Replace with a single Patreon username
5
- open_collective: # Replace with a single Open Collective username
6
- ko_fi: # Replace with a single Ko-fi username
7
- tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8
- community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9
- liberapay: # Replace with a single Liberapay username
10
- issuehunt: # Replace with a single IssueHunt username
11
- otechie: # Replace with a single Otechie username
1
+ # These are supported funding model platforms
2
+
3
+ github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4
+ patreon: # Replace with a single Patreon username
5
+ open_collective: # Replace with a single Open Collective username
6
+ ko_fi: # Replace with a single Ko-fi username
7
+ tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8
+ community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9
+ liberapay: # Replace with a single Liberapay username
10
+ issuehunt: # Replace with a single IssueHunt username
11
+ otechie: # Replace with a single Otechie username
12
12
  custom: ['https://zxl20070701.github.io/notebook/home.html']
package/AUTHORS.txt CHANGED
@@ -1,6 +1,6 @@
1
- Authors ordered by first contribution.
2
-
3
- zxl20070701 <1904314465@qq.com> <https://github.com/zxl20070701>
4
-
5
- -----------------------------------------------------
6
- We thank all of them for their contributions!
1
+ Authors ordered by first contribution.
2
+
3
+ zxl20070701 <1904314465@qq.com> <https://github.com/zxl20070701>
4
+
5
+ -----------------------------------------------------
6
+ We thank all of them for their contributions!
package/CHANGELOG CHANGED
@@ -1,4 +1,6 @@
1
- v1.0.0:
2
- date:
3
- changes:
4
- - 初始化版本
1
+ v1.0.0:
2
+ date:2025-11-18
3
+ changes:
4
+ - 初始化版本
5
+ 1、Time 时间&日期
6
+ 2、Digits 数字
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) zxl20070701 走一步,再走一步
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1
+ MIT License
2
+
3
+ Copyright (c) zxl20070701 走一步,再走一步
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
package/README.md CHANGED
@@ -1,34 +1,51 @@
1
- # [@oipage/core.js](https://github.com/oi-contrib/oipage-corejs)
2
- 包括日期、时间、浮点运算等常用的一些业务API
3
-
4
- <p>
5
- <a href="https://zxl20070701.github.io/toolbox/#/npm-download?packages=@oipage/core.js&interval=7">
6
- <img src="https://img.shields.io/npm/dm/@oipage/core.js.svg" alt="downloads">
7
- </a>
8
- <a href="https://www.npmjs.com/package/@oipage/core.js">
9
- <img src="https://img.shields.io/npm/v/@oipage/core.js.svg" alt="npm">
10
- </a>
11
- <a href="https://github.com/oi-contrib/oipage-corejs/issues">
12
- <img src="https://img.shields.io/github/issues/oi-contrib/oipage-corejs" alt="issue">
13
- </a>
14
- <a href="https://github.com/oi-contrib/oipage-corejs" target='_blank'>
15
- <img alt="GitHub repo stars" src="https://img.shields.io/github/stars/oi-contrib/oipage-corejs?style=social">
16
- </a>
17
- <a href="https://github.com/oi-contrib/oipage-corejs">
18
- <img src="https://img.shields.io/github/forks/oi-contrib/oipage-corejs" alt="forks">
19
- </a>
20
- <a href="https://gitee.com/oi-contrib/oipage-corejs" target='_blank'>
21
- <img alt="Gitee repo stars" src="https://gitee.com/oi-contrib/oipage-corejs/badge/star.svg">
22
- </a>
23
- <a href="https://gitee.com/oi-contrib/oipage-corejs">
24
- <img src="https://gitee.com/oi-contrib/oipage-corejs/badge/fork.svg" alt="forks">
25
- </a>
26
- </p>
27
-
28
- <img src="https://nodei.co/npm/@oipage/core.js.png?downloads=true&amp;downloadRank=true&amp;stars=true" alt="NPM">
29
-
30
- ## 版权
31
-
32
- MIT License
33
-
1
+ # [@oipage/core.js](https://github.com/oi-contrib/oipage-corejs)
2
+ 包括日期、时间、浮点运算等常用的一些业务API
3
+
4
+ <p>
5
+ <a href="https://zxl20070701.github.io/toolbox/#/npm-download?packages=@oipage/core.js&interval=7">
6
+ <img src="https://img.shields.io/npm/dm/@oipage/core.js.svg" alt="downloads">
7
+ </a>
8
+ <a href="https://www.npmjs.com/package/@oipage/core.js">
9
+ <img src="https://img.shields.io/npm/v/@oipage/core.js.svg" alt="npm">
10
+ </a>
11
+ <a href="https://github.com/oi-contrib/oipage-corejs/issues">
12
+ <img src="https://img.shields.io/github/issues/oi-contrib/oipage-corejs" alt="issue">
13
+ </a>
14
+ <a href="https://github.com/oi-contrib/oipage-corejs" target='_blank'>
15
+ <img alt="GitHub repo stars" src="https://img.shields.io/github/stars/oi-contrib/oipage-corejs?style=social">
16
+ </a>
17
+ <a href="https://github.com/oi-contrib/oipage-corejs">
18
+ <img src="https://img.shields.io/github/forks/oi-contrib/oipage-corejs" alt="forks">
19
+ </a>
20
+ <a href="https://gitee.com/oi-contrib/oipage-corejs" target='_blank'>
21
+ <img alt="Gitee repo stars" src="https://gitee.com/oi-contrib/oipage-corejs/badge/star.svg">
22
+ </a>
23
+ <a href="https://gitee.com/oi-contrib/oipage-corejs">
24
+ <img src="https://gitee.com/oi-contrib/oipage-corejs/badge/fork.svg" alt="forks">
25
+ </a>
26
+ </p>
27
+
28
+ <img src="https://nodei.co/npm/@oipage/core.js.png?downloads=true&amp;downloadRank=true&amp;stars=true" alt="NPM">
29
+
30
+ ## 如何使用?
31
+
32
+ ```
33
+ npm install --save @oipage/core.js
34
+ ```
35
+
36
+ 然后直接引入需要的工具对象使用即可,比如Timer:
37
+
38
+ ```js
39
+ import { Time } from "@oipage/core.js"
40
+ ```
41
+
42
+ 更多具体的工具对象清单如下:
43
+
44
+ - [Time 时间&日期](./docs/Time.md)
45
+ - [Digits 数字](./docs/Digits.md)
46
+
47
+ ## 版权
48
+
49
+ MIT License
50
+
34
51
  Copyright (c) [zxl20070701](https://zxl20070701.github.io/notebook/home.html) 走一步,再走一步
package/dist/core.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @oipage/corejs v1.0.0-alpha.0
2
+ * @oipage/corejs v1.0.0
3
3
  * git+https://github.com/oi-contrib/oipage-corejs.git
4
4
  */
5
5
  (function (global, factory) {
@@ -8,14 +8,220 @@
8
8
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.corejs = {}));
9
9
  })(this, (function (exports) { 'use strict';
10
10
 
11
- function Timer() {
11
+ /*!
12
+ * format of OIPage v1.4.1
13
+ * git+https://github.com/oi-contrib/OIPage.git
14
+ */
12
15
 
16
+ function numberFormat(input) {
17
+ if (!input && input !== 0) {
18
+ return '';
19
+ } else {
20
+ input = input + "";
21
+
22
+ var decimalValue, integerValue;
23
+
24
+ var dotIndex = input.indexOf('.');
25
+ if (dotIndex == -1) {
26
+ decimalValue = ".00";
27
+ integerValue = input;
28
+ } else {
29
+ var temp = input.split(".");
30
+ decimalValue = "." + (temp[1] + "0").substring(0, 2);
31
+ integerValue = temp[0];
32
+ }
33
+
34
+ var integerArray = [];
35
+ for (var index = integerValue.length; index > 0; index -= 3) {
36
+ integerArray.unshift(integerValue.substring(index > 2 ? index - 3 : 0, index));
37
+ }
38
+
39
+ return integerArray.join(",") + decimalValue;
40
+ }
41
+ }
42
+
43
+ function dateFormat(input, option) {
44
+ option = option || option;
45
+ var dateObj;
46
+
47
+ // 如果无值,就用当前日期
48
+ if (!input) {
49
+ dateObj = new Date();
50
+ }
51
+
52
+ // 日期对象
53
+ else if (typeof input == 'object' && input.constructor === Date) {
54
+ dateObj = input;
55
+ }
56
+
57
+ // 否则就尝试创建日期对象
58
+ else if (!option.inputFormat) {
59
+ dateObj = new Date(input);
60
+ if (dateObj + "" == "Invalid Date") {
61
+ console.error("非法的日期格式:" + input, dateObj);
62
+ return "";
63
+ }
64
+ }
65
+
66
+ // 年、月、日、时、分、秒
67
+ var single = {
68
+ yyyy: 0, MM: 0, dd: 0, HH: 0, mm: 0, ss: 0
69
+ };
70
+
71
+ // 如果获取了日期实例
72
+ if (dateObj) {
73
+
74
+ single.yyyy = dateObj.getFullYear();
75
+ single.MM = dateObj.getMonth() + 1;
76
+ single.dd = dateObj.getDate();
77
+
78
+ single.HH = dateObj.getHours();
79
+ single.mm = dateObj.getMinutes();
80
+ single.ss = dateObj.getSeconds();
81
+ }
82
+
83
+ // 否则只能借助 inputFormat 来解析
84
+ else {
85
+ var inputFormat = option.inputFormat + "", valIndex = 0;
86
+ for (var index = 0; index < inputFormat.length; index++) {
87
+
88
+ // 年
89
+ if (inputFormat.substring(index, index + 4) == "yyyy") {
90
+ single.yyyy = +input.substring(valIndex, valIndex + 4);
91
+
92
+ valIndex += 4;
93
+ index += 3;
94
+ } else {
95
+ var curFlag = inputFormat.substring(index, index + 2);
96
+ if (["MM", "dd", "HH", "mm", "ss"].indexOf(curFlag) > -1) {
97
+ var valContent = input[valIndex];
98
+ if (/\d/.test(input[valIndex + 1])) {
99
+ valContent += input[valIndex + 1];
100
+ valIndex += 2;
101
+ } else {
102
+ valIndex += 1;
103
+ }
104
+
105
+ (single)[curFlag] = +valContent;
106
+ index += 1;
107
+ } else {
108
+ valIndex += 1;
109
+ }
110
+ }
111
+ }
112
+ }
113
+
114
+ if (single.MM < 10) single.MM = "0" + single.MM;
115
+ if (single.dd < 10) single.dd = "0" + single.dd;
116
+ if (single.HH < 10) single.HH = "0" + single.HH;
117
+ if (single.mm < 10) single.mm = "0" + single.mm;
118
+ if (single.ss < 10) single.ss = "0" + single.ss;
119
+
120
+ var format = option.format || "yyyy/MM/dd";
121
+ return format.replace("yyyy", single.yyyy).replace("MM", single.MM).replace("dd", single.dd).replace("HH", single.HH).replace("mm", single.mm).replace("ss", single.ss);
13
122
  }
14
123
 
15
- Timer.prototype.now = function () {
16
- console.log("设置时间为当前时间");
124
+ function is(value, type) {
125
+ return Object.prototype.toString.call(value) === '[object ' + type + ']'
126
+ }
127
+
128
+ // 计算某月多少天
129
+ let calcMonthDays = function (year, month) {
130
+
131
+ if (month == 2) {
132
+
133
+ if ((year % 4 != 0) || (year % 100 == 0 && year % 400 != 0)) {
134
+ return 28
135
+ } else {
136
+ return 29
137
+ }
138
+
139
+ } else {
140
+ return [31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1]
141
+ }
142
+
143
+ };
144
+
145
+ function Time(date, format = {}) {
146
+ this.inputFormat = format.input || "yyyy/MM/dd";
147
+ this.outputFormat = format.output || "yyyy/MM/dd";
148
+
149
+ // 字符串
150
+ if (date && typeof date === "string") {
151
+ let temp = dateFormat(date, {
152
+ inputFormat: this.inputFormat
153
+ }).split("/");
154
+
155
+ this.year = +temp[0];
156
+ this.month = +temp[1];
157
+ this.day = +temp[2];
158
+ }
159
+
160
+ else {
161
+ // 不是Date对象
162
+ if (!is(date, "Date")) date = new Date();
163
+
164
+ this.year = date.getFullYear();
165
+ this.month = date.getMonth() + 1;
166
+ this.day = date.getDate();
167
+ }
168
+ }
169
+
170
+ Time.prototype.valueOf = function () {
171
+ return dateFormat(`${this.year}/${this.month}/${this.day}`, {
172
+ format: this.outputFormat
173
+ })
174
+ };
175
+
176
+ Time.prototype.addYear = function (year) {
177
+ this.year += year;
178
+ return this
179
+ };
180
+
181
+ Time.prototype.addMonth = function (month) {
182
+ let month1 = month % 12; // 零碎的月份
183
+ this.year += (month - month1) / 12; // 整齐的年份
184
+
185
+ if (month1 > 0) {
186
+ let month2 = 12 - this.month; // 新增月份剩余空间
187
+ if (month2 >= month1) {
188
+ this.month += month1;
189
+ } else {
190
+ this.year += 1;
191
+ this.month = month1 - month2;
192
+ }
193
+ } else if (month1 < 0) {
194
+ if (this.month + month1 > 0) {
195
+ this.month += month1;
196
+ } else {
197
+ this.year -= 1;
198
+ this.month += 12 + month1;
199
+ }
200
+ }
201
+
202
+ let totalDay = calcMonthDays(this.year, this.month);
203
+ if (totalDay < this.day) this.day = totalDay;
204
+
205
+ return this
206
+ };
207
+
208
+ Time.prototype.addDay = function (day) {
209
+ let dateObj = new Date(+new Date(`${this.year}/${this.month}/${this.day}`) + (day * 86400000)); // 86400000 = 24 * 60 * 60 * 1000
210
+ this.year = dateObj.getFullYear();
211
+ this.month = dateObj.getMonth() + 1;
212
+ this.day = dateObj.getDate();
213
+ return this
214
+ };
215
+
216
+ function Digits(value = 0) {
217
+ this.value = value;
218
+ }
219
+
220
+ Digits.prototype.format = function () {
221
+ return numberFormat(this.value)
17
222
  };
18
223
 
19
- exports.Timer = Timer;
224
+ exports.Digits = Digits;
225
+ exports.Time = Time;
20
226
 
21
227
  }));
package/dist/core.min.js CHANGED
@@ -1,5 +1,9 @@
1
1
  /*!
2
- * @oipage/corejs v1.0.0-alpha.0
2
+ * @oipage/corejs v1.0.0
3
3
  * git+https://github.com/oi-contrib/oipage-corejs.git
4
4
  */
5
- !function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports):"function"==typeof define&&define.amd?define(["exports"],o):o((e="undefined"!=typeof globalThis?globalThis:e||self).corejs={})}(this,function(e){"use strict";function o(){}o.prototype.now=function(){console.log("设置时间为当前时间")},e.Timer=o});
5
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).corejs={})}(this,function(t){"use strict";
6
+ /*!
7
+ * format of OIPage v1.4.1
8
+ * git+https://github.com/oi-contrib/OIPage.git
9
+ */function e(t,e){var i;if(e=e||e,t){if("object"==typeof t&&t.constructor===Date)i=t;else if(!e.inputFormat&&(i=new Date(t))+""=="Invalid Date")return console.error("非法的日期格式:"+t,i),""}else i=new Date;var s={yyyy:0,MM:0,dd:0,HH:0,mm:0,ss:0};if(i)s.yyyy=i.getFullYear(),s.MM=i.getMonth()+1,s.dd=i.getDate(),s.HH=i.getHours(),s.mm=i.getMinutes(),s.ss=i.getSeconds();else for(var n=e.inputFormat+"",r=0,o=0;o<n.length;o++)if("yyyy"==n.substring(o,o+4))s.yyyy=+t.substring(r,r+4),r+=4,o+=3;else{var a=n.substring(o,o+2);if(["MM","dd","HH","mm","ss"].indexOf(a)>-1){var y=t[r];/\d/.test(t[r+1])?(y+=t[r+1],r+=2):r+=1,s[a]=+y,o+=1}else r+=1}return s.MM<10&&(s.MM="0"+s.MM),s.dd<10&&(s.dd="0"+s.dd),s.HH<10&&(s.HH="0"+s.HH),s.mm<10&&(s.mm="0"+s.mm),s.ss<10&&(s.ss="0"+s.ss),(e.format||"yyyy/MM/dd").replace("yyyy",s.yyyy).replace("MM",s.MM).replace("dd",s.dd).replace("HH",s.HH).replace("mm",s.mm).replace("ss",s.ss)}function i(t,i={}){if(this.inputFormat=i.input||"yyyy/MM/dd",this.outputFormat=i.output||"yyyy/MM/dd",t&&"string"==typeof t){let i=e(t,{inputFormat:this.inputFormat}).split("/");this.year=+i[0],this.month=+i[1],this.day=+i[2]}else s=t,n="Date",Object.prototype.toString.call(s)!=="[object "+n+"]"&&(t=new Date),this.year=t.getFullYear(),this.month=t.getMonth()+1,this.day=t.getDate();var s,n}function s(t=0){this.value=t}i.prototype.valueOf=function(){return e(`${this.year}/${this.month}/${this.day}`,{format:this.outputFormat})},i.prototype.addYear=function(t){return this.year+=t,this},i.prototype.addMonth=function(t){let e=t%12;if(this.year+=(t-e)/12,e>0){let t=12-this.month;t>=e?this.month+=e:(this.year+=1,this.month=e-t)}else e<0&&(this.month+e>0?this.month+=e:(this.year-=1,this.month+=12+e));let i=function(t,e){return 2==e?t%4!=0||t%100==0&&t%400!=0?28:29:[31,-1,31,30,31,30,31,31,30,31,30,31][e-1]}(this.year,this.month);return i<this.day&&(this.day=i),this},i.prototype.addDay=function(t){let e=new Date(+new Date(`${this.year}/${this.month}/${this.day}`)+864e5*t);return this.year=e.getFullYear(),this.month=e.getMonth()+1,this.day=e.getDate(),this},s.prototype.format=function(){return function(t){if(t||0===t){var e,i;if(-1==(t+="").indexOf("."))e=".00",i=t;else{var s=t.split(".");e="."+(s[1]+"0").substring(0,2),i=s[0]}for(var n=[],r=i.length;r>0;r-=3)n.unshift(i.substring(r>2?r-3:0,r));return n.join(",")+e}return""}(this.value)},t.Digits=s,t.Time=i});
package/package.json CHANGED
@@ -1,30 +1,34 @@
1
- {
2
- "name": "@oipage/core.js",
3
- "version": "1.0.0-alpha.0",
4
- "description": "包括日期、时间、浮点运算等常用的一些业务API",
5
- "main": "./dist/core.js",
6
- "typings": "./types/index.d.ts",
7
- "scripts": {
8
- "start": "npm install && npm run dev",
9
- "dev": "oipage-cli run \"rollup --config ./rollup.config.js -w\" \"oipage-cli serve -p 20000\"",
10
- "build": "rollup --config ./rollup.config.js && terser ./dist/core.js --compress --mangle --output ./dist/core.min.js"
11
- },
12
- "repository": {
13
- "type": "git",
14
- "url": "git+https://github.com/oi-contrib/oipage-corejs.git"
15
- },
16
- "author": {
17
- "name": "zxl20070701",
18
- "url": "https://zxl20070701.github.io/notebook/home.html"
19
- },
20
- "keywords": [
21
- "oipage",
22
- "core",
23
- "javascript"
24
- ],
25
- "license": "MIT",
26
- "devDependencies": {
27
- "rollup": "^4.6.1",
28
- "terser": "^5.17.1"
29
- }
30
- }
1
+ {
2
+ "name": "@oipage/core.js",
3
+ "version": "1.0.0",
4
+ "description": "包括日期、时间、浮点运算等常用的一些业务API",
5
+ "main": "./dist/core.js",
6
+ "typings": "./types/index.d.ts",
7
+ "scripts": {
8
+ "start": "npm install && npm run dev",
9
+ "dev": "oipage-cli run \"rollup --config ./rollup.config.js -w\" \"oipage-cli serve -p 20000\"",
10
+ "build": "rollup --config ./rollup.config.js && terser ./dist/core.js --compress --mangle --output ./dist/core.min.js"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/oi-contrib/oipage-corejs.git"
15
+ },
16
+ "author": {
17
+ "name": "zxl20070701",
18
+ "url": "https://zxl20070701.github.io/notebook/home.html"
19
+ },
20
+ "keywords": [
21
+ "oipage",
22
+ "core",
23
+ "javascript"
24
+ ],
25
+ "license": "MIT",
26
+ "devDependencies": {
27
+ "@rollup/plugin-node-resolve": "^16.0.1",
28
+ "rollup": "^4.6.1",
29
+ "terser": "^5.17.1"
30
+ },
31
+ "dependencies": {
32
+ "oipage": "^1.4.1"
33
+ }
34
+ }
@@ -0,0 +1,9 @@
1
+ import { numberFormat } from "oipage/web/format/index"
2
+
3
+ export function Digits(value = 0) {
4
+ this.value = value
5
+ }
6
+
7
+ Digits.prototype.format = function () {
8
+ return numberFormat(this.value)
9
+ }
@@ -0,0 +1,74 @@
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
+ }
@@ -0,0 +1,16 @@
1
+ // 计算某月多少天
2
+ export let calcMonthDays = function (year, month) {
3
+
4
+ if (month == 2) {
5
+
6
+ if ((year % 4 != 0) || (year % 100 == 0 && year % 400 != 0)) {
7
+ return 28
8
+ } else {
9
+ return 29
10
+ }
11
+
12
+ } else {
13
+ return [31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1]
14
+ }
15
+
16
+ }
package/src/index.js CHANGED
@@ -1 +1,2 @@
1
- export * from "./Timer/index.js"
1
+ export * from "./Time/index.js"
2
+ export * from "./Digits/index.js"
package/src/tool.js ADDED
@@ -0,0 +1,3 @@
1
+ export function is(value, type) {
2
+ return Object.prototype.toString.call(value) === '[object ' + type + ']'
3
+ }
@@ -0,0 +1,13 @@
1
+ export default interface DigitsType {
2
+ new(value: number): this
3
+
4
+ /**
5
+ * 值
6
+ */
7
+ value: number
8
+
9
+ /**
10
+ * 返回格式化数字
11
+ */
12
+ format(): string
13
+ }
package/types/Time.ts ADDED
@@ -0,0 +1,52 @@
1
+ export default interface TimeType {
2
+ new(date?: string | Date, format?: {
3
+
4
+ /**
5
+ * 输入格式
6
+ */
7
+ input?: "string"
8
+
9
+ /**
10
+ * 输出格式
11
+ */
12
+ outout?: "string"
13
+ }): this
14
+
15
+ /**
16
+ * 年
17
+ */
18
+ year: number
19
+
20
+ /**
21
+ * 月
22
+ */
23
+ month: number
24
+
25
+ /**
26
+ * 日
27
+ */
28
+ day: number
29
+
30
+ /**
31
+ * 按照format.outout格式返回当前维护的日期
32
+ */
33
+ valueOf(): string
34
+
35
+ /**
36
+ * 新增year年后的日期(如果year为负数,就是减少)
37
+ * @param year
38
+ */
39
+ addYear(year: number): this
40
+
41
+ /**
42
+ * 新增month月后的日期(如果month为负数,就是减少)
43
+ * @param month
44
+ */
45
+ addMonth(month: number): this
46
+
47
+ /**
48
+ * 新增day天后的日期(如果day为负数,就是减少)
49
+ * @param day
50
+ */
51
+ addDay(day: number): this
52
+ }
package/types/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
- import TimerType from "./Timer"
2
-
3
- export let Timer: TimerType
1
+ import TimeType from "./Time"
2
+ import DigitsType from "./Digits"
3
+
4
+ export let Time: TimeType
5
+ export let Digits: DigitsType
@@ -1,7 +0,0 @@
1
- export function Timer() {
2
-
3
- }
4
-
5
- Timer.prototype.now = function () {
6
- console.log("设置时间为当前时间");
7
- }
package/types/Timer.d.ts DELETED
@@ -1,8 +0,0 @@
1
- export default interface IimerType {
2
- new(): this
3
-
4
- /**
5
- * 设置时间为当前时间
6
- */
7
- now(): this
8
- }