Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pre1ude/03bff2c431d6c4586fc20b8b49658195 to your computer and use it in GitHub Desktop.
Save pre1ude/03bff2c431d6c4586fc20b8b49658195 to your computer and use it in GitHub Desktop.
Helpers for creating class like functions that can be invoked without new
const assoc = ( prop, value, obj ) =>
Object.assign( {}, obj, { [prop]: value })
const reducer = ( $values, accumulate, [key,val] ) => assoc( key, val.bind( undefined, ...$values ), accumulate )
export const bindValuesToMethods = ( $methods, ...$values ) =>
Object.entries( $methods ).reduce( reducer.bind( undefined, $values ), {} )
export const prepareInstance = (instanceMethods, staticMethods = ({}) ) => Object.assign(
bindValuesToMethods.bind( undefined, instanceMethods ),
staticMethods
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment