Skip to content

Commit

Permalink
Merge pull request #1164 from iamkun/dev
Browse files Browse the repository at this point in the history
D2M
  • Loading branch information
allmoviestvshowslistsfilmography28 committed Oct 23, 2020
2 parents 6f60f20 + fa3d829 commit 5e724e0
Show file tree
Hide file tree
Showing 17 changed files with 529 additions and 75 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,5 +401,5 @@ dayjs.unix = timestamp => (

dayjs.en = Ls[L]
dayjs.Ls = Ls

dayjs.p = {}
export default dayjs
6 changes: 5 additions & 1 deletion src/locale/en-nz.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ const locale = {
weekdaysShort: 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_'),
monthsShort: 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_'),
weekdaysMin: 'Su_Mo_Tu_We_Th_Fr_Sa'.split('_'),
ordinal: n => n,
ordinal: (n) => {
const s = ['th', 'st', 'nd', 'rd']
const v = n % 100
return `[${n}${s[(v - 20) % 10] || s[v] || s[0]}]`
},
formats: {
LT: 'h:mm A',
LTS: 'h:mm:ss A',
Expand Down
23 changes: 11 additions & 12 deletions src/locale/hu.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ const locale = {
relativeTime: {
future: '%s múlva',
past: '%s',
s: 'néhány másodperc',
m: 'egy perc',
mm: '%d perc',
h: 'egy óra',
hh: '%d óra',
d: 'egy nap',
dd: '%d nap',
M: 'egy hónap',
MM: '%d hónap',
y: 'egy éve',
yy: '%d év'
s: (_, s, ___, isFuture) => `néhány másodperc${isFuture || s ? '' : 'e'}`,
m: (_, s, ___, isFuture) => `egy perc${isFuture || s ? '' : 'e'}`,
mm: (n, s, ___, isFuture) => `${n} perc${isFuture || s ? '' : 'e'}`,
h: (_, s, ___, isFuture) => `egy ${isFuture || s ? 'óra' : 'órája'}`,
hh: (n, s, ___, isFuture) => `${n} ${isFuture || s ? 'óra' : 'órája'}`,
d: (_, s, ___, isFuture) => `egy ${isFuture || s ? 'nap' : 'napja'}`,
dd: (n, s, ___, isFuture) => `${n} ${isFuture || s ? 'nap' : 'napja'}`,
M: (_, s, ___, isFuture) => `egy ${isFuture || s ? 'hónap' : 'hónapja'}`,
MM: (n, s, ___, isFuture) => `${n} ${isFuture || s ? 'hónap' : 'hónapja'}`,
y: (_, s, ___, isFuture) => `egy ${isFuture || s ? 'év' : 'éve'}`,
yy: (n, s, ___, isFuture) => `${n} ${isFuture || s ? 'év' : 'éve'}`
},
formats: {
LT: 'H:mm',
Expand All @@ -38,4 +38,3 @@ const locale = {
dayjs.locale(locale, null, true)

export default locale

1 change: 1 addition & 0 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ const parseFormattedInput = (input, format, utc) => {


export default (o, C, d) => {
d.p.customParseFormat = true
const proto = C.prototype
const oldParse = proto.parse
proto.parse = function (cfg) {
Expand Down
18 changes: 18 additions & 0 deletions src/plugin/devHelper/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable no-console */
export default (o, c, d) => {
if (process.env.NODE_ENV !== 'production') {
const proto = c.prototype
const oldParse = proto.parse
proto.parse = function (cfg) {
const { date } = cfg
if (typeof date === 'string' && date.length === 13) {
console.warn(`To parse a Unix timestamp like ${date}, you should pass it as a Number. https://day.js.org/docs/en/parse/unix-timestamp-milliseconds`)
}
if (cfg.args.length >= 2 && !d.p.customParseFormat) {
console.warn(`To parse a date-time string like ${date} using the given format, you should enable customParseFormat plugin first. https://day.js.org/docs/en/parse/string-format`)
}
return oldParse.bind(this)(cfg)
}
}
}

14 changes: 8 additions & 6 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,13 @@ export default (option, Dayjs, dayjs) => {
dayjs.isDuration = isDuration

const oldAdd = Dayjs.prototype.add
Dayjs.prototype.add = function (addition, units) {
if (isDuration(addition)) {
addition = addition.asMilliseconds()
units = 'ms'
}
return oldAdd.bind(this)(addition, units)
const oldSubtract = Dayjs.prototype.subtract
Dayjs.prototype.add = function (value, unit) {
if (isDuration(value)) value = value.asMilliseconds()
return oldAdd.bind(this)(value, unit)
}
Dayjs.prototype.subtract = function (value, unit) {
if (isDuration(value)) value = value.asMilliseconds()
return oldSubtract.bind(this)(value, unit)
}
}
4 changes: 2 additions & 2 deletions src/plugin/minMax/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default (o, c, d) => {
const sortBy = (method, dates) => {
if (!dates.length) {
return d()
if (!dates || !dates.length || !dates[0] || (dates.length === 1 && !dates[0].length)) {
return null
}
if (dates.length === 1 && dates[0].length > 0) {
[dates] = dates
Expand Down
19 changes: 13 additions & 6 deletions src/plugin/timezone/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { MIN, MS } from '../../constant'

const typeToPos = {
year: 0,
month: 1,
Expand All @@ -7,8 +9,6 @@ const typeToPos = {
second: 5
}

const ms = 'ms'

// Cache time-zone lookups from Intl.DateTimeFormat,
// as it is a *very* slow method.
const dtfCache = {}
Expand Down Expand Up @@ -94,10 +94,15 @@ export default (o, c, d) => {

const proto = c.prototype

proto.tz = function (timezone = defaultTimezone) {
proto.tz = function (timezone = defaultTimezone, keepLocalTime) {
const oldOffset = this.utcOffset()
const target = this.toDate().toLocaleString('en-US', { timeZone: timezone })
const diff = Math.round((this.toDate() - new Date(target)) / 1000 / 60)
const ins = d(target).$set(ms, this.$ms).utcOffset(localUtcOffset - diff, true)
let ins = d(target).$set(MS, this.$ms).utcOffset(localUtcOffset - diff, true)
if (keepLocalTime) {
const newOffset = ins.utcOffset()
ins = ins.add(oldOffset - newOffset, MIN)
}
ins.$x.$timezone = timezone
return ins
}
Expand All @@ -109,14 +114,16 @@ export default (o, c, d) => {
return result && result.value
}

d.tz = function (input, timezone = defaultTimezone) {
d.tz = function (input, arg1, arg2) {
const parseFormat = arg2 && arg1
const timezone = arg2 || arg1 || defaultTimezone
const previousOffset = tzOffset(+d(), timezone)
let localTs
if (typeof input !== 'string') {
// timestamp number || js Date || Day.js
localTs = d(input) + (previousOffset * 60 * 1000)
}
localTs = localTs || d.utc(input).valueOf()
localTs = localTs || d.utc(input, parseFormat).valueOf()
const [targetTs, targetOffset] = fixOffset(localTs, previousOffset, timezone)
const ins = d(targetTs).utcOffset(targetOffset)
ins.$x.$timezone = timezone
Expand Down
45 changes: 45 additions & 0 deletions test/locale/hu.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import moment from 'moment'
import dayjs from '../../src'
import relativeTime from '../../src/plugin/relativeTime'
import '../../src/locale/hu'

dayjs.extend(relativeTime)

it('RelativeTime: Time from X', () => {
const T = [
[44.4, 'second'], // a few seconds
[89.5, 'second'], // a minute
[2, 'minute'], // 2 minutes
[43, 'minute'], // 43 minutes
[45, 'minute'], // an hour
[3, 'hour'], // 3 hours
[21, 'hour'], // 21 hours
[1, 'day'], // a day
[3, 'day'], // 3 day
[25, 'day'], // 25 days
[1, 'month'], // a month
[2, 'month'], // 2 month
[10, 'month'], // 10 month
[1, 'year'], // a year
[2, 'year'], // 2 year
[5, 'year'], // 5 year
[18, 'month'] // 2 years
]

T.forEach((t) => {
dayjs.locale('hu')
moment.locale('hu')

const dayjsDay = dayjs()
const momentDay = moment()

const dayjsCompare = dayjs().add(t[0], t[1])
const momentCompare = moment().add(t[0], t[1])

expect(dayjsDay.from(dayjsCompare)).toBe(momentDay.from(momentCompare))

expect(dayjsDay.to(dayjsCompare)).toBe(momentDay.to(momentCompare))

expect(dayjsDay.from(dayjsCompare, true)).toBe(momentDay.from(momentCompare, true))
})
})
7 changes: 6 additions & 1 deletion test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('Add', () => {
expect(a.add({ days: 5 }).days()).toBe(6)
})

describe('Add duration', () => {
test('Add duration', () => {
const a = dayjs('2020-10-01')
const days = dayjs.duration(2, 'days')
expect(a.add(days).format('YYYY-MM-DD')).toBe('2020-10-03')
Expand All @@ -151,6 +151,11 @@ describe('Subtract', () => {
expect(a.subtract(b).days()).toBe(1)
})

test('Subtract duration', () => {
const a = dayjs('2020-10-20')
const days = dayjs.duration(2, 'days')
expect(a.subtract(days).format('YYYY-MM-DD')).toBe('2020-10-18')
})

describe('Seconds', () => {
expect(dayjs.duration(500).seconds()).toBe(0)
Expand Down
19 changes: 15 additions & 4 deletions test/plugin/minMax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@ const arg3 = dayjs('2017-01-01')
const arg4 = dayjs('Invalid Date')

it('Return current time if no argument', () => {
expect(dayjs.max().format())
.toBe(dayjs().format())
expect(dayjs.min().format())
.toBe(dayjs().format())
expect(dayjs.max())
.toBe(null)
expect(dayjs.min())
.toBe(null)
expect(dayjs.max(null))
.toBe(null)
expect(dayjs.min(null))
.toBe(null)
})

it('Return current time if passing empty array', () => {
expect(dayjs.max([]))
.toBe(null)
expect(dayjs.min([]))
.toBe(null)
})

it('Compare between arguments', () => {
Expand Down
23 changes: 22 additions & 1 deletion test/plugin/timezone.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import MockDate from 'mockdate'
import moment from 'moment-timezone'
import dayjs from '../../src'
import utc from '../../src/plugin/utc'
import timezone from '../../src/plugin/timezone'
import customParseFormat from '../../src/plugin/customParseFormat'
import utc from '../../src/plugin/utc'

dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(customParseFormat)

beforeEach(() => {
MockDate.set(new Date())
Expand All @@ -17,6 +19,7 @@ afterEach(() => {

const NY = 'America/New_York'
const VAN = 'America/Vancouver'
const DEN = 'America/Denver'
const TOKYO = 'Asia/Tokyo'

describe('Guess', () => {
Expand Down Expand Up @@ -260,6 +263,14 @@ describe('set Default', () => {
})
})

describe('keepLocalTime', () => {
const base = dayjs.tz('2013-11-18 11:55', 'America/Toronto')
it('keepLocalTime', () => {
expect(base.tz('Europe/Berlin').format()).toBe('2013-11-18T17:55:00+01:00')
expect(base.tz('Europe/Berlin', true).format()).toBe('2013-11-18T11:55:00+01:00')
})
})

describe('Get offsetName', () => {
const dtz = dayjs.tz('2012-03-11 01:59:59', NY)
it('short', () => {
Expand All @@ -273,3 +284,13 @@ describe('Get offsetName', () => {
expect(d).toBe('Eastern Standard Time')
})
})

describe('CustomPraseFormat', () => {
const result = 1602786600
it('normal', () => {
expect(dayjs.tz('2020/10/15 12:30', DEN).unix()).toBe(result)
})
it('custom', () => {
expect(dayjs.tz('10/15/2020 12:30', 'MM/DD/YYYY HH:mm', DEN).unix()).toBe(result)
})
})
Loading

0 comments on commit 5e724e0

Please sign in to comment.