Last active
June 21, 2017 04:45
-
-
Save Arwid/c2e0ab1b330d572a5ca244b667e19d2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ = 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