Comparing version 1.3.0 to 1.4.0
{ | ||
"name": "dayjs", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -0,1 +1,8 @@ | ||
import * as Constant from './constant' | ||
const padStart = (string, length, pad) => { | ||
if (!string || string.length >= length) return string | ||
return `${Array((length + 1) - string.length).join(pad)}${string}` | ||
} | ||
class Dayjs { | ||
@@ -6,2 +13,11 @@ constructor(config) { | ||
this.date = new Date(args) | ||
this.timeZone = this.date.getTimezoneOffset() / 60 | ||
this.timeZoneString = padStart(String(this.timeZone * -1).replace(/^(.)?(\d)/, '$10$200'), 5, '+') | ||
this.mYear = this.date.getFullYear() | ||
this.mMonth = this.date.getMonth() | ||
this.mDay = this.date.getDate() | ||
this.mWeek = this.date.getDay() | ||
this.mHour = this.date.getHours() | ||
this.mMinute = this.date.getMinutes() | ||
this.mSecond = this.date.getSeconds() | ||
} | ||
@@ -23,11 +39,12 @@ | ||
year() { | ||
return this.date.getFullYear() | ||
return this.mYear | ||
} | ||
month() { | ||
return this.date.getMonth() | ||
return this.mMonth | ||
} | ||
unix() { | ||
const zonePad = this.utc ? this.date.getTimezoneOffset() * 60 * 1000 : 0 | ||
// timezone(hour) * 60 * 60 * 1000 => ms | ||
const zonePad = !this.utc ? 0 : this.timeZone * 60 * 60 * 1000 | ||
return Math.floor((this.date.getTime() + zonePad) / 1000) | ||
@@ -50,2 +67,75 @@ } | ||
} | ||
add(number, string) { | ||
let step | ||
switch (string) { | ||
case 'm': | ||
case 'minutes': | ||
step = Constant.SECONDS_A_MINUTE | ||
break | ||
case 'h': | ||
case 'hours': | ||
step = Constant.SECONDS_A_HOUR | ||
break | ||
case 'd': | ||
case 'days': | ||
step = Constant.SECONDS_A_DAY | ||
break | ||
case 'w': | ||
case 'weeks': | ||
step = Constant.SECONDS_A_WEEK | ||
break | ||
default: // s seconds | ||
step = 1 | ||
} | ||
const nextTimeStamp = this.unix() + (number * step) | ||
return new Dayjs(nextTimeStamp * 1000) | ||
} | ||
subtract(number, string) { | ||
return this.add(number * -1, string) | ||
} | ||
format(formatStr = 'YYYY-MM-DDTHH:mm:ssZ') { | ||
const weeks = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] | ||
return formatStr.replace(/Y{2,4}|M{1,2}|D{1,2}|d{1,4}|H{1,2}|m{1,2}|s{1,2}|Z{1,2}/g, (match) => { | ||
switch (match) { | ||
case 'YY': | ||
return String(this.mYear).slice(-2) | ||
case 'YYYY': | ||
return String(this.mYear) | ||
case 'M': | ||
return String(this.mMonth + 1) | ||
case 'MM': | ||
return padStart(String(this.mMonth + 1), 2, '0') | ||
case 'D': | ||
return String(this.mDay) | ||
case 'DD': | ||
return padStart(String(this.mDay), 2, '0') | ||
case 'd': | ||
return String(this.mWeek) | ||
case 'dddd': | ||
return weeks[this.mWeek] | ||
case 'H': | ||
return String(this.mHour) | ||
case 'HH': | ||
return padStart(String(this.mHour), 2, '0') | ||
case 'm': | ||
return String(this.mMinute) | ||
case 'mm': | ||
return padStart(String(this.mMinute), 2, '0') | ||
case 's': | ||
return String(this.mSecond) | ||
case 'ss': | ||
return padStart(String(this.mSecond), 2, '0') | ||
case 'Z': | ||
return `${this.timeZoneString.slice(0, -2)}:00` | ||
case 'ZZ': | ||
return this.timeZoneString | ||
default: | ||
return match | ||
} | ||
}) | ||
} | ||
} | ||
@@ -52,0 +142,0 @@ |
@@ -16,1 +16,17 @@ import moment from 'moment' | ||
test('Add Time days', () => { | ||
expect(dayjs().add(1, 's').unix()).toBe(moment().add(1, 's').unix()) | ||
expect(dayjs().add(1, 'seconds').unix()).toBe(moment().add(1, 'seconds').unix()) | ||
expect(dayjs().add(1, 'm').unix()).toBe(moment().add(1, 'm').unix()) | ||
expect(dayjs().add(1, 'minutes').unix()).toBe(moment().add(1, 'minutes').unix()) | ||
expect(dayjs().add(1, 'h').unix()).toBe(moment().add(1, 'h').unix()) | ||
expect(dayjs().add(1, 'hours').unix()).toBe(moment().add(1, 'hours').unix()) | ||
expect(dayjs().add(1, 'w').unix()).toBe(moment().add(1, 'w').unix()) | ||
expect(dayjs().add(1, 'weeks').unix()).toBe(moment().add(1, 'weeks').unix()) | ||
expect(dayjs().add(1, 'd').unix()).toBe(moment().add(1, 'd').unix()) | ||
expect(dayjs().add(1, 'days').unix()).toBe(moment().add(1, 'days').unix()) | ||
}) | ||
test('Subtract Time days', () => { | ||
expect(dayjs().subtract(1, 'days').unix()).toBe(moment().subtract(1, 'days').unix()) | ||
}) |
@@ -12,2 +12,7 @@ import moment from 'moment' | ||
test('String timestamp 1523520536000 ms', () => { | ||
const timestamp = 1523520536000 | ||
expect(dayjs(timestamp).unix()).toBe(moment(timestamp).unix()) | ||
}) | ||
test('String Other', () => { | ||
@@ -14,0 +19,0 @@ global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn |
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
10747
12
224