tools-for-js 0.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/lib/index.js ADDED
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "countDown", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _time.countDown;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "dateFormat", {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _time.dateFormat;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "getTimeDistance", {
19
+ enumerable: true,
20
+ get: function get() {
21
+ return _time.getTimeDistance;
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "moneyFormat", {
25
+ enumerable: true,
26
+ get: function get() {
27
+ return _transform.moneyFormat;
28
+ }
29
+ });
30
+ Object.defineProperty(exports, "numToChinese", {
31
+ enumerable: true,
32
+ get: function get() {
33
+ return _transform.numToChinese;
34
+ }
35
+ });
36
+ Object.defineProperty(exports, "textTransform", {
37
+ enumerable: true,
38
+ get: function get() {
39
+ return _transform.textTransform;
40
+ }
41
+ });
42
+ Object.defineProperty(exports, "timeStamp", {
43
+ enumerable: true,
44
+ get: function get() {
45
+ return _time.timeStamp;
46
+ }
47
+ });
48
+ var _transform = require("./utils/transform");
49
+ var _time = require("./utils/time");
@@ -0,0 +1,190 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.timeStamp = exports.getTimeDistance = exports.dateFormat = exports.countDown = void 0;
7
+ /*******
8
+ * @description: 生成时间格式化
9
+ * @author: 琴时
10
+ * @param {params} 需要格式化的时间串(''/null/undefined ==> 默认当前时间)
11
+ * @param {format} 格式化样式标识(年-月-日:YYYY-MM-DD)(不传默认返回年-月-日 时:分:秒)
12
+ * @return {*} 返回格式化后的时间字符串
13
+ */
14
+ var dateFormat = exports.dateFormat = function dateFormat(params) {
15
+ var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'YYYY-MM-DD hh:mm:ss';
16
+ //如果params是时间字符串:2021-05-20 00:00:00 为了兼容ios需要将其转换成 2021/05/20 00:00:00
17
+ var pattern = /^(?=.*T)(?=.*).*$/;
18
+ var patternNum = /^\d*$/;
19
+ if (!params) {
20
+ params = new Date();
21
+ } else if (patternNum.test(params)) {
22
+ params = parseInt(params);
23
+ } else if (typeof params === 'string' && !pattern.test(params)) {
24
+ params = "".concat(params).replace(/-/g, '/');
25
+ }
26
+ /* 处理格式化时间 */
27
+ var dt = new Date(params); //创建时间对象
28
+ var yy = dt.getFullYear(); //年
29
+ var qt = Math.floor((dt.getMonth() + 3) / 3); //季度
30
+ var mm = (dt.getMonth() + 1 + '').padStart(2, '0'); //月(padStart:字符串不满2位数,开头补全'0')
31
+ var dd = (dt.getDate() + '').padStart(2, '0'); //日
32
+ var wk = '星期' + '日一二三四五六'.charAt(dt.getDay()); //星期
33
+ var hh = (dt.getHours() + '').padStart(2, '0'); //时
34
+ var mi = (dt.getMinutes() + '').padStart(2, '0'); //分
35
+ var ss = (dt.getSeconds() + '').padStart(2, '0'); //秒
36
+ var ms = dt.getMilliseconds(); //毫秒
37
+ var timeObj = {
38
+ date1: "".concat(yy, "-").concat(mm, "-").concat(dd, " ").concat(hh, ":").concat(mi, ":").concat(ss, " ").concat(qt).concat(wk),
39
+ //年-月-日 时:分:秒 季度星期
40
+ date2: "".concat(yy, "-").concat(mm, "-").concat(dd, " ").concat(hh, ":").concat(mi, ":").concat(ss, " ").concat(qt),
41
+ //年-月-日 时:分:秒 季度
42
+ date3: "".concat(yy, "-").concat(mm, "-").concat(dd, " ").concat(hh, ":").concat(mi, ":").concat(ss, " ").concat(wk),
43
+ //年-月-日 时:分:秒 星期
44
+ date4: "".concat(yy, "-").concat(mm, "-").concat(dd, " ").concat(hh, ":").concat(mi, ":").concat(ss),
45
+ //年-月-日 时:分:秒
46
+ date5: "".concat(yy, "-").concat(mm, "-").concat(dd),
47
+ //年-月-日
48
+ date6: "".concat(yy, "-").concat(mm),
49
+ //年-月
50
+ date7: "".concat(mm, "-").concat(dd),
51
+ //月-日
52
+ date8: "".concat(hh, ":").concat(mi, ":").concat(ss),
53
+ //时:分:秒
54
+ date9: "".concat(yy),
55
+ //年
56
+ date10: "".concat(mm),
57
+ //月
58
+ date11: "".concat(dd),
59
+ //日
60
+ date12: "".concat(hh),
61
+ //时
62
+ date13: "".concat(mm),
63
+ //分
64
+ date14: "".concat(ss),
65
+ //秒
66
+ date15: "".concat(qt),
67
+ //季度
68
+ date16: "".concat(wk),
69
+ //星期
70
+ date17: "".concat(yy).concat(mm).concat(dd).concat(hh).concat(mi).concat(ss) //时间串
71
+ };
72
+ /* 检测时间格式的标识 */
73
+ var list = [/^YYYY\-MM\-DD hh\:mm\:ss qtwk$/,
74
+ //年-月-日 时:分:秒 季度星期
75
+ /^YYYY\-MM\-DD hh\:mm\:ss qt$/,
76
+ //年-月-日 时:分:秒 季度
77
+ /^YYYY\-MM\-DD hh\:mm\:ss wk$/,
78
+ //年-月-日 时:分:秒 星期
79
+ /^YYYY\-MM\-DD hh\:mm\:ss$/,
80
+ //年-月-日 时:分:秒
81
+ /^YYYY\-MM\-DD$/,
82
+ //年-月-日
83
+ /^YYYY\-MM$/,
84
+ //年-月
85
+ /^MM\-DD$/,
86
+ //月-日
87
+ /^hh\:mm\:ss$/,
88
+ //时:分:秒
89
+ /^YYYY$/,
90
+ //年
91
+ /^MM$/,
92
+ //月
93
+ /^DD$/,
94
+ //日
95
+ /^hh$/,
96
+ //时
97
+ /^mm$/,
98
+ //分
99
+ /^ss$/,
100
+ //秒
101
+ /^qt$/,
102
+ //季度
103
+ /^ss$/,
104
+ //星期
105
+ /^YYYYMMDDhhmmss$/ //时间串
106
+ ];
107
+ var newDate = '';
108
+ var news = list.some(function (item, index) {
109
+ if (item.test(format)) {
110
+ newDate = timeObj["date".concat(index + 1)];
111
+ return true;
112
+ }
113
+ return false;
114
+ });
115
+ if (!news) newDate = "".concat(yy, "-").concat(mm, "-").concat(dd, " ").concat(hh, ":").concat(mi, ":").concat(ss); //如果传过来的格式化标识符异常则默认返回:年-月-日 时:分:秒
116
+ return newDate;
117
+ };
118
+
119
+ /*******
120
+ * @description: 获取时间间隔
121
+ * @author: 琴时
122
+ * @param {start_Time} [开始时间]
123
+ * @param {end_Time} [结束时间]
124
+ * @return {*} 处理好的时间间隔相关对象
125
+ */
126
+ var getTimeDistance = exports.getTimeDistance = function getTimeDistance(start_Time, end_Time) {
127
+ var startTime = +new Date(start_Time); // 开始时间戳
128
+ var endTime = +new Date(end_Time); // 结束时间戳
129
+ var times = (endTime - startTime) / 1000; // 剩余时间总的秒数
130
+ var dd = parseInt(times / 60 / 60 / 24).toString().padStart(2, '0'); //天
131
+ var hh = parseInt(times / 60 / 60 % 24 + '').toString().padStart(2, '0'); //时
132
+ var mm = parseInt(times / 60 % 60 + '').toString().padStart(2, '0'); //分
133
+ var ss = parseInt(times % 60 + '').toString().padStart(2, '0'); //秒
134
+ if (ss < 0 || mm < 0 || hh < 0 || dd < 0) return null;
135
+ return {
136
+ timeStr: "".concat(dd, "\u5929").concat(hh, "\u65F6").concat(mm, "\u5206").concat(ss, "\u79D2"),
137
+ timeStamp: times,
138
+ //剩余时间秒数
139
+ timeDistance: {
140
+ dd: dd,
141
+ hh: hh,
142
+ mm: mm,
143
+ ss: ss
144
+ },
145
+ week: dd > 0 && dd < 7,
146
+ //7天内
147
+ month: dd > 0 && dd < 30,
148
+ //30天内
149
+ year: dd > 0 && dd < 365 //一年内
150
+ };
151
+ };
152
+
153
+ /*******
154
+ * @description: 转换时间戳
155
+ * @author: 琴时
156
+ * @param {*} params [时间字符串]/[时间对象]
157
+ * @return {*} [时间戳,当前时间戳]
158
+ */
159
+ /* */
160
+ var timeStamp = exports.timeStamp = function timeStamp(params) {
161
+ var date = +new Date(params); //转换成时间戳
162
+ var newDate = Date.now(); //H5-生成当前时间戳
163
+ return {
164
+ date: date,
165
+ newDate: newDate
166
+ };
167
+ };
168
+
169
+ /*******
170
+ * @description: 倒计时
171
+ * @author: 琴时
172
+ * @param {params} [截止时间]
173
+ * @return {*} [倒计时间/结束将返回null]
174
+ */
175
+ var countDown = exports.countDown = function countDown(params) {
176
+ var sign = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'cn';
177
+ var nowTime = +new Date(); // 当前时间戳
178
+ var endTime = +new Date(params); // 结束时间戳
179
+ var times = (endTime - nowTime) / 1000; // 剩余时间总的秒数
180
+ var dd = parseInt(times / 60 / 60 / 24).toString().padStart(2, '0'); // 天
181
+ var hh = parseInt(times / 60 / 60 % 24 + '').toString().padStart(2, '0'); //时
182
+ var mm = parseInt(times / 60 % 60 + '').toString().padStart(2, '0'); // 分
183
+ var ss = parseInt(times % 60 + '').toString().padStart(2, '0'); // 当前的秒
184
+ if (times <= 0) return null; //结束返回null
185
+ var result = {
186
+ cn: "".concat(dd, "\u5929").concat(hh, "\u65F6").concat(mm, "\u5206").concat(ss, "\u79D2"),
187
+ ss: parseInt(times)
188
+ };
189
+ return result[sign];
190
+ };
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.textTransform = exports.numToChinese = exports.moneyFormat = void 0;
7
+ var chnNumChar = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
8
+ var chnUnitSection = ['', '万', '亿', '万亿', '亿亿'];
9
+ var chnUnitChar = ['', '十', '百', '千'];
10
+ // 如果数字含有小数部分,那么可以将小数部分单独取出,将小数部分的数字转换为字符串的方法
11
+ var numToChn = function numToChn(params) {
12
+ var index = params.toString().indexOf('.');
13
+ if (index != -1) {
14
+ var str = params.toString().slice(index);
15
+ var a = '点';
16
+ for (var i = 1; i < str.length; i++) {
17
+ a += chnNumChar[parseInt(str[i])];
18
+ }
19
+ return a;
20
+ } else {
21
+ return '';
22
+ }
23
+ };
24
+
25
+ // 定义在每个小节的内部进行转化的方法,其他部分则与小节内部转化方法相同
26
+ var sectionToChinese = function sectionToChinese(section) {
27
+ var str = '',
28
+ chnstr = '',
29
+ zero = false,
30
+ count = 0; //zero为是否进行补零, 第一次进行取余由于为个位数,默认不补零
31
+ while (section > 0) {
32
+ var v = section % 10; //对数字取余10,得到的数即为个位数
33
+ if (v == 0) {
34
+ //如果数字为零,则对字符串进行补零
35
+ if (zero) {
36
+ zero = false; //如果遇到连续多次取余都是0,那么只需补一个零即可
37
+ chnstr = chnNumChar[v] + chnstr;
38
+ }
39
+ } else {
40
+ zero = true; //第一次取余之后,如果再次取余为零,则需要补零
41
+ str = chnNumChar[v];
42
+ str += chnUnitChar[count];
43
+ chnstr = str + chnstr;
44
+ }
45
+ count++;
46
+ section = Math.floor(section / 10);
47
+ }
48
+ return chnstr;
49
+ };
50
+ /* 定义整个数字全部转换的方法,需要依次对数字进行10000为单位的取余,然后分成小节,按小节计算,当每个小节的数不足1000时,则需要进行补零 */
51
+
52
+ /*******
53
+ * @description: 阿拉伯数字转中文数字
54
+ * @author: 琴时
55
+ * @param {Number} params
56
+ * @return {*}
57
+ * @example transformToChinese(123456)
58
+ * 输出:一十二万三千四百五十六
59
+ */
60
+ var numToChinese = exports.numToChinese = function numToChinese(params) {
61
+ var a = numToChn(params);
62
+ params = Math.floor(params);
63
+ var unitPos = 0;
64
+ var strIns = '',
65
+ chnStr = '';
66
+ var needZero = false;
67
+ if (params === 0) {
68
+ return chnNumChar[0];
69
+ }
70
+ while (params > 0) {
71
+ var section = params % 10000;
72
+ if (needZero) {
73
+ chnStr = chnNumChar[0] + chnStr;
74
+ }
75
+ strIns = sectionToChinese(section);
76
+ strIns += section !== 0 ? chnUnitSection[unitPos] : chnUnitSection[0];
77
+ chnStr = strIns + chnStr;
78
+ needZero = section < 1000 && section > 0;
79
+ params = Math.floor(params / 10000);
80
+ unitPos++;
81
+ }
82
+ return chnStr + a;
83
+ };
84
+
85
+ /*******
86
+ * @description: 金额格式化函数
87
+ * @author: 琴时
88
+ * @param {*} value [金额]
89
+ * @param {*} currency [货币符号]
90
+ * @param {*} decimals [小数位数]
91
+ * @return {String} [返回格式好的金额]
92
+ * @example: moneyFormat(123456.789, '¥', 2) => ¥123,456.79
93
+ */
94
+ var moneyFormat = exports.moneyFormat = function moneyFormat(value, currency, decimals) {
95
+ var digitsRE = /(\d{3})(?=\d)/g; //匹配数字之前的3个数字
96
+ value = parseFloat(value); //转换成数值保留小数
97
+ // 如果传过来的值不是有限数值或者值不存在且值不等于0
98
+ if (!isFinite(value) || !value && value !== 0) return ''; //isFinite判断是否是有限数值
99
+ currency = currency ? currency : '$'; //如果有指定金币符号就直接用,否则使用美元符号
100
+ decimals = decimals ? decimals : 2; //如果有指定小数位就直接用,否则使用2位小数
101
+ var stringified = Math.abs(value).toFixed(decimals); //取数值的绝对值后decimals位小数
102
+ var _int = stringified.slice(0, -1 - decimals); //取整数部分
103
+ var remainder = _int.length % 3; //因为金额都是每3个划分,划分后取剩余的(整数部分取余)
104
+ var head = remainder > 0 ? _int.slice(0, remainder) + (_int.length > 3 ? ',' : '') : '';
105
+ // 计算划分后头部(整数部分超过三位头部加上逗号)
106
+ var _float = stringified.slice(-1 - decimals); //取小数部分加上小数点
107
+ var sign = value < 0 ? '-' : ''; //判断正负数
108
+ return sign + currency + head + _int.slice(remainder).replace(digitsRE, '$1,') +
109
+ //digitsRE, '$1,'==>每3位加1逗号
110
+ _float;
111
+ };
112
+
113
+ /*******
114
+ * @description: Txt 转数组对象
115
+ * 将txt文件内容转化成定制数组对象
116
+ * 将ZFB账号---运动账号---运动密码==>[{'antName','username','password','remark'}]
117
+ * 将运动账号---运动密码==>[{'username','password','remark'}]
118
+ * @author: 琴时
119
+ * @param {Object} params
120
+ * @return {*}
121
+ */
122
+ var textTransform = exports.textTransform = function textTransform(params) {
123
+ var _params$content = params.content,
124
+ content = _params$content === void 0 ? '' : _params$content,
125
+ symbol = params.symbol,
126
+ _params$sign = params.sign,
127
+ sign = _params$sign === void 0 ? 'ants' : _params$sign,
128
+ password = params.password,
129
+ _params$remark = params.remark,
130
+ remark = _params$remark === void 0 ? '' : _params$remark,
131
+ _params$index = params.index,
132
+ index = _params$index === void 0 ? 0 : _params$index;
133
+ // 将每行字符进行切割成数组
134
+ var list = content.split(/[(\r\n)\r\n]+/); // 根据换行或者回车进行识别
135
+ var arrayTemp = [];
136
+ list.forEach(function (item) {
137
+ item = item.trim(); // 去除首尾空格
138
+ if (item) {
139
+ // 将每行数据通过[symbol]定制符合进行切割
140
+ var newList = item.split(symbol).map(function (ele) {
141
+ return ele.trim();
142
+ });
143
+ var tempData = {};
144
+ if (sign === 'ants') {
145
+ tempData = {
146
+ antName: newList[index],
147
+ username: index === 1 || newList.length === 1 ? newList[0] : newList[1],
148
+ password: password || newList[2],
149
+ remark: newList[3] || remark
150
+ };
151
+ } else {
152
+ tempData = {
153
+ username: newList[0],
154
+ password: password || newList[1],
155
+ remark: newList[2] || remark
156
+ };
157
+ }
158
+ arrayTemp.push(tempData);
159
+ }
160
+ });
161
+ return arrayTemp;
162
+ };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "tools-for-js",
3
+ "version": "0.0.1",
4
+ "description": "基于babel构建的javascript工具库",
5
+ "main": "lib/index.js",
6
+ "scripts": {
7
+ "build": "sh build/build.sh",
8
+ "release": "sh build/release.sh"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/kimshift/tools-for-js.git"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/kimshift/tools-for-js/issues"
16
+ },
17
+ "keywords": [
18
+ "js",
19
+ "tools"
20
+ ],
21
+ "author": "kimshift",
22
+ "license": "ISC",
23
+ "files": [
24
+ "README.md",
25
+ "lib/"
26
+ ],
27
+ "devDependencies": {
28
+ "@babel/cli": "^7.24.5",
29
+ "@babel/core": "^7.24.5",
30
+ "@babel/preset-env": "^7.24.5"
31
+ }
32
+ }