tools-min-ns 1.13.0 → 1.14.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/README.md +5 -0
- package/esm/array/ArrayUtil.d.ts +38 -0
- package/esm/array/ArrayUtil.js +62 -0
- package/esm/date/DateUtil.d.ts +2 -2
- package/esm/date/DateUtil.js +26 -23
- package/lib/array/ArrayUtil.d.ts +38 -0
- package/lib/array/ArrayUtil.js +62 -0
- package/lib/date/DateUtil.d.ts +2 -2
- package/lib/date/DateUtil.js +26 -23
- package/package.json +1 -1
package/README.md
CHANGED
package/esm/array/ArrayUtil.d.ts
CHANGED
|
@@ -135,5 +135,43 @@ console.log(universalSort(objArray, obj => obj.name.length)); // [{ name: 'Jim',
|
|
|
135
135
|
console.log(universalSort([5, 3, 8, 1, 2], null, 'desc')); // [8, 5, 3, 2, 1]
|
|
136
136
|
*/
|
|
137
137
|
function universalSort<T>(array: T[], key?: keyof T | ((item: T) => any), order?: 'asc' | 'desc'): T[];
|
|
138
|
+
/**
|
|
139
|
+
* 根据对象的属性值进行分组
|
|
140
|
+
* @param array 对象数组
|
|
141
|
+
* @param key 对象的key
|
|
142
|
+
* @example
|
|
143
|
+
const data = [
|
|
144
|
+
{ name: 'Alice', age: 21 },
|
|
145
|
+
{ name: 'Bob', age: 25 },
|
|
146
|
+
{ name: 'Charlie', age: 21 },
|
|
147
|
+
{ name: 'David', age: 25 }
|
|
148
|
+
];
|
|
149
|
+
|
|
150
|
+
const groupedByAge = groupBy(data, 'age');
|
|
151
|
+
console.log(groupedByAge); // {21: Array(2), 25: Array(2)}
|
|
152
|
+
*/
|
|
153
|
+
function groupBy<T>(array: T[], key: keyof T): Record<keyof T, T[]>;
|
|
154
|
+
/**
|
|
155
|
+
* 按条件分组
|
|
156
|
+
* @param array 对象数组
|
|
157
|
+
* @param condition filter
|
|
158
|
+
* @example
|
|
159
|
+
const numbers: number[] = [1, 2, 3, 4, 5, 6];
|
|
160
|
+
|
|
161
|
+
const groupedByOddEven = groupByCondition(numbers, num => num % 2 === 0);
|
|
162
|
+
console.log(groupedByOddEven); // {false: Array(3), true: Array(3)}
|
|
163
|
+
*/
|
|
164
|
+
function groupByCondition<T>(array: T[], condition: (value: T) => boolean): Record<'true' | 'false', T[]>;
|
|
165
|
+
/**
|
|
166
|
+
* 按固定大小分组
|
|
167
|
+
* @param array 对象数组
|
|
168
|
+
* @param size 数量
|
|
169
|
+
* @example
|
|
170
|
+
const data: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
171
|
+
|
|
172
|
+
const chunkedArray = chunkArray(data, 3);
|
|
173
|
+
console.log(chunkedArray);// [Array(3), Array(3), Array(3)]
|
|
174
|
+
*/
|
|
175
|
+
function chunkArray<T>(array: T[], size: number): T[][];
|
|
138
176
|
}
|
|
139
177
|
export default ArrayUtil;
|
package/esm/array/ArrayUtil.js
CHANGED
|
@@ -309,5 +309,67 @@ var ArrayUtil;
|
|
|
309
309
|
return array.sort(compareFunction);
|
|
310
310
|
}
|
|
311
311
|
ArrayUtil.universalSort = universalSort;
|
|
312
|
+
/**
|
|
313
|
+
* 根据对象的属性值进行分组
|
|
314
|
+
* @param array 对象数组
|
|
315
|
+
* @param key 对象的key
|
|
316
|
+
* @example
|
|
317
|
+
const data = [
|
|
318
|
+
{ name: 'Alice', age: 21 },
|
|
319
|
+
{ name: 'Bob', age: 25 },
|
|
320
|
+
{ name: 'Charlie', age: 21 },
|
|
321
|
+
{ name: 'David', age: 25 }
|
|
322
|
+
];
|
|
323
|
+
const groupedByAge = groupBy(data, 'age');
|
|
324
|
+
console.log(groupedByAge); // {21: Array(2), 25: Array(2)}
|
|
325
|
+
*/
|
|
326
|
+
function groupBy(array, key) {
|
|
327
|
+
return array.reduce(function (result, currentValue) {
|
|
328
|
+
var groupKey = currentValue[key];
|
|
329
|
+
if (!result[groupKey]) {
|
|
330
|
+
result[groupKey] = [];
|
|
331
|
+
}
|
|
332
|
+
result[groupKey].push(currentValue);
|
|
333
|
+
return result;
|
|
334
|
+
}, {});
|
|
335
|
+
}
|
|
336
|
+
ArrayUtil.groupBy = groupBy;
|
|
337
|
+
/**
|
|
338
|
+
* 按条件分组
|
|
339
|
+
* @param array 对象数组
|
|
340
|
+
* @param condition filter
|
|
341
|
+
* @example
|
|
342
|
+
const numbers: number[] = [1, 2, 3, 4, 5, 6];
|
|
343
|
+
const groupedByOddEven = groupByCondition(numbers, num => num % 2 === 0);
|
|
344
|
+
console.log(groupedByOddEven); // {false: Array(3), true: Array(3)}
|
|
345
|
+
*/
|
|
346
|
+
function groupByCondition(array, condition) {
|
|
347
|
+
return array.reduce(function (result, currentValue) {
|
|
348
|
+
var groupKey = condition(currentValue) ? 'true' : 'false';
|
|
349
|
+
if (!result[groupKey]) {
|
|
350
|
+
result[groupKey] = [];
|
|
351
|
+
}
|
|
352
|
+
result[groupKey].push(currentValue);
|
|
353
|
+
return result;
|
|
354
|
+
}, {});
|
|
355
|
+
}
|
|
356
|
+
ArrayUtil.groupByCondition = groupByCondition;
|
|
357
|
+
/**
|
|
358
|
+
* 按固定大小分组
|
|
359
|
+
* @param array 对象数组
|
|
360
|
+
* @param size 数量
|
|
361
|
+
* @example
|
|
362
|
+
const data: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
363
|
+
const chunkedArray = chunkArray(data, 3);
|
|
364
|
+
console.log(chunkedArray);// [Array(3), Array(3), Array(3)]
|
|
365
|
+
*/
|
|
366
|
+
function chunkArray(array, size) {
|
|
367
|
+
var result = [];
|
|
368
|
+
for (var i = 0; i < array.length; i += size) {
|
|
369
|
+
result.push(array.slice(i, i + size));
|
|
370
|
+
}
|
|
371
|
+
return result;
|
|
372
|
+
}
|
|
373
|
+
ArrayUtil.chunkArray = chunkArray;
|
|
312
374
|
})(ArrayUtil || (ArrayUtil = {}));
|
|
313
375
|
export default ArrayUtil;
|
package/esm/date/DateUtil.d.ts
CHANGED
|
@@ -38,13 +38,13 @@ declare namespace DateUtil {
|
|
|
38
38
|
/**
|
|
39
39
|
* 将日期格式化成指定格式的字符串
|
|
40
40
|
* @param {Date | Number | String} date 要格式化的日期,不传时默认当前时间,也可以是一个时间戳/字符串
|
|
41
|
-
* @param {String} format 目标字符串格式,支持的字符有:y,M,d,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
|
|
41
|
+
* @param {String} format 目标字符串格式,支持的字符有:y,Y,M,d,D,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
|
|
42
42
|
* @returns {String} 返回格式化后的日期字符串,日期不合法时返回字符串 'invalid date'
|
|
43
43
|
* @example
|
|
44
44
|
* formatDate(); => 2016-09-02 13:17:13
|
|
45
45
|
* formatDate('2016-09-02','yyyy年MM月dd日'); => 2016年09月02日
|
|
46
46
|
* formatDate(new Date(), 'yyyy-MM-dd'); => 2016-09-02
|
|
47
|
-
* formatDate(new Date(), 'yyyy-MM-dd 第q季度
|
|
47
|
+
* formatDate(new Date(), 'yyyy-MM-dd 第q季度 星期w HH:mm:ss:SSS'); => 2016-09-02 第3季度 星期五 13:19:15:792
|
|
48
48
|
* formatDate(1472793615764); => 2016-09-02 13:20:15
|
|
49
49
|
*/
|
|
50
50
|
const formatDate: (date?: Date | number | string, format?: string) => string;
|
package/esm/date/DateUtil.js
CHANGED
|
@@ -65,7 +65,7 @@ var DateUtil;
|
|
|
65
65
|
dateFormatRules.forEach(function (item) {
|
|
66
66
|
for (var index = 0, rules = item.rules, len = rules.length; index < len; index++) {
|
|
67
67
|
rule = rules[index];
|
|
68
|
-
sIndex = format.indexOf("".concat(rule[0]));
|
|
68
|
+
sIndex = format.replaceAll('D', 'd').replaceAll('Y', 'y').indexOf("".concat(rule[0]));
|
|
69
69
|
if (sIndex !== -1) {
|
|
70
70
|
dates.push(parseFloat(dateStr.substring(sIndex, sIndex + Number(rule[1]))) + (item.offset || 0));
|
|
71
71
|
// 如果匹配到规则中的第一条,则退出
|
|
@@ -81,13 +81,13 @@ var DateUtil;
|
|
|
81
81
|
/**
|
|
82
82
|
* 将日期格式化成指定格式的字符串
|
|
83
83
|
* @param {Date | Number | String} date 要格式化的日期,不传时默认当前时间,也可以是一个时间戳/字符串
|
|
84
|
-
* @param {String} format 目标字符串格式,支持的字符有:y,M,d,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
|
|
84
|
+
* @param {String} format 目标字符串格式,支持的字符有:y,Y,M,d,D,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
|
|
85
85
|
* @returns {String} 返回格式化后的日期字符串,日期不合法时返回字符串 'invalid date'
|
|
86
86
|
* @example
|
|
87
87
|
* formatDate(); => 2016-09-02 13:17:13
|
|
88
88
|
* formatDate('2016-09-02','yyyy年MM月dd日'); => 2016年09月02日
|
|
89
89
|
* formatDate(new Date(), 'yyyy-MM-dd'); => 2016-09-02
|
|
90
|
-
* formatDate(new Date(), 'yyyy-MM-dd 第q季度
|
|
90
|
+
* formatDate(new Date(), 'yyyy-MM-dd 第q季度 星期w HH:mm:ss:SSS'); => 2016-09-02 第3季度 星期五 13:19:15:792
|
|
91
91
|
* formatDate(1472793615764); => 2016-09-02 13:20:15
|
|
92
92
|
*/
|
|
93
93
|
DateUtil.formatDate = function (date, format) {
|
|
@@ -108,35 +108,38 @@ var DateUtil;
|
|
|
108
108
|
return 'invalid date';
|
|
109
109
|
}
|
|
110
110
|
date = date;
|
|
111
|
-
var
|
|
111
|
+
var map = {
|
|
112
112
|
y: date.getFullYear(),
|
|
113
|
+
Y: date.getFullYear(),
|
|
113
114
|
M: date.getMonth() + 1,
|
|
114
115
|
d: date.getDate(),
|
|
116
|
+
D: date.getDate(),
|
|
115
117
|
q: Math.floor((date.getMonth() + 3) / 3),
|
|
116
|
-
w: date.getDay(),
|
|
118
|
+
w: ['日', '一', '二', '三', '四', '五', '六'][date.getDay()],
|
|
117
119
|
H: date.getHours(),
|
|
118
120
|
h: date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
|
|
119
121
|
m: date.getMinutes(),
|
|
120
|
-
|
|
121
|
-
|
|
122
|
+
S: date.getMilliseconds(),
|
|
123
|
+
s: date.getSeconds()
|
|
122
124
|
};
|
|
123
|
-
var
|
|
124
|
-
|
|
125
|
-
format = format.replace(new RegExp(i + '+', 'g'), function (m) {
|
|
126
|
-
var val = "".concat(obj[i]);
|
|
127
|
-
if (i === 'w') {
|
|
128
|
-
return (m.length > 2 ? '星期' : '周') + week[Number(val)];
|
|
129
|
-
}
|
|
130
|
-
for (var j = 0, len = val.length; j < m.length - len; j++) {
|
|
131
|
-
val = '0' + val;
|
|
132
|
-
}
|
|
133
|
-
return m.length === 1 ? val : val.substring(val.length - m.length);
|
|
134
|
-
});
|
|
125
|
+
var formatNumber = function formatNumber(n) {
|
|
126
|
+
return n.toString().padStart(2, '0');
|
|
135
127
|
};
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
return format
|
|
128
|
+
var formatMilliseconds = function formatMilliseconds(n) {
|
|
129
|
+
return n.toString().padStart(3, '0');
|
|
130
|
+
};
|
|
131
|
+
return format.replace(/(y+|Y+|M+|d+|D+|q+|w+|H+|h+|m+|s+|S+)/g, function (match) {
|
|
132
|
+
var char = match[0];
|
|
133
|
+
var result = map[char];
|
|
134
|
+
if (char.toLowerCase() === 'y') {
|
|
135
|
+
result = result.toString().substr(4 - match.length);
|
|
136
|
+
} else if (char === 'S') {
|
|
137
|
+
result = formatMilliseconds(result);
|
|
138
|
+
} else if (['M', 'd', 'D', 'H', 'h', 'm', 's'].includes(char)) {
|
|
139
|
+
result = formatNumber(result);
|
|
140
|
+
}
|
|
141
|
+
return result;
|
|
142
|
+
});
|
|
140
143
|
};
|
|
141
144
|
/**
|
|
142
145
|
* 将一个日期格式化成友好格式,比如,1分钟以内的返回“刚刚”,
|
package/lib/array/ArrayUtil.d.ts
CHANGED
|
@@ -135,5 +135,43 @@ console.log(universalSort(objArray, obj => obj.name.length)); // [{ name: 'Jim',
|
|
|
135
135
|
console.log(universalSort([5, 3, 8, 1, 2], null, 'desc')); // [8, 5, 3, 2, 1]
|
|
136
136
|
*/
|
|
137
137
|
function universalSort<T>(array: T[], key?: keyof T | ((item: T) => any), order?: 'asc' | 'desc'): T[];
|
|
138
|
+
/**
|
|
139
|
+
* 根据对象的属性值进行分组
|
|
140
|
+
* @param array 对象数组
|
|
141
|
+
* @param key 对象的key
|
|
142
|
+
* @example
|
|
143
|
+
const data = [
|
|
144
|
+
{ name: 'Alice', age: 21 },
|
|
145
|
+
{ name: 'Bob', age: 25 },
|
|
146
|
+
{ name: 'Charlie', age: 21 },
|
|
147
|
+
{ name: 'David', age: 25 }
|
|
148
|
+
];
|
|
149
|
+
|
|
150
|
+
const groupedByAge = groupBy(data, 'age');
|
|
151
|
+
console.log(groupedByAge); // {21: Array(2), 25: Array(2)}
|
|
152
|
+
*/
|
|
153
|
+
function groupBy<T>(array: T[], key: keyof T): Record<keyof T, T[]>;
|
|
154
|
+
/**
|
|
155
|
+
* 按条件分组
|
|
156
|
+
* @param array 对象数组
|
|
157
|
+
* @param condition filter
|
|
158
|
+
* @example
|
|
159
|
+
const numbers: number[] = [1, 2, 3, 4, 5, 6];
|
|
160
|
+
|
|
161
|
+
const groupedByOddEven = groupByCondition(numbers, num => num % 2 === 0);
|
|
162
|
+
console.log(groupedByOddEven); // {false: Array(3), true: Array(3)}
|
|
163
|
+
*/
|
|
164
|
+
function groupByCondition<T>(array: T[], condition: (value: T) => boolean): Record<'true' | 'false', T[]>;
|
|
165
|
+
/**
|
|
166
|
+
* 按固定大小分组
|
|
167
|
+
* @param array 对象数组
|
|
168
|
+
* @param size 数量
|
|
169
|
+
* @example
|
|
170
|
+
const data: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
171
|
+
|
|
172
|
+
const chunkedArray = chunkArray(data, 3);
|
|
173
|
+
console.log(chunkedArray);// [Array(3), Array(3), Array(3)]
|
|
174
|
+
*/
|
|
175
|
+
function chunkArray<T>(array: T[], size: number): T[][];
|
|
138
176
|
}
|
|
139
177
|
export default ArrayUtil;
|
package/lib/array/ArrayUtil.js
CHANGED
|
@@ -319,5 +319,67 @@ var ArrayUtil;
|
|
|
319
319
|
return array.sort(compareFunction);
|
|
320
320
|
}
|
|
321
321
|
ArrayUtil.universalSort = universalSort;
|
|
322
|
+
/**
|
|
323
|
+
* 根据对象的属性值进行分组
|
|
324
|
+
* @param array 对象数组
|
|
325
|
+
* @param key 对象的key
|
|
326
|
+
* @example
|
|
327
|
+
const data = [
|
|
328
|
+
{ name: 'Alice', age: 21 },
|
|
329
|
+
{ name: 'Bob', age: 25 },
|
|
330
|
+
{ name: 'Charlie', age: 21 },
|
|
331
|
+
{ name: 'David', age: 25 }
|
|
332
|
+
];
|
|
333
|
+
const groupedByAge = groupBy(data, 'age');
|
|
334
|
+
console.log(groupedByAge); // {21: Array(2), 25: Array(2)}
|
|
335
|
+
*/
|
|
336
|
+
function groupBy(array, key) {
|
|
337
|
+
return array.reduce(function (result, currentValue) {
|
|
338
|
+
var groupKey = currentValue[key];
|
|
339
|
+
if (!result[groupKey]) {
|
|
340
|
+
result[groupKey] = [];
|
|
341
|
+
}
|
|
342
|
+
result[groupKey].push(currentValue);
|
|
343
|
+
return result;
|
|
344
|
+
}, {});
|
|
345
|
+
}
|
|
346
|
+
ArrayUtil.groupBy = groupBy;
|
|
347
|
+
/**
|
|
348
|
+
* 按条件分组
|
|
349
|
+
* @param array 对象数组
|
|
350
|
+
* @param condition filter
|
|
351
|
+
* @example
|
|
352
|
+
const numbers: number[] = [1, 2, 3, 4, 5, 6];
|
|
353
|
+
const groupedByOddEven = groupByCondition(numbers, num => num % 2 === 0);
|
|
354
|
+
console.log(groupedByOddEven); // {false: Array(3), true: Array(3)}
|
|
355
|
+
*/
|
|
356
|
+
function groupByCondition(array, condition) {
|
|
357
|
+
return array.reduce(function (result, currentValue) {
|
|
358
|
+
var groupKey = condition(currentValue) ? 'true' : 'false';
|
|
359
|
+
if (!result[groupKey]) {
|
|
360
|
+
result[groupKey] = [];
|
|
361
|
+
}
|
|
362
|
+
result[groupKey].push(currentValue);
|
|
363
|
+
return result;
|
|
364
|
+
}, {});
|
|
365
|
+
}
|
|
366
|
+
ArrayUtil.groupByCondition = groupByCondition;
|
|
367
|
+
/**
|
|
368
|
+
* 按固定大小分组
|
|
369
|
+
* @param array 对象数组
|
|
370
|
+
* @param size 数量
|
|
371
|
+
* @example
|
|
372
|
+
const data: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
373
|
+
const chunkedArray = chunkArray(data, 3);
|
|
374
|
+
console.log(chunkedArray);// [Array(3), Array(3), Array(3)]
|
|
375
|
+
*/
|
|
376
|
+
function chunkArray(array, size) {
|
|
377
|
+
var result = [];
|
|
378
|
+
for (var i = 0; i < array.length; i += size) {
|
|
379
|
+
result.push(array.slice(i, i + size));
|
|
380
|
+
}
|
|
381
|
+
return result;
|
|
382
|
+
}
|
|
383
|
+
ArrayUtil.chunkArray = chunkArray;
|
|
322
384
|
})(ArrayUtil || (ArrayUtil = {}));
|
|
323
385
|
exports.default = ArrayUtil;
|
package/lib/date/DateUtil.d.ts
CHANGED
|
@@ -38,13 +38,13 @@ declare namespace DateUtil {
|
|
|
38
38
|
/**
|
|
39
39
|
* 将日期格式化成指定格式的字符串
|
|
40
40
|
* @param {Date | Number | String} date 要格式化的日期,不传时默认当前时间,也可以是一个时间戳/字符串
|
|
41
|
-
* @param {String} format 目标字符串格式,支持的字符有:y,M,d,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
|
|
41
|
+
* @param {String} format 目标字符串格式,支持的字符有:y,Y,M,d,D,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
|
|
42
42
|
* @returns {String} 返回格式化后的日期字符串,日期不合法时返回字符串 'invalid date'
|
|
43
43
|
* @example
|
|
44
44
|
* formatDate(); => 2016-09-02 13:17:13
|
|
45
45
|
* formatDate('2016-09-02','yyyy年MM月dd日'); => 2016年09月02日
|
|
46
46
|
* formatDate(new Date(), 'yyyy-MM-dd'); => 2016-09-02
|
|
47
|
-
* formatDate(new Date(), 'yyyy-MM-dd 第q季度
|
|
47
|
+
* formatDate(new Date(), 'yyyy-MM-dd 第q季度 星期w HH:mm:ss:SSS'); => 2016-09-02 第3季度 星期五 13:19:15:792
|
|
48
48
|
* formatDate(1472793615764); => 2016-09-02 13:20:15
|
|
49
49
|
*/
|
|
50
50
|
const formatDate: (date?: Date | number | string, format?: string) => string;
|
package/lib/date/DateUtil.js
CHANGED
|
@@ -75,7 +75,7 @@ var DateUtil;
|
|
|
75
75
|
dateFormatRules.forEach(function (item) {
|
|
76
76
|
for (var index = 0, rules = item.rules, len = rules.length; index < len; index++) {
|
|
77
77
|
rule = rules[index];
|
|
78
|
-
sIndex = format.indexOf("".concat(rule[0]));
|
|
78
|
+
sIndex = format.replaceAll('D', 'd').replaceAll('Y', 'y').indexOf("".concat(rule[0]));
|
|
79
79
|
if (sIndex !== -1) {
|
|
80
80
|
dates.push(parseFloat(dateStr.substring(sIndex, sIndex + Number(rule[1]))) + (item.offset || 0));
|
|
81
81
|
// 如果匹配到规则中的第一条,则退出
|
|
@@ -91,13 +91,13 @@ var DateUtil;
|
|
|
91
91
|
/**
|
|
92
92
|
* 将日期格式化成指定格式的字符串
|
|
93
93
|
* @param {Date | Number | String} date 要格式化的日期,不传时默认当前时间,也可以是一个时间戳/字符串
|
|
94
|
-
* @param {String} format 目标字符串格式,支持的字符有:y,M,d,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
|
|
94
|
+
* @param {String} format 目标字符串格式,支持的字符有:y,Y,M,d,D,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
|
|
95
95
|
* @returns {String} 返回格式化后的日期字符串,日期不合法时返回字符串 'invalid date'
|
|
96
96
|
* @example
|
|
97
97
|
* formatDate(); => 2016-09-02 13:17:13
|
|
98
98
|
* formatDate('2016-09-02','yyyy年MM月dd日'); => 2016年09月02日
|
|
99
99
|
* formatDate(new Date(), 'yyyy-MM-dd'); => 2016-09-02
|
|
100
|
-
* formatDate(new Date(), 'yyyy-MM-dd 第q季度
|
|
100
|
+
* formatDate(new Date(), 'yyyy-MM-dd 第q季度 星期w HH:mm:ss:SSS'); => 2016-09-02 第3季度 星期五 13:19:15:792
|
|
101
101
|
* formatDate(1472793615764); => 2016-09-02 13:20:15
|
|
102
102
|
*/
|
|
103
103
|
DateUtil.formatDate = function (date, format) {
|
|
@@ -118,35 +118,38 @@ var DateUtil;
|
|
|
118
118
|
return 'invalid date';
|
|
119
119
|
}
|
|
120
120
|
date = date;
|
|
121
|
-
var
|
|
121
|
+
var map = {
|
|
122
122
|
y: date.getFullYear(),
|
|
123
|
+
Y: date.getFullYear(),
|
|
123
124
|
M: date.getMonth() + 1,
|
|
124
125
|
d: date.getDate(),
|
|
126
|
+
D: date.getDate(),
|
|
125
127
|
q: Math.floor((date.getMonth() + 3) / 3),
|
|
126
|
-
w: date.getDay(),
|
|
128
|
+
w: ['日', '一', '二', '三', '四', '五', '六'][date.getDay()],
|
|
127
129
|
H: date.getHours(),
|
|
128
130
|
h: date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
|
|
129
131
|
m: date.getMinutes(),
|
|
130
|
-
|
|
131
|
-
|
|
132
|
+
S: date.getMilliseconds(),
|
|
133
|
+
s: date.getSeconds()
|
|
132
134
|
};
|
|
133
|
-
var
|
|
134
|
-
|
|
135
|
-
format = format.replace(new RegExp(i + '+', 'g'), function (m) {
|
|
136
|
-
var val = "".concat(obj[i]);
|
|
137
|
-
if (i === 'w') {
|
|
138
|
-
return (m.length > 2 ? '星期' : '周') + week[Number(val)];
|
|
139
|
-
}
|
|
140
|
-
for (var j = 0, len = val.length; j < m.length - len; j++) {
|
|
141
|
-
val = '0' + val;
|
|
142
|
-
}
|
|
143
|
-
return m.length === 1 ? val : val.substring(val.length - m.length);
|
|
144
|
-
});
|
|
135
|
+
var formatNumber = function formatNumber(n) {
|
|
136
|
+
return n.toString().padStart(2, '0');
|
|
145
137
|
};
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
return format
|
|
138
|
+
var formatMilliseconds = function formatMilliseconds(n) {
|
|
139
|
+
return n.toString().padStart(3, '0');
|
|
140
|
+
};
|
|
141
|
+
return format.replace(/(y+|Y+|M+|d+|D+|q+|w+|H+|h+|m+|s+|S+)/g, function (match) {
|
|
142
|
+
var char = match[0];
|
|
143
|
+
var result = map[char];
|
|
144
|
+
if (char.toLowerCase() === 'y') {
|
|
145
|
+
result = result.toString().substr(4 - match.length);
|
|
146
|
+
} else if (char === 'S') {
|
|
147
|
+
result = formatMilliseconds(result);
|
|
148
|
+
} else if (['M', 'd', 'D', 'H', 'h', 'm', 's'].includes(char)) {
|
|
149
|
+
result = formatNumber(result);
|
|
150
|
+
}
|
|
151
|
+
return result;
|
|
152
|
+
});
|
|
150
153
|
};
|
|
151
154
|
/**
|
|
152
155
|
* 将一个日期格式化成友好格式,比如,1分钟以内的返回“刚刚”,
|