pages/search: simplify labels logic

This commit is contained in:
He4eT 2024-01-21 03:52:14 +01:00
commit 9c24d16071
2 changed files with 12 additions and 41 deletions

View file

@ -1,38 +1,9 @@
const labels = {
/* tabId: label, */
}
const lettersOrder = 'lkjhyuionm'
/* */
const digits2label = (digits) => [...digits]
.map((digit) => lettersOrder[digit])
.join('')
export const id2label = (id, tabs) => {
if (!labels[id]) {
const label = Object.keys(labels).length
labels[id] = String(label)
}
const keyLength = String(tabs?.length).length
const digits = String(labels[id]).padStart(keyLength, 0)
return digits2label(digits)
}
/* */
const label2digits = (label) => [...label]
.map((letter) => [...lettersOrder].findIndex((x) => x === letter))
.join('')
export const label2id = (label) => {
const paddedDigits = label2digits(label)
const digits = String(parseInt(paddedDigits))
const [key] = Object.entries(labels)
.find(([_, value]) => value === digits)
return parseInt(key)
const letterOrder = 'lkjhyuionm'
export const fromNumber = (number, length) => {
const labelLength = String(length).length
const digits = String(number).padStart(labelLength, 0)
return [...digits]
.map((digit) => letterOrder[digit])
.join('')
}

View file

@ -14,8 +14,8 @@ export const init = ({
/* */
const addLabel = (tabs) => (tab) => ({
label: labels.id2label(tab.id, tabs),
const addLabel = (tab, i, tabs) => ({
label: labels.fromNumber(i, tabs.length),
...tab,
})
@ -28,13 +28,13 @@ export const init = ({
].reduce((acc, x) => (acc[x] = tab[x], acc), {})
const shapeTabs = (tabs) => tabs
.map(addLabel(tabs))
.map(addLabel)
.map(pickFields)
.reverse()
const fetchTabs = () =>
browserTabs.query({ currentWindow: true, active: false })
.then(shapeTabs)
.then((tabs) => tabs.reverse())
.then((tabs) => void (state.tabs = tabs))
/* */