JSFiddle - React, Tailwind, and code Playground

by brigand

JavaScript

const obj = {
  foo: {
    bar: () => "meow"
  },
  bar: {
    foo: () => "woof"
  }
}
let pattern = ["foo.bar", "bar.foo", "foo.foo"];
const validate = (arr) => arr.map(p => {
  const path = p.split('.');
  const fn = obj[path[0]][path[1]];
  // invalid patterns should return a function that return themselves
  if (typeof fn === 'function') {
    return fn;
  } else {
    return () => p;
  }
});
let validated = validate(pattern);
console.log(validated)
console.log(validated.map(f => f())); // done in a loop