Skip to content

Commit

Permalink
Merge pull request #952 from iamkun/dev
Browse files Browse the repository at this point in the history
D2M
  • Loading branch information
iamkun authored Jul 2, 2020
2 parents bc07156 + f779a08 commit 10276dc
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 19 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
English | [简体中文](./docs/zh-cn/README.zh-CN.md) | [日本語](./docs/ja/README-ja.md) | [Português Brasileiro](./docs/pt-br/README-pt-br.md) | [한국어](./docs/ko/README-ko.md) | [Español (España)](./docs/es-es/README-es-es.md) | [Русский](./docs/ru/README-ru.md)

<p align="center"><a href="#" target="_blank" rel="noopener noreferrer"><img width="550"
<p align="center"><a href="https://day.js.org/" target="_blank" rel="noopener noreferrer"><img width="550"
src="https://user-images.githubusercontent.com/17680888/39081119-3057bbe2-456e-11e8-862c-646133ad4b43.png"
alt="Day.js"></a></p>
<p align="center">Fast <b>2kB</b> alternative to Moment.js with the same modern API</p>
Expand Down Expand Up @@ -103,6 +103,9 @@ dayjs().format('Q Do k kk X x') // more available formats

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/dayjs#sponsor)]

<a href="https://tracking.gitads.io/?repo=dayjs" target="_blank" rel="noopener noreferrer">
<img src="https://images.gitads.io/dayjs" alt="sponsors"></a>

<a href="https://opencollective.com/dayjs/sponsor/0/website" target="_blank"><img src="https://opencollective.com/dayjs/sponsor/0/avatar.svg"></a>
<a href="https://opencollective.com/dayjs/sponsor/1/website" target="_blank"><img src="https://opencollective.com/dayjs/sponsor/1/avatar.svg"></a>
<a href="https://opencollective.com/dayjs/sponsor/2/website" target="_blank"><img src="https://opencollective.com/dayjs/sponsor/2/avatar.svg"></a>
Expand Down
16 changes: 8 additions & 8 deletions docs/ru/README-ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
</a>
</p>

> Day.js - это миниатюрная JavaScript библиотека, которая анализирует, валидирует, манипулирует, и отображает даты и время для современных браузеров, также обладает внушительной совместимостью с Moment.js API. Если вы используете Moment.js, тогда вы уже знаете как пользоваться Day.js.
> Day.js - это миниатюрная JavaScript библиотека, которая парсит, валидирует, управляет, и отображает даты и время для современных браузеров, обладающая большой совместимостью с Moment.js API. Если вы используете Moment.js, вы уже знаете как пользоваться Day.js.
```js
dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:mm:ss');
```

* 🕒 Хорошо знакомые API и паттерны (шаблоны) Moment.js
* 🕒 Хорошо знакомые API и паттерны Moment.js
* 💪 Неизменная
* 🔥 Цепная
* 🌐 Поддержка интернационализации (I18n)
* 📦 2kb мини-библиотека
* 👫 Поддержка всеми браузерами
* 👫 Поддерживающаяся всеми браузерами

---

Expand All @@ -52,16 +52,16 @@ npm install dayjs --save

### API

API Day.js легко использовать для анализа, проверки, воздействия, и отображения дат и времени.
API Day.js легко использовать для парсинга, валидации, управления, и отображения дат и времен.

```javascript
dayjs('2018-08-08') // parse
dayjs('2018-08-08') // парсинг

dayjs().format('{YYYY} MM-DDTHH:mm:ss SSS [Z] A') // отображение

dayjs().set('month', 3).month() // получение значения и его установка
dayjs().set('month', 3).month() // получение и установка

dayjs().add(1, 'year') // влияние
dayjs().add(1, 'year') // управление

dayjs().isBefore(dayjs()) // осведомление
```
Expand Down Expand Up @@ -121,4 +121,4 @@ dayjs().format('Q Do k kk X x') // больше доступных формат

## Лицензия

