TeamAntioch / screference

An application for viewing reference images of StarCraft 2 cliffs, doodads, tilesets, and map inspirations.
0 stars 0 forks source link

Simplyfy filter function #1

Open abrahamYG opened 5 years ago

abrahamYG commented 5 years ago

https://github.com/TeamAntioch/screference/blob/85b7f8e2c1b0021bbe29069cea398fd5f1372f94/src/components/ImageGallery.vue#L66

function wordAppears(image) {
          let splitFilters = filter.split(',');
          let matches = 0;
          splitFilters.forEach(function(element){
            if (image.name.toLowerCase().indexOf(element) >= 0) {
              matches += 1;
            }
          });
          return matches > 0;
        }
        const filteredImages = images.filter(wordAppears);

Maybe this could be condensed to

filteredImages = images.filter(
    ({name})=>(
        myfilter.split(",").reduce(
            (acc,filter)=>acc||(name.toLowerCase().includes(filter)),false
        )
    )
)
abrahamYG commented 5 years ago

Ok, i think this could add more filtering options

const filters = "Korhal,Tarsonis,+City,-Man".split(",")
const inclusionFilters = filters.filter(f=>f.startsWith("+"));
const exclusionFilters = filters.filter(f=>f.startsWith("-"));
const regularFilters = filters.filter(f=>!exclusionFilters.includes(f)&&!inclusionFilters.includes(f))

images.filter(({name})=>{
    return regularFilters.reduce((acc,filter)=>{return acc||name.toLowerCase().includes(filter.toLowerCase())},false) && 
    exclusionFilters.reduce((acc,filter)=>{return acc&&!name.toLowerCase().includes(filter.toLowerCase().replace("-",""))},true) && 
    inclusionFilters.reduce((acc,filter)=>{return acc&&name.toLowerCase().includes(filter.toLowerCase().replace("+",""))},true);
})

/* 
returns in cliffs
[
    {
        "name": "Korhal City Ex2",
        "url": "Korhal_City_Ex2.jpg",
        "tags": ""
    },
    {
        "name": "TarsonisCityCliff0",
        "url": "TarsonisCityCliff0.jpg",
        "tags": ""
    },
    {
        "name": "TarsonisCityCliff1",
        "url": "TarsonisCityCliff1.jpg",
        "tags": ""
    }
]
excludes
{name: "Korhal City Manmade", url: "Korhal_City_Manmade.jpg", tags: ""}
*/