-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.ts
41 lines (36 loc) · 1.01 KB
/
utilities.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type { TimeDataDateString } from './types.js'
import { type DateColumn } from './workSheetTypes.js'
const dateColumnRegularExpression = /^\w{3} \d/
const monthNumbers = {
Jan: '01',
Feb: '02',
Mar: '03',
Apr: '04',
May: '05',
Jun: '06',
Jul: '07',
Aug: '08',
Sep: '09',
Oct: '10',
Nov: '11',
Dec: '12'
}
/**
* Tests if a column name is a date column.
* @param {string} columnName - A column name
* @returns {boolean} - True when the column name is a date column.
*/
export function isDateColumn(columnName: string): columnName is DateColumn {
return dateColumnRegularExpression.test(columnName)
}
/**
* Formats a date column name.
* @param {DateColumn} columnName - ex. 'Tue 16-Jan-2024'
* @returns {TimeDataDateString} ex. '2024/01/16'
*/
export function formatDate(columnName: DateColumn): TimeDataDateString {
const datePieces = columnName.slice(4).split('-')
return `${datePieces[2]}/${
monthNumbers[datePieces[1]]
}/${datePieces[0].padStart(2, '0')}` as TimeDataDateString
}