ddan-js 1.0.0 → 1.0.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.
package/LICENSE CHANGED
@@ -0,0 +1,7 @@
1
+ Copyright 2017 yury <yurangyi@heywoods.cn>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -0,0 +1,10 @@
1
+ # ddanJs
2
+
3
+ A simple library
4
+
5
+ ### Usage
6
+
7
+ ```bash
8
+ npm i ddanJs
9
+ ```
10
+
@@ -0,0 +1,239 @@
1
+ /**
2
+ *
3
+ * @param str
4
+ * @returns
5
+ */
6
+ function gbkLength(str) {
7
+ let len = 0;
8
+ if (!str)
9
+ return 0;
10
+ for (let i = 0; i < str.length; i++) {
11
+ if (str.charCodeAt(i) > 127 || str.charCodeAt(i) === 94) {
12
+ len += 2;
13
+ }
14
+ else {
15
+ len++;
16
+ }
17
+ }
18
+ return len;
19
+ }
20
+ /**
21
+ * 根据gbk字符长度截取内容
22
+ * @param {string} source
23
+ * @param {number} len
24
+ */
25
+ function gbkCut(source, len) {
26
+ if (!source || len <= 0)
27
+ return "";
28
+ let n = 0;
29
+ let idx = 0;
30
+ for (let i = 0; i < source.length; i++) {
31
+ if (source.charCodeAt(i) > 127 || source.charCodeAt(i) === 94) {
32
+ n += 2;
33
+ }
34
+ else {
35
+ n++;
36
+ }
37
+ if (n > len) {
38
+ break;
39
+ }
40
+ idx = i;
41
+ }
42
+ return source.substr(0, idx + 1);
43
+ }
44
+
45
+ var gbk = /*#__PURE__*/Object.freeze({
46
+ __proto__: null,
47
+ gbkLength: gbkLength,
48
+ gbkCut: gbkCut
49
+ });
50
+
51
+ /**
52
+ * 是否是今天
53
+ * @param {*} date
54
+ * @returns
55
+ */
56
+ const isToday = (date) => new Date().toDateString() === new Date(date).toDateString();
57
+ /**
58
+ * 时间格式化
59
+ * @param {string|number|Date} time
60
+ * @param {string} reg "yyyyMMdd hhmmss"
61
+ * @returns
62
+ */
63
+ const timeFormat = (time, reg) => {
64
+ const date = typeof time === "string" || typeof time === "number" ? new Date(time) : time;
65
+ const map = {};
66
+ map.yyyy = date.getFullYear();
67
+ map.yy = `${map.yyyy}`.substr(2);
68
+ map.M = date.getMonth() + 1;
69
+ map.MM = (map.M < 10 ? "0" : "") + map.M;
70
+ map.d = date.getDate();
71
+ map.dd = (map.d < 10 ? "0" : "") + map.d;
72
+ map.h = date.getHours();
73
+ map.hh = (map.h < 10 ? "0" : "") + map.h;
74
+ map.m = date.getMinutes();
75
+ map.mm = (map.m < 10 ? "0" : "") + map.m;
76
+ map.s = date.getSeconds();
77
+ map.ss = (map.s < 10 ? "0" : "") + map.s;
78
+ map.S = date.getMilliseconds();
79
+ map.SS = (map.S < 10 ? "0" : "") + map.S;
80
+ map.SSS = map.S.toString().padStart(3, 0);
81
+ return reg.replace(/\byyyy|yy|MM|M|dd|d|hh|h|mm|m|ss|s|SSS|SS|S\b/g, ($1) => map[$1]);
82
+ };
83
+ function parseTime({ year = 0, month = 0, date = 0, hour = 0, minute = 0, second = 0 }) {
84
+ const format = `${year || "yyyy"}/${month || "MM"}/${date || "dd"} ${hour}:${minute}:${second}`;
85
+ return new Date(timeFormat(Date.now(), format)).getTime();
86
+ }
87
+
88
+ var time = /*#__PURE__*/Object.freeze({
89
+ __proto__: null,
90
+ isToday: isToday,
91
+ timeFormat: timeFormat,
92
+ parseTime: parseTime
93
+ });
94
+
95
+ /**
96
+ * 休眠
97
+ * @param {number} time
98
+ * @returns
99
+ */
100
+ const sleep = (time = 1000) => new Promise((resolve) => {
101
+ setTimeout(resolve, time);
102
+ });
103
+ function run(cb) {
104
+ try {
105
+ if (!cb)
106
+ return null;
107
+ return cb();
108
+ }
109
+ catch (err) {
110
+ return undefined;
111
+ }
112
+ }
113
+ const task = (promise) => promise.then((data) => [data, null]).catch((err) => [undefined, err]);
114
+ const debounce = (fn, interval = 300) => {
115
+ let timer;
116
+ const intervalTime = interval || 300;
117
+ return function func(...args) {
118
+ const context = this;
119
+ if (timer)
120
+ clearTimeout(timer);
121
+ timer = setTimeout(() => {
122
+ fn.call(context, ...args);
123
+ }, intervalTime);
124
+ };
125
+ };
126
+ const throttle = (fn, delay = 300) => {
127
+ let timer;
128
+ const delayTime = delay || 300;
129
+ let lastTime = Date.now();
130
+ return function func(...args) {
131
+ const context = this;
132
+ const curTime = Date.now();
133
+ const remaining = delayTime - (curTime - lastTime);
134
+ if (remaining < 0) {
135
+ timer && clearTimeout(timer);
136
+ timer = null;
137
+ fn.call(context, ...args);
138
+ lastTime = curTime;
139
+ }
140
+ else if (!timer) {
141
+ timer = setTimeout(() => {
142
+ fn.call(context, ...args);
143
+ lastTime = Date.now();
144
+ }, delayTime);
145
+ }
146
+ };
147
+ };
148
+
149
+ var hook = /*#__PURE__*/Object.freeze({
150
+ __proto__: null,
151
+ sleep: sleep,
152
+ run: run,
153
+ task: task,
154
+ debounce: debounce,
155
+ throttle: throttle
156
+ });
157
+
158
+ class math {
159
+ /**
160
+ * 随机取值[0, max)
161
+ * @param max 最大值
162
+ */
163
+ static random(max) {
164
+ return Math.floor(Math.random() * max);
165
+ }
166
+ /**
167
+ * 范围随机取值[min, max]
168
+ * @param min 最小值
169
+ * @param max 最大值
170
+ */
171
+ static randomRange(min, max) {
172
+ return Math.floor(Math.random() * (max - min + 1) + min);
173
+ }
174
+ /**
175
+ * 插值
176
+ * @param start
177
+ * @param end
178
+ * @param t
179
+ */
180
+ static lerp(start, end, t) {
181
+ return start * (1 - t) + end * t;
182
+ }
183
+ }
184
+
185
+ const { toString } = Object.prototype;
186
+ const util = {
187
+ getTag(value) {
188
+ if (value == null) {
189
+ return value === undefined ? "[object Undefined]" : "[object Null]";
190
+ }
191
+ return toString.call(value);
192
+ },
193
+ getType(value) {
194
+ let str = "";
195
+ if (value == null) {
196
+ str = value === undefined ? "[object Undefined]" : "[object Null]";
197
+ }
198
+ else {
199
+ str = toString.call(value);
200
+ }
201
+ const mat = str.match(/\w+/g) || ["object", "Undefined"];
202
+ return mat[1] || "";
203
+ }
204
+ };
205
+
206
+ /**
207
+ * 判断是否数字
208
+ * @param value
209
+ * @returns
210
+ */
211
+ function isNumber(value) {
212
+ return util.getTag(value) === "[object Number]";
213
+ }
214
+ /**
215
+ * 判断是否是字符串
216
+ * @param value
217
+ * @returns
218
+ */
219
+ function isString(value) {
220
+ return util.getTag(value) === "[object String]";
221
+ }
222
+ function isChinese(content) {
223
+ if (!content)
224
+ return false;
225
+ const reg = /^[\u4E00-\u9FA5]+$/;
226
+ if (!reg.test(content)) {
227
+ return false;
228
+ }
229
+ return true;
230
+ }
231
+
232
+ var is = /*#__PURE__*/Object.freeze({
233
+ __proto__: null,
234
+ isNumber: isNumber,
235
+ isString: isString,
236
+ isChinese: isChinese
237
+ });
238
+
239
+ export { gbk, hook, is, math, time };
package/bin/ddan-js.js ADDED
@@ -0,0 +1,253 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.DdanJs = {}));
5
+ })(this, (function (exports) { 'use strict';
6
+
7
+ /**
8
+ *
9
+ * @param str
10
+ * @returns
11
+ */
12
+ function gbkLength(str) {
13
+ let len = 0;
14
+ if (!str)
15
+ return 0;
16
+ for (let i = 0; i < str.length; i++) {
17
+ if (str.charCodeAt(i) > 127 || str.charCodeAt(i) === 94) {
18
+ len += 2;
19
+ }
20
+ else {
21
+ len++;
22
+ }
23
+ }
24
+ return len;
25
+ }
26
+ /**
27
+ * 根据gbk字符长度截取内容
28
+ * @param {string} source
29
+ * @param {number} len
30
+ */
31
+ function gbkCut(source, len) {
32
+ if (!source || len <= 0)
33
+ return "";
34
+ let n = 0;
35
+ let idx = 0;
36
+ for (let i = 0; i < source.length; i++) {
37
+ if (source.charCodeAt(i) > 127 || source.charCodeAt(i) === 94) {
38
+ n += 2;
39
+ }
40
+ else {
41
+ n++;
42
+ }
43
+ if (n > len) {
44
+ break;
45
+ }
46
+ idx = i;
47
+ }
48
+ return source.substr(0, idx + 1);
49
+ }
50
+
51
+ var gbk = /*#__PURE__*/Object.freeze({
52
+ __proto__: null,
53
+ gbkLength: gbkLength,
54
+ gbkCut: gbkCut
55
+ });
56
+
57
+ /**
58
+ * 是否是今天
59
+ * @param {*} date
60
+ * @returns
61
+ */
62
+ const isToday = (date) => new Date().toDateString() === new Date(date).toDateString();
63
+ /**
64
+ * 时间格式化
65
+ * @param {string|number|Date} time
66
+ * @param {string} reg "yyyyMMdd hhmmss"
67
+ * @returns
68
+ */
69
+ const timeFormat = (time, reg) => {
70
+ const date = typeof time === "string" || typeof time === "number" ? new Date(time) : time;
71
+ const map = {};
72
+ map.yyyy = date.getFullYear();
73
+ map.yy = `${map.yyyy}`.substr(2);
74
+ map.M = date.getMonth() + 1;
75
+ map.MM = (map.M < 10 ? "0" : "") + map.M;
76
+ map.d = date.getDate();
77
+ map.dd = (map.d < 10 ? "0" : "") + map.d;
78
+ map.h = date.getHours();
79
+ map.hh = (map.h < 10 ? "0" : "") + map.h;
80
+ map.m = date.getMinutes();
81
+ map.mm = (map.m < 10 ? "0" : "") + map.m;
82
+ map.s = date.getSeconds();
83
+ map.ss = (map.s < 10 ? "0" : "") + map.s;
84
+ map.S = date.getMilliseconds();
85
+ map.SS = (map.S < 10 ? "0" : "") + map.S;
86
+ map.SSS = map.S.toString().padStart(3, 0);
87
+ return reg.replace(/\byyyy|yy|MM|M|dd|d|hh|h|mm|m|ss|s|SSS|SS|S\b/g, ($1) => map[$1]);
88
+ };
89
+ function parseTime({ year = 0, month = 0, date = 0, hour = 0, minute = 0, second = 0 }) {
90
+ const format = `${year || "yyyy"}/${month || "MM"}/${date || "dd"} ${hour}:${minute}:${second}`;
91
+ return new Date(timeFormat(Date.now(), format)).getTime();
92
+ }
93
+
94
+ var time = /*#__PURE__*/Object.freeze({
95
+ __proto__: null,
96
+ isToday: isToday,
97
+ timeFormat: timeFormat,
98
+ parseTime: parseTime
99
+ });
100
+
101
+ /**
102
+ * 休眠
103
+ * @param {number} time
104
+ * @returns
105
+ */
106
+ const sleep = (time = 1000) => new Promise((resolve) => {
107
+ setTimeout(resolve, time);
108
+ });
109
+ function run(cb) {
110
+ try {
111
+ if (!cb)
112
+ return null;
113
+ return cb();
114
+ }
115
+ catch (err) {
116
+ return undefined;
117
+ }
118
+ }
119
+ const task = (promise) => promise.then((data) => [data, null]).catch((err) => [undefined, err]);
120
+ const debounce = (fn, interval = 300) => {
121
+ let timer;
122
+ const intervalTime = interval || 300;
123
+ return function func(...args) {
124
+ const context = this;
125
+ if (timer)
126
+ clearTimeout(timer);
127
+ timer = setTimeout(() => {
128
+ fn.call(context, ...args);
129
+ }, intervalTime);
130
+ };
131
+ };
132
+ const throttle = (fn, delay = 300) => {
133
+ let timer;
134
+ const delayTime = delay || 300;
135
+ let lastTime = Date.now();
136
+ return function func(...args) {
137
+ const context = this;
138
+ const curTime = Date.now();
139
+ const remaining = delayTime - (curTime - lastTime);
140
+ if (remaining < 0) {
141
+ timer && clearTimeout(timer);
142
+ timer = null;
143
+ fn.call(context, ...args);
144
+ lastTime = curTime;
145
+ }
146
+ else if (!timer) {
147
+ timer = setTimeout(() => {
148
+ fn.call(context, ...args);
149
+ lastTime = Date.now();
150
+ }, delayTime);
151
+ }
152
+ };
153
+ };
154
+
155
+ var hook = /*#__PURE__*/Object.freeze({
156
+ __proto__: null,
157
+ sleep: sleep,
158
+ run: run,
159
+ task: task,
160
+ debounce: debounce,
161
+ throttle: throttle
162
+ });
163
+
164
+ class math {
165
+ /**
166
+ * 随机取值[0, max)
167
+ * @param max 最大值
168
+ */
169
+ static random(max) {
170
+ return Math.floor(Math.random() * max);
171
+ }
172
+ /**
173
+ * 范围随机取值[min, max]
174
+ * @param min 最小值
175
+ * @param max 最大值
176
+ */
177
+ static randomRange(min, max) {
178
+ return Math.floor(Math.random() * (max - min + 1) + min);
179
+ }
180
+ /**
181
+ * 插值
182
+ * @param start
183
+ * @param end
184
+ * @param t
185
+ */
186
+ static lerp(start, end, t) {
187
+ return start * (1 - t) + end * t;
188
+ }
189
+ }
190
+
191
+ const { toString } = Object.prototype;
192
+ const util = {
193
+ getTag(value) {
194
+ if (value == null) {
195
+ return value === undefined ? "[object Undefined]" : "[object Null]";
196
+ }
197
+ return toString.call(value);
198
+ },
199
+ getType(value) {
200
+ let str = "";
201
+ if (value == null) {
202
+ str = value === undefined ? "[object Undefined]" : "[object Null]";
203
+ }
204
+ else {
205
+ str = toString.call(value);
206
+ }
207
+ const mat = str.match(/\w+/g) || ["object", "Undefined"];
208
+ return mat[1] || "";
209
+ }
210
+ };
211
+
212
+ /**
213
+ * 判断是否数字
214
+ * @param value
215
+ * @returns
216
+ */
217
+ function isNumber(value) {
218
+ return util.getTag(value) === "[object Number]";
219
+ }
220
+ /**
221
+ * 判断是否是字符串
222
+ * @param value
223
+ * @returns
224
+ */
225
+ function isString(value) {
226
+ return util.getTag(value) === "[object String]";
227
+ }
228
+ function isChinese(content) {
229
+ if (!content)
230
+ return false;
231
+ const reg = /^[\u4E00-\u9FA5]+$/;
232
+ if (!reg.test(content)) {
233
+ return false;
234
+ }
235
+ return true;
236
+ }
237
+
238
+ var is = /*#__PURE__*/Object.freeze({
239
+ __proto__: null,
240
+ isNumber: isNumber,
241
+ isString: isString,
242
+ isChinese: isChinese
243
+ });
244
+
245
+ exports.gbk = gbk;
246
+ exports.hook = hook;
247
+ exports.is = is;
248
+ exports.math = math;
249
+ exports.time = time;
250
+
251
+ Object.defineProperty(exports, '__esModule', { value: true });
252
+
253
+ }));
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isChinese = exports.isString = exports.isNumber = void 0;
4
+ const util_1 = require("./util");
5
+ /**
6
+ * 判断是否数字
7
+ * @param value
8
+ * @returns
9
+ */
10
+ function isNumber(value) {
11
+ return util_1.default.getTag(value) === "[object Number]";
12
+ }
13
+ exports.isNumber = isNumber;
14
+ /**
15
+ * 判断是否是字符串
16
+ * @param value
17
+ * @returns
18
+ */
19
+ function isString(value) {
20
+ return util_1.default.getTag(value) === "[object String]";
21
+ }
22
+ exports.isString = isString;
23
+ function isChinese(content) {
24
+ if (!content)
25
+ return false;
26
+ const reg = /^[\u4E00-\u9FA5]+$/;
27
+ if (!reg.test(content)) {
28
+ return false;
29
+ }
30
+ return true;
31
+ }
32
+ exports.isChinese = isChinese;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const { toString } = Object.prototype;
4
+ const util = {
5
+ getTag(value) {
6
+ if (value == null) {
7
+ return value === undefined ? "[object Undefined]" : "[object Null]";
8
+ }
9
+ return toString.call(value);
10
+ },
11
+ getType(value) {
12
+ let str = "";
13
+ if (value == null) {
14
+ str = value === undefined ? "[object Undefined]" : "[object Null]";
15
+ }
16
+ else {
17
+ str = toString.call(value);
18
+ }
19
+ const mat = str.match(/\w+/g) || ["object", "Undefined"];
20
+ return mat[1] || "";
21
+ }
22
+ };
23
+ exports.default = util;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.is = exports.math = exports.hook = exports.time = exports.gbk = void 0;
4
+ const gbk = require("./modules/gbk");
5
+ exports.gbk = gbk;
6
+ const time = require("./modules/time");
7
+ exports.time = time;
8
+ const hook = require("./modules/hook");
9
+ exports.hook = hook;
10
+ const math_1 = require("./modules/math");
11
+ exports.math = math_1.default;
12
+ const is = require("./common/is");
13
+ exports.is = is;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.gbkCut = exports.gbkLength = void 0;
4
+ /**
5
+ *
6
+ * @param str
7
+ * @returns
8
+ */
9
+ function gbkLength(str) {
10
+ let len = 0;
11
+ if (!str)
12
+ return 0;
13
+ for (let i = 0; i < str.length; i++) {
14
+ if (str.charCodeAt(i) > 127 || str.charCodeAt(i) === 94) {
15
+ len += 2;
16
+ }
17
+ else {
18
+ len++;
19
+ }
20
+ }
21
+ return len;
22
+ }
23
+ exports.gbkLength = gbkLength;
24
+ /**
25
+ * 根据gbk字符长度截取内容
26
+ * @param {string} source
27
+ * @param {number} len
28
+ */
29
+ function gbkCut(source, len) {
30
+ if (!source || len <= 0)
31
+ return "";
32
+ let n = 0;
33
+ let idx = 0;
34
+ for (let i = 0; i < source.length; i++) {
35
+ if (source.charCodeAt(i) > 127 || source.charCodeAt(i) === 94) {
36
+ n += 2;
37
+ }
38
+ else {
39
+ n++;
40
+ }
41
+ if (n > len) {
42
+ break;
43
+ }
44
+ idx = i;
45
+ }
46
+ return source.substr(0, idx + 1);
47
+ }
48
+ exports.gbkCut = gbkCut;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.throttle = exports.debounce = exports.task = exports.run = exports.sleep = void 0;
4
+ /**
5
+ * 休眠
6
+ * @param {number} time
7
+ * @returns
8
+ */
9
+ const sleep = (time = 1000) => new Promise((resolve) => {
10
+ setTimeout(resolve, time);
11
+ });
12
+ exports.sleep = sleep;
13
+ function run(cb) {
14
+ try {
15
+ if (!cb)
16
+ return null;
17
+ return cb();
18
+ }
19
+ catch (err) {
20
+ return undefined;
21
+ }
22
+ }
23
+ exports.run = run;
24
+ const task = (promise) => promise.then((data) => [data, null]).catch((err) => [undefined, err]);
25
+ exports.task = task;
26
+ const debounce = (fn, interval = 300) => {
27
+ let timer;
28
+ const intervalTime = interval || 300;
29
+ return function func(...args) {
30
+ const context = this;
31
+ if (timer)
32
+ clearTimeout(timer);
33
+ timer = setTimeout(() => {
34
+ fn.call(context, ...args);
35
+ }, intervalTime);
36
+ };
37
+ };
38
+ exports.debounce = debounce;
39
+ const throttle = (fn, delay = 300) => {
40
+ let timer;
41
+ const delayTime = delay || 300;
42
+ let lastTime = Date.now();
43
+ return function func(...args) {
44
+ const context = this;
45
+ const curTime = Date.now();
46
+ const remaining = delayTime - (curTime - lastTime);
47
+ if (remaining < 0) {
48
+ timer && clearTimeout(timer);
49
+ timer = null;
50
+ fn.call(context, ...args);
51
+ lastTime = curTime;
52
+ }
53
+ else if (!timer) {
54
+ timer = setTimeout(() => {
55
+ fn.call(context, ...args);
56
+ lastTime = Date.now();
57
+ }, delayTime);
58
+ }
59
+ };
60
+ };
61
+ exports.throttle = throttle;