Skip to content

Commit f7c69b1

Browse files
committed
#178 - explicit support for telling if the Www date should be same, earlier than the first day of the week or later than the last day of the week
- syntax W1 W1- W1+
1 parent 2204982 commit f7c69b1

File tree

5 files changed

+66
-19
lines changed

5 files changed

+66
-19
lines changed

src/custom-sort/matchers.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ export const Date_dd_Mmm_yyyy_RegexStr: string = ' *([0-3]*[0-9]-(?:Jan|Feb|Mar|
1717
export const Date_Mmm_dd_yyyy_RegexStr: string = ' *((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-[0-3]*[0-9]-\\d{4})'; // Date like Jan-01-2020
1818

1919
export const Date_yyyy_Www_mm_dd_RegexStr: string = ' *(\\d{4}-W[0-5]*[0-9] \\([0-3]*[0-9]-[0-3]*[0-9]\\))'
20-
export const Date_yyyy_WwwISO_RegexStr: string = ' *(\\d{4}-W[0-5]*[0-9])'
20+
export const Date_yyyy_WwwISO_RegexStr: string = ' *(\\d{4}-W[0-5]*[0-9][-+]?)'
2121
export const Date_yyyy_Www_RegexStr: string = Date_yyyy_WwwISO_RegexStr
2222

23-
export const DOT_SEPARATOR = '.'
23+
export const DOT_SEPARATOR = '.' // ASCII 46
2424
export const DASH_SEPARATOR = '-'
2525

26-
const SLASH_SEPARATOR = '/' // ASCII 47
26+
const SLASH_SEPARATOR = '/' // ASCII 47, right before ASCII 48 = '0'
27+
const COLON_SEPARATOR = ':' // ASCII 58, first non-digit character
2728
const PIPE_SEPARATOR = '|' // ASCII 124
2829

30+
const EARLIER_THAN_SLASH_SEPARATOR = DOT_SEPARATOR
31+
const LATER_THAN_SLASH_SEPARATOR = COLON_SEPARATOR
32+
2933
export const DEFAULT_NORMALIZATION_PLACES = 8; // Fixed width of a normalized number (with leading zeros)
3034

3135
// Property escapes:
@@ -62,9 +66,9 @@ export function getNormalizedNumber(s: string = '', separator?: string, places?:
6266
// guarantees correct order (/ = ASCII 47, | = ASCII 124)
6367
if (separator) {
6468
const components: Array<string> = s.split(separator).filter(s => s)
65-
return `${components.map((c) => prependWithZeros(c, places ?? DEFAULT_NORMALIZATION_PLACES)).join(PIPE_SEPARATOR)}//`
69+
return `${components.map((c) => prependWithZeros(c, places ?? DEFAULT_NORMALIZATION_PLACES)).join(PIPE_SEPARATOR)}${SLASH_SEPARATOR}${SLASH_SEPARATOR}`
6670
} else {
67-
return `${prependWithZeros(s, places ?? DEFAULT_NORMALIZATION_PLACES)}//`
71+
return `${prependWithZeros(s, places ?? DEFAULT_NORMALIZATION_PLACES)}${SLASH_SEPARATOR}${SLASH_SEPARATOR}`
6872
}
6973
}
7074

@@ -108,9 +112,9 @@ export function getNormalizedRomanNumber(s: string, separator?: string, places?:
108112
// guarantees correct order (/ = ASCII 47, | = ASCII 124)
109113
if (separator) {
110114
const components: Array<string> = s.split(separator).filter(s => s)
111-
return `${components.map((c) => prependWithZeros(romanToIntStr(c), places ?? DEFAULT_NORMALIZATION_PLACES)).join(PIPE_SEPARATOR)}//`
115+
return `${components.map((c) => prependWithZeros(romanToIntStr(c), places ?? DEFAULT_NORMALIZATION_PLACES)).join(PIPE_SEPARATOR)}${SLASH_SEPARATOR}${SLASH_SEPARATOR}`
112116
} else {
113-
return `${prependWithZeros(romanToIntStr(s), places ?? DEFAULT_NORMALIZATION_PLACES)}//`
117+
return `${prependWithZeros(romanToIntStr(s), places ?? DEFAULT_NORMALIZATION_PLACES)}${SLASH_SEPARATOR}${SLASH_SEPARATOR}`
114118
}
115119
}
116120

