@oipage/core.js 0.1.0 → 1.0.0-alpha.1

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.
@@ -0,0 +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
12
+ custom: ['https://zxl20070701.github.io/notebook/home.html']
package/CHANGELOG CHANGED
@@ -1,6 +1,5 @@
1
- v0.1.0:
2
- date:2024-08-29
1
+ v1.0.0:
2
+ date:
3
3
  changes:
4
4
  - 初始化版本
5
- 1、animation 动画
6
- 2、throttle 节流函数
5
+ 1、Dater 日期
package/README.md CHANGED
@@ -1,15 +1,27 @@
1
- # [core.js](https://github.com/oi-contrib/core.js)
2
- 前端开发常用基础&核心方法
1
+ # [@oipage/core.js](https://github.com/oi-contrib/oipage-corejs)
2
+ 包括日期、时间、浮点运算等常用的一些业务API
3
3
 
4
4
  <p>
5
5
  <a href="https://zxl20070701.github.io/toolbox/#/npm-download?packages=@oipage/core.js&interval=7">
6
6
  <img src="https://img.shields.io/npm/dm/@oipage/core.js.svg" alt="downloads">
7
7
  </a>
8
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="Version">
9
+ <img src="https://img.shields.io/npm/v/@oipage/core.js.svg" alt="npm">
10
10
  </a>
11
- <a href="https://github.com/oi-contrib/core.js" target='_blank'>
12
- <img alt="GitHub repo stars" src="https://img.shields.io/github/stars/oi-contrib/core.js?style=social">
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">
13
25
  </a>
14
26
  </p>
15
27
 
@@ -17,58 +29,19 @@
17
29
 
18
30
  ## 如何使用?
19
31
 
20
- 首先,我们需要使用npm进行安装:
21
-
22
- ```shell
23
- npm install --save @oipage/core.js
24
- ```
25
-
26
- 安装完成以后,就可以在项目中引入了,下面就不同的环境,使用`aniamtion`举例子。
27
-
28
- ### 浏览器环境下
29
-
30
- 可以直接使用import语法:
31
-
32
- ```js
33
- import { animation } from "@oipage/core.js"
34
- ```
35
-
36
- 当然,也可以单独引入:
37
-
38
- ```js
39
- import { animation } from "@oipage/core.js/src/animation/index"
40
32
  ```
41
-
42
- > 支持Typescript环境。
43
-
44
- ### Nodejs环境下
45
-
46
- 直接使用require语法即可:
47
-
48
- ```js
49
- const { animation } = require("@oipage/core.js");
50
- ```
51
-
52
- ### 使用CDN方式
53
-
54
- 此外,在浏览器端,如果你不想使用npm管理,或者是老项目,也支持CDN的方式:
55
-
56
- ```html
57
- <script src="https://cdn.jsdelivr.net/npm/@oipage/core.js"></script>
33
+ npm install --save @oipage/core.js
58
34
  ```
59
35
 
60
- 然后直接:
36
+ 然后直接引入需要的工具对象使用即可,比如Timer:
61
37
 
62
38
  ```js
63
- Corejs.animation(function(){
64
- // todo
65
- });
39
+ import { Dater } from "@oipage/core.js"
66
40
  ```
67
41
 
68
- 具体的API细节(使用import语法说明,其余方式类似),如下:
42
+ 更多具体的工具对象清单如下:
69
43
 
70
- - [animation 动画](./docs/animation.md)
71
- - [throttle 节流函数](./docs/throttle.md)
44
+ - [Dater 日期](./docs/Dater.md)
72
45
 
73
46
  ## 版权
74
47
 
