Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add customParseFormat support for isoweek #2709

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { u } from '../localizedFormat/utils'

const formattingTokens = /(\[[^[]*\])|([-_:/.,()\s]+)|(A|a|Q|YYYY|YY?|ww?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g
const formattingTokens = /(\[[^[]*\])|([-_:/.,()\s]+)|(A|a|Q|YYYY|YY?|ww?|WW?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g

const match1 = /\d/ // 0 - 9
const match2 = /\d\d/ // 00 - 99
Expand Down Expand Up @@ -100,6 +100,8 @@ const expressions = {
}],
w: [match1to2, addInput('week')],
ww: [match2, addInput('week')],
W: [match1to2, addInput('week')],
WW: [match2, addInput('week')],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems there no logic dealing with ISOWeek?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the customParseFormat scenario, similar to the code in pull request #2705, it's sufficient to support parsing uppercase W/WW for ISO week notation. For stricter validation, one could additionally check if the week number exceeds 53. However, this basic approach is adequate for most use cases.

Take, for instance, date strings like '2024-W24' or '2024-w24'. In these cases, it's ambiguous whether '24' denotes a regular weekday or an ISO week number.

Consider the following practical scenario: Suppose we generate a string using the format 'YYYY-[W]WW'. Later, we need to verify if a given string adheres to this format. In such cases, we would typically employ customParseFormat, reusing the original format string. This is where support for 'WW' in ISO week notation becomes crucial. (It's worth noting that using 'ww' would yield consistent results in parsing scenarios, but 'WW' maintains format consistency.)

const fileDateFormat = "YYYY-[W]WW";

function buildFileName(date) {
   return dayjs(date).format(fileDateFormat);
}

function checkNoteName(fileName) {
  // here we reused the fileDateFormat
  const parsedDate = dayjs(fileName, fileDateFormat);
  return parsedDate && parsedDate.format(fileDateFormat) === fileName;
}

M: [match1to2, addInput('month')],
MM: [match2, addInput('month')],
MMM: [matchWord, function (input) {
Expand Down
12 changes: 10 additions & 2 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import customParseFormat from '../../src/plugin/customParseFormat'
import advancedFormat from '../../src/plugin/advancedFormat'
import localizedFormats from '../../src/plugin/localizedFormat'
import weekOfYear from '../../src/plugin/weekOfYear'
import isoWeek from '../../src/plugin/isoWeek'

dayjs.extend(customParseFormat)
dayjs.extend(localizedFormats)
dayjs.extend(weekOfYear) // test parse w, ww
dayjs.extend(isoWeek) // test parse W, WW

beforeEach(() => {
MockDate.set(new Date())
Expand Down Expand Up @@ -452,11 +454,17 @@ it('parse Q, [Q]', () => {
expect(dayjs(input4, format).valueOf()).toBe(moment(input4, format).valueOf())
})

it('parse w, ww', () => {
it('parse w, ww, W, WW', () => {
const input = '2024-w1'
const format1 = 'YYYY-[w]w'
expect(dayjs(input, format1).format(format1)).toBe(input)
const input2 = '2024-w32'
const format2 = 'YYYY-[w]ww'
expect(dayjs(input2, format2).format(format1)).toBe(input2)
expect(dayjs(input2, format2).format(format2)).toBe(input2)
const input3 = '2024-w1'
const format3 = 'YYYY-[w]W'
expect(dayjs(input3, format3).format(format3)).toBe(input3)
const input4 = '2024-w33'
const format4 = 'YYYY-[w]WW'
expect(dayjs(input4, format4).format(format4)).toBe(input4)
})