Skip to content

Commit

Permalink
Merge pull request #1631 from iamkun/dev
Browse files Browse the repository at this point in the history
D2M
  • Loading branch information
iamkun authored Sep 10, 2021
2 parents 42e494b + b5a1391 commit 417233b
Show file tree
Hide file tree
Showing 21 changed files with 126 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ English | [简体中文](./docs/zh-cn/README.zh-CN.md) | [日本語](./docs/ja/R
<p align="center">Fast <b>2kB</b> alternative to Moment.js with the same modern API</p>
<p align="center">
<a href="https://unpkg.com/dayjs/dayjs.min.js"><img
src="http://img.badgesize.io/https://unpkg.com/dayjs/dayjs.min.js?compression=gzip&style=flat-square"
src="https://img.badgesize.io/https://unpkg.com/dayjs/dayjs.min.js?compression=gzip&style=flat-square"
alt="Gzip Size"></a>
<a href="https://www.npmjs.com/package/dayjs"><img src="https://img.shields.io/npm/v/dayjs.svg?style=flat-square&colorB=51C838"
alt="NPM Version"></a>
Expand Down
2 changes: 1 addition & 1 deletion docs/es-es/README-es-es.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<br>
<p align="center">
<a href="https://unpkg.com/dayjs/dayjs.min.js"><img
src="http://img.badgesize.io/https://unpkg.com/dayjs/dayjs.min.js?compression=gzip&style=flat-square"
src="https://img.badgesize.io/https://unpkg.com/dayjs/dayjs.min.js?compression=gzip&style=flat-square"
alt="Gzip Size"></a>
<a href="https://www.npmjs.com/package/dayjs"><img src="https://img.shields.io/npm/v/dayjs.svg?style=flat-square&colorB=51C838"
alt="NPM Version"></a>
Expand Down
2 changes: 1 addition & 1 deletion docs/ja/README-ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<br>
<p align="center">
<a href="https://unpkg.com/dayjs/dayjs.min.js"><img
src="http://img.badgesize.io/https://unpkg.com/dayjs/dayjs.min.js?compression=gzip&style=flat-square"
src="https://img.badgesize.io/https://unpkg.com/dayjs/dayjs.min.js?compression=gzip&style=flat-square"
alt="Gzip Size"></a>
<a href="https://www.npmjs.com/package/dayjs"><img src="https://img.shields.io/npm/v/dayjs.svg?style=flat-square"
alt="NPM Version"></a>
Expand Down
6 changes: 3 additions & 3 deletions docs/ko/README-ko.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<br>
<p align="center">
<a href="https://unpkg.com/dayjs/dayjs.min.js"><img
src="http://img.badgesize.io/https://unpkg.com/dayjs/dayjs.min.js?compression=gzip&style=flat-square"
src="https://img.badgesize.io/https://unpkg.com/dayjs/dayjs.min.js?compression=gzip&style=flat-square"
alt="Gzip Size"></a>
<a href="https://www.npmjs.com/package/dayjs"><img src="https://img.shields.io/npm/v/dayjs.svg?style=flat-square"
alt="NPM Version"></a>
Expand Down Expand Up @@ -42,9 +42,9 @@ dayjs()

## 시작해볼까요!

### Documentation
### 문서

You can find for more details, API, and other docs on [day.js.org](https://day.js.org/) website.
더 많은 세부 사항과 API, 그리고 다른 문서를 [day.js.org](https://day.js.org/) 웹사이트에서 볼 수 있습니다.

### 설치

Expand Down
2 changes: 1 addition & 1 deletion docs/pt-br/README-pt-br.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<br>
<p align="center">
<a href="https://unpkg.com/dayjs/dayjs.min.js"><img
src="http://img.badgesize.io/https://unpkg.com/dayjs/dayjs.min.js?compression=gzip&style=flat-square"
src="https://img.badgesize.io/https://unpkg.com/dayjs/dayjs.min.js?compression=gzip&style=flat-square"
alt="Gzip Size"></a>
<a href="https://www.npmjs.com/package/dayjs"><img src="https://img.shields.io/npm/v/dayjs.svg?style=flat-square"
alt="NPM Version"></a>
Expand Down
2 changes: 1 addition & 1 deletion docs/ru/README-ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<br>
<p align="center">
<a href="https://unpkg.com/dayjs/dayjs.min.js"><img
src="http://img.badgesize.io/https://unpkg.com/dayjs/dayjs.min.js?compression=gzip&style=flat-square"
src="https://img.badgesize.io/https://unpkg.com/dayjs/dayjs.min.js?compression=gzip&style=flat-square"
alt="Gzip Size"></a>
<a href="https://www.npmjs.com/package/dayjs"><img src="https://img.shields.io/npm/v/dayjs.svg?style=flat-square&colorB=51C838"
alt="NPM Version"></a>
Expand Down
2 changes: 1 addition & 1 deletion docs/zh-cn/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<br>
<p align="center">
<a href="https://unpkg.com/dayjs/dayjs.min.js"><img
src="http://img.badgesize.io/https://unpkg.com/dayjs/dayjs.min.js?compression=gzip&style=flat-square"
src="https://img.badgesize.io/https://unpkg.com/dayjs/dayjs.min.js?compression=gzip&style=flat-square"
alt="Gzip Size"></a>
<a href="https://www.npmjs.com/package/dayjs"><img src="https://img.shields.io/npm/v/dayjs.svg?style=flat-square"
alt="NPM Version"></a>
Expand Down
41 changes: 41 additions & 0 deletions src/locale/ar-iq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Arabic (Iraq) [ar-iq]
import dayjs from 'dayjs'

const locale = {
name: 'ar-iq',
weekdays: 'الأحد_الإثنين_الثلاثاء_الأربعاء_الخميس_الجمعة_السبت'.split('_'),
months: 'كانون الثاني_شباط_آذار_نيسان_أيار_حزيران_تموز_آب_أيلول_تشرين الأول_ تشرين الثاني_كانون الأول'.split('_'),
weekStart: 1,
weekdaysShort: 'أحد_إثنين_ثلاثاء_أربعاء_خميس_جمعة_سبت'.split('_'),
monthsShort: 'كانون الثاني_شباط_آذار_نيسان_أيار_حزيران_تموز_آب_أيلول_تشرين الأول_ تشرين الثاني_كانون الأول'.split('_'),
weekdaysMin: 'ح_ن_ث_ر_خ_ج_س'.split('_'),
ordinal: n => n,
formats: {
LT: 'HH:mm',
LTS: 'HH:mm:ss',
L: 'DD/MM/YYYY',
LL: 'D MMMM YYYY',
LLL: 'D MMMM YYYY HH:mm',
LLLL: 'dddd D MMMM YYYY HH:mm'
},
meridiem: hour => (hour > 12 ? 'ص' : 'م'),
relativeTime: {
future: 'في %s',
past: 'منذ %s',
s: 'ثوان',
m: 'دقيقة',
mm: '%d دقائق',
h: 'ساعة',
hh: '%d ساعات',
d: 'يوم',
dd: '%d أيام',
M: 'شهر',
MM: '%d أشهر',
y: 'سنة',
yy: '%d سنوات'
}
}

dayjs.locale(locale, null, true)

export default locale
40 changes: 40 additions & 0 deletions src/locale/es-mx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Spanish (Mexico) [es-mx]
import dayjs from 'dayjs'

const locale = {
name: 'es-mx',
weekdays: 'domingo_lunes_martes_miércoles_jueves_viernes_sábado'.split('_'),
weekdaysShort: 'dom._lun._mar._mié._jue._vie._sáb.'.split('_'),
weekdaysMin: 'do_lu_ma_mi_ju_vi_sá'.split('_'),
months: 'enero_febrero_marzo_abril_mayo_junio_julio_agosto_septiembre_octubre_noviembre_diciembre'.split('_'),
monthsShort: 'ene_feb_mar_abr_may_jun_jul_ago_sep_oct_nov_dic'.split('_'),
relativeTime: {
future: 'en %s',
past: 'hace %s',
s: 'unos segundos',
m: 'un minuto',
mm: '%d minutos',
h: 'una hora',
hh: '%d horas',
d: 'un día',
dd: '%d días',
M: 'un mes',
MM: '%d meses',
y: 'un año',
yy: '%d años'
},
ordinal: n => `${n}º`,
formats: {
LT: 'H:mm',
LTS: 'H:mm:ss',
L: 'DD/MM/YYYY',
LL: 'D [de] MMMM [de] YYYY',
LLL: 'D [de] MMMM [de] YYYY H:mm',
LLLL: 'dddd, D [de] MMMM [de] YYYY H:mm'
}
}

dayjs.locale(locale, null, true)

export default locale

2 changes: 1 addition & 1 deletion src/locale/lt.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const locale = {
hh: '%d valandas',
d: 'dieną',
dd: '%d dienas',
M: 'menesį',
M: 'mėnesį',
MM: '%d mėnesius',
y: 'metus',
yy: '%d metus'
Expand Down
1 change: 1 addition & 0 deletions src/locale/nb.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const locale = {
monthsShort: 'jan._feb._mars_april_mai_juni_juli_aug._sep._okt._nov._des.'.split('_'),
ordinal: n => `${n}.`,
weekStart: 1,
yearStart: 4,
formats: {
LT: 'HH:mm',
LTS: 'HH:mm:ss',
Expand Down
8 changes: 4 additions & 4 deletions src/locale/si.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import dayjs from 'dayjs'
const locale = {
name: 'si',
weekdays: 'ඉරිදා_සඳුදා_අඟහරුවාදා_බදාදා_බ්‍රහස්පතින්දා_සිකුරාදා_සෙනසුරාදා'.split('_'),
months: 'ජනවාරි_පෙබරවාරි_මාර්තු_අප්‍රේල්_මැයි_ජූනි_ජූලි_අගෝස්තු_සැප්තැම්බර්_ඔක්තෝබර්_නොවැම්බර්_දෙසැම්බර්'.split('_'),
months: 'දුරුතු_නවම්_මැදින්_බක්_වෙසක්_පොසොන්_ඇසළ_නිකිණි_බිනර_වප්_ඉල්_උඳුවප්'.split('_'),
weekdaysShort: 'ඉරි_සඳු_අඟ_බදා_බ්‍රහ_සිකු_සෙන'.split('_'),
monthsShort: 'ජන_පෙබ_මාර්_අප්_මැයි_ජූනි_ජූලි_අගෝ_සැප්_ඔක්_නොවැ_දෙසැ'.split('_'),
monthsShort: 'දුරු_නව_මැදි_බක්_වෙස_පොසො_ඇස_නිකි_බින_වප්_ඉල්_උඳු'.split('_'),
weekdaysMin: 'ඉ_ස_අ_බ_බ්‍ර_සි_සෙ'.split('_'),
ordinal: n => n,
formats: {
Expand All @@ -21,8 +21,8 @@ const locale = {
future: '%sකින්',
past: '%sකට පෙර',
s: 'තත්පර කිහිපය',
m: 'මිනිත්තුව',
mm: 'මිනිත්තු %d',
m: 'විනාඩිය',
mm: 'විනාඩි %d',
h: 'පැය',
hh: 'පැය %d',
d: 'දිනය',
Expand Down
5 changes: 5 additions & 0 deletions src/plugin/advancedFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export default (o, c, d) => { // locale needed later
// extend en locale here
proto.format = function (formatStr) {
const locale = this.$locale()

if (!this.isValid()) {
return oldFormat.bind(this)(formatStr)
}

const utils = this.$utils()
const str = formatStr || FORMAT_DEFAULT
const result = str.replace(/\[([^\]]+)]|Q|wo|ww|w|WW|W|zzz|z|gggg|GGGG|Do|X|x|k{1,2}|S/g, (match) => {
Expand Down
5 changes: 4 additions & 1 deletion src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ export default (o, C, d) => {
this.$d = parseFormattedInput(date, format, utc)
this.init()
if (pl && pl !== true) this.$L = this.locale(pl).$L
if (isStrict && date !== this.format(format)) {
// use != to treat
// input number 1410715640579 and format string '1410715640579' equal
// eslint-disable-next-line eqeqeq
if (isStrict && date != this.format(format)) {
this.$d = new Date('')
}
// reset global locale to make parallel unit test
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Duration {
const d = input.match(durationRegex)
if (d) {
const properties = d.slice(2)
const numberD = properties.map(value => Number(value));
const numberD = properties.map(value => (value != null ? Number(value) : 0));
[
this.$d.years,
this.$d.months,
Expand Down
4 changes: 4 additions & 0 deletions test/plugin/advancedFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ afterEach(() => {
MockDate.reset()
})

it('Format of invalid date', () => {
expect(dayjs(null).format('z').toLowerCase()).toEqual(moment(null).format('z').toLowerCase())
})

it('Format empty string', () => {
expect(dayjs().format()).toBe(moment().format())
})
Expand Down
5 changes: 5 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../../src/locale/ru'
import uk from '../../src/locale/uk'
import '../../src/locale/zh-cn'
import customParseFormat from '../../src/plugin/customParseFormat'
import advancedFormat from '../../src/plugin/advancedFormat'
import localizedFormats from '../../src/plugin/localizedFormat'

dayjs.extend(customParseFormat)
Expand Down Expand Up @@ -376,4 +377,8 @@ it('parse X x', () => {
const input2 = '1410715640579'
const format2 = 'x'
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())

// x X starct parse requires advancedFormat plugin
dayjs.extend(advancedFormat)
expect(dayjs(input2, format2, true).valueOf()).toBe(moment(input2, format2, true).valueOf())
})
3 changes: 3 additions & 0 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ describe('Parse ISO string', () => {
it('Part ISO string', () => {
expect(dayjs.duration('PT2777H46M40S').toISOString()).toBe('PT2777H46M40S')
})
it('Formatting missing components', () => {
expect(dayjs.duration('PT1H').format('YYYY-MM-DDTHH:mm:ss')).toBe('0000-00-00T01:00:00')
})
it('ISO string with week', () => {
const d = dayjs.duration('P2M3W4D')
expect(d.toISOString()).toBe('P2M25D')
Expand Down
10 changes: 6 additions & 4 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ declare namespace dayjs {

export type ConfigType = ConfigTypeMap[keyof ConfigTypeMap]

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

export type OptionType = FormatObject | string | string[]

export type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms'

Expand All @@ -27,7 +29,7 @@ declare namespace dayjs {

export type OpUnitType = UnitType | "week" | "weeks" | 'w';
export type QUnitType = UnitType | "quarter" | "quarters" | 'Q';

export type ManipulateType = Omit<OpUnitType, 'date' | 'dates'>;
class Dayjs {
constructor (config?: ConfigType)
/**
Expand Down Expand Up @@ -235,7 +237,7 @@ declare namespace dayjs {
*
* Docs: https://day.js.org/docs/en/manipulate/add
*/
add(value: number, unit?: OpUnitType): Dayjs
add(value: number, unit?: ManipulateType): Dayjs
/**
* Returns a cloned Day.js object with a specified amount of time subtracted.
* ```
Expand All @@ -245,7 +247,7 @@ declare namespace dayjs {
*
* Docs: https://day.js.org/docs/en/manipulate/subtract
*/
subtract(value: number, unit?: OpUnitType): Dayjs
subtract(value: number, unit?: ManipulateType): Dayjs
/**
* Returns a cloned Day.js object and set it to the start of a unit of time.
* ```
Expand Down
2 changes: 1 addition & 1 deletion types/plugin/duration.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PluginFunc } from 'dayjs'
import { OpUnitType, UnitTypeLongPlural } from "../index";
import { OpUnitType, UnitTypeLongPlural } from 'dayjs';

declare const plugin: PluginFunc
export as namespace plugin;
Expand Down
2 changes: 1 addition & 1 deletion types/plugin/utc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ declare module 'dayjs' {

isUTC(): boolean

utcOffset(offset: number, keepLocalTime?: boolean): Dayjs
utcOffset(offset: number | string, keepLocalTime?: boolean): Dayjs
}

export function utc(config?: ConfigType, format?: string, strict?: boolean): Dayjs
Expand Down

0 comments on commit 417233b

Please sign in to comment.