-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
120 lines (106 loc) · 3.42 KB
/
index.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import agTicketTypes from './ticketTypes/ag.js'
import bnTicketTypes from './ticketTypes/bn.js'
import pgTicketTypes from './ticketTypes/pg.js'
import spTicketTypes from './ticketTypes/sp.js'
import type {
ParsedTicketType,
TicketType,
TicketTypePrefix,
TicketTypeRecord
} from './types.js'
/**
* All available ticket types
*/
export const ticketTypes = {
...bnTicketTypes,
...spTicketTypes,
...agTicketTypes,
...pgTicketTypes
}
/**
* Checks if a record exists for a given ticket type.
* @param possibleTicketType - A possible ticket type
* @returns `true` if there is a record for the ticket type.
*/
export function isTicketType(
possibleTicketType: string
): possibleTicketType is TicketType<TicketTypePrefix> {
return Object.hasOwn(ticketTypes, possibleTicketType)
}
/**
* Parses a valid ticket type into its two-letter prefix and numeric suffix.
* @param possibleTicketType - A possible ticket type
* @returns an object parsing the ticket type into its two-letter prefix and numeric suffix.
*/
export function parseTicketType(
possibleTicketType: string
): ParsedTicketType<TicketTypePrefix> | undefined {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
return isTicketType(possibleTicketType)
? {
ticketTypePrefix: possibleTicketType.slice(0, 2) as TicketTypePrefix,
ticketTypeNumber: Number.parseInt(possibleTicketType.slice(2))
}
: undefined
}
/**
* Compare function to sort ticket types alphabetically, with invalid ticket types at the end of the list.
* @param ticketTypeA - Ticket type A
* @param ticketTypeB - Ticket type B
* @returns the sort value
*/
export function ticketTypeSortFunction(
ticketTypeA: string,
ticketTypeB: string
): number {
/*
* Handle equal ticket types
*/
if (ticketTypeA === ticketTypeB) {
return 0
}
const parsedTicketTypeA = parseTicketType(ticketTypeA)
const parsedTicketTypeB = parseTicketType(ticketTypeB)
/*
* Handle invalid ticket types
*/
if (parsedTicketTypeA === undefined && parsedTicketTypeB === undefined) {
return ticketTypeA > ticketTypeB ? 1 : -1
} else if (parsedTicketTypeA === undefined) {
return 1
} else if (parsedTicketTypeB === undefined) {
return -1
}
/*
* Handle parsed ticket numbers
*/
if (
parsedTicketTypeA.ticketTypePrefix !== parsedTicketTypeB.ticketTypePrefix
) {
return parsedTicketTypeA.ticketTypePrefix >
parsedTicketTypeB.ticketTypePrefix
? 1
: -1
}
return parsedTicketTypeA.ticketTypeNumber - parsedTicketTypeB.ticketTypeNumber
}
/**
* Compare function to sort ticket type records alphabetically by ticket type, with invalid ticket types at the end of the list.
* @param ticketTypeRecordA - Ticket type record A
* @param ticketTypeRecordB - Ticket type record B
* @returns the sort value
*/
export function ticketTypeRecordSortFunction(
ticketTypeRecordA: TicketTypeRecord<TicketType<TicketTypePrefix>>,
ticketTypeRecordB: TicketTypeRecord<TicketType<TicketTypePrefix>>
): number {
return ticketTypeSortFunction(
ticketTypeRecordA.ticketType,
ticketTypeRecordB.ticketType
)
}
export { default as agTicketTypes } from './ticketTypes/ag.js'
export { default as bnTicketTypes } from './ticketTypes/bn.js'
export { default as pgTicketTypes } from './ticketTypes/pg.js'
export { default as spTicketTypes } from './ticketTypes/sp.js'
export type { TicketTypePrefix, TicketType, TicketTypeRecord } from './types.js'