You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
651 B
22 lines
651 B
const SEARCH_HISTORY_LIMIT = 5
|
|
|
|
export function getSearchHistory(storageKey) {
|
|
if (!storageKey) return []
|
|
const cached = uni.getStorageSync(storageKey)
|
|
if (!Array.isArray(cached)) return []
|
|
return cached
|
|
.map(item => String(item || '').trim())
|
|
.filter(Boolean)
|
|
.slice(0, SEARCH_HISTORY_LIMIT)
|
|
}
|
|
|
|
export function saveSearchHistory(storageKey, keyword) {
|
|
const word = String(keyword || '').trim()
|
|
const current = getSearchHistory(storageKey)
|
|
if (!storageKey || !word) return current
|
|
const next = [word]
|
|
.concat(current.filter(item => item !== word))
|
|
.slice(0, SEARCH_HISTORY_LIMIT)
|
|
uni.setStorageSync(storageKey, next)
|
|
return next
|
|
}
|
|
|