28 lines
739 B
JavaScript
28 lines
739 B
JavaScript
import { search } from "@inquirer/prompts"
|
|
import Fuse from "fuse.js";
|
|
|
|
const colors = [
|
|
{ title: 'Red', value: 'red' },
|
|
{ title: 'Green', value: 'green' },
|
|
{ title: 'Blue', value: 'blue' },
|
|
{ title: 'Yellow', value: 'yellow' },
|
|
];
|
|
|
|
(async () => {
|
|
const fuse = new Fuse(colors, {
|
|
keys: ['title'],
|
|
threshold: 0.4,
|
|
})
|
|
const color = await search({
|
|
message: "Pick a color",
|
|
source: async (term) => {
|
|
if (!term) {
|
|
return colors.map(s => s.value);
|
|
}
|
|
const result = fuse.search(term);
|
|
return result.map(s => s.item.value);
|
|
}
|
|
})
|
|
console.log(color); // => { color: 'green' }
|
|
})();
|