From 3ec2f18b6bc8161c608319dca22f4954a4ee0bc6 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 2 Jan 2013 17:23:29 -0800 Subject: [PATCH] get options and unpack --- moonscript/util.lua | 17 ++++++++++++++++- moonscript/util.moon | 14 +++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/moonscript/util.lua b/moonscript/util.lua index 47ed390..a2cdbe6 100644 --- a/moonscript/util.lua +++ b/moonscript/util.lua @@ -1,4 +1,5 @@ local concat = table.concat +local unpack = unpack or table.unpack local moon = { is_object = function(value) return type(value) == "table" and value.__class @@ -173,6 +174,18 @@ local getfenv = getfenv or function(fn) end return nil end +local get_options +get_options = function(...) + local count = select("#", ...) + local opts = select(count, ...) + if type(opts) == "table" then + return opts, unpack({ + ... + }, nil, count - 1) + else + return { }, ... + end +end return { moon = moon, pos_to_line = pos_to_line, @@ -184,5 +197,7 @@ return { dump = dump, debug_posmap = debug_posmap, getfenv = getfenv, - setfenv = setfenv + setfenv = setfenv, + get_options = get_options, + unpack = unpack } diff --git a/moonscript/util.moon b/moonscript/util.moon index 65fcaae..93d9797 100644 --- a/moonscript/util.moon +++ b/moonscript/util.moon @@ -1,6 +1,8 @@ import concat from table +unpack = unpack or table.unpack + moon = is_object: (value) -> -- is a moonscript object type(value) == "table" and value.__class @@ -105,8 +107,18 @@ getfenv = getfenv or (fn) -> i += 1 nil +-- moves the last argument to the front if it's a table, or returns empty table +-- inserted to the front of args +get_options = (...) -> + count = select "#", ... + opts = select count, ... + if type(opts) == "table" + opts, unpack {...}, nil, count - 1 + else + {}, ... + { :moon, :pos_to_line, :get_closest_line, :get_line, :reversed, :trim, :split, - :dump, :debug_posmap, :getfenv, :setfenv + :dump, :debug_posmap, :getfenv, :setfenv, :get_options, :unpack }