tools-for-js 1.2.2 → 1.2.5

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
@@ -49,4 +49,6 @@ import { API } from 'tools-for-js'
49
49
  | 按指定长度分段字符串 | splitString |
50
50
  | 将对象转换为查询字符串 | objectToQs |
51
51
  | 计算数据分页 | getPagination |
52
- | 根据[key]值排序,相同时则根据时间[timeKey]值排序 | sortByKeyAndTime |
52
+ | 根据 key 值排序,相同时则根据时间 timeKey 值排序 | sortByKeyAndTime |
53
+ | 根据 key 获取对象值 | getValByKey |
54
+ | 数组随机打乱 | shuffleArray |
package/lib/index.js CHANGED
@@ -87,6 +87,12 @@ Object.defineProperty(exports, "exactAdd", {
87
87
  return _calculate.exactAdd;
88
88
  }
89
89
  });
90
+ Object.defineProperty(exports, "formatDate", {
91
+ enumerable: true,
92
+ get: function get() {
93
+ return _time.formatDate;
94
+ }
95
+ });
90
96
  Object.defineProperty(exports, "getPagination", {
91
97
  enumerable: true,
92
98
  get: function get() {
@@ -147,6 +153,12 @@ Object.defineProperty(exports, "objectToQs", {
147
153
  return _object.objectToQs;
148
154
  }
149
155
  });
156
+ Object.defineProperty(exports, "shuffleArray", {
157
+ enumerable: true,
158
+ get: function get() {
159
+ return _array.shuffleArray;
160
+ }
161
+ });
150
162
  Object.defineProperty(exports, "sortByKeyAndTime", {
151
163
  enumerable: true,
152
164
  get: function get() {
@@ -3,7 +3,9 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.treeLastChildSum = exports.sortByKeyAndTime = exports.randomNumEnum = exports.getValueFromArray = exports.getPagination = exports.deWeightArray = exports.checkKeyEmpty = exports.arrayToTree = exports.arrExchange = exports.arrEleMove = void 0;
6
+ exports.randomNumEnum = exports.getValueFromArray = exports.getPagination = exports.deWeightArray = exports.checkKeyEmpty = exports.arrayToTree = exports.arrExchange = exports.arrEleMove = void 0;
7
+ exports.shuffleArray = shuffleArray;
8
+ exports.treeLastChildSum = exports.sortByKeyAndTime = void 0;
7
9
  var _calculate = require("./calculate");
8
10
  var _common = require("./common");
9
11
  var _object = require("./object");
@@ -177,7 +179,7 @@ var arrayToTree = exports.arrayToTree = function arrayToTree(array, config) {
177
179
  var getValueFromArray = exports.getValueFromArray = function getValueFromArray(list, labelVal) {
178
180
  var label = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'label';
179
181
  var value = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'value';
180
- list = JSON.parse(JSON.stringify(list));
182
+ list = (0, _common.deepCopy)(list);
181
183
  var res = list.find(function (item) {
182
184
  return item[label] == labelVal;
183
185
  }) || {};
@@ -217,7 +219,8 @@ var randomNumEnum = exports.randomNumEnum = function randomNumEnum(array, num) {
217
219
  var getPagination = exports.getPagination = function getPagination(list, params) {
218
220
  var page = params.page,
219
221
  pageSize = params.pageSize;
220
- return list.value.slice((page - 1) * pageSize, page * pageSize);
222
+ list = (0, _common.deepCopy)(list);
223
+ return list.slice((page - 1) * pageSize, page * pageSize);
221
224
  };
222
225
 
223
226
  /*******
@@ -240,15 +243,32 @@ var sortByKeyAndTime = exports.sortByKeyAndTime = function sortByKeyAndTime(list
240
243
  order = _ref3$order === void 0 ? 'asc' : _ref3$order,
241
244
  _ref3$timeOrder = _ref3.timeOrder,
242
245
  timeOrder = _ref3$timeOrder === void 0 ? 'desc' : _ref3$timeOrder;
243
- var listCopy = (0, _common.deepCopy)(list);
244
- return listCopy.sort(function (a, b) {
246
+ list = (0, _common.deepCopy)(list);
247
+ return list.sort(function (a, b) {
245
248
  var a_val = (0, _object.getValByKey)(a, key);
246
- var a_time = new Date((0, _object.getValByKey)(a, timeKey));
249
+ var a_time = +new Date((0, _object.getValByKey)(a, timeKey));
247
250
  var b_val = (0, _object.getValByKey)(b, key);
248
- var b_time = new Date((0, _object.getValByKey)(b, timeKey));
251
+ var b_time = +new Date((0, _object.getValByKey)(b, timeKey));
249
252
  if (a_val === b_val) {
250
253
  return timeOrder === 'desc' ? b_time - a_time : a_time - b_time;
251
254
  }
252
255
  return order === 'asc' ? a_val - b_val : b_val - a_val;
253
256
  });
254
- };
257
+ };
258
+
259
+ /*******
260
+ * @description: 数组随机打乱
261
+ * @author: 琴时
262
+ * @param {Array} arr
263
+ * @return {Array}
264
+ */
265
+ function shuffleArray(arr) {
266
+ var array = _toConsumableArray(arr);
267
+ for (var i = array.length - 1; i > 0; i--) {
268
+ var j = Math.floor(Math.random() * (i + 1));
269
+ var _ref4 = [array[j], array[i]];
270
+ array[i] = _ref4[0];
271
+ array[j] = _ref4[1];
272
+ }
273
+ return array;
274
+ }
package/lib/utils/time.js CHANGED
@@ -3,117 +3,70 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.transformDate = exports.timeStamp = exports.getTimeDistance = exports.dateFormat = exports.countDown = void 0;
6
+ exports.transformDate = exports.timeStamp = exports.getTimeDistance = exports.formatDate = exports.dateFormat = exports.countDown = void 0;
7
+ require("dayjs/locale/zh-cn");
8
+ var _relativeTime = _interopRequireDefault(require("dayjs/plugin/relativeTime"));
9
+ var _dayjs = _interopRequireDefault(require("dayjs"));
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
+ // ES 2015
12
+
13
+ _dayjs["default"].locale('zh-cn'); // 全局使用
14
+ _dayjs["default"].extend(_relativeTime["default"]);
15
+
7
16
  /*******
8
17
  * @description: 生成时间格式化
9
18
  * @author: 琴时
10
- * @param {params} 需要格式化的时间串(''/null/undefined ==> 默认当前时间)
11
- * @param {format} 格式化样式标识(年-月-日:YYYY-MM-DD)(不传默认返回年-月-日 时:分:秒)
19
+ * @param {date} 需要格式化的时间串(''/null/undefined ==> 默认当前时间)
20
+ * @param {fmt} 格式化样式标识(年-月-日:YYYY-MM-DD)(不传默认返回年-月-日 时:分:秒)
12
21
  * @return {*} 返回格式化后的时间字符串
13
22
  */
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
23
+ var dateFormat = exports.dateFormat = function dateFormat(date) {
24
+ var fmt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'YYYY-MM-DD HH:mm:ss';
17
25
  var pattern = /^(?=.*T)(?=.*).*$/;
18
26
  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, '/');
27
+ if (!date) {
28
+ // 不传参数
29
+ date = new Date();
30
+ } else if (patternNum.test(date)) {
31
+ // 时间戳
32
+ date = parseInt(date);
33
+ } else if (typeof date === 'string' && !pattern.test(date)) {
34
+ //兼容ios: 将时间字符串中的'-'替换成'/' 2021-05-20 00:00:00=>2021/05/20 00:00:00
35
+ date = "".concat(date).replace(/-/g, '/');
25
36
  }
26
37
  /* 处理格式化时间 */
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),
38
+ var dt = new Date(date); //创建时间对象
39
+ // 构造正则匹配:(value)padStart:字符串不满2位数,开头补全'0'
40
+ var o = {
41
+ '[Yy]+': dt.getFullYear(),
55
42
  //年
56
- date10: "".concat(mm),
43
+ 'M+': (dt.getMonth() + 1 + '').padStart(2, '0'),
57
44
  //月
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) //时间串
45
+ 'D+': (dt.getDate() + '').padStart(2, '0'),
46
+ // 日
47
+ 'H+': (dt.getHours() + '').padStart(2, '0'),
48
+ // 时
49
+ 'm+': (dt.getMinutes() + '').padStart(2, '0'),
50
+ // 分
51
+ 's+': (dt.getSeconds() + '').padStart(2, '0'),
52
+ // 秒
53
+ 'w+': '星期' + '日一二三四五六'.charAt(dt.getDay()),
54
+ // 星期
55
+ 'q+': Math.floor((dt.getMonth() + 3) / 3),
56
+ // 季度
57
+ S: dt.getMilliseconds() // 毫秒
71
58
  };
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;
59
+ // 将正则匹配上的字符替换对应的时间值
60
+ for (var k in o) {
61
+ if (new RegExp("(".concat(k, ")")).test(fmt)) {
62
+ fmt = fmt.replace(RegExp.$1, o[k]);
112
63
  }
113
- return false;
114
- });
115
- if (!news) newDate = "".concat(yy, "-").concat(mm, "-").concat(dd, " ").concat(hh, ":").concat(mi, ":").concat(ss); //如果传过来的格式化标识符异常则默认返回:年-月-日 时:分:秒
116
- return newDate;
64
+ }
65
+ return fmt;
66
+ };
67
+ var formatDate = exports.formatDate = function formatDate(date) {
68
+ var fmt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'YYYY-MM-DD HH:mm:ss';
69
+ return (0, _dayjs["default"])(date).format(fmt);
117
70
  };
118
71
 
119
72
  /*******
@@ -196,21 +149,7 @@ var countDown = exports.countDown = function countDown(params) {
196
149
  * @return {String}
197
150
  */
198
151
  var transformDate = exports.transformDate = function transformDate(date) {
199
- var createAt = new Date(date);
200
- var interval = new Date().getTime() - createAt.getTime(); //现在的时间-传入的时间 = 相差的时间(单位 = 毫秒)
201
- if (interval < 0) {
202
- return '';
203
- } else if (interval / 1000 < 60) {
204
- return '刚刚';
205
- } else if (interval / 60000 < 60) {
206
- return parseInt(interval / 60000) + '分钟前';
207
- } else if (interval / 3600000 < 24) {
208
- return parseInt(interval / 3600000) + '小时前';
209
- } else if (interval / 86400000 < 31) {
210
- return parseInt(interval / 86400000) + '天前';
211
- } else if (interval / 2592000000 < 12) {
212
- return parseInt(interval / 2592000000) + '月前';
213
- } else {
214
- return parseInt(interval / 31536000000) + '年前';
215
- }
152
+ var diff = (0, _dayjs["default"])(date).fromNow().replace(/\s+/g, '');
153
+ if (diff === '几秒前') diff = '刚刚';
154
+ return diff;
216
155
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tools-for-js",
3
- "version": "1.2.2",
3
+ "version": "1.2.5",
4
4
  "description": "基于babel构建的javascript工具库",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -31,5 +31,8 @@
31
31
  "@babel/node": "^7.23.9",
32
32
  "@babel/preset-env": "^7.24.5",
33
33
  "nodemon": "^3.1.0"
34
+ },
35
+ "dependencies": {
36
+ "dayjs": "^1.11.11"
34
37
  }
35
38
  }