package/dist/core.js ADDED
@@ -0,0 +1,160 @@
1
+ /*!
2
+ * @oipage/corejs v1.0.0-alpha.1
3
+ * git+https://github.com/oi-contrib/oipage-corejs.git
4
+ */
5
+ (function (global, factory) {
6
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
7
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
8
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.corejs = {}));
9
+ })(this, (function (exports) { 'use strict';
10
+
11
+ /*!
12
+ * format of OIPage v1.4.1
13
+ * git+https://github.com/oi-contrib/OIPage.git
14
+ */
15
+
16
+
17
+ function dateFormat(input, option) {
18
+ option = option || option;
19
+ var dateObj;
20
+
21
+ // 如果无值,就用当前日期
22
+ if (!input) {
23
+ dateObj = new Date();
24
+ }
25
+
26
+ // 日期对象
27
+ else if (typeof input == 'object' && input.constructor === Date) {
28
+ dateObj = input;
29
+ }
30
+
31
+ // 否则就尝试创建日期对象
32
+ else if (!option.inputFormat) {
33
+ dateObj = new Date(input);
34
+ if (dateObj + "" == "Invalid Date") {
35
+ console.error("非法的日期格式:" + input, dateObj);
36
+ return "";
37
+ }
38
+ }
39
+
40
+ // 年、月、日、时、分、秒
41
+ var single = {
42
+ yyyy: 0, MM: 0, dd: 0, HH: 0, mm: 0, ss: 0
43
+ };
44
+
45
+ // 如果获取了日期实例
46
+ if (dateObj) {
47
+
48
+ single.yyyy = dateObj.getFullYear();
49
+ single.MM = dateObj.getMonth() + 1;
50
+ single.dd = dateObj.getDate();
51
+
52
+ single.HH = dateObj.getHours();
53
+ single.mm = dateObj.getMinutes();
54
+ single.ss = dateObj.getSeconds();
55
+ }
56
+
57
+ // 否则只能借助 inputFormat 来解析
58
+ else {
59
+ var inputFormat = option.inputFormat + "", valIndex = 0;
60
+ for (var index = 0; index < inputFormat.length; index++) {
61
+
62
+ // 年
63
+ if (inputFormat.substring(index, index + 4) == "yyyy") {
64
+ single.yyyy = +input.substring(valIndex, valIndex + 4);
65
+
66
+ valIndex += 4;
67
+ index += 3;
68
+ } else {
69
+ var curFlag = inputFormat.substring(index, index + 2);
70
+ if (["MM", "dd", "HH", "mm", "ss"].indexOf(curFlag) > -1) {
71
+ var valContent = input[valIndex];
72
+ if (/\d/.test(input[valIndex + 1])) {
73
+ valContent += input[valIndex + 1];
74
+ valIndex += 2;
75
+ } else {
76
+ valIndex += 1;
77
+ }
78
+
79
+ (single)[curFlag] = +valContent;
80
+ index += 1;
81
+ } else {
82
+ valIndex += 1;
83
+ }
84
+ }
85
+ }
86
+ }
87
+
88
+ if (single.MM < 10) single.MM = "0" + single.MM;
89
+ if (single.dd < 10) single.dd = "0" + single.dd;
90
+ if (single.HH < 10) single.HH = "0" + single.HH;
91
+ if (single.mm < 10) single.mm = "0" + single.mm;
92
+ if (single.ss < 10) single.ss = "0" + single.ss;
93
+
94
+ var format = option.format || "yyyy/MM/dd";
95
+ 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
+ }
97
+
98
+ function Dater(date, format = {}) {
99
+ this.inputFormat = format.input || "yyyy/MM/dd";
100
+ this.outputFormat = format.output || "yyyy/MM/dd";
101
+
102
+ // 字符串
103
+ if (date && typeof date === "string") {
104
+ let temp = dateFormat(date, {
105
+ inputFormat: this.inputFormat
106
+ }).split("/");
107
+
108
+ this.year = +temp[0];
109
+ this.month = +temp[1];
110
+ this.day = +temp[2];
111
+ }
112
+
113
+ else {
114
+ // 不是Date对象
115
+ if (!(date && typeof date === "object" && date.constructor === Date)) date = new Date();
116
+
117
+ this.year = date.getFullYear();
118
+ this.month = date.getMonth() + 1;
119
+ this.day = date.getDate();
120
+ }
121
+ }
122
+
123
+ Dater.prototype.valueOf = function () {
124
+ return dateFormat(`${this.year}/${this.month}/${this.day}`, {
125
+ format: this.outputFormat
126
+ })
127
+ };
128
+
129
+ Dater.prototype.addYear = function (year) {
130
+ this.year += year;
131
+ return this
132
+ };
133
+
134
+ Dater.prototype.addMonth = function (month) {
135
+ let month1 = month % 12; // 零碎的月份
136
+ this.year += (month - month1) / 12; // 整齐的年份
137
+
138
+ if (month1 > 0) {
139
+ let month2 = 12 - this.month; // 新增月份剩余空间
140
+ if (month2 >= month1) {
141
+ this.month += month1;
142
+ } else {
143
+ this.year += 1;
144
+ this.month = month1 - month2;
145
+ }
146
+ } else if (month1 < 0) {
147
+ if (this.month + month1 > 0) {
148
+ this.month += month1;
149
+ } else {
150
+ this.year -= 1;
151
+ this.month += 12 + month1;
152
+ }
153
+ }
154
+
155
+ return this
156
+ };
157
+
158
+ exports.Dater = Dater;
159
+
160
+ }));
@@ -0,0 +1,9 @@
1
+ /*!
2
+ * @oipage/corejs v1.0.0-alpha.1
3
+ * git+https://github.com/oi-contrib/oipage-corejs.git
4
+ */
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 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});
package/package.json CHANGED
@@ -1,33 +1,34 @@
1
1
  {
2
2
  "name": "@oipage/core.js",
3
- "version": "0.1.0",
4
- "description": "前端开发常用基础&核心方法",
5
- "main": "dist/corejs.min.js",
3
+ "version": "1.0.0-alpha.1",
4
+ "description": "包括日期、时间、浮点运算等常用的一些业务API",
5
+ "main": "./dist/core.js",
6
6
  "typings": "./types/index.d.ts",
7
- "sideEffects": false,
8
7
  "scripts": {
9
8
  "start": "npm install && npm run dev",
10
- "dev": "devby --run \"rollup --config ./rollup.config.js -w\" \"devby -s 20000\"",
11
- "build": "rollup --config ./rollup.config.js && terser ./dist/corejs.js --compress --mangle --output ./dist/corejs.min.js"
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"
12
11
  },
13
12
  "repository": {
14
13
  "type": "git",
15
- "url": "git+https://github.com/oi-contrib/core.js.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"
16
19
  },
17
20
  "keywords": [
18
- "h5",
19
- "core.js",
20
- "oipage"
21
+ "oipage",
22
+ "core",
23
+ "javascript"
21
24
  ],
22
- "author": "zxl20070701",
23
25
  "license": "MIT",
24
- "bugs": {
25
- "url": "https://github.com/oi-contrib/core.js/issues"
26
- },
27
- "homepage": "https://github.com/oi-contrib/core.js",
28
26
  "devDependencies": {
29
- "devby": "^0.7.0",
27
+ "@rollup/plugin-node-resolve": "^16.0.1",
30
28
  "rollup": "^4.6.1",
31
29
  "terser": "^5.17.1"
30
+ },
31
+ "dependencies": {
32
+ "oipage": "^1.4.1"
32
33
  }
33
- }
34
+ }
@@ -0,0 +1,61 @@
1
+ import { dateFormat } from "oipage/web/format"
2
+
3
+ export function Dater(date, format = {}) {
4
+ this.inputFormat = format.input || "yyyy/MM/dd"
5
+ this.outputFormat = format.output || "yyyy/MM/dd"
6
+
7
+ // 字符串
8
+ if (date && typeof date === "string") {
9
+ let temp = dateFormat(date, {
10
+ inputFormat: this.inputFormat
11
+ }).split("/")
12
+
13
+ this.year = +temp[0]
14
+ this.month = +temp[1]
15
+ this.day = +temp[2]
16
+ }
17
+
18
+ else {
19
+ // 不是Date对象
20
+ if (!(date && typeof date === "object" && date.constructor === Date)) date = new Date()
21
+
22
+ this.year = date.getFullYear()
23
+ this.month = date.getMonth() + 1
24
+ this.day = date.getDate()
25
+ }
26
+ }
27
+
28
+ Dater.prototype.valueOf = function () {
29
+ return dateFormat(`${this.year}/${this.month}/${this.day}`, {
30
+ format: this.outputFormat
31
+ })
32
+ }
33
+
34
+ Dater.prototype.addYear = function (year) {
35
+ this.year += year
36
+ return this
37
+ }
38
+
39
+ Dater.prototype.addMonth = function (month) {
40
+ let month1 = month % 12 // 零碎的月份
41
+ this.year += (month - month1) / 12 // 整齐的年份
42
+
43
+ if (month1 > 0) {
44
+ let month2 = 12 - this.month // 新增月份剩余空间
45
+ if (month2 >= month1) {
46
+ this.month += month1
47
+ } else {
48
+ this.year += 1
49
+ this.month = month1 - month2
50
+ }
51
+ } else if (month1 < 0) {
52
+ if (this.month + month1 > 0) {
53
+ this.month += month1
54
+ } else {
55
+ this.year -= 1
56
+ this.month += 12 + month1
57
+ }
58
+ }
59
+
60
+ return this
61
+ }
@@ -0,0 +1,25 @@
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
+ // 计算某月多少天
11
+ export let calcMonthDays = function (year, month) {
12
+
13
+ if (month == 2) {
14
+
15
+ if ((year % 4 != 0) || (year % 100 == 0 && year % 400 != 0)) {
16
+ return 28
17
+ } else {
18
+ return 29
19
+ }
20
+
21
+ } else {
22
+ return [31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1]
23
+ }
24
+
25
+ }
package/src/index.js CHANGED
@@ -1,7 +1 @@
1
- import { animation } from "./animation/index.js";
2
- import { throttle } from "./throttle/index.js";
3
-
4
- export default {
5
- animation: animation,
6
- throttle: throttle
7
- };
1
+ export * from "./Dater/index.js"
package/types/Dater.ts ADDED
@@ -0,0 +1,31 @@
1
+ export default interface DaterType {
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
+ * 按照format.outout格式返回当前维护的日期
17
+ */
18
+ valueOf(): string
19
+
20
+ /**
21
+ * 新增year年后的日期(如果year为负数,就是减少)
22
+ * @param year
23
+ */
24
+ addYear(year: number): this
25
+
26
+ /**
27
+ * 新增month月后的日期(如果month为负数,就是减少)
28
+ * @param month
29
+ */
30
+ addMonth(month: number): this
31
+ }
package/types/index.d.ts CHANGED
@@ -1,10 +1,3 @@
1
- import { animationType } from "../src/animation"
2
- import { throttleType } from "../src/throttle"
1
+ import DaterType from "./Dater"
3
2
 
