UNPKG

jinbi-utils

Version:

这是一个实用工具库,包含了多个常用的功能模块。以下是各个模块的详细说明:

107 lines (102 loc) 4.39 kB
import { compatibleDate, newDate, formatTime, fromNow, addDays } from '../../src'; describe('compatibleDate', () => { test(`compatibleDate('2020-03-04') 返回 2020/03/04`, () => { expect(compatibleDate('2020-03-04')).toBe('2020/03/04'); }) test(`compatibleDate(Date.now()) 返回 Date.now()`, () => { const now = Date.now(); expect(compatibleDate(now)).toBe(now); }) test(`compatibleDate(new Date()) 返回 new Date()`, () => { const newDate = new Date(); expect(compatibleDate(newDate)).toBe(newDate); }) }); describe('newDate', () => { const date = new Date(); test(`newDate() 返回 new Date()`, () => { expect(new Date(date)).toEqual(date); }) test(`newDate('2020-03-04') 返回 new Date(2020/03/04)`, () => { expect(newDate('2020-03-04')).toEqual(new Date('2020/03/04')); }) test(`newDate(date) 返回 date`, () => { const date = new Date(); expect(newDate(date)).toEqual(date); }) }); describe('formatTime', () => { test(`formatTime() 返回 ${formatTime()}`, () => { expect(formatTime()).toBe(formatTime()); }) test(`formatTime('2018-02-02 00:00:00') 返回 2018-02-02 00:00:00`, () => { expect(formatTime('2018-02-02 00:00:00')).toBe('2018-02-02 00:00:00'); }) test(`formatTime('2018-02-02', 'yyyy/MM/dd') 返回 2018/02/02`, () => { expect(formatTime('2018-02-02', 'yyyy/MM/dd')).toBe('2018/02/02'); }) test(`formatTime('2018-02-02 23:50:50', 'yyyy/MM/dd HH:mm:ss') 返回 2018/02/02 23:50:50`, () => { expect(formatTime('2018-02-02 23:50:50', 'yyyy/MM/dd HH:mm:ss')).toBe('2018/02/02 23:50:50'); }) test(`formatTime('2018-02-02 23:50:50', 'MM/dd HH:mm:ss') 返回 02/02 23:50:50`, () => { expect(formatTime('2018-02-02 23:50:50', 'MM/dd HH:mm:ss')).toBe('02/02 23:50:50'); }) test(`formatTime(new Date('2018-02-02 23:50:50') + 200, 'yyyy/MM/dd HH:mm:ss S') 返回 2018/02/02 23:50:50 200`, () => { const date = new Date('2018-02-02 23:50:50').getTime() + 200; expect(formatTime(date, 'yyyy/MM/dd HH:mm:ss S')).toBe('2018/02/02 23:50:50 200'); }) }); describe('fromNow', () => { const date = new Date(); const now = Date.now(); test(`fromNow('') 返回 -`, () => { expect(fromNow('')).toBe('-'); }) test(`fromNow(Date.now() + 2000) 返回 Date.now() + 2000`, () => { expect(fromNow(now + 2000)).toBe(now + 2000); }) test(`fromNow(Date.now()) 返回 刚刚`, () => { expect(fromNow(now)).toBe('刚刚'); }) test(`fromNow(new Date().getTime()) 返回 刚刚`, () => { expect(fromNow(date.getTime())).toBe('刚刚'); }) // 之所返回的是 58 是因为 Date.now() 获取的时机在调用之后 test(`fromNow(now - 3420 * 1000) 返回 58分钟前面`, () => { expect(fromNow(now - 3420 * 1000)).toBe('58分钟前'); }) test(`fromNow(now - 3600 * 1000 * 3) 返回 3小时前`, () => { expect(fromNow(now - 3600 * 1000 * 3)).toBe('3小时前'); }) test(`fromNow(now - 3600 * 1000 * 24 * 1.5) 返回 1天前`, () => { expect(fromNow(now - 3600 * 1000 * 24 * 1.5)).toBe('1天前'); }) const day5 = 3600 * 1000 * 24 * 5; test(`fromNow(now - day5) 返回 formatTime(now - day5, 'yyyy年MM月dd日 HH时mm分ss秒')`, () => { expect(fromNow(now - day5, 'yyyy年MM月dd日 HH时mm分ss秒')).toBe(formatTime(now - day5, 'yyyy年MM月dd日 HH时mm分ss秒')); }) test(`fromNow('', '', 'defaultValue') 等于 defaultValue`, () => { expect(fromNow('', '', 'defaultValue')).toBe('defaultValue'); }) }); describe('addDays', () => { test(`addDays('') 返回 -`, () => { expect(addDays('')).toBe('-'); }) test(`addDays('2018-02-02') 返回 2018-02-02`, () => { expect(addDays('2018-02-02')).toBe('2018-02-02'); }) test(`addDays('2018-02-02', 2) 返回 2018-02-04`, () => { expect(addDays('2018-02-02', 2)).toBe('2018-02-04'); }) test(`addDays('2018-02-02', 2, 'yyyy/MM/dd') 返回 2018/02/04`, () => { expect(addDays('2018-02-02', 2, 'yyyy/MM/dd')).toBe('2018/02/04'); }) test(`addDays(new Date('2020-03-04'), 2, 'yyyy/MM/dd') 返回 2020/03/06`, () => { expect(addDays(new Date('2020-03-04'), 2, 'yyyy/MM/dd')).toBe('2020/03/06'); }) test(`addDays(Date.now(), 2, 'yyyy/MM/dd') 返回 今天 + 2天`, () => { const now = Date.now(); expect(addDays(now, 2, 'yyyy/MM/dd')).toBe(formatTime(now + 2 * 24 * 60 * 60 * 1000, 'yyyy/MM/dd')); }) });