youtil 1.0.2 → 1.0.3

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/README.md CHANGED
@@ -1,3 +1,33 @@
1
1
  # youtil
2
2
 
3
- 油梯,一个与框架(Vue、React等)、环境(Nodejs、Web等)无关的小巧、精简、实用的JavaScript工具库
3
+ `youtil`,发音同`util`,中文可以叫做`油梯`,一个与框架(Vue、React等)、环境(Nodejs、Web等)无关的小巧、精简、实用、不依赖任何第三方库的`JavaScript`工具库。
4
+
5
+ > 代码还在不断扩充,敬请期待!
6
+
7
+ ## 如何使用
8
+
9
+ 安装:`npm i youtil -S`
10
+
11
+ 使用示例:
12
+
13
+ ```js
14
+ import { formatDate, parseDate } from youtil;
15
+ console.log(formatDate(Date.now()));
16
+ conssole.log(parseDate('2023-02-14', 'yyyy-MM-dd'));
17
+ ```
18
+
19
+ ## API
20
+
21
+ 参见[index.d.ts]('./dist/index.d.ts');
22
+
23
+ ## 关于命名
24
+
25
+ 之前想了很多名字,包括`autil`、`zutil`、`feutil`等,从a-z试下来只有几个没有被注册,但是不好记,最后才取了这个名字,因为比较好记。
26
+
27
+ ## 代码提交顺序
28
+
29
+ * 修改代码;
30
+ * 更新changelog和package.json
31
+ * npm publish
32
+ * git push
33
+
package/dist/index.js CHANGED
@@ -119,7 +119,7 @@ exports.default = {
119
119
  ${config.hasMask ? `<div class="mask-wrapper" style="background-color: ${config.maskColor}"></div>` : ''}
120
120
  <div class="loading-wrapper">
121
121
  <div class="loading-content">
122
- <img src="//img.alicdn.com/tfs/TB1bnUsQBLoK1RjSZFuXXXn0XXa-32-32.svg" alt="加载中">
122
+ <img src="https://img.alicdn.com/tfs/TB1bnUsQBLoK1RjSZFuXXXn0XXa-32-32.svg" alt="加载中">
123
123
  <div>${text}${config.cancelInline ? ' ' : '</div>'}
124
124
  ${config.onCancel ? '<a href="javascript:;" class="cancel">取消</a>' : ''}
125
125
  ${config.cancelInline ? '</div>' : ''}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "youtil",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "油梯,一个与框架、环境无关的小巧、精简、实用的JavaScript工具库",
5
5
  "main": "dist/index.js",
6
6
  "dependencies": {},
package/src/index.ts CHANGED
@@ -124,7 +124,7 @@ export default {
124
124
  ${config.hasMask ? `<div class="mask-wrapper" style="background-color: ${config.maskColor}"></div>` : ''}
125
125
  <div class="loading-wrapper">
126
126
  <div class="loading-content">
127
- <img src="//img.alicdn.com/tfs/TB1bnUsQBLoK1RjSZFuXXXn0XXa-32-32.svg" alt="加载中">
127
+ <img src="https://img.alicdn.com/tfs/TB1bnUsQBLoK1RjSZFuXXXn0XXa-32-32.svg" alt="加载中">
128
128
  <div>${text}${config.cancelInline ? ' ' : '</div>'}
129
129
  ${config.onCancel ? '<a href="javascript:;" class="cancel">取消</a>' : ''}
130
130
  ${config.cancelInline ? '</div>' : ''}
package/src/index.js DELETED
@@ -1,206 +0,0 @@
1
- "use strict";
2
- exports.__esModule = true;
3
- exports["default"] = {
4
- /**
5
- * 将日期格式化成指定格式的字符串
6
- * @param date 要格式化的日期,不传时默认当前时间,也可以是一个时间戳
7
- * @param fmt 目标字符串格式,支持的字符有:y,M,d,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
8
- * @returns 返回格式化后的日期字符串
9
- */
10
- formatDate: function (date, fmt) {
11
- if (!date) {
12
- return '';
13
- }
14
- date = typeof date === 'number' ? new Date(date) : date;
15
- fmt = fmt || 'yyyy-MM-dd HH:mm:ss';
16
- var obj = {
17
- y: date.getFullYear(),
18
- M: date.getMonth() + 1,
19
- d: date.getDate(),
20
- q: Math.floor((date.getMonth() + 3) / 3),
21
- w: date.getDay(),
22
- H: date.getHours(),
23
- h: date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
24
- m: date.getMinutes(),
25
- s: date.getSeconds(),
26
- S: date.getMilliseconds()
27
- };
28
- var week = ['天', '一', '二', '三', '四', '五', '六'];
29
- var _loop_1 = function (i) {
30
- fmt = fmt.replace(new RegExp("".concat(i, "+"), 'g'), function (m) {
31
- var val = "".concat(obj[i]);
32
- if (i === 'w') {
33
- return (m.length > 2 ? '星期' : '周') + week[val];
34
- }
35
- for (var j = 0, len = val.length; j < m.length - len; j++) {
36
- val = "0".concat(val);
37
- }
38
- return m.length === 1 ? val : val.substring(val.length - m.length);
39
- });
40
- };
41
- // eslint-disable-next-line guard-for-in
42
- for (var i in obj) {
43
- _loop_1(i);
44
- }
45
- return fmt;
46
- },
47
- /**
48
- * 将字符串解析成日期
49
- * @param str 输入的日期字符串,如'2014-09-13'
50
- * @param fmt 字符串格式,默认'yyyy-MM-dd',支持如下:y、M、d、H、m、s、S,不支持w和q
51
- * @returns 解析后的Date类型日期
52
- */
53
- parseDate: function (str, fmt) {
54
- fmt = fmt || 'yyyy-MM-dd';
55
- var obj = { y: 0, M: 1, d: 0, H: 0, h: 0, m: 0, s: 0, S: 0 };
56
- fmt.replace(/([^yMdHmsS]*?)(([yMdHmsS])\3*)([^yMdHmsS]*?)/g, function (m, $1, $2, $3, $4) {
57
- str = str.replace(new RegExp("".concat($1, "(\\d{").concat($2.length, "})").concat($4)), function (_m, _$1) {
58
- obj[$3] = parseInt(_$1);
59
- return '';
60
- });
61
- return '';
62
- });
63
- obj.M--; // 月份是从0开始的,所以要减去1
64
- var date = new Date(obj.y, obj.M, obj.d, obj.H, obj.m, obj.s);
65
- if (obj.S !== 0) {
66
- date.setMilliseconds(obj.S); // 如果设置了毫秒
67
- }
68
- return date;
69
- },
70
- /**
71
- * 显示全局loading
72
- * @param {*} text
73
- * @param {*} seconds
74
- * @param {*} options
75
- */
76
- showLoading: function (text, seconds, config) {
77
- var _this = this;
78
- if (text === void 0) { text = '请稍候'; }
79
- if (seconds === void 0) { seconds = 10; }
80
- if (config === void 0) { config = {}; }
81
- var defaultConfig = {
82
- hasMask: true,
83
- maskColor: 'transparent',
84
- onCancel: null,
85
- cancelInline: false,
86
- id: 'com_global_page_loading'
87
- };
88
- config = Object.assign({}, defaultConfig, config);
89
- var id = config.id;
90
- var timeoutKey = "_".concat(id, "_timeout");
91
- if (window[timeoutKey]) {
92
- clearTimeout(window[timeoutKey]);
93
- }
94
- var dom = document.getElementById(id);
95
- if (!dom) {
96
- dom = document.createElement('div');
97
- dom.id = id;
98
- dom.className = id;
99
- document.body.append(dom);
100
- }
101
- var styleId = "".concat(id, "_style");
102
- if (!document.getElementById(styleId)) {
103
- var style = document.createElement('style');
104
- style.id = styleId;
105
- style.innerHTML = "\n .".concat(id, " {\n position: fixed;\n top: calc(50vh - 60px);\n left: calc(50vw - 60px);\n width: 120px;\n height: 120px;\n z-index: 8000;\n background: rgba(0, 0, 0, 0.6);\n border-radius: 8px;\n text-align: center;\n color: white;\n padding-top: 20px;\n }\n .").concat(id, " img {\n width: 50px;\n margin-bottom: 10px;\n }");
106
- document.head.appendChild(style);
107
- }
108
- dom.innerHTML = "\n ".concat(config.hasMask ? "<div class=\"mask-wrapper\" style=\"background-color: ".concat(config.maskColor, "\"></div>") : '', "\n <div class=\"loading-wrapper\">\n <div class=\"loading-content\">\n <img src=\"//img.alicdn.com/tfs/TB1bnUsQBLoK1RjSZFuXXXn0XXa-32-32.svg\" alt=\"\u52A0\u8F7D\u4E2D\">\n <div>").concat(text).concat(config.cancelInline ? ' ' : '</div>', "\n ").concat(config.onCancel ? '<a href="javascript:;" class="cancel">取消</a>' : '', "\n ").concat(config.cancelInline ? '</div>' : '', "\n </div>\n </div>");
109
- if (config.onCancel) {
110
- var btn = dom.querySelector('.cancel');
111
- btn && btn.addEventListener('click', function () {
112
- _this.hideLoading();
113
- config.onCancel();
114
- });
115
- }
116
- dom.style.display = 'block';
117
- if (seconds > 0) {
118
- window[timeoutKey] = setTimeout(function () {
119
- _this.hideLoading();
120
- }, seconds * 1000);
121
- }
122
- },
123
- // 隐藏全局loading
124
- hideLoading: function () {
125
- var id = 'com_global_page_loading';
126
- var loading = document.getElementById(id);
127
- if (loading) {
128
- loading.style.display = 'none';
129
- }
130
- },
131
- /**
132
- * 从URL中获取某个参数,如果不存在返回 undefined ,如果存在多个同名参数,返回第一个匹配值
133
- * getParam('a', '?a=1&b=&c=3&c=33#abc') // '1'
134
- * getParam('b', '?a=1&b=&c=3&c=33#abc') // ''
135
- * getParam('c', '?a=1&b=&c=3&c=33#abc') // 3
136
- * getParam('d', '?a=1&b=&c=3&c=33#abc') // undefined
137
- * @param {*} name 参数名
138
- * @param {*} url 要获取的URL,默认当前地址
139
- */
140
- getParam: function (name, url) {
141
- if (url === void 0) { url = location.search; }
142
- return (new RegExp("(^|\\?|&)".concat(name, "=(.*?)(?=&|#|$)"), 'g').exec(url) || [])[2];
143
- },
144
- /**
145
- * 从URL中获取int参数
146
- * @param {*} name 参数名
147
- * @param {*} url 要获取的URL,默认当前地址
148
- */
149
- getParamInt: function (name, url) {
150
- if (url === void 0) { url = location.search; }
151
- return parseInt(this.getParam(name, url) || '0', 10);
152
- },
153
- /**
154
- * 获取某个URL的全部参数
155
- * getParams('?a=1&b=2#cc') // {a: '1', b: '2'}
156
- * @param url
157
- * @returns 参数对象
158
- */
159
- getParams: function (url) {
160
- if (url === void 0) { url = location.search; }
161
- var search = ((url || '').split('?').pop() || '').split('#')[0] || '';
162
- var params = {};
163
- search.split('&').map(function (item) { return item.split('='); }).forEach(function (_a) {
164
- var key = _a[0], value = _a[1];
165
- params[key] = value || '';
166
- });
167
- return params;
168
- },
169
- /**
170
- * 给URL设置参数,如果已经存在,替换之,兼容hash存在的情况
171
- * setParam('a', '123', '?a=1&b=2&a=3#d') // '?a=123&b=2&a=123#d'
172
- * setParam('d', '444', '?a=1&b=2&a=3#d') // '?a=1&b=2&a=3&d=444#d'
173
- * @param {Object} name 参数名
174
- * @param {Object} value 参数值
175
- * @param {Object} url 如果不传默认当前页面URL
176
- */
177
- setParam: function (name, value, url) {
178
- url = url || "".concat(location.pathname).concat(location.search);
179
- // 如果参数已经存在,替换之
180
- if (this.getParam(name, url) !== undefined) {
181
- return url.replace(new RegExp("(^|\\?|&)".concat(name, "=(.*?)(?=&|#|$)"), 'g'), "$1".concat(name, "=").concat(value));
182
- }
183
- var _a = url.split('#'), pathname = _a[0], hash = _a[1]; // 处理存在hash的情况
184
- return "".concat(pathname).concat(pathname.indexOf('?') < 0 ? '?' : '&').concat(name, "=").concat(value).concat(hash ? '#' : '').concat(hash || '');
185
- },
186
- /**
187
- * 删除URL中某个参数
188
- * delParam('a', '?a=1&b=2&a=3#d') // '?b=2#d'
189
- * delParam('b', '?a=1&b=2&a=3#d') // '?a=1&a=3#d'
190
- * delParam('a', '?a=1#d') // '#d'
191
- * @param name 参数名
192
- * @param url 要删除的URL,默认当前页面URL
193
- * @returns 处理完后的URL
194
- */
195
- delParam: function (name, url) {
196
- url = url || "".concat(location.pathname).concat(location.search);
197
- return url.replace(new RegExp("(^|\\?|&)".concat(name, "=.*?(&|#|$)"), 'g'), function (_m, $1, $2) { return $2 === '&' ? $1 : $2; });
198
- },
199
- /**
200
- * 休息一段时间,单位毫秒
201
- * 示例:await sleep(200); // 休息200毫秒
202
- * @param time 要休息的时间,单位毫秒,不传默认0
203
- * @returns
204
- */
205
- sleep: function (time) { return new Promise(function (resolve) { return setTimeout(resolve, time || 0); }); }
206
- };