use crate::{levenshtein, strprox};
use strprox::MeasuredPrefix;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct Autocompleter {
base: strprox::Autocompleter<'static, u8, u32>,
}
#[wasm_bindgen]
impl Autocompleter {
#[wasm_bindgen(constructor)]
pub fn new(source: js_sys::Array) -> Autocompleter {
let mut strings: Vec<String> = Vec::<String>::with_capacity(source.length() as usize);
for value in source {
if let Some(string) = value.as_string() {
strings.push(string);
}
}
strings.sort_unstable();
strings.dedup();
let mut static_string_refs = Vec::<&'static str>::with_capacity(strings.len());
for string in strings {
static_string_refs.push(Box::leak(Box::new(string)).as_str());
}
let slice: &'static [&'static str] = Box::leak(Box::new(static_string_refs)).as_slice();
let base = strprox::Autocompleter::<'static, u8, u32>::new(slice);
Self { base }
}
pub fn autocomplete(&self, query: &str, requested: usize) -> Vec<MeasuredPrefix> {
self.base.autocomplete(query, requested)
}
}
impl From<strprox::Autocompleter<'static, u8, u32>> for Autocompleter {
fn from(base: strprox::Autocompleter<'static, u8, u32>) -> Self {
Self { base }
}
}
#[wasm_bindgen]
pub fn unindexed_autocomplete(
query: &str,
requested: usize,
source: js_sys::Array,
) -> Vec<MeasuredPrefix> {
let mut internal_strings = Vec::<String>::with_capacity(source.length() as usize);
for value in source {
if let Some(string) = value.as_string() {
internal_strings.push(string);
}
}
let strings: Vec<&str> = internal_strings
.iter()
.map(|string| string.as_str())
.collect();
levenshtein::unindexed_autocomplete(query, requested, &strings)
}