Skip to content

Instantly share code, notes, and snippets.

@codeAdrian
Last active May 2, 2023 09:20
Show Gist options
  • Save codeAdrian/101afa24cded00d743fcae966c497552 to your computer and use it in GitHub Desktop.
Save codeAdrian/101afa24cded00d743fcae966c497552 to your computer and use it in GitHub Desktop.
/* SAMPLE DATA */
const DATA = [
{
id: "1",
name: "Super mega mall",
address: "Some address 1",
city: "Super Mega City",
services: ["restaurant", "caffee", "shops", "cinema", "arcade"]
},
{
id: "2",
name: "Super cool mall",
address: "Some address 2",
city: "Super Cool City",
services: ["caffee", "shops", "cinema", "arcade"]
},
{
id: "3",
name: "Super medium mall",
address: "Some address 3",
city: "Super Medium City",
services: ["caffee", "shops", "arcade"]
},
{
id: "4",
name: "Super small mall",
address: "Some address 4",
city: "Small Town",
services: ["caffee", "shops"]
},
{
id: "5",
name: "Just shops mall",
address: "Some address 5",
city: "Really Small Town",
services: ["shops"]
},
{
id: "6",
name: "Party town arcade",
address: "Some address 6",
city: "Some Other Town",
services: ["caffee", "restaurants", "arcade"]
}
];
const FILTERS = [
"all",
"restaurant",
"caffee",
"shops",
"cinema",
"arcade"
];
/* PROXY */
const isService = (prop) => FILTERS.includes(prop.toString());
const handleGet = (obj, prop) => {
if (prop in obj) {
return Reflect.get(obj, prop);
}
if (prop === "all") {
return obj;
}
if (isService(prop)) {
return obj.filter(({ services }) => services.includes(prop));
}
return undefined;
};
const createStoresProxy = (stores) => {
const proxy = new Proxy(stores, {
get: handleGet
});
return proxy;
};
/* INIT */
const stores = createStoresProxy(DATA);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment