|
1 | 1 | import type { Token } from "./parser"; |
2 | | -import { |
3 | | - Day, |
4 | | - DayOfTheMonth, |
5 | | - DayOfTheWeek, |
6 | | - FullMonth, |
7 | | - FullYear, |
8 | | - Hour, |
9 | | - Hour24, |
10 | | - Minutes, |
11 | | - NumberMonth, |
12 | | - PartialMonth, |
13 | | - PartialYear, |
14 | | - PostOrAnteMeridiem, |
15 | | - Seconds, |
16 | | - UserText, |
17 | | -} from "./subs"; |
| 2 | +import { Tokens } from "./subs"; |
18 | 3 | import type { TinyTimeOptions } from "./tinytime"; |
19 | 4 |
|
20 | 5 | const months = [ |
@@ -54,14 +39,9 @@ function padWithZeros(int: number): string { |
54 | 39 | * Adds suffix to day, so 16 becomes 16th. |
55 | 40 | */ |
56 | 41 | function suffix(int: number): string { |
57 | | - const suf = |
58 | | - int % 10 === 1 && int !== 11 |
59 | | - ? "st" |
60 | | - : int % 10 === 2 && int !== 12 |
61 | | - ? "nd" |
62 | | - : int % 10 === 3 && int !== 13 |
63 | | - ? "rd" |
64 | | - : "th"; |
| 42 | + const s = ["th", "st", "nd", "rd"]; |
| 43 | + const v = int % 100; |
| 44 | + const suf = s[(v - 20) % 10] || s[v] || s[0]; |
65 | 45 | return `${int}${suf}`; |
66 | 46 | } |
67 | 47 |
|
@@ -91,75 +71,55 @@ export default function compiler( |
91 | 71 | while (index < tokens.length) { |
92 | 72 | const token = tokens[index]; |
93 | 73 |
|
94 | | - if (!token) { |
95 | | - break; |
96 | | - } |
97 | | - |
98 | | - switch (token[0]) { |
99 | | - case UserText: |
| 74 | + const tokenHandlers = { |
| 75 | + [Tokens.UserText]: () => { |
100 | 76 | compiled += token[1]; |
101 | | - break; |
102 | | - |
103 | | - case Day: |
| 77 | + }, |
| 78 | + [Tokens.Day]: () => { |
104 | 79 | compiled += suffix(day); |
105 | | - break; |
106 | | - |
107 | | - case PartialMonth: |
| 80 | + }, |
| 81 | + [Tokens.PartialMonth]: () => { |
108 | 82 | compiled += months[month]?.slice(0, 3); |
109 | | - break; |
110 | | - |
111 | | - case FullMonth: |
| 83 | + }, |
| 84 | + [Tokens.FullMonth]: () => { |
112 | 85 | compiled += months[month]; |
113 | | - break; |
114 | | - |
115 | | - case NumberMonth: |
116 | | - { |
117 | | - const next = month + 1; |
118 | | - compiled += options.padMonth |
119 | | - ? padWithZeros(next) |
120 | | - : `${next}`; |
121 | | - } |
122 | | - break; |
123 | | - |
124 | | - case FullYear: |
| 86 | + }, |
| 87 | + [Tokens.NumberMonth]: () => { |
| 88 | + const next = month + 1; |
| 89 | + compiled += options.padMonth ? padWithZeros(next) : `${next}`; |
| 90 | + }, |
| 91 | + [Tokens.FullYear]: () => { |
125 | 92 | compiled += year; |
126 | | - break; |
127 | | - |
128 | | - case PartialYear: |
| 93 | + }, |
| 94 | + [Tokens.PartialYear]: () => { |
129 | 95 | compiled += `${year % 100}`; |
130 | | - break; |
131 | | - |
132 | | - case DayOfTheWeek: |
| 96 | + }, |
| 97 | + [Tokens.DayOfTheWeek]: () => { |
133 | 98 | compiled += days[date.getDay()]; |
134 | | - break; |
135 | | - |
136 | | - case DayOfTheMonth: |
| 99 | + }, |
| 100 | + [Tokens.DayOfTheMonth]: () => { |
137 | 101 | compiled += options.padDays ? padWithZeros(day) : day; |
138 | | - break; |
139 | | - |
140 | | - case Hour: |
141 | | - { |
142 | | - const hour = hours % 12 || 12; |
143 | | - compiled += options.padHours ? padWithZeros(hour) : hour; |
144 | | - } |
145 | | - break; |
146 | | - |
147 | | - case Hour24: |
| 102 | + }, |
| 103 | + [Tokens.Hour]: () => { |
| 104 | + const hour = hours % 12 || 12; |
| 105 | + compiled += options.padHours ? padWithZeros(hour) : hour; |
| 106 | + }, |
| 107 | + [Tokens.Hour24]: () => { |
148 | 108 | compiled += options.padHours ? padWithZeros(hours) : hours; |
149 | | - break; |
150 | | - |
151 | | - case Minutes: |
| 109 | + }, |
| 110 | + [Tokens.Minutes]: () => { |
152 | 111 | compiled += padWithZeros(minutes); |
153 | | - break; |
154 | | - |
155 | | - case Seconds: |
| 112 | + }, |
| 113 | + [Tokens.Seconds]: () => { |
156 | 114 | compiled += padWithZeros(seconds); |
157 | | - break; |
158 | | - |
159 | | - case PostOrAnteMeridiem: |
| 115 | + }, |
| 116 | + [Tokens.PostOrAnteMeridiem]: () => { |
160 | 117 | compiled += hours >= 12 ? "PM" : "AM"; |
161 | | - break; |
162 | | - } |
| 118 | + }, |
| 119 | + }; |
| 120 | + |
| 121 | + tokenHandlers[token[0]](); |
| 122 | + |
163 | 123 | index++; |
164 | 124 | } |
165 | 125 | return compiled; |
|
0 commit comments