assembly/composables/useSearchFilter.ts
Georges KABBOUCHI 639dd118a3 WIP
2021-07-22 01:08:46 +03:00

30 lines
943 B
TypeScript

import { computed, ref } from '@nuxtjs/composition-api'
/**
* Simple filtering of objects by search term.
*
* @param {Ref<{}[]>} collectionRef Ref of object array with objects which items will get filtered
* @param {...string} propertyNames Names of properties that will be compared with the search term
*/
export function useSearchFilter(collectionRef, ...propertyNames) {
const search = ref('')
const filtered = computed(() => {
if (!search.value) return collectionRef.value
const term = search.value.toLowerCase()
return collectionRef.value.filter((item) => {
const properties = propertyNames.map((propertyName) => item[propertyName]).filter((p) => !!p)
const isTermIncludedInProperties = properties
.map((property) => property.toString().toLowerCase())
.some((property) => property.includes(term))
return isTermIncludedInProperties
})
})
return { filtered, search }
}