Write a function. Test real cases. Improve your solution.
Given command objects {id, title} and a query string, return matching ids for a command palette. Compare titles and the trimmed query using JavaScript toLowerCase. A nonempty query must appear anywhere in a title. Rank prefix matches first, then other substring matches; within either group, shorter original titles rank first, and equal-length titles keep input order. An empty trimmed query returns all ids in input order without sorting. Titles and ids are strings. Do not trim titles; measure their length with JavaScript .length. Leave the input unchanged.
[
[
{
"id": "b",
"title": "Reopen"
},
{
"id": "a",
"title": "Open workspace"
},
{
"id": "c",
"title": "Open"
}
],
"open"
][ "c", "a", "b" ]
[
[
{
"id": "a",
"title": "Save File"
},
{
"id": "b",
"title": "Autosave"
},
{
"id": "c",
"title": "Close"
}
],
" SAVE "
][ "a", "b" ]
[
[
{
"id": "a",
"title": "Open"
}
],
"delete"
][]
[
[
{
"id": "long",
"title": "Long command"
},
{
"id": "short",
"title": "Go"
}
],
" "
][ "long", "short" ]
[
[
{
"id": "first",
"title": "Open B"
},
{
"id": "second",
"title": "Open A"
},
{
"id": "third",
"title": "OPEN C"
}
],
"open"
][ "first", "second", "third" ]
[
[
{
"id": "long",
"title": "Run task"
},
{
"id": "short",
"title": "a task"
},
{
"id": "prefix",
"title": "Task settings"
}
],
"task"
][ "prefix", "short", "long" ]
[ [], "open" ]
[]
[
[
{
"id": "leading",
"title": " Open"
},
{
"id": "prefix",
"title": "Open settings"
}
],
"open"
][ "prefix", "leading" ]
Your code runs in your browser. These exercises use synchronous JavaScript functions and built-in data types; DOM access, network requests, and npm imports are unavailable. Runs stop after about four seconds. Test results support self-study and don’t certify proficiency.