4
- export default class Corejs {
5
- static animation: animationType
6
- static throttle: throttleType
7
- }
8
-
9
- export let animation: animationType
10
- export let throttle: throttleType
3
+ export let Dater: DaterType
package/dist/corejs.js DELETED
@@ -1,172 +0,0 @@
1
- /*!
2
- * @oipage/core.js v0.1.0
3
- * git+https://github.com/oi-contrib/core.js.git
4
- */
5
- (function (global, factory) {
6
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
7
- typeof define === 'function' && define.amd ? define(factory) :
8
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Corejs = factory());
9
- })(this, (function () { 'use strict';
10
-
11
- //当前正在运动的动画的tick函数堆栈
12
- var $timers = [];
13
- //唯一定时器的定时间隔
14
- var $interval = 13;
15
- //定时器ID
16
- var $timerId;
17
-
18
- /**
19
- * 动画轮播
20
- * @param {function} doback 轮询函数,有一个形参deep,0-1,表示执行进度
21
- * @param {number} duration 动画时长,可选
22
- * @param {function} callback 动画结束回调,可选,有一个形参deep,0-1,表示执行进度
23
- *
24
- * @returns {function} 返回一个函数,调用该函数,可以提前结束动画
25
- */
26
- function animation(doback, duration, callback) {
27
- if (arguments.length < 2) duration = 400;
28
- if (arguments.length < 3) callback = function () { };
29
-
30
- var clock = {
31
- //把tick函数推入堆栈
32
- "timer": function (tick, duration, callback) {
33
- if (!tick) {
34
- throw new Error('Tick is required!');
35
- }
36
- var id = new Date().valueOf() + "_" + (Math.random() * 1000).toFixed(0);
37
- $timers.push({
38
- "id": id,
39
- "createTime": new Date(),
40
- "tick": tick,
41
- "duration": duration,
42
- "callback": callback
43
- });
44
- clock.start();
45
- return id;
46
- },
47
-
48
- //开启唯一的定时器timerId
49
- "start": function () {
50
- if (!$timerId) {
51
- $timerId = setInterval(clock.tick, $interval);
52
- }
53
- },
54
-
55
- //被定时器调用,遍历timers堆栈
56
- "tick": function () {
57
- var createTime, flag, tick, callback, timer, duration, passTime, timers = $timers;
58
-
59
- $timers = [];
60
- $timers.length = 0;
61
-
62
- for (flag = 0; flag < timers.length; flag++) {
63
- //初始化数据
64
- timer = timers[flag];
65
- createTime = timer.createTime;
66
- tick = timer.tick;
67
- duration = timer.duration;
68
- callback = timer.callback;
69
-
70
- //执行
71
- passTime = (+new Date().valueOf() - createTime.valueOf()) / duration;
72
- passTime = passTime > 1 ? 1 : passTime;
73
- tick(passTime);
74
- if (passTime < 1 && timer.id) {
75
- //动画没有结束再添加
76
- $timers.push(timer);
77
- } else {
78
- callback(passTime);
79
- }
80
- }
81
- if ($timers.length <= 0) {
82
- clock.stop();
83
- }
84
- },
85
-
86
- //停止定时器,重置timerId=null
87
- "stop": function () {
88
- if ($timerId) {
89
- clearInterval($timerId);
90
- $timerId = null;
91
- }
92
- }
93
- };
94
-
95
- var id = clock.timer(function (deep) {
96
- //其中deep为0-1,表示改变的程度
97
- doback(deep);
98
- }, duration, callback);
99
-
100
- // 返回一个函数
101
- // 用于在动画结束前结束动画
102
- return function () {
103
- var i;
104
- for (i in $timers) {
105
- if ($timers[i].id == id) {
106
- $timers[i].id = void 0;
107
- return;
108
- }
109
- }
110
- };
111
-
112
- }
113
-
114
- function throttle(callback, _option) {
115
-
116
- // 缺省值
117
- var option = {
118
- time: 200,
119
- keep: false,
120
- opportunity: "end"
121
- };
122
-
123
- // 校对
124
- if (_option) {
125
- for (var key in _option) {
126
- option[key] = _option[key];
127
- }
128
- }
129
-
130
- var hadInterval = false, hadClick = false, oneClick = false, arg;
131
- return function () {
132
- const _this = this;
133
- arg = arguments;
134
-
135
- // 如果前置任务都完成了
136
- if (!hadInterval) {
137
- if (option.opportunity != 'end') {
138
- callback.apply(_this, arg);
139
- }
140
- hadInterval = true;
141
-
142
- var interval = setInterval(() => {
143
- if (hadClick) {
144
- if (!option.keep) {
145
- callback.apply(_this, arg);
146
- }
147
- } else {
148
- if (option.opportunity != 'begin') {
149
- if (oneClick || option.opportunity == 'end') callback.apply(_this, arg);
150
- }
151
- hadInterval = false;
152
- oneClick = false;
153
- clearInterval(interval);
154
- }
155
- hadClick = false;
156
- }, option.time);
157
- } else {
158
- hadClick = true;
159
- oneClick = true;
160
- }
161
-
162
- };
163
- }
164
-
165
- var index = {
166
- animation: animation,
167
- throttle: throttle
168
- };
169
-
170
- return index;
171
-
172
- }));
@@ -1,5 +0,0 @@
1
- /*!
2
- * @oipage/core.js v0.1.0
3
- * git+https://github.com/oi-contrib/core.js.git
4
- */
5
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).Corejs=e()}(this,(function(){"use strict";var t,e=[];var n={animation:function(n,i,r){arguments.length<2&&(i=400),arguments.length<3&&(r=function(){});var o={timer:function(t,n,i){if(!t)throw new Error("Tick is required!");var r=(new Date).valueOf()+"_"+(1e3*Math.random()).toFixed(0);return e.push({id:r,createTime:new Date,tick:t,duration:n,callback:i}),o.start(),r},start:function(){t||(t=setInterval(o.tick,13))},tick:function(){var t,n,i,r,a,u,f,l=e;for((e=[]).length=0,n=0;n<l.length;n++)t=(a=l[n]).createTime,i=a.tick,u=a.duration,r=a.callback,i(f=(f=(+(new Date).valueOf()-t.valueOf())/u)>1?1:f),f<1&&a.id?e.push(a):r(f);e.length<=0&&o.stop()},stop:function(){t&&(clearInterval(t),t=null)}},a=o.timer((function(t){n(t)}),i,r);return function(){var t;for(t in e)if(e[t].id==a)return void(e[t].id=void 0)}},throttle:function(t,e){var n={time:200,keep:!1,opportunity:"end"};if(e)for(var i in e)n[i]=e[i];var r,o=!1,a=!1,u=!1;return function(){const e=this;if(r=arguments,o)a=!0,u=!0;else{"end"!=n.opportunity&&t.apply(e,r),o=!0;var i=setInterval((()=>{a?n.keep||t.apply(e,r):("begin"!=n.opportunity&&(u||"end"==n.opportunity)&&t.apply(e,r),o=!1,u=!1,clearInterval(i)),a=!1}),n.time)}}}};return n}));
@@ -1,12 +0,0 @@
1
- interface animationFun {
2
- (deep: number): void
3
- }
4
-
5
- /**
6
- * 轮询动画,返回一个函数,调用该函数,可以提前结束动画
7
- */
8
- export interface animationType {
9
- (doback: animationFun, duration?: number, callback?: animationFun): Function
10
- }
11
-
12
- export let animation: animationType
@@ -1,102 +0,0 @@
1
- //当前正在运动的动画的tick函数堆栈
2
- var $timers = [];
3
- //唯一定时器的定时间隔
4
- var $interval = 13;
5
- //定时器ID
6
- var $timerId;
7
-
8
- /**
9
- * 动画轮播
10
- * @param {function} doback 轮询函数,有一个形参deep,0-1,表示执行进度
11
- * @param {number} duration 动画时长,可选
12
- * @param {function} callback 动画结束回调,可选,有一个形参deep,0-1,表示执行进度
13
- *
14
- * @returns {function} 返回一个函数,调用该函数,可以提前结束动画
15
- */
16
- export function animation(doback, duration, callback) {
17
- if (arguments.length < 2) duration = 400;
18
- if (arguments.length < 3) callback = function () { };
19
-
20
- var clock = {
21
- //把tick函数推入堆栈
22
- "timer": function (tick, duration, callback) {
23
- if (!tick) {
24
- throw new Error('Tick is required!');
25
- }
26
- var id = new Date().valueOf() + "_" + (Math.random() * 1000).toFixed(0);
27
- $timers.push({
28
- "id": id,
29
- "createTime": new Date(),
30
- "tick": tick,
31
- "duration": duration,
32
- "callback": callback
33
- });
34
- clock.start();
35
- return id;
36
- },
37
-
38
- //开启唯一的定时器timerId
39
- "start": function () {
40
- if (!$timerId) {
41
- $timerId = setInterval(clock.tick, $interval);
42
- }
43
- },
44
-
45
- //被定时器调用,遍历timers堆栈
46
- "tick": function () {
47
- var createTime, flag, tick, callback, timer, duration, passTime, timers = $timers;
48
-
49
- $timers = [];
50
- $timers.length = 0;
51
-
52
- for (flag = 0; flag < timers.length; flag++) {
53
- //初始化数据
54
- timer = timers[flag];
55
- createTime = timer.createTime;
56
- tick = timer.tick;
57
- duration = timer.duration;
58
- callback = timer.callback;
59
-
60
- //执行
61
- passTime = (+new Date().valueOf() - createTime.valueOf()) / duration;
62
- passTime = passTime > 1 ? 1 : passTime;
63
- tick(passTime);
64
- if (passTime < 1 && timer.id) {
65
- //动画没有结束再添加
66
- $timers.push(timer);
67
- } else {
68
- callback(passTime);
69
- }
70
- }
71
- if ($timers.length <= 0) {
72
- clock.stop();
73
- }
74
- },
75
-
76
- //停止定时器,重置timerId=null
77
- "stop": function () {
78
- if ($timerId) {
79
- clearInterval($timerId);
80
- $timerId = null;
81
- }
82
- }
83
- };
84
-
85
- var id = clock.timer(function (deep) {
86
- //其中deep为0-1,表示改变的程度
87
- doback(deep);
88
- }, duration, callback);
89
-
90
- // 返回一个函数
91
- // 用于在动画结束前结束动画
92
- return function () {
93
- var i;
94
- for (i in $timers) {
95
- if ($timers[i].id == id) {
96
- $timers[i].id = void 0;
97
- return;
98
- }
99
- }
100
- };
101
-
102
- };
@@ -1,31 +0,0 @@
1
- export type throttleOpportunityType = "begin" | "end" | "wide"
2
-
3
- export interface throttleOptionType {
4
-
5
- /**
6
- * 节流时长
7
- */
8
- time?: number
9
-
10
- /**
11
- * 是否持续节流
12
- */
13
- keep?: boolean
14
-
15
- /**
16
- * 执行时机
17
- *
18
- * begin(开始触发)、end(结束触发)、wide(第一次开始触发,其余结束触发)
19
- */
20
- opportunity?: throttleOpportunityType
21
-
22
- }
23
-
24
- /**
25
- * 节流函数
26
- */
27
- export interface throttleType {
28
- (callback: Function, option?: throttleOptionType): Function
29
- }
30
-
31
- export let throttle: throttleType
@@ -1,50 +0,0 @@
1
- export function throttle(callback, _option) {
2
-
3
- // 缺省值
4
- var option = {
5
- time: 200,
6
- keep: false,
7
- opportunity: "end"
8
- };
9
-
10
- // 校对
11
- if (_option) {
12
- for (var key in _option) {
13
- option[key] = _option[key];
14
- }
15
- }
16
-
17
- var hadInterval = false, hadClick = false, oneClick = false, arg;
18
- return function () {
19
- const _this = this;
20
- arg = arguments;
21
-
22
- // 如果前置任务都完成了
23
- if (!hadInterval) {
24
- if (option.opportunity != 'end') {
25
- callback.apply(_this, arg);
26
- }
27
- hadInterval = true;
28
-
29
- var interval = setInterval(() => {
30
- if (hadClick) {
31
- if (!option.keep) {
32
- callback.apply(_this, arg);
33
- }
34
- } else {
35
- if (option.opportunity != 'begin') {
36
- if (oneClick || option.opportunity == 'end') callback.apply(_this, arg);
37
- }
38
- hadInterval = false;
39
- oneClick = false;
40
- clearInterval(interval);
41
- }
42
- hadClick = false;
43
- }, option.time);
44
- } else {
45
- hadClick = true;
46
- oneClick = true;
47
- }
48
-
49
- };
50
- };