xt-element-ui 2.1.4 → 2.1.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.
@@ -177,6 +177,272 @@ export const onConfigChange = function(listener) {
177
177
  }
178
178
  }
179
179
 
180
+ // ==================== 数字格式化 ====================
181
+
182
+ export const formatNumber = function(value, options) {
183
+ if (value === null || value === undefined || value === '') {
184
+ return ''
185
+ }
186
+
187
+ const num = typeof value === 'string' ? parseFloat(value) : value
188
+ if (isNaN(num)) {
189
+ return String(value)
190
+ }
191
+
192
+ const opts = Object.assign({
193
+ decimals: 2,
194
+ thousand: true,
195
+ prefix: '',
196
+ suffix: '',
197
+ showSign: false
198
+ }, options || {})
199
+
200
+ let result = num.toFixed(opts.decimals)
201
+
202
+ if (opts.thousand) {
203
+ const parts = result.split('.')
204
+ parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
205
+ result = parts.join('.')
206
+ }
207
+
208
+ if (opts.showSign && num > 0) {
209
+ result = '+' + result
210
+ }
211
+
212
+ return opts.prefix + result + opts.suffix
213
+ }
214
+
215
+ export const formatThousand = function(value, decimals) {
216
+ return formatNumber(value, {
217
+ decimals: decimals != null ? decimals : 2,
218
+ thousand: true
219
+ })
220
+ }
221
+
222
+ export const formatPercent = function(value, decimals) {
223
+ if (value === null || value === undefined || value === '') {
224
+ return ''
225
+ }
226
+ const num = typeof value === 'string' ? parseFloat(value) : value
227
+ if (isNaN(num)) {
228
+ return String(value)
229
+ }
230
+ return formatNumber(num * 100, {
231
+ decimals: decimals != null ? decimals : 2,
232
+ thousand: true,
233
+ suffix: '%'
234
+ })
235
+ }
236
+
237
+ // ==================== 日期格式化 ====================
238
+
239
+ function padZero(num, len) {
240
+ len = len || 2
241
+ return String(num).padStart(len, '0')
242
+ }
243
+
244
+ export const formatDate = function(date, format) {
245
+ if (!date) {
246
+ return ''
247
+ }
248
+
249
+ let d
250
+ if (date instanceof Date) {
251
+ d = date
252
+ } else if (typeof date === 'number') {
253
+ d = new Date(date.toString().length === 10 ? date * 1000 : date)
254
+ } else if (typeof date === 'string') {
255
+ date = date.trim()
256
+ if (/^\d+$/.test(date)) {
257
+ d = new Date(date.length === 10 ? parseInt(date) * 1000 : parseInt(date))
258
+ } else {
259
+ d = new Date(date.replace(/-/g, '/'))
260
+ }
261
+ } else {
262
+ return ''
263
+ }
264
+
265
+ if (isNaN(d.getTime())) {
266
+ return ''
267
+ }
268
+
269
+ const fmt = format || 'yyyy-MM-dd'
270
+ const year = d.getFullYear()
271
+ const month = d.getMonth() + 1
272
+ const day = d.getDate()
273
+ const hour = d.getHours()
274
+ const minute = d.getMinutes()
275
+ const second = d.getSeconds()
276
+ const week = d.getDay()
277
+
278
+ const weekMap = ['日', '一', '二', '三', '四', '五', '六']
279
+
280
+ return fmt
281
+ .replace('yyyy', year)
282
+ .replace('MM', padZero(month))
283
+ .replace('M', month)
284
+ .replace('dd', padZero(day))
285
+ .replace('d', day)
286
+ .replace('HH', padZero(hour))
287
+ .replace('H', hour)
288
+ .replace('hh', padZero(hour % 12 || 12))
289
+ .replace('h', hour % 12 || 12)
290
+ .replace('mm', padZero(minute))
291
+ .replace('m', minute)
292
+ .replace('ss', padZero(second))
293
+ .replace('s', second)
294
+ .replace('w', weekMap[week])
295
+ .replace('W', '星期' + weekMap[week])
296
+ }
297
+
298
+ export const formatDateTime = function(date, format) {
299
+ return formatDate(date, format || 'yyyy-MM-dd HH:mm:ss')
300
+ }
301
+
302
+ export const formatTime = function(date, format) {
303
+ return formatDate(date, format || 'HH:mm:ss')
304
+ }
305
+
306
+ export const formatRelativeTime = function(date) {
307
+ if (!date) {
308
+ return ''
309
+ }
310
+
311
+ let d
312
+ if (date instanceof Date) {
313
+ d = date
314
+ } else if (typeof date === 'number') {
315
+ d = new Date(date.toString().length === 10 ? date * 1000 : date)
316
+ } else if (typeof date === 'string') {
317
+ date = date.trim()
318
+ if (/^\d+$/.test(date)) {
319
+ d = new Date(date.length === 10 ? parseInt(date) * 1000 : parseInt(date))
320
+ } else {
321
+ d = new Date(date.replace(/-/g, '/'))
322
+ }
323
+ } else {
324
+ return ''
325
+ }
326
+
327
+ if (isNaN(d.getTime())) {
328
+ return ''
329
+ }
330
+
331
+ const now = new Date()
332
+ const diff = now.getTime() - d.getTime()
333
+ const minute = 60 * 1000
334
+ const hour = 60 * minute
335
+ const day = 24 * hour
336
+ const week = 7 * day
337
+ const month = 30 * day
338
+ const year = 365 * day
339
+
340
+ if (diff < minute) {
341
+ return '刚刚'
342
+ } else if (diff < hour) {
343
+ return Math.floor(diff / minute) + '分钟前'
344
+ } else if (diff < day) {
345
+ return Math.floor(diff / hour) + '小时前'
346
+ } else if (diff < week) {
347
+ return Math.floor(diff / day) + '天前'
348
+ } else if (diff < month) {
349
+ return Math.floor(diff / week) + '周前'
350
+ } else if (diff < year) {
351
+ return Math.floor(diff / month) + '个月前'
352
+ } else {
353
+ return Math.floor(diff / year) + '年前'
354
+ }
355
+ }
356
+
357
+ // ==================== 金额格式化 ====================
358
+
359
+ export const formatMoney = function(value, options) {
360
+ if (value === null || value === undefined || value === '') {
361
+ return ''
362
+ }
363
+
364
+ const num = typeof value === 'string' ? parseFloat(value) : value
365
+ if (isNaN(num)) {
366
+ return String(value)
367
+ }
368
+
369
+ const opts = Object.assign({
370
+ currency: 'CNY',
371
+ decimals: 2,
372
+ prefix: '',
373
+ suffix: '',
374
+ showSign: false
375
+ }, options || {})
376
+
377
+ const currencySymbols = {
378
+ CNY: '¥',
379
+ USD: '$',
380
+ EUR: '€',
381
+ JPY: '¥',
382
+ GBP: '£',
383
+ AUD: 'A$',
384
+ CAD: 'C$'
385
+ }
386
+
387
+ let symbol = currencySymbols[opts.currency] || ''
388
+ let formatted = num.toFixed(opts.decimals)
389
+
390
+ const parts = formatted.split('.')
391
+ parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
392
+ formatted = parts.join('.')
393
+
394
+ if (opts.showSign && num > 0) {
395
+ formatted = '+' + formatted
396
+ }
397
+
398
+ if (opts.prefix) {
399
+ formatted = opts.prefix + formatted
400
+ }
401
+
402
+ if (opts.suffix) {
403
+ formatted = formatted + opts.suffix
404
+ }
405
+
406
+ return symbol + formatted
407
+ }
408
+
409
+ export const formatCNY = function(value, decimals) {
410
+ return formatMoney(value, {
411
+ currency: 'CNY',
412
+ decimals: decimals != null ? decimals : 2
413
+ })
414
+ }
415
+
416
+ export const formatUSD = function(value, decimals) {
417
+ return formatMoney(value, {
418
+ currency: 'USD',
419
+ decimals: decimals != null ? decimals : 2
420
+ })
421
+ }
422
+
423
+ // ==================== 文件大小格式化 ====================
424
+
425
+ export const formatFileSize = function(bytes) {
426
+ if (bytes === null || bytes === undefined || bytes === '') {
427
+ return ''
428
+ }
429
+
430
+ const num = typeof bytes === 'string' ? parseFloat(bytes) : bytes
431
+ if (isNaN(num)) {
432
+ return String(bytes)
433
+ }
434
+
435
+ if (num === 0) {
436
+ return '0 B'
437
+ }
438
+
439
+ const k = 1024
440
+ const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
441
+ const i = Math.floor(Math.log(num) / Math.log(k))
442
+
443
+ return parseFloat((num / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
444
+ }
445
+
180
446
  export default {
181
447
  setTheme,
182
448
  setSize,
@@ -187,5 +453,16 @@ export default {
187
453
  getSize,
188
454
  getPrimaryColor,
189
455
  resetConfig,
190
- onConfigChange
456
+ onConfigChange,
457
+ formatNumber,
458
+ formatThousand,
459
+ formatPercent,
460
+ formatDate,
461
+ formatDateTime,
462
+ formatTime,
463
+ formatRelativeTime,
464
+ formatMoney,
465
+ formatCNY,
466
+ formatUSD,
467
+ formatFileSize
191
468
  }