Skip to content

Instantly share code, notes, and snippets.

@Arwid
Last active June 21, 2017 04:45
Show Gist options
  • Save Arwid/c2e0ab1b330d572a5ca244b667e19d2a to your computer and use it in GitHub Desktop.
Save Arwid/c2e0ab1b330d572a5ca244b667e19d2a to your computer and use it in GitHub Desktop.
_ = require "lodash"
DEFAULT_OPTIONS =
filter: /_/
silent: false
camelize = (obj, options) ->
options = _.merge {}, DEFAULT_OPTIONS, options
methodNames = _
.chain obj
.keys()
.filter (name) -> name.match(options.filter)
.filter (name) -> name isnt _.camelCase(name)
.value()
_.each methodNames, (name) =>
camelCasedName = _.camelCase name
if obj[camelCasedName]
unless options.silent
throw new Error "Method #{camelCasedName} already exists"
else
obj[camelCasedName] = obj[name]
camelize model.Instance.prototype, /^(get|set|create).*(_.*)+/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment