forked from yanlele/node-index
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
147 lines (130 loc) · 4.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import moment from 'moment';
/**
* 查询是否为空对象
* @param {Object} obj 查询对象
* @return {Boolean} 查询结果
*/
export function isObjEmpty(obj) {
// Speed up calls to hasOwnProperty
let hasOwnProperty = Object.prototype.hasOwnProperty;
// null and undefined are "empty"
if (obj == null) return true;
// Assume if it has a length property with a non-zero value
// that that property is correct.
if (obj.length > 0) return false;
if (obj.length === 0) return true;
// If it isn't an object at this point
// it is empty, but it can't be anything *but* empty
// Is it empty? Depends on your application.
if (typeof obj !== 'object') return true;
// Otherwise, does it have any properties of its own?
// Note that this doesn't handle
// toString and valueOf enumeration bugs in IE < 9
for (let key in obj) {
if (hasOwnProperty.call(obj, key)) return false;
}
return true;
}
/**
* 获取文件大小信息
* @param {Number} size kb数
* @return {String} 实际大小
*/
export function getSizeInfo(size) {
if(size === 0) return '0KB';
let sOutput;
for (let aMultiples = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], nMultiple = 0, nApprox = size / 1024; nApprox > 1; nApprox /= 1024, nMultiple++) {
sOutput = parseInt(nApprox) + aMultiples[nMultiple];
}
return sOutput;
}
/**
* 格式化数字
* @param {Number} number 格式化前数字
* @param {Number} digit 位数
* @return {Number} 格式化后数字
*/
export function formatNumber(number, digit) {
const accuracyNum = Math.pow(10, digit);
return parseFloat(Math.round(number * accuracyNum) / accuracyNum);
}
/**
* 格式化价格
* @param {Number} price 价格
* @return {String} 格式化后的价格
*/
export function formatPrice(price) {
return formatNumber(price, 2).toFixed(2);
}
/**
* 格式化日期
* @param {Any} dateData 支持时间戳10/13位,日期对象,字符串
* @param {String} format 格式化形式
* @return {String} 格式化后日期
*/
export function formatDate(dateData, format) {
switch(typeof dateData) {
case 'string':
case 'number':
dateData = dateData.toString();
if(isNaN(dateData)) { // 如果是纯字符串
return moment(new Date(dateData)).format(format);
}
if(dateData.length === 10) return moment(new Date(parseInt(dateData) * 1000)).format(format);
if(dateData.length === 13) return moment(new Date(parseInt(dateData))).format(format);
break;
case 'object':
if(dateData instanceof Date) {
return moment(dateData).format(format);
}
break;
}
return '-';
}
/**
* 拆分价格,传入实体数额,然后才分为价格和单位的形式
* @param price
* @returns {*[]}
*/
export function splitPrice(price) {
let unit = '元', rebuildPrice = price;
if(price < 10000) {
unit = '元';
rebuildPrice = price;
} else if(price >= 10000 && price < 100000000 ) {
unit = '万';
rebuildPrice = price / 10000;
} else if(price >= 100000000) {
unit = '亿';
rebuildPrice = price / 100000000;
}
return [parseInt(rebuildPrice), unit];
}
/**
* 重新合并价格
* @param price 传入的价格
* @param unit 单位
* @returns {*}
*/
export function recombinePrice(price, unit) {
let returnPrice = price;
switch (unit) {
case '元':
returnPrice = price;
break;
case '万':
returnPrice = price * 10000;
break;
case '亿':
returnPrice = price * 100000000;
break;
default:
returnPrice = price;
}
return returnPrice;
}
// 日期标准格式
export const dateFormat = 'YYYY-MM-DD HH:mm:ss';
export const dateFormatOnlyDate = 'YYYY-MM-DD';
export const timeFormat = 'HH:mm';
export const dateFormatWithMin = 'YYYY-MM-DD HH:mm';