import "pkg:/source/utils/ArrayUtils.bs" import "pkg:/source/utils/RegistryUtils.bs" import "pkg:/source/utils/StringUtils.bs" namespace SearchHistoryUtils function Save(preferencesNode as object, q as string, maxItems = 10 as integer) as void seachHistoryEnabled = preferencesNode["search_history.enabled"] if not seachHistoryEnabled return end if history = GetAllSaved(preferencesNode) index = ArrayUtils.IndexOfString(history, q) if index <> -1 history.Delete(index) end if history.Unshift(q) if history.Count() > maxItems history.Pop() end if history = FormatJson(history) RegistryUtils.Write(RegistryUtils.SEARCH_HISTORY, history) end function function GetSaved(preferencesNode as object, q as string) as object history = GetAllSaved(preferencesNode) if history.count() = 0 return [] end if if StringUtils.IsNullOrEmpty(q) return history end if result = [] for each keyword in history if keyword.InStr(q) = 0 result.push(keyword) end if end for return result end function function GetAllSaved(preferencesNode as object) as object seachHistoryEnabled = preferencesNode["search_history.enabled"] if not seachHistoryEnabled return [] end if history = RegistryUtils.Read(RegistryUtils.SEARCH_HISTORY) if history = invalid return [] end if history = StringUtils.ParseJsonSafe(history) if not IsArray(history) return [] end if return history end function function Clear() RegistryUtils.Delete(RegistryUtils.SEARCH_HISTORY) end function end namespace