Skip to content

Instantly share code, notes, and snippets.

@dougdroper
Created April 2, 2019 15:46
Show Gist options
  • Save dougdroper/2e9364b363bcf3bf6a78f52d05be0f6d to your computer and use it in GitHub Desktop.
Save dougdroper/2e9364b363bcf3bf6a78f52d05be0f6d to your computer and use it in GitHub Desktop.
HELP
const foo = ({bar}) => ({foo: `hellllllo ${bar}`})
const myDefaultFn = (args) => {...args}
const overRides = {
doug: (...args) => foo(args),
...catchEverythingElsePlease: (...args) => myDefaultFn(args)
}
overRides['doug']({bar: "doug"})
overRides['dom']({bar: "Dominic"}) # should default to something
@domtronn
Copy link

domtronn commented Apr 3, 2019

const foo = ({ bar }) => ({ foo: `hellllllo ${bar}` })
const myDefaultFn = (args) => args

const curry = (fn, arity = fn.length, ...args) =>
  arity <= args.length ? fn(...args) : curry.bind(null, fn, arity, ...args)

const sw = curry(
  (o, key, ...args) => o.hasOwnProperty(key)
    ? o[key](...args)
    : o.default(...args)
)

const overRides = sw({
  doug: foo,
  default: myDefaultFn
})

overRides('doug', {bar: "doug"})

overRides('dom', {bar: "Dominic"})

@domtronn
Copy link

domtronn commented Apr 3, 2019

Something alone the lines of that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment