Comparing version 1.5.5 to 1.5.6
{ | ||
"name": "dayjs", | ||
"version": "1.5.5", | ||
"version": "1.5.6", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import babel from 'rollup-plugin-babel' | ||
import uglify from 'rollup-plugin-uglify' | ||
import packageInfo from './package.json'; | ||
@@ -7,3 +8,3 @@ export default { | ||
output: { | ||
file: 'dist/index.js', | ||
file: `dist/${packageInfo.name}.min.js`, | ||
format: 'umd', | ||
@@ -10,0 +11,0 @@ name: 'dayjs' |
@@ -6,3 +6,3 @@ export const SECONDS_A_MINUTE = 60 | ||
export const MILLISECONDS_A_SECOND = 1000 | ||
export const MILLISECONDS_A_SECOND = 1e3 | ||
export const MILLISECONDS_A_MINUTE = SECONDS_A_MINUTE * MILLISECONDS_A_SECOND | ||
@@ -12,1 +12,12 @@ export const MILLISECONDS_A_HOUR = SECONDS_A_HOUR * MILLISECONDS_A_SECOND | ||
export const MILLISECONDS_A_WEEK = SECONDS_A_WEEK * MILLISECONDS_A_SECOND | ||
export const MS = 'millisecond' | ||
export const S = 'second' | ||
export const MIN = 'minute' | ||
export const H = 'hour' | ||
export const D = 'day' | ||
export const W = 'week' | ||
export const M = 'month' | ||
export const Q = 'quarter' | ||
export const Y = 'year' | ||
export const DATE = 'date' |
223
src/index.js
@@ -1,2 +0,2 @@ | ||
import * as Constant from './constant' | ||
import * as C from './constant' | ||
import * as Utils from './utils' | ||
@@ -19,3 +19,3 @@ | ||
constructor(config) { | ||
this.$date = parseConfig(config) | ||
this.$d = parseConfig(config) | ||
this.init() | ||
@@ -25,40 +25,60 @@ } | ||
init() { | ||
this.timeZone = this.$date.getTimezoneOffset() / 60 | ||
this.timeZone = this.$d.getTimezoneOffset() / 60 | ||
this.timeZoneString = Utils.padStart(String(this.timeZone * -1).replace(/^(.)?(\d)/, '$10$200'), 5, '+') | ||
this.$year = this.$date.getFullYear() | ||
this.$month = this.$date.getMonth() | ||
this.$day = this.$date.getDate() | ||
this.$week = this.$date.getDay() | ||
this.$hour = this.$date.getHours() | ||
this.$minute = this.$date.getMinutes() | ||
this.$second = this.$date.getSeconds() | ||
this.$milliseconds = this.$date.getMilliseconds() | ||
this.$y = this.$d.getFullYear() | ||
this.$M = this.$d.getMonth() | ||
this.$D = this.$d.getDate() | ||
this.$W = this.$d.getDay() | ||
this.$H = this.$d.getHours() | ||
this.$m = this.$d.getMinutes() | ||
this.$s = this.$d.getSeconds() | ||
this.$ms = this.$d.getMilliseconds() | ||
} | ||
isValid() { | ||
return !(this.$d.toString() === 'Invalid Date') | ||
} | ||
isLeapYear() { | ||
return ((this.$y % 4 === 0) && (this.$y % 100 !== 0)) || (this.$y % 400 === 0) | ||
} | ||
isSame(that) { | ||
return this.valueOf() === that.valueOf() | ||
} | ||
isBefore(that) { | ||
return this.valueOf() < that.valueOf() | ||
} | ||
isAfter(that) { | ||
return this.valueOf() > that.valueOf() | ||
} | ||
year() { | ||
return this.$year | ||
return this.$y | ||
} | ||
month() { | ||
return this.$month | ||
return this.$M | ||
} | ||
date() { | ||
return this.$day | ||
return this.$D | ||
} | ||
hour() { | ||
return this.$hour | ||
return this.$H | ||
} | ||
minute() { | ||
return this.$minute | ||
return this.$m | ||
} | ||
second() { | ||
return this.$second | ||
return this.$s | ||
} | ||
millisecond() { | ||
return this.$milliseconds | ||
return this.$ms | ||
} | ||
@@ -72,18 +92,19 @@ | ||
// timezone(hour) * 60 * 60 * 1000 => ms | ||
return this.$date.getTime() | ||
return this.$d.getTime() | ||
} | ||
startOf(arg, isStartOf = true) { | ||
switch (arg) { | ||
case 'year': | ||
startOf(units, isStartOf = true) { // isStartOf -> endOf | ||
const unit = Utils.prettyUnit(units) | ||
switch (unit) { | ||
case C.Y: | ||
if (isStartOf) { | ||
return new Dayjs(new Date(this.year(), 0, 1)) | ||
return new Dayjs(new Date(this.$y, 0, 1)) | ||
} | ||
return new Dayjs(new Date(this.year(), 11, 31)).endOf('day') | ||
case 'month': | ||
return new Dayjs(new Date(this.$y, 11, 31)).endOf('day') | ||
case C.M: | ||
if (isStartOf) { | ||
return new Dayjs(new Date(this.year(), this.month(), 1)) | ||
return new Dayjs(new Date(this.$y, this.$M, 1)) | ||
} | ||
return new Dayjs(new Date(this.year(), this.month() + 1, 0)).endOf('day') | ||
case 'day': | ||
return new Dayjs(new Date(this.$y, this.$M + 1, 0)).endOf('day') | ||
case C.D: | ||
if (isStartOf) { | ||
@@ -102,13 +123,13 @@ return new Dayjs(this.toDate().setHours(0, 0, 0, 0)) | ||
set(string, int) { | ||
if (!Utils.isNumber(int)) return this | ||
switch (string) { | ||
case 'date': | ||
this.$date.setDate(int) | ||
mSet(units, int) { | ||
const unit = Utils.prettyUnit(units) | ||
switch (unit) { | ||
case C.DATE: | ||
this.$d.setDate(int) | ||
break | ||
case 'month': | ||
this.$date.setMonth(int) | ||
case C.M: | ||
this.$d.setMonth(int) | ||
break | ||
case 'year': | ||
this.$date.setFullYear(int) | ||
case C.Y: | ||
this.$d.setFullYear(int) | ||
break | ||
@@ -122,30 +143,37 @@ default: | ||
add(number, string) { | ||
if (['M', 'months'].indexOf(string) > -1) { | ||
const date = this.clone() | ||
date.set('date', 1) | ||
date.set('month', this.month() + number) | ||
date.set('date', Math.min(this.date(), date.daysInMonth())) | ||
set(string, int) { | ||
if (!Utils.isNumber(int)) return this | ||
return this.clone().mSet(string, int) | ||
} | ||
add(number, units) { | ||
const unit = (units && units.length === 1) ? units : Utils.prettyUnit(units) | ||
if (['M', C.M].indexOf(unit) > -1) { | ||
let date = this.set(C.DATE, 1).set(C.M, this.$M + number) | ||
date = date.set(C.DATE, Math.min(this.$D, date.daysInMonth())) | ||
return date | ||
} | ||
if (['y', C.Y].indexOf(unit) > -1) { | ||
return this.set(C.Y, this.$y + number) | ||
} | ||
let step | ||
switch (string) { | ||
switch (unit) { | ||
case 'm': | ||
case 'minutes': | ||
step = Constant.MILLISECONDS_A_MINUTE | ||
case C.MIN: | ||
step = C.MILLISECONDS_A_MINUTE | ||
break | ||
case 'h': | ||
case 'hours': | ||
step = Constant.MILLISECONDS_A_HOUR | ||
case C.H: | ||
step = C.MILLISECONDS_A_HOUR | ||
break | ||
case 'd': | ||
case 'days': | ||
step = Constant.MILLISECONDS_A_DAY | ||
case C.D: | ||
step = C.MILLISECONDS_A_DAY | ||
break | ||
case 'w': | ||
case 'weeks': | ||
step = Constant.MILLISECONDS_A_WEEK | ||
case C.W: | ||
step = C.MILLISECONDS_A_WEEK | ||
break | ||
default: // s seconds | ||
step = Constant.MILLISECONDS_A_SECOND | ||
step = C.MILLISECONDS_A_SECOND | ||
} | ||
@@ -166,29 +194,29 @@ const nextTimeStamp = this.valueOf() + (number * step) | ||
case 'YY': | ||
return String(this.$year).slice(-2) | ||
return String(this.$y).slice(-2) | ||
case 'YYYY': | ||
return String(this.$year) | ||
return String(this.$y) | ||
case 'M': | ||
return String(this.$month + 1) | ||
return String(this.$M + 1) | ||
case 'MM': | ||
return Utils.padStart(String(this.$month + 1), 2, '0') | ||
return Utils.padStart(String(this.$M + 1), 2, '0') | ||
case 'D': | ||
return String(this.$day) | ||
return String(this.$D) | ||
case 'DD': | ||
return Utils.padStart(String(this.$day), 2, '0') | ||
return Utils.padStart(String(this.$D), 2, '0') | ||
case 'd': | ||
return String(this.$week) | ||
return String(this.$W) | ||
case 'dddd': | ||
return weeks[this.$week] | ||
return weeks[this.$W] | ||
case 'H': | ||
return String(this.$hour) | ||
return String(this.$H) | ||
case 'HH': | ||
return Utils.padStart(String(this.$hour), 2, '0') | ||
return Utils.padStart(String(this.$H), 2, '0') | ||
case 'm': | ||
return String(this.$minute) | ||
return String(this.$m) | ||
case 'mm': | ||
return Utils.padStart(String(this.$minute), 2, '0') | ||
return Utils.padStart(String(this.$m), 2, '0') | ||
case 's': | ||
return String(this.$second) | ||
return String(this.$s) | ||
case 'ss': | ||
return Utils.padStart(String(this.$second), 2, '0') | ||
return Utils.padStart(String(this.$s), 2, '0') | ||
case 'Z': | ||
@@ -202,23 +230,24 @@ return `${this.timeZoneString.slice(0, -2)}:00` | ||
diff(otherDate, unit, float = false) { | ||
const other = otherDate instanceof Dayjs ? otherDate : new Dayjs(otherDate) | ||
const diff = this - other | ||
let result = Utils.monthDiff(this, other) | ||
diff(input, units, float = false) { | ||
const unit = Utils.prettyUnit(units) | ||
const that = input instanceof Dayjs ? input : new Dayjs(input) | ||
const diff = this - that | ||
let result = Utils.monthDiff(this, that) | ||
switch (unit) { | ||
case 'years': | ||
case C.Y: | ||
result /= 12 | ||
break | ||
case 'months': | ||
case C.M: | ||
break | ||
case 'quarters': | ||
case C.Q: | ||
result /= 3 | ||
break | ||
case 'weeks': | ||
result = diff / Constant.MILLISECONDS_A_WEEK | ||
case C.W: | ||
result = diff / C.MILLISECONDS_A_WEEK | ||
break | ||
case 'days': | ||
result = diff / Constant.MILLISECONDS_A_DAY | ||
case C.D: | ||
result = diff / C.MILLISECONDS_A_DAY | ||
break | ||
case 'seconds': | ||
result = diff / Constant.MILLISECONDS_A_SECOND | ||
case C.S: | ||
result = diff / C.MILLISECONDS_A_SECOND | ||
break | ||
@@ -232,3 +261,3 @@ default: // milliseconds | ||
daysInMonth() { | ||
return this.endOf('month').date() | ||
return this.endOf(C.M).$D | ||
} | ||
@@ -241,3 +270,3 @@ | ||
toDate() { | ||
return new Date(this.$date) | ||
return new Date(this.$d) | ||
} | ||
@@ -247,9 +276,9 @@ | ||
return [ | ||
this.year(), | ||
this.month(), | ||
this.date(), | ||
this.hour(), | ||
this.minute(), | ||
this.second(), | ||
this.millisecond() | ||
this.$y, | ||
this.$M, | ||
this.$D, | ||
this.$H, | ||
this.$m, | ||
this.$s, | ||
this.$ms | ||
] | ||
@@ -268,9 +297,9 @@ } | ||
return { | ||
years: this.year(), | ||
months: this.month(), | ||
date: this.date(), | ||
hours: this.hour(), | ||
minutes: this.minute(), | ||
seconds: this.second(), | ||
milliseconds: this.millisecond() | ||
years: this.$y, | ||
months: this.$M, | ||
date: this.$D, | ||
hours: this.$H, | ||
minutes: this.$m, | ||
seconds: this.$s, | ||
milliseconds: this.$ms | ||
} | ||
@@ -280,3 +309,3 @@ } | ||
toString() { | ||
return this.$date.toUTCString() | ||
return this.$d.toUTCString() | ||
} | ||
@@ -283,0 +312,0 @@ } |
@@ -26,1 +26,2 @@ export const padStart = (string, length, pad) => { | ||
export const prettyUnit = u => (u && String(u).toLowerCase().replace(/s$/, '')) |
@@ -25,4 +25,21 @@ import moment from 'moment' | ||
it('Hour', () => { | ||
expect(dayjs().hour()).toBe(moment().hour()) | ||
}) | ||
it('Minute', () => { | ||
expect(dayjs().minute()).toBe(moment().minute()) | ||
}) | ||
it('Second', () => { | ||
expect(dayjs().second()).toBe(moment().second()) | ||
}) | ||
it('Millisecond', () => { | ||
expect(dayjs().millisecond()).toBe(moment().millisecond()) | ||
}) | ||
it('Set Unknown String', () => { | ||
expect(dayjs().set('Unknown String', 1).unix()) | ||
const newDate = dayjs().set('Unknown String', 1) | ||
expect(newDate.unix()) | ||
.toBe(moment().set('Unknown String', 1).unix()) | ||
@@ -32,5 +49,15 @@ }) | ||
it('Set Not Int', () => { | ||
expect(dayjs().set('year', 'Not Int').unix()) | ||
const newDate = dayjs().set('year', 'Not Int') | ||
expect(newDate.unix()) | ||
.toBe(moment().set('year', 'Not Int').unix()) | ||
}) | ||
it('Immutable Set', () => { | ||
const dayjsA = dayjs() | ||
const dayjsB = dayjsA.set('year', 2011) | ||
const momentA = moment() | ||
const momentB = momentA.set('year', 2011) | ||
expect(dayjsA.unix()).not.toBe(dayjsB.unix()) | ||
expect(momentA.unix()).toBe(momentB.unix()) | ||
}) | ||
@@ -13,4 +13,4 @@ import moment from 'moment' | ||
it('StartOf EndOf Year', () => { | ||
expect(dayjs().startOf('year').unix()).toBe(moment().startOf('year').unix()) | ||
it('StartOf EndOf Year with s and upper case', () => { | ||
expect(dayjs().startOf('YearS').unix()).toBe(moment().startOf('year').unix()) | ||
expect(dayjs().endOf('year').unix()).toBe(moment().endOf('year').unix()) | ||
@@ -46,2 +46,3 @@ }) | ||
expect(dayjs().add(1, 'M').unix()).toBe(moment().add(1, 'M').unix()) | ||
expect(dayjs().add(1, 'y').unix()).toBe(moment().add(1, 'y').unix()) | ||
expect(dayjs('20111031').add(1, 'months').unix()).toBe(moment('20111031').add(1, 'months').unix()) | ||
@@ -48,0 +49,0 @@ }) |
@@ -21,3 +21,3 @@ import moment from 'moment' | ||
it('String ISO 8601 date, time and zone ', () => { | ||
it('String ISO 8601 date, time and zone', () => { | ||
const time = '2018-04-04T16:00:00.000Z' | ||
@@ -27,5 +27,7 @@ expect(dayjs(time).unix()).toBe(moment(time).unix()) | ||
it('String Other', () => { | ||
it('String Other and isValid', () => { | ||
global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn | ||
expect(dayjs('otherString').toString().toLowerCase()).toBe(moment('otherString').toString().toLowerCase()) | ||
expect(dayjs().isValid()).toBe(true) | ||
expect(dayjs('otherString').isValid()).toBe(false) | ||
}) | ||
@@ -42,4 +44,3 @@ }) | ||
const year = base.year() | ||
const another = base.clone() | ||
another.set('year', year + 1) | ||
const another = base.set('year', year + 1) | ||
expect(another.unix() - base.unix()).toBe(31536000) | ||
@@ -51,5 +52,5 @@ }) | ||
const year = base.year() | ||
base.set('year', year + 1) | ||
const another = base.clone() | ||
expect(base.toString()).toBe(another.toString()) | ||
const newBase = base.set('year', year + 1) | ||
const another = newBase.clone() | ||
expect(newBase.toString()).toBe(another.toString()) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28466
16
632