deno.land / x / dayjs@v1.11.5 / test / plugin / objectSupport.test.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387import MockDate from 'mockdate'import moment from 'moment'import dayjs from '../../src'import objectSupport from '../../src/plugin/objectSupport'import quarterOfYear from '../../src/plugin/quarterOfYear'import utc from '../../src/plugin/utc'import utils from '../../src/utils'
dayjs.extend(utc)dayjs.extend(quarterOfYear)dayjs.extend(objectSupport)
beforeEach(() => { MockDate.set(new Date())})
afterEach(() => { MockDate.reset()})const now = new Date()const currentYear = now.getFullYear()const currentMonth = utils.s(now.getMonth() + 1, 2, '0')const currentDate = utils.s(now.getDate(), 2, '0')const currentUTCYear = now.getUTCFullYear()const currentUTCMonth = utils.s(now.getUTCMonth() + 1, 2, '0')const currentUTCDate = utils.s(now.getUTCDate(), 2, '0')const fmt = 'YYYY-MM-DD HH:mm:ss.SSS'const tests = [ [{ year: 2010 }, '2010-01-01 00:00:00.000'], [{ year: 2010, month: 1 }, '2010-02-01 00:00:00.000'], [{ year: 2010, month: 1, day: 12 }, '2010-02-12 00:00:00.000'], [{ year: 2010, month: 1, date: 12 }, '2010-02-12 00:00:00.000'], [ { hour: 15, minute: 25, second: 50, millisecond: 125 }, `${currentYear}-${currentMonth}-${currentDate} 15:25:50.125`, `${currentUTCYear}-${currentUTCMonth}-${currentUTCDate} 15:25:50.125`], [ { year: 2010, month: 1, day: 12, hours: 1 }, '2010-02-12 01:00:00.000' ], [ { year: 2010, month: 1, date: 12, hours: 1 }, '2010-02-12 01:00:00.000' ], [ { year: 2010, month: 1, day: 12, hours: 1, minutes: 1 }, '2010-02-12 01:01:00.000' ], [ { year: 2010, month: 1, date: 12, hours: 1, minutes: 1 }, '2010-02-12 01:01:00.000' ], [ { year: 2010, month: 1, day: 12, hours: 1, minutes: 1, seconds: 1 }, '2010-02-12 01:01:01.000' ], [ { year: 2010, month: 1, day: 12, hours: 1, minutes: 1, seconds: 1, milliseconds: 1 }, '2010-02-12 01:01:01.001' ], [ { years: 2010, months: 1, days: 14, hours: 15, minutes: 25, seconds: 50, milliseconds: 125 }, '2010-02-14 15:25:50.125' ], [ { year: 2010, month: 1, day: 14, hour: 15, minute: 25, second: 50, millisecond: 125 }, '2010-02-14 15:25:50.125' ], [ { y: 2010, M: 1, d: 14, h: 15, m: 25, s: 50, ms: 125 }, '2010-02-14 15:25:50.125' ]]
describe('parse empty object', () => { it('local', () => { expect(dayjs({}).format()) .toBe(moment({}).format()) }) it('utc', () => { expect(dayjs.utc({}).format()) .toBe(moment.utc({}).format()) })})
it('Constructor from Object', () => { for (let i = 0; i < tests.length; i += 1) { expect(dayjs(tests[i][0]).format(fmt)).toBe(tests[i][1]) expect(moment(tests[i][0]).format(fmt)).toBe(tests[i][1]) }})
it('Constructor from Object UTC', () => { for (let i = 0; i < tests.length; i += 1) { const result = tests[i][2] || tests[i][1] expect(dayjs.utc(tests[i][0]).format(fmt)).toBe(result) expect(moment.utc(tests[i][0]).format(fmt)).toBe(result) }})it('Set from Object', () => { for (let i = 0; i < tests.length; i += 1) { expect(dayjs(now).set(tests[i][0]).format(fmt)).toBe(moment(now).set(tests[i][0]).format(fmt)) }})
it('add short reverse args', () => { const a = dayjs({ year: 2011, month: 9, date: 12, hour: 6, minute: 7, second: 8, millisecond: 500 }) expect(a.add({ ms: 50 }).millisecond()).toBe(550) expect(a.add({ s: 1 }).second()).toBe(9) expect(a.add({ m: 1 }).minute()).toBe(8) expect(a.add({ h: 1 }).hour()).toBe(7) expect(a.add({ d: 1 }).date()).toBe(13) expect(a.add({ w: 1 }).date()).toBe(19) expect(a.add({ M: 1 }).month()).toBe(10) expect(a.add({ y: 1 }).year()).toBe(2012) expect(a.add({ Q: 1 }).month()).toBe(0)
const aM = moment({ year: 2011, month: 9, date: 12, hour: 6, minute: 7, second: 8, millisecond: 500 }) expect(aM.clone().add({ ms: 50 }).millisecond()).toBe(550) expect(aM.clone().add({ s: 1 }).second()).toBe(9) expect(aM.clone().add({ m: 1 }).minute()).toBe(8) expect(aM.clone().add({ h: 1 }).hour()).toBe(7) expect(aM.clone().add({ d: 1 }).date()).toBe(13) expect(aM.clone().add({ w: 1 }).date()).toBe(19) expect(aM.clone().add({ M: 1 }).month()).toBe(10) expect(aM.clone().add({ y: 1 }).year()).toBe(2012) expect(aM.clone().add({ Q: 1 }).month()).toBe(0)
const b = dayjs([2010, 1, 31]).add({ M: 1 }) const c = dayjs([2010, 2, 28]).subtract({ M: 1 }) const d = dayjs([2010, 2, 28]).subtract({ Q: 1 })
expect(b.month()).toBe(1) expect(b.date()).toBe(28) expect(c.month()).toBe(0) expect(c.date()).toBe(28) expect(d.month()).toBe(10) expect(d.date()).toBe(28) expect(d.year()).toBe(2009)})
it('add long reverse args', () => { const a = dayjs({ year: 2011, month: 9, date: 12, hour: 6, minute: 7, second: 8, millisecond: 500 })
expect(a.add({ milliseconds: 50 }).millisecond()).toBe(550) expect(a.add({ seconds: 1 }).second()).toBe(9) expect(a.add({ minutes: 1 }).minute()).toBe(8) expect(a.add({ hours: 1 }).hour()).toBe(7) expect(a.add({ days: 1 }).date()).toBe(13) expect(a.add({ weeks: 1 }).date()).toBe(19) expect(a.add({ months: 1 }).month()).toBe(10) expect(a.add({ years: 1 }).year()).toBe(2012) expect(a.add({ quarters: 1 }).month()).toBe(0)})
it('add long singular reverse args', () => { const a = dayjs({ year: 2011, month: 9, date: 12, hour: 6, minute: 7, second: 8, millisecond: 500 })
expect(a.add({ millisecond: 50 }).millisecond()).toBe(550) expect(a.add({ second: 1 }).second()).toBe(9) expect(a.add({ minute: 1 }).minute()).toBe(8) expect(a.add({ hour: 1 }).hour()).toBe(7) expect(a.add({ day: 1 }).date()).toBe(13) expect(a.add({ week: 1 }).date()).toBe(19) expect(a.add({ month: 1 }).month()).toBe(10) expect(a.add({ year: 1 }).year()).toBe(2012) expect(a.add({ quarter: 1 }).month()).toBe(0)})
it('add string long', () => { const a = dayjs({ year: 2011, month: 9, date: 12, hour: 6, minute: 7, second: 8, millisecond: 500 })
expect(a.add(50, 'millisecond').millisecond()).toBe(550) expect(a.add(1, 'second').second()).toBe(9) expect(a.add(1, 'minute').minute()).toBe(8) expect(a.add(1, 'hour').hour()).toBe(7) expect(a.add(1, 'day').date()).toBe(13) expect(a.add(1, 'week').date()).toBe(19) expect(a.add(1, 'month').month()).toBe(10) expect(a.add(1, 'year').year()).toBe(2012) expect(a.add(1, 'quarter').month()).toBe(0)})
it('add string long singular', () => { const a = dayjs({ year: 2011, month: 9, date: 12, hour: 6, minute: 7, second: 8, millisecond: 500 })
expect(a.add(50, 'milliseconds').millisecond()).toBe(550) expect(a.add(1, 'seconds').second()).toBe(9) expect(a.add(1, 'minutes').minute()).toBe(8) expect(a.add(1, 'hours').hour()).toBe(7) expect(a.add(1, 'days').date()).toBe(13) expect(a.add(1, 'weeks').date()).toBe(19) expect(a.add(1, 'months').month()).toBe(10) expect(a.add(1, 'years').year()).toBe(2012) expect(a.add(1, 'quarters').month()).toBe(0)})
it('add string short', () => { const a = dayjs({ year: 2011, month: 9, date: 12, hour: 6, minute: 7, second: 8, millisecond: 500 })
expect(a.add(50, 'ms').millisecond()).toBe(550) expect(a.add(1, 's').second()).toBe(9) expect(a.add(1, 'm').minute()).toBe(8) expect(a.add(1, 'h').hour()).toBe(7) expect(a.add(1, 'd').date()).toBe(13) expect(a.add(1, 'w').date()).toBe(19) expect(a.add(1, 'M').month()).toBe(10) expect(a.add(1, 'y').year()).toBe(2012) expect(a.add(1, 'Q').month()).toBe(0)})
it('add strings string short', () => { const a = dayjs({ year: 2011, month: 9, date: 12, hour: 6, minute: 7, second: 8, millisecond: 500 })
expect(a.add('50', 'ms').millisecond()).toBe(550) expect(a.add('1', 's').second()).toBe(9) expect(a.add('1', 'm').minute()).toBe(8) expect(a.add('1', 'h').hour()).toBe(7) expect(a.add('1', 'd').date()).toBe(13) expect(a.add('1', 'w').date()).toBe(19) expect(a.add('1', 'M').month()).toBe(10) expect(a.add('1', 'y').year()).toBe(2012) expect(a.add('1', 'Q').month()).toBe(0)})
it('add no string with milliseconds default', () => { const a = dayjs({ year: 2011, month: 9, date: 12, hour: 6, minute: 7, second: 8, millisecond: 500 })
expect(a.add(50).millisecond()).toBe(550)})
it('subtract strings string short', () => { const a = dayjs({ year: 2011, month: 9, date: 12, hour: 6, minute: 7, second: 8, millisecond: 500 }) expect(a.subtract('50', 'ms').millisecond()).toBe(450) expect(a.subtract('1', 's').second()).toBe(7) expect(a.subtract('1', 'm').minute()).toBe(6) expect(a.subtract('1', 'h').hour()).toBe(5) expect(a.subtract('1', 'd').date()).toBe(11) expect(a.subtract('1', 'w').date()).toBe(5) expect(a.subtract('1', 'M').month()).toBe(8) expect(a.subtract('1', 'y').year()).toBe(2010) expect(a.subtract('1', 'Q').month()).toBe(6)})
it('add decimal values of days and months', () => { expect(dayjs([2016, 4, 3]).add(1.6, 'days').date()).toBe(5) expect(dayjs([2016, 4, 3]).add(-1.6, 'days').date()).toBe(1) expect(dayjs([2016, 4, 1]).add(-1.6, 'days').date()).toBe(30) expect(dayjs([2016, 4, 3]).add(1.6, 'months').month()).toBe(4) expect(dayjs([2016, 4, 3]).add(-1.6, 'months').month()).toBe(1) expect(dayjs([2016, 1, 3]).add(-1.6, 'months').month()).toBe(11) expect(dayjs([2016, 4, 3]).subtract(1.6, 'days').date()).toBe(1) expect(dayjs([2016, 4, 2]).subtract(1.6, 'days').date()).toBe(31) expect(dayjs([2016, 2, 1]).subtract(1.1, 'days').date()).toBe(31) expect(dayjs([2016, 4, 3]).subtract(-1.6, 'days').date()).toBe(5) expect(dayjs([2016, 4, 30]).subtract(-1.6, 'days').date()).toBe(2) expect(dayjs([2016, 4, 3]).subtract(1.6, 'months').month()).toBe(1) expect(dayjs([2016, 4, 3]).subtract(-1.6, 'months').month()).toBe(4) expect(dayjs([2016, 12, 31]).subtract(-1.6, 'months').month()).toBe(0) expect(dayjs([2016, 1, 1]).add(1.6, 'years').format('YYYY-MM-DD')).toBe('2017-01-01') expect(dayjs([2016, 7, 1]).add(1.6, 'years').format('YYYY-MM-DD')).toBe('2017-07-01') expect(dayjs([2016, 1, 1]).add(1.1, 'quarters').format('YYYY-MM-DD')).toBe('2016-04-01')})
Version Info