forked from ultraworkers/claw-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption-map.ts
More file actions
50 lines (42 loc) · 1.16 KB
/
Copy pathoption-map.ts
File metadata and controls
50 lines (42 loc) · 1.16 KB
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
import type { ReactNode } from 'react'
import type { OptionWithDescription } from './select.js'
type OptionMapItem<T> = {
label: ReactNode
value: T
description?: string
previous: OptionMapItem<T> | undefined
next: OptionMapItem<T> | undefined
index: number
}
export default class OptionMap<T> extends Map<T, OptionMapItem<T>> {
readonly first: OptionMapItem<T> | undefined
readonly last: OptionMapItem<T> | undefined
constructor(options: OptionWithDescription<T>[]) {
const items: Array<[T, OptionMapItem<T>]> = []
let firstItem: OptionMapItem<T> | undefined
let lastItem: OptionMapItem<T> | undefined
let previous: OptionMapItem<T> | undefined
let index = 0
for (const option of options) {
const item = {
label: option.label,
value: option.value,
description: option.description,
previous,
next: undefined,
index,
}
if (previous) {
previous.next = item
}
firstItem ||= item
lastItem = item
items.push([option.value, item])
index++
previous = item
}
super(items)
this.first = firstItem
this.last = lastItem
}
}