@@ -128,7 +132,7 @@ export function getNormalizedDate_NormalizerFn_for(separator: string, dayIdx: nu
128132
const monthValue = months ? `${1 + MONTHS.indexOf(components[monthIdx])}` : components[monthIdx]
129133
const month = prependWithZeros(monthValue, MONTH_POSITIONS)
130134
const year = prependWithZeros(components[yearIdx], YEAR_POSITIONS)
131-
return `${year}-${month}-${day}//`
135+
return `${year}-${month}-${day}${SLASH_SEPARATOR}${SLASH_SEPARATOR}`
132136
}
133137
}
134138

@@ -137,14 +141,18 @@ export const getNormalizedDate_yyyy_dd_mm_NormalizerFn = getNormalizedDate_Norma
137141
export const getNormalizedDate_dd_Mmm_yyyy_NormalizerFn = getNormalizedDate_NormalizerFn_for('-', 0, 1, 2, MONTHS)
138142
export const getNormalizedDate_Mmm_dd_yyyy_NormalizerFn = getNormalizedDate_NormalizerFn_for('-', 1, 0, 2, MONTHS)
139143

144+
const DateExtractor_orderModifier_earlier_than = '-'
145+
const DateExtractor_orderModifier_later_than = '+'
146+
140147
const DateExtractor_yyyy_Www_mm_dd_Regex = /(\d{4})-W(\d{1,2}) \((\d{2})-(\d{2})\)/
141-
const DateExtractor_yyyy_Www_Regex = /(\d{4})-W(\d{1,2})/
148+
const DateExtractor_yyyy_Www_Regex = /(\d{4})-W(\d{1,2})([-+]?)/
142149

143150
// Matching groups
144151
const YEAR_IDX = 1
145152
const WEEK_IDX = 2
146153
const MONTH_IDX = 3
147154
const DAY_IDX = 4
155+
const RELATIVE_ORDER_IDX = 3 // For the yyyy-Www only: yyyy-Www> or yyyy-Www<
148156

149157
const DECEMBER = 12
150158
const JANUARY = 1
@@ -157,10 +165,19 @@ export function getNormalizedDate_NormalizerFn_yyyy_Www_mm_dd(consumeWeek: boole
157165
let yearNumber = Number.parseInt(yearStr,10)
158166
let monthNumber: number
159167
let dayNumber: number
168+
let separator = SLASH_SEPARATOR // different values enforce relative > < order of same dates
169+
let useLastDayOfWeek: boolean = false
160170
if (consumeWeek) {
161171
const weekNumberStr = matches![WEEK_IDX]
162172
const weekNumber = Number.parseInt(weekNumberStr, 10)
163-
const dateForWeek = getDateForWeekOfYear(yearNumber, weekNumber, weeksISO)
173+
const orderModifier: string|undefined = matches![RELATIVE_ORDER_IDX]
174+
if (orderModifier === DateExtractor_orderModifier_earlier_than) {
175+
separator = EARLIER_THAN_SLASH_SEPARATOR
176+
} else if (orderModifier === DateExtractor_orderModifier_later_than) {
177+
separator = LATER_THAN_SLASH_SEPARATOR // Will also need to adjust the date to the last day of the week
178+
useLastDayOfWeek = true
179+
}
180+
const dateForWeek = getDateForWeekOfYear(yearNumber, weekNumber, weeksISO, useLastDayOfWeek)
164181
monthNumber = dateForWeek.getMonth()+1 // 1 - 12
165182
dayNumber = dateForWeek.getDate() // 1 - 31
166183
// Be careful with edge dates, which can belong to previous or next year
@@ -178,7 +195,10 @@ export function getNormalizedDate_NormalizerFn_yyyy_Www_mm_dd(consumeWeek: boole
178195
monthNumber = Number.parseInt(matches![MONTH_IDX],10)
179196
dayNumber = Number.parseInt(matches![DAY_IDX], 10)
180197
}
181-
return `${prependWithZeros(`${yearNumber}`, YEAR_POSITIONS)}-${prependWithZeros(`${monthNumber}`, MONTH_POSITIONS)}-${prependWithZeros(`${dayNumber}`, DAY_POSITIONS)}//`
198+
return `${prependWithZeros(`${yearNumber}`, YEAR_POSITIONS)}` +
199+
`-${prependWithZeros(`${monthNumber}`, MONTH_POSITIONS)}` +
200+
`-${prependWithZeros(`${dayNumber}`, DAY_POSITIONS)}` +
201+
`${separator}${SLASH_SEPARATOR}`
182202
}
183203
}
184204

