Permalink
Browse files

extract options tool

  • Loading branch information...
Nicolae Claudius
Nicolae Claudius committed Aug 21, 2011
1 parent 465378a commit 49793089db6455b05777ee8bc0ff94b5e3c14a85
Showing with 26 additions and 1 deletion.
  1. +2 −1 package.json
  2. +9 −0 test/extract_options.coffee
  3. +15 −0 tools/extract_options.coffee
View
@@ -18,7 +18,8 @@
},
"dependencies": {
"coffee-script": ">= 1.1.1",
- "underscore": ">= 1.1.7"
+ "underscore": ">= 1.1.7",
+ "lingo": ">= 0.0.4"
},
"devDependencies": {
"expresso": ">= 0.8.1"
@@ -0,0 +1,9 @@
+assert = require 'assert'
+metaCode = require '../index'
+
+# test the extract options tool
+exports.extractOptions = ->
+ assert.eql {}, metaCode.extractOptions []
+ assert.eql {}, metaCode.extractOptions [1, 2]
+ assert.eql {a: 1, b: 2}, metaCode.extractOptions [1, 2, {a: 1, b: 2}]
+
@@ -0,0 +1,15 @@
+# Extracts options from a set of arguments. Removes and returns the last
+# element in the array if it's a hash, otherwise returns a blank hash.
+#
+# metaCode = require 'meta_code'
+# metaCode.extract_options(1, 2) # => {}
+# metaCode.extract_options(1, 2, a: 1) # => {a: 1}
+#
+extract_options = (args) ->
+ throw new Error("args must be an Array") unless args instanceof Array
+ last = args[args.length - 1]
+ if typeof last == 'object' then args.pop() else {}
+
+
+module.exports = extract_options
+

0 comments on commit 4979308

Please sign in to comment.