-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.ts
197 lines (169 loc) · 5.58 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { Temporal } from 'temporal-spec'
// Unfortunately necessary as typescript does not include typings for ecma drafts
// TODO: Remove when ListFormat becomes part of the official spec
declare global {
namespace Intl {
interface ListFormatOptions {
localeMatcher?: 'best fit' | 'lookup'
type?: 'conjunction' | 'disjunction' | 'unit'
style?: 'long' | 'short' | 'narrow'
}
interface ListFormatPart<T = string> {
type: 'literal' | 'element'
value: T
}
class ListFormat {
constructor(locales?: string | string[], options?: Intl.ListFormatOptions)
public formatToParts: (elements: string[]) => Intl.ListFormatPart[]
public format: (items: [string?]) => string
}
}
}
function largestCommonString(a: string, b: string): string {
const [short, long] = a.length < b.length ? [a, b] : [b, a]
// Iterates from full short string by removing last character each iteration
for (let i = short.length; i >= 0; i--) {
const sub = short.substring(0, i)
// If condition holds, its isn't possible for there to be a larger string in the loop
if (long.includes(sub)) {
return sub
}
}
return ''
}
type DurationFormatOptions = { style: 'long' | 'short' | 'narrow' }
const durationFields: (keyof Temporal.DurationLike)[] = [
'years',
'months',
'weeks',
'days',
'hours',
'minutes',
'seconds',
'milliseconds',
'microseconds',
'nanoseconds',
]
function getLiteralPartsValue(
partsArr: Intl.RelativeTimeFormatPart[],
index: number,
): string {
return partsArr[index].type === 'literal' ? partsArr[index].value : ''
}
// Combines adjacent literal parts and removes blank literal parts
function collapseLiteralParts(
parts: Intl.RelativeTimeFormatPart[],
): Intl.RelativeTimeFormatPart[] {
const finalParts: Intl.RelativeTimeFormatPart[] = []
// As we iterate the parts, will be a reference to the previous part, only if it was a literal.
let prevLiteralPart: Intl.RelativeTimeFormatPart | undefined
for (const part of parts) {
if (part.type === 'literal') {
// Don't consider literals that are blank
if (part.value) {
if (prevLiteralPart) {
prevLiteralPart.value += part.value
} else {
prevLiteralPart = { ...part } // Copy in case we concat the value later (necessary?)
finalParts.push(prevLiteralPart)
}
}
} else {
prevLiteralPart = undefined
finalParts.push(part)
}
}
return finalParts
}
function combinePartsArrays(
durationPartArrays: Intl.RelativeTimeFormatPart[][],
listFormatter: Intl.ListFormat,
): Intl.RelativeTimeFormatPart[] {
// Produce an array of strings like ['0', '1', '2']
const indexStrings = durationPartArrays.map((_part, index) => {
return String(index)
})
const listParts = listFormatter.formatToParts(indexStrings)
const combinedParts: Intl.RelativeTimeFormatPart[] = []
for (const listPart of listParts) {
if (listPart.type === 'element') {
// When an element is encountered (a string like '1'),
// inject parts from corresponding duration
combinedParts.push(...durationPartArrays[parseInt(listPart.value)])
} else {
// Otherwise, inject a literal
combinedParts.push(listPart)
}
}
return collapseLiteralParts(combinedParts)
}
export class DurationFormat {
private timeFormatter: Intl.RelativeTimeFormat
private listFormatter: Intl.ListFormat
constructor(
readonly locale: string = 'en-us',
{ style }: DurationFormatOptions = { style: 'long' },
) {
this.timeFormatter = new Intl.RelativeTimeFormat(locale, {
numeric: 'always',
style,
})
this.listFormatter = new Intl.ListFormat(locale, {
style,
type: style !== 'long' ? 'unit' : 'conjunction',
})
}
formatToParts(
durationLike: Temporal.Duration | Temporal.DurationLike,
): Intl.RelativeTimeFormatPart[] {
const duration =
durationLike instanceof Temporal.Duration
? durationLike
: Temporal.Duration.from(durationLike)
// Storage array
const durationPartArrays: Intl.RelativeTimeFormatPart[][] = []
for (const key of durationFields) {
const val = duration[key]
if (val !== 0 && key !== 'milliseconds') {
const forwardParts = this.timeFormatter.formatToParts(
val,
key as Intl.RelativeTimeFormatUnit,
)
const backwardParts = this.timeFormatter.formatToParts(
-val,
key as Intl.RelativeTimeFormatUnit,
)
// Extract largest common string from formatted parts
const before = largestCommonString(
getLiteralPartsValue(forwardParts, 0),
getLiteralPartsValue(backwardParts, 0),
)
const after = largestCommonString(
getLiteralPartsValue(forwardParts, forwardParts.length - 1),
getLiteralPartsValue(backwardParts, backwardParts.length - 1),
)
durationPartArrays.push([
// Append pretext into accumulator
{
type: 'literal',
value: before,
},
// Append actual value into accumulator
forwardParts[forwardParts[0].type === 'literal' ? 1 : 0],
// Append posttext into accumulator
{ type: 'literal', value: after },
])
}
}
// Flatten 2D array into 1D
return combinePartsArrays(durationPartArrays, this.listFormatter)
}
format(durationLike: Temporal.Duration | Temporal.DurationLike): string {
const parts = this.formatToParts(durationLike)
return parts
.map((val) => {
return val.value
})
.join('')
}
}