Skip to content

Commit

Permalink
fix: Update advancedFormat plugin to support w ww wo week tokens … (#678
Browse files Browse the repository at this point in the history
)

* fix: Update advancedFormat plugin to support w ww wo week tokens

fix #676

* docs: Update AdvancedFormat doc

* docs: Update docs
  • Loading branch information
iamkun committed Oct 9, 2019
1 parent f8ca3d4 commit 26cfa63
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 49 deletions.
20 changes: 12 additions & 8 deletions docs/en/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ dayjs().format('Q Do k kk X x')

List of added formats:

| Format | Output | Description |
| ------ | ---------------- | ---------------------------------- |
| `Q` | 1-4 | Quarter |
| `Do` | 1st 2nd ... 31st | Day of Month with ordinal |
| `k` | 1-24 | The hour, beginning at 1 |
| `kk` | 01-24 | The hour, 2-digits, beginning at 1 |
| `X` | 1360013296 | Unix Timestamp in second |
| `x` | 1360013296123 | Unix Timestamp in millisecond |
| Format | Output | Description |
| ------ | --------------------- | ----------------------------------------------------- |
| `Q` | 1-4 | Quarter |
| `Do` | 1st 2nd ... 31st | Day of Month with ordinal |
| `k` | 1-24 | The hour, beginning at 1 |
| `kk` | 01-24 | The hour, 2-digits, beginning at 1 |
| `X` | 1360013296 | Unix Timestamp in second |
| `x` | 1360013296123 | Unix Timestamp in millisecond |
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |

### LocalizedFormat

Expand Down
20 changes: 12 additions & 8 deletions docs/es-es/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ dayjs().format('Q Do k kk X x')

Lista de formatos añadidos:

| Formato | Salida | Descripción |
| ------- | ------------- | ------------------------------------- |
| `Q` | 1-4 | Cuarto |
| `Do` | 1º 2º ... 31º | Día del mes con ordinal |
| `k` | 1-24 | Hora, contando desde 1 |
| `kk` | 01-24 | Hora, con 2 dígitos, contando desde 1 |
| `X` | 1360013296 | Tiempo Unix en segundos |
| `x` | 1360013296123 | Tiempo Unix en milisegundos |
| Formato | Salida | Descripción |
| ------- | --------------------- | ----------------------------------------------------- |
| `Q` | 1-4 | Cuarto |
| `Do` | 1º 2º ... 31º | Día del mes con ordinal |
| `k` | 1-24 | Hora, contando desde 1 |
| `kk` | 01-24 | Hora, con 2 dígitos, contando desde 1 |
| `X` | 1360013296 | Tiempo Unix en segundos |
| `x` | 1360013296123 | Tiempo Unix en milisegundos |
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |

### LocalizedFormat

Expand Down
20 changes: 12 additions & 8 deletions docs/ja/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ dayjs().format('Q Do k kk X x')

追加されるフォーマットの一覧:

| フォーマット | 出力 | 説明 |
| ------------ | ---------------- | ---------------------------- |
| `Q` | 1-4 | 四半期 |
| `Do` | 1st 2nd ... 31st | 序数付きの日 |
| `k` | 1-24 | 1 始まりの時間 |
| `kk` | 01-24 | 1 始まりで 2 桁の時間 |
| `X` | 1360013296 | Unix タイムスタンプ (秒) |
| `x` | 1360013296123 | Unix タイムスタンプ (ミリ秒) |
| フォーマット | 出力 | 説明 |
| ------------ | --------------------- | ----------------------------------------------------- |
| `Q` | 1-4 | 四半期 |
| `Do` | 1st 2nd ... 31st | 序数付きの日 |
| `k` | 1-24 | 1 始まりの時間 |
| `kk` | 01-24 | 1 始まりで 2 桁の時間 |
| `X` | 1360013296 | Unix タイムスタンプ (秒) |
| `x` | 1360013296123 | Unix タイムスタンプ (ミリ秒) |
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |

### LocalizedFormat

Expand Down
20 changes: 12 additions & 8 deletions docs/ko/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ dayjs().format('Q Do k kk X x')

추가된 형식 목록:

| Format | Output | Description |
| ------ | ---------------- | ---------------------------- |
| `Q` | 1-4 | 분기 |
| `Do` | 1st 2nd ... 31st | 서수형식의 일자 명 |
| `k` | 1-24 | 시간, 1부터 시작 |
| `kk` | 01-24 | 시간, 2자리 표현, 1부터 시작 |
| `X` | 1360013296 | 유닉스 타임스템프, 초 |
| `x` | 1360013296123 | 유닉스 타임스탬프, 밀리 초 |
| Format | Output | Description |
| ------ | --------------------- | ----------------------------------------------------- |
| `Q` | 1-4 | 분기 |
| `Do` | 1st 2nd ... 31st | 서수형식의 일자 명 |
| `k` | 1-24 | 시간, 1부터 시작 |
| `kk` | 01-24 | 시간, 2자리 표현, 1부터 시작 |
| `X` | 1360013296 | 유닉스 타임스템프, 초 |
| `x` | 1360013296123 | 유닉스 타임스탬프, 밀리 초 |
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |

