From d7dd18b4761a9d607c1e60465e4c5cf5ea0c7688 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 26 Dec 2009 21:55:56 -0800 Subject: [PATCH] more underscore, and removing custom_assign and return from conditional compilation --- examples/underscore.coffee | 41 +++++++++++++++++--------------------- lib/coffee_script/nodes.rb | 10 ++++++---- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/examples/underscore.coffee b/examples/underscore.coffee index 3b709bb3..078074bb 100644 --- a/examples/underscore.coffee +++ b/examples/underscore.coffee @@ -93,31 +93,26 @@ _.reduceRight: obj, memo, iterator, context => if obj and _.isFunction(obj.filter) then return obj.filter(iterator, context). results: [] _.each(obj, (value, index, list => - iterator.call(context, value, index, list) and results.push(value).)) + results.push(value) if iterator.call(context, value, index, list).)) results. -# -# # Return all the elements for which a truth test fails. -# _.reject = function(obj, iterator, context) { -# var results = []; -# _.each(obj, function(value, index, list) { -# !iterator.call(context, value, index, list) && results.push(value); -# }); -# return results; -# }; -# -# # Determine whether all of the elements match a truth test. Delegate to -# # JavaScript 1.6's every(), if it is present. -# _.all = function(obj, iterator, context) { -# iterator = iterator || _.identity; -# if (obj && _.isFunction(obj.every)) return obj.every(iterator, context); -# var result = true; -# _.each(obj, function(value, index, list) { -# if (!(result = result && iterator.call(context, value, index, list))) _.breakLoop(); -# }); -# return result; -# }; -# + # Return all the elements for which a truth test fails. + _.reject: obj, iterator, context => + results: [] + _.each(obj, (value, index, list => + results.push(value) if not iterator.call(context, value, index, list).)) + results. + + # Determine whether all of the elements match a truth test. Delegate to + # JavaScript 1.6's every(), if it is present. + _.all: obj, iterator, context => + iterator ||= _.identity + return obj.every(iterator, context) if obj and _.isFunction(obj.every) + result: true + _.each(obj, (value, index, list => + _.breakLoop() unless result: result and iterator.call(context, value, index, list).)) + result. + # # Determine if at least one element in the object matches a truth test. Use # # JavaScript 1.6's some(), if it exists. # _.any = function(obj, iterator, context) { diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index 46c2ad74..bfe1aead 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -377,12 +377,11 @@ module CoffeeScript last = @variable.last.to_s proto = name[PROTO_ASSIGN, 1] o = o.merge(:assign => @variable, :last_assign => last, :proto_assign => proto) - postfix = o[:return] ? ";\n#{o[:indent]}return #{name}" : '' return write("#{name}: #{@value.compile(o)}") if @context == :object - return write("#{name} = #{@value.compile(o)}#{postfix}") if @variable.properties? && !@value.custom_assign? o[:scope].find(name) unless @variable.properties? return write(@value.compile(o)) if @value.custom_assign? - write("#{name} = #{@value.compile(o)}#{postfix}") + val = "#{name} = #{@value.compile(o)}" + write(o[:return] && !@value.custom_return? ? "return (#{val})" : val) end end @@ -728,8 +727,11 @@ module CoffeeScript # force sub-else bodies into statement form. def compile_statement(o) indent = o[:indent] + cond_o = o.dup + cond_o.delete(:assign) + cond_o.delete(:return) o[:indent] += TAB - if_part = "if (#{@condition.compile(o)}) {\n#{Expressions.wrap(@body).compile(o)}\n#{indent}}" + if_part = "if (#{@condition.compile(cond_o)}) {\n#{Expressions.wrap(@body).compile(o)}\n#{indent}}" return if_part unless @else_body else_part = chain? ? " else #{@else_body.compile(o.merge(:indent => indent))}" :