@oipage/core.js 1.0.0-alpha.1 → 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.
package/CHANGELOG CHANGED
@@ -1,5 +1,6 @@
1
1
  v1.0.0:
2
- date:
2
+ date:2025-11-18
3
3
  changes:
4
4
  - 初始化版本
5
- 1、Dater 日期
5
+ 1、Time 时间&日期
6
+ 2、Digits 数字
package/README.md CHANGED
@@ -36,12 +36,13 @@ npm install --save @oipage/core.js
36
36
  然后直接引入需要的工具对象使用即可,比如Timer:
37
37
 
38
38
  ```js
39
- import { Dater } from "@oipage/core.js"
39
+ import { Time } from "@oipage/core.js"
40
40
  ```
41
41
 
42
42
  更多具体的工具对象清单如下:
43
43
 
44
- - [Dater 日期](./docs/Dater.md)
44
+ - [Time 时间&日期](./docs/Time.md)
45
+ - [Digits 数字](./docs/Digits.md)
45
46
 
46
47
  ## 版权
47
48
 
package/dist/core.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @oipage/corejs v1.0.0-alpha.1
2
+ * @oipage/corejs v1.0.0
3
3
  * git+https://github.com/oi-contrib/oipage-corejs.git
4
4
  */
5
5
  (function (global, factory) {
@@ -13,6 +13,32 @@
13
13
  * git+https://github.com/oi-contrib/OIPage.git
14
14
  */
15
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
+ }
16
42
 
17
43
  function dateFormat(input, option) {
18
44
  option = option || option;
@@ -95,7 +121,28 @@
95
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);
96
122
  }
97
123
 
98
- function Dater(date, format = {}) {
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 = {}) {
99
146
  this.inputFormat = format.input || "yyyy/MM/dd";
100
147
  this.outputFormat = format.output || "yyyy/MM/dd";
101
148
 
@@ -112,7 +159,7 @@
112
159
 
113
160
  else {
114
161
  // 不是Date对象
115
- if (!(date && typeof date === "object" && date.constructor === Date)) date = new Date();
162
+ if (!is(date, "Date")) date = new Date();
116
163
 
117
164
  this.year = date.getFullYear();
118
165
  this.month = date.getMonth() + 1;
@@ -120,18 +167,18 @@
120
167
  }
121
168
  }
122
169
 
123
- Dater.prototype.valueOf = function () {
170
+ Time.prototype.valueOf = function () {
124
171
  return dateFormat(`${this.year}/${this.month}/${this.day}`, {
125
172
  format: this.outputFormat
126
173
  })
127
174
  };
128
175
 
129
- Dater.prototype.addYear = function (year) {
176
+ Time.prototype.addYear = function (year) {
130
177
  this.year += year;
131
178
  return this
132
179
  };
133
180
 
134
- Dater.prototype.addMonth = function (month) {
181
+ Time.prototype.addMonth = function (month) {
135
182
  let month1 = month % 12; // 零碎的月份
136
183
  this.year += (month - month1) / 12; // 整齐的年份
137
184
 
@@ -152,9 +199,29 @@
152
199
  }
153
200
  }
154
201
 
202
+ let totalDay = calcMonthDays(this.year, this.month);
203
+ if (totalDay < this.day) this.day = totalDay;
204
+
155
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)
156
222
  };
157
223
 
158
- exports.Dater = Dater;
224
+ exports.Digits = Digits;
225
+ exports.Time = Time;
159
226
 
160
227
  }));
package/dist/core.min.js CHANGED
@@ -1,9 +1,9 @@
1
1
  /*!
2
- * @oipage/corejs v1.0.0-alpha.1
2
+ * @oipage/corejs v1.0.0
3
3
  * git+https://github.com/oi-contrib/oipage-corejs.git
4
4
  */
5
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
6
  /*!
7
7
  * format of OIPage v1.4.1
8
8
  * git+https://github.com/oi-contrib/OIPage.git
9
- */function e(t,e){var s;if(e=e||e,t){if("object"==typeof t&&t.constructor===Date)s=t;else if(!e.inputFormat&&(s=new Date(t))+""=="Invalid Date")return console.error("非法的日期格式:"+t,s),""}else s=new Date;var o={yyyy:0,MM:0,dd:0,HH:0,mm:0,ss:0};if(s)o.yyyy=s.getFullYear(),o.MM=s.getMonth()+1,o.dd=s.getDate(),o.HH=s.getHours(),o.mm=s.getMinutes(),o.ss=s.getSeconds();else for(var i=e.inputFormat+"",r=0,n=0;n<i.length;n++)if("yyyy"==i.substring(n,n+4))o.yyyy=+t.substring(r,r+4),r+=4,n+=3;else{var y=i.substring(n,n+2);if(["MM","dd","HH","mm","ss"].indexOf(y)>-1){var a=t[r];/\d/.test(t[r+1])?(a+=t[r+1],r+=2):r+=1,o[y]=+a,n+=1}else r+=1}return o.MM<10&&(o.MM="0"+o.MM),o.dd<10&&(o.dd="0"+o.dd),o.HH<10&&(o.HH="0"+o.HH),o.mm<10&&(o.mm="0"+o.mm),o.ss<10&&(o.ss="0"+o.ss),(e.format||"yyyy/MM/dd").replace("yyyy",o.yyyy).replace("MM",o.MM).replace("dd",o.dd).replace("HH",o.HH).replace("mm",o.mm).replace("ss",o.ss)}function s(t,s={}){if(this.inputFormat=s.input||"yyyy/MM/dd",this.outputFormat=s.output||"yyyy/MM/dd",t&&"string"==typeof t){let s=e(t,{inputFormat:this.inputFormat}).split("/");this.year=+s[0],this.month=+s[1],this.day=+s[2]}else t&&"object"==typeof t&&t.constructor===Date||(t=new Date),this.year=t.getFullYear(),this.month=t.getMonth()+1,this.day=t.getDate()}s.prototype.valueOf=function(){return e(`${this.year}/${this.month}/${this.day}`,{format:this.outputFormat})},s.prototype.addYear=function(t){return this.year+=t,this},s.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));return this},t.Dater=s});
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@oipage/core.js",
3
- "version": "1.0.0-alpha.1",
3
+ "version": "1.0.0",
4
4
  "description": "包括日期、时间、浮点运算等常用的一些业务API",