### LocalizedFormat

Expand Down
20 changes: 12 additions & 8 deletions docs/pt-br/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ dayjs().format('Q Do k kk X x')

Lista de formatos adicionados:

| Formato | Saída | Descrição |
| ------- | ---------------- | ------------------------------------ |
| `Q` | 1-4 | Quarter |
| `Do` | 1st 2nd ... 31st | Dia do mês com ordinal |
| `k` | 1-24 | Hora (começando do 1) |
| `kk` | 01-24 | Hora, com 2 dígitos (começando do 1) |
| `X` | 1360013296 | Unix Timestamp em segundos |
| `x` | 1360013296123 | Unix Timestamp em milissegundos |
| Formato | Saída | Descrição |
| ------- | --------------------- | ----------------------------------------------------- |
| `Q` | 1-4 | Quarter |
| `Do` | 1st 2nd ... 31st | Dia do mês com ordinal |
| `k` | 1-24 | Hora (começando do 1) |
| `kk` | 01-24 | Hora, com 2 dígitos (começando do 1) |
| `X` | 1360013296 | Unix Timestamp em segundos |
| `x` | 1360013296123 | Unix Timestamp em milissegundos |
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |

### LocalizedFormat

Expand Down
20 changes: 12 additions & 8 deletions docs/zh-cn/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,18 @@ dayjs().format('Q Do k kk X x')

扩展的模版列表:

| 模版 | 输出 | 简介 |
| ---- | ---------------- | ---------------------- |
| `Q` | 1-4 | 季度 |
| `Do` | 1st 2nd ... 31st | 带序号的月份 |
| `k` | 1-24 | 时:由 1 开始 |
| `kk` | 01-24 | 时:由 1 开始,二位数 |
| `X` | 1360013296 | 秒为单位的 Unix 时间戳 |
| `x` | 1360013296123 | 毫秒单位的 Unix 时间戳 |
| 模版 | 输出 | 简介 |
| ------ | --------------------- | ------------------------------------------ |
| `Q` | 1-4 | 季度 |
| `Do` | 1st 2nd ... 31st | 带序号的月份 |
| `k` | 1-24 | 时:由 1 开始 |
| `kk` | 01-24 | 时:由 1 开始,二位数 |
| `X` | 1360013296 | 秒为单位的 Unix 时间戳 |
| `x` | 1360013296123 | 毫秒单位的 Unix 时间戳 |
| `w` | 1 2 ... 52 53 | 年中第几周 (依赖: weekOfYear 插件) |
| `ww` | 01 02 ... 52 53 | 年中第几周,二位数 (依赖: weekOfYear 插件) |
| `wo` | 1st 2nd ... 52nd 53rd | 带序号的年中第几周 (依赖: weekOfYear 插件) |
| `gggg` | 2017 | 根据周计算的年份 (依赖: weekYear 插件) |

### LocalizedFormat

Expand Down
5 changes: 4 additions & 1 deletion src/plugin/advancedFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default (o, c, d) => { // locale needed later
const locale = this.$locale()
const utils = this.$utils()
const str = formatStr || FORMAT_DEFAULT
const result = str.replace(/\[([^\]]+)]|Q|wo|gggg|Do|X|x|k{1,2}|S/g, (match) => {
const result = str.replace(/\[([^\]]+)]|Q|wo|ww|w|gggg|Do|X|x|k{1,2}|S/g, (match) => {
switch (match) {
case 'Q':
return Math.ceil((this.$M + 1) / 3)
Expand All @@ -23,6 +23,9 @@ export default (o, c, d) => { // locale needed later
return this.weekYear()
case 'wo':
return locale.ordinal(this.week(), 'W') // W for week
case 'w':
case 'ww':
return utils.s(this.week(), match === 'w' ? 1 : 2, '0')
case 'k':
case 'kk':
return utils.s(String(this.$H === 0 ? 24 : this.$H), match === 'k' ? 1 : 2, '0')
Expand Down
9 changes: 9 additions & 0 deletions test/plugin/weekOfYear.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../../src'
import weekOfYear from '../../src/plugin/weekOfYear'
import advancedFormat from '../../src/plugin/advancedFormat'
import '../../src/locale/en-gb'

dayjs.extend(advancedFormat)
dayjs.extend(weekOfYear)

beforeEach(() => {
Expand Down Expand Up @@ -39,3 +41,10 @@ it('Week of year with locale', () => {
expect(dayjs('2018-12-30').week()).toBe(moment('2018-12-30').week())
expect(dayjs('2019-12-29').week()).toBe(moment('2019-12-29').week())
})

it('Format w ww wo', () => {
const day = '2019-07-28'
const D = dayjs(day)
const M = moment(day)
expect(D.format('w ww wo')).toBe(M.format('w ww wo'))
})

0 comments on commit 26cfa63

Please sign in to comment.