Day.js распростроняется под [лицензией MIT](./LICENSE).
Day.js распространяется под [лицензией MIT](./LICENSE).
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
],
"author": "iamkun",
"license": "MIT",
"homepage": "https://day.js.org/",
"homepage": "https://day.js.org",
"repository": {
"type": "git",
"url": "https://github.com/iamkun/dayjs.git"
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Duration {
const d = input.match(durationRegex)
if (d) {
[,,
this.$d.years, this.$d.months,,
this.$d.years, this.$d.months, this.$d.weeks,
this.$d.days, this.$d.hours, this.$d.minutes, this.$d.seconds] = d
this.calMilliseconds()
return this
Expand Down Expand Up @@ -83,7 +83,7 @@ class Duration {
toISOString() {
const Y = this.$d.years ? `${this.$d.years}Y` : ''
const M = this.$d.months ? `${this.$d.months}M` : ''
let days = this.$d.days || 0
let days = +this.$d.days || 0
if (this.$d.weeks) {
days += this.$d.weeks * 7
}
Expand Down
13 changes: 8 additions & 5 deletions src/plugin/localeData/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
export default (o, c, dayjs) => { // locale needed later
const proto = c.prototype
const getLocalePart = part => (part && (part.indexOf ? part : part.s))
const getShort = (ins, target, full, num) => {
const getShort = (ins, target, full, num, localeOrder) => {
const locale = ins.name ? ins : ins.$locale()
const targetLocale = getLocalePart(locale[target])
const fullLocale = getLocalePart(locale[full])
return targetLocale || fullLocale.map(f => f.substr(0, num))
const result = targetLocale || fullLocale.map(f => f.substr(0, num))
if (!localeOrder) return result
const { weekStart } = locale
return result.map((_, index) => (result[(index + (weekStart || 0)) % 7]))
}
const getDayjsLocaleObject = () => dayjs.Ls[dayjs.locale()]
const localeData = function () {
Expand Down Expand Up @@ -42,9 +45,9 @@ export default (o, c, dayjs) => { // locale needed later

dayjs.monthsShort = () => getShort(getDayjsLocaleObject(), 'monthsShort', 'months', 3)

dayjs.weekdays = () => getDayjsLocaleObject().weekdays
dayjs.weekdays = localeOrder => getShort(getDayjsLocaleObject(), 'weekdays', null, null, localeOrder)

dayjs.weekdaysShort = () => getShort(getDayjsLocaleObject(), 'weekdaysShort', 'weekdays', 3)
dayjs.weekdaysShort = localeOrder => getShort(getDayjsLocaleObject(), 'weekdaysShort', 'weekdays', 3, localeOrder)

dayjs.weekdaysMin = () => getShort(getDayjsLocaleObject(), 'weekdaysMin', 'weekdays', 2)
dayjs.weekdaysMin = localeOrder => getShort(getDayjsLocaleObject(), 'weekdaysMin', 'weekdays', 2, localeOrder)
}
6 changes: 6 additions & 0 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ describe('Parse ISO string', () => {
it('Part ISO string', () => {
expect(dayjs.duration('PT2777H46M40S').toISOString()).toBe('PT2777H46M40S')
})
it('ISO string with week', () => {
const d = dayjs.duration('P2M3W4D')
expect(d.toISOString()).toBe('P2M25D')
expect(d.asDays()).toBe(85) // moment 85, count 2M as 61 days
expect(d.asWeeks()).toBe(12.142857142857142) // moment 12.285714285714286
})
it('Invalid ISO string', () => {
expect(dayjs.duration('Invalid').toISOString()).toBe('P0D')
})
Expand Down
12 changes: 12 additions & 0 deletions test/plugin/localeData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,15 @@ it('Month function', () => {
expect(dayjs.months()).toEqual(moment.months())
expect(dayjs.monthsShort()).toEqual(moment.monthsShort())
})

it('Locale order', () => {
dayjs.locale('fr')
moment.locale('fr')
expect(dayjs.weekdays(true)).toEqual(moment.weekdays(true))
expect(dayjs.weekdaysShort(true)).toEqual(moment.weekdaysShort(true))
expect(dayjs.weekdaysMin(true)).toEqual(moment.weekdaysMin(true))
expect(dayjs.weekdays()).not.toEqual(dayjs.weekdays(true))
dayjs.locale('en')
moment.locale('en')
expect(dayjs.weekdays(true)).toEqual(moment.weekdays(true))
})
9 changes: 7 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
/// <reference path="./locale/index.d.ts" />

export = dayjs;
declare function dayjs (date?: dayjs.ConfigType, option?: dayjs.OptionType, locale?: string): dayjs.Dayjs

declare function dayjs (date?: dayjs.ConfigType): dayjs.Dayjs

declare function dayjs (date?: dayjs.ConfigType, format?: dayjs.OptionType, strict?: boolean): dayjs.Dayjs

declare function dayjs (date?: dayjs.ConfigType, format?: dayjs.OptionType, locale?: string, strict?: boolean): dayjs.Dayjs

declare namespace dayjs {
export type ConfigType = string | number | Date | Dayjs

export type OptionType = { locale?: string, format?: string, utc?: boolean } | string
export type OptionType = { locale?: string, format?: string, utc?: boolean } | string | string[]

type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms'
export type UnitType = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year' | 'date' | UnitTypeShort;
Expand Down

0 comments on commit 10276dc

Please sign in to comment.