5
5
  "main": "./dist/core.js",
6
6
  "typings": "./types/index.d.ts",
@@ -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
+ }
@@ -1,6 +1,8 @@
1
- import { dateFormat } from "oipage/web/format"
1
+ import { dateFormat } from "oipage/web/format/index"
2
+ import { is } from "../tool"
3
+ import { calcMonthDays } from "./tool"
2
4
 
3
- export function Dater(date, format = {}) {
5
+ export function Time(date, format = {}) {
4
6
  this.inputFormat = format.input || "yyyy/MM/dd"
5
7
  this.outputFormat = format.output || "yyyy/MM/dd"
6
8
 
@@ -17,7 +19,7 @@ export function Dater(date, format = {}) {
17
19
 
18
20
  else {
19
21
  // 不是Date对象
20
- if (!(date && typeof date === "object" && date.constructor === Date)) date = new Date()
22
+ if (!is(date, "Date")) date = new Date()
21
23
 
22
24
  this.year = date.getFullYear()
23
25
  this.month = date.getMonth() + 1
@@ -25,18 +27,18 @@ export function Dater(date, format = {}) {
25
27
  }
26
28
  }
27
29
 
28
- Dater.prototype.valueOf = function () {
30
+ Time.prototype.valueOf = function () {
29
31
  return dateFormat(`${this.year}/${this.month}/${this.day}`, {
30
32
  format: this.outputFormat
31
33
  })
32
34
  }
33
35
 
34
- Dater.prototype.addYear = function (year) {
36
+ Time.prototype.addYear = function (year) {
35
37
  this.year += year
36
38
  return this
37
39
  }
38
40
 
39
- Dater.prototype.addMonth = function (month) {
41
+ Time.prototype.addMonth = function (month) {
40
42
  let month1 = month % 12 // 零碎的月份
41
43
  this.year += (month - month1) / 12 // 整齐的年份
42
44
 
@@ -57,5 +59,16 @@ Dater.prototype.addMonth = function (month) {
57
59
  }
58
60
  }
59
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()
60
73
  return this
61
74
  }
@@ -1,12 +1,3 @@
1
- // 计算某年多少天
2
- export let calcYearDays = function () {
3
- if ((year % 4 != 0) || (year % 100 == 0 && year % 400 != 0)) {
4
- return 365
5
- } else {
6
- return 366
7
- }
8
- }
9
-
10
1
  // 计算某月多少天
11
2
  export let calcMonthDays = function (year, month) {
12
3
 
package/src/index.js CHANGED
@@ -1 +1,2 @@
1
- export * from "./Dater/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
+ }
@@ -1,4 +1,4 @@
1
- export default interface DaterType {
1
+ export default interface TimeType {
2
2
  new(date?: string | Date, format?: {
3
3
 
4
4
  /**
@@ -12,6 +12,21 @@ export default interface DaterType {
12
12
  outout?: "string"
13
13
  }): this
14
14
 
15
+ /**
16
+ * 年
17
+ */
18
+ year: number
19
+
20
+ /**
21
+ * 月
22
+ */
23
+ month: number
24
+
25
+ /**
26
+ * 日
27
+ */
28
+ day: number
29
+
15
30
  /**
16
31
  * 按照format.outout格式返回当前维护的日期
17
32
  */
@@ -28,4 +43,10 @@ export default interface DaterType {
28
43
  * @param month
29
44
  */
30
45
  addMonth(month: number): this
46
+
47
+ /**
48
+ * 新增day天后的日期(如果day为负数,就是减少)
49
+ * @param day
50
+ */
51
+ addDay(day: number): this
31
52
  }
package/types/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
- import DaterType from "./Dater"
1
+ import TimeType from "./Time"
2
+ import DigitsType from "./Digits"
2
3
 
3
- export let Dater: DaterType
4
+ export let Time: TimeType
5
+ export let Digits: DigitsType