src/test/unit/matchers.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,19 @@ describe('getNormalizedDate_yyyy_Www_mm_dd_NormalizerFn', () => {
458458
expect(getNormalizedDate_yyyy_Www_mm_dd_NormalizerFn(s)).toBe(out)
459459
})
460460
})
461+
462+
describe('getNormalizedDate_yyyy_Www_NormalizerFn', () => {
463+
/* ORDER for week numbers vs. dates of 1st day / last day of the week:
464+
W1 - exactly on the first day of 1st week - the actual title then decides about relative order
465+
W1- - before the first day of 1st week, yet after the last day of prev week)
466+
W1+ - after the last day of 1st week, yet before the first day of next week)
467+
*/
468+
const params = [
469+
['2012-W1', '2011-12-26//'],
470+
['2012-W1+', '2012-01-01:/'],
471+
['2012-W1-', '2011-12-26./'],
472+
];
473+
it.each(params)('>%s< should become %s', (s: string, out: string) => {
474+
expect(getNormalizedDate_yyyy_Www_NormalizerFn(s)).toBe(out)
475+
})
476+
})

src/test/unit/sorting-spec-processor.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,13 @@ const expectedSortSpecsExampleSortingSymbols: { [key: string]: CustomSortSpec }
458458
}, {
459459
type: CustomSortGroupType.ExactName,
460460
regexPrefix: {
461-
regex: /^Week number interpreted in ISO standard *(\d{4}-W[0-5]*[0-9])$/i,
461+
regex: /^Week number interpreted in ISO standard *(\d{4}-W[0-5]*[0-9][-+]?)$/i,
462462
normalizerFn: Date_yyyy_WwwISO_NormalizerFn
463463
}
464464
}, {
465465
type: CustomSortGroupType.ExactName,
466466
regexPrefix: {
467-
regex: /^Week number interpreted in U\.S\. standard *(\d{4}-W[0-5]*[0-9])$/i,
467+
regex: /^Week number interpreted in U\.S\. standard *(\d{4}-W[0-5]*[0-9][-+]?)$/i,
468468
normalizerFn: Date_yyyy_Www_NormalizerFn
469469
}
470470
}, {

src/test/unit/week-of-year.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ describe('getDateForWeekOfYear', () => {
4343
expect(getDateForWeekOfYear(year, 10)).toStrictEqual(dateUS)
4444
expect(getDateForWeekOfYear(year, 10, true)).toStrictEqual(dateISO)
4545
})
46-
it('should correctly handle edge case - a year spanning 54 weeks (leap year staring on Sun)', () => {
46+
it('should correctly handle edge case - a year spanning 54 weeks (leap year starting on Sun)', () => {
47+
const USstandard = false
48+
const SUNDAY = true
4749
// This works in U.S. standard only, where 1st week can start on Sunday
4850
expect(getDateForWeekOfYear(2012,1)).toStrictEqual(new Date('2011-12-26T00:00:00.000Z'))
51+
expect(getDateForWeekOfYear(2012,1, USstandard, SUNDAY)).toStrictEqual(new Date('2012-01-01T00:00:00.000Z'))
4952
expect(getDateForWeekOfYear(2012,54)).toStrictEqual(new Date('2012-12-31T00:00:00.000Z'))
53+
expect(getDateForWeekOfYear(2012,54, USstandard, SUNDAY)).toStrictEqual(new Date('2013-01-06T00:00:00.000Z'))
5054
})
5155
})

src/utils/week-of-year.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// Cache of start of years and number of days in the 1st week
33
interface MondayCache {
44
year: number // full year, e.g. 2015
5-
mondayDateOf1stWeekUS: number // U.S. standard, can be in Dec of previous year
5+
mondayDateOf1stWeekUS: number // U.S. standard, the 1st of Jan determines the first week, monday can be in Dec of previous year
6+
sundayDateOf1stWeekUS: number
67
mondayDateOf1stWeekISO: number // ISO standard, when the first Thursday of the year determines week numbering
8+
sundayDateOf1stWeekISO: number
79
}
810

911
type YEAR = number
@@ -35,20 +37,25 @@ const calculateMondayDateIn1stWeekOfYear = (year: number): MondayCache => {
3537
return {
3638
year: year,
3739
mondayDateOf1stWeekUS: new Date(firstSecondOfYear).setDate(firstSecondOfYear.getDate() - daysToPrevMonday),
40+
sundayDateOf1stWeekUS: new Date(firstSecondOfYear).setDate(firstSecondOfYear.getDate() - daysToPrevMonday + DAYS_IN_WEEK - 1),
3841
mondayDateOf1stWeekISO: new Date(firstSecondOfYear).setDate(firstSecondOfYear.getDate() - daysToPrevMonday + useISOoffset),
42+
sundayDateOf1stWeekISO: new Date(firstSecondOfYear).setDate(firstSecondOfYear.getDate() - daysToPrevMonday + useISOoffset + DAYS_IN_WEEK - 1),
3943
}
4044
}
4145

4246
// Week number = 1 to 54, U.S. standard by default, can also work in ISO (parameter driven)
43-
export const getDateForWeekOfYear = (year: number, weekNumber: number, useISO?: boolean): Date => {
47+
export const getDateForWeekOfYear = (year: number, weekNumber: number, useISO?: boolean, sunday?: boolean): Date => {
4448
const WEEK_OF_MILIS = DAYS_IN_WEEK * DAY_OF_MILIS
4549
const dataOfMondayIn1stWeekOfYear = (MondaysCache[year] ??= calculateMondayDateIn1stWeekOfYear(year))
46-
const mondayOfTheRequestedWeek = new Date(
50+
const mondayOfTheRequestedWeek =
4751
(useISO ? dataOfMondayIn1stWeekOfYear.mondayDateOf1stWeekISO : dataOfMondayIn1stWeekOfYear.mondayDateOf1stWeekUS)
4852
+ (weekNumber-1)*WEEK_OF_MILIS
49-
)
5053

51-
return mondayOfTheRequestedWeek
54+
const sundayOfTheRequestedWeek =
55+
(useISO ? dataOfMondayIn1stWeekOfYear.sundayDateOf1stWeekISO : dataOfMondayIn1stWeekOfYear.sundayDateOf1stWeekUS)
56+
+ (weekNumber-1)*WEEK_OF_MILIS
57+
58+
return new Date(sunday ? sundayOfTheRequestedWeek : mondayOfTheRequestedWeek)
5259
}
5360

5461
export const _unitTests = {

0 commit comments

Comments
 (0)