Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: e735812cb6f3
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 5dc48486185b
Choose a head ref
  • 6 commits
  • 7 files changed
  • 1 contributor

Commits on Oct 26, 2013

  1. Copy the full SHA
    c472744 View commit details
  2. Copy the full SHA
    8a5a1bf View commit details
  3. Copy the full SHA
    9409609 View commit details
  4. Copy the full SHA
    d782f0f View commit details
  5. Add Opal.destructure helper

    meh committed Oct 26, 2013
    Copy the full SHA
    4e8132c View commit details
  6. Copy the full SHA
    5dc4848 View commit details
113 changes: 50 additions & 63 deletions corelib/enumerable.rb
Original file line number Diff line number Diff line change
@@ -1,77 +1,67 @@
module Enumerable
def all?(&block)
%x{
var result = true, proc;
var result = true;
if (block !== nil) {
proc = function(obj) {
var value;
var args = $slice.call(arguments);
self.$each._p = function() {
var value = Opal.$yieldX(block, arguments);
if ((value = block.apply(#{self}, args)) === $breaker) {
return $breaker.$v;
if (value === $breaker) {
result = $breaker.$v;
return $breaker;
}
if (value === false || value === nil) {
if (#{Opal.falsy?(`value`)}) {
result = false;
$breaker.$v = nil;
return $breaker;
}
}
}
else {
proc = function(obj) {
if ((obj === false || obj === nil) && arguments.length < 2) {
result = false;
$breaker.$v = nil;
self.$each._p = function(obj) {
if (arguments.length == 1 && #{Opal.falsy?(`obj`)}) {
result = false;
return $breaker;
}
}
}
#{self}.$each._p = proc;
#{self}.$each();
self.$each();
return result;
}
end

def any?(&block)
%x{
var result = false, proc;
var result = false;
if (block !== nil) {
proc = function(obj) {
var value;
var args = $slice.call(arguments);
self.$each._p = function() {
var value = Opal.$yieldX(block, arguments);
if ((value = block.apply(#{self}, args)) === $breaker) {
return $breaker.$v;
if (value === $breaker) {
result = $breaker.$v;
return $breaker;
}
if (value !== false && value !== nil) {
result = true;
$breaker.$v = nil;
if (#{Opal.truthy?(`value`)}) {
result = true;
return $breaker;
}
}
};
}
else {
proc = function(obj) {
if ((obj !== false && obj !== nil) || arguments.length >= 2) {
result = true;
$breaker.$v = nil;
self.$each._p = function(obj) {
if (arguments.length != 1 || #{Opal.truthy?(`obj`)}) {
result = true;
return $breaker;
}
}
}
#{self}.$each._p = proc;
#{self}.$each();
self.$each();
return result;
}
@@ -83,46 +73,46 @@ def collect(&block)
%x{
var result = [];
var proc = function() {
var value, args = $slice.call(arguments);
self.$each._p = function() {
var value = Opal.$yieldX(block, arguments);
if (block.length > 1 && args.length === 1 && args[0]._isArray) {
args = args[0]
}
if ((value = block.apply(null, args)) === $breaker) {
return $breaker.$v;
if (value === $breaker) {
result = $breaker.$v;
return $breaker;
}
result.push(value);
};
#{self}.$each._p = proc;
#{self}.$each();
self.$each();
return result;
}
end

def reduce(object = undefined, &block)
%x{
var result = #{object} == undefined ? 0 : #{object};
var result = object;
var proc = function() {
var obj = $slice.call(arguments), value;
self.$each._p = function() {
var value = #{Opal.destructure(`arguments`)};
if ((value = block.apply(nil, [result].concat(obj))) === $breaker) {
result = $breaker.$v;
$breaker.$v = nil;
if (result === undefined) {
result = value;
return;
}
value = Opal.$yieldX(block, [result, value]);
if (value === $breaker) {
result = $breaker.$v;
return $breaker;
}
result = value;
};
#{self}.$each._p = proc;
#{self}.$each();
self.$each();
return result;
}
@@ -134,30 +124,27 @@ def count(object = undefined, &block)
if (object != null) {
block = function() {
var param = arguments.length == 1 ?
arguments[0] : $slice.call(arguments);
return #{ `param` == `object` };
return #{Opal.destructure(`arguments`) == `object`};
};
}
else if (block === nil) {
block = function() { return true; };
}
var proc = function() {
var value, param = $slice.call(arguments);
self.$each._p = function() {
var value = Opal.$yieldX(block, arguments);
if ((value = block.apply(null, param)) === $breaker) {
return $breaker.$v;
if (value === $breaker) {
result = $breaker.$v;
return $breaker;
}
if (value !== false && value !== nil) {
if (#{Opal.truthy?(`value`)}) {
result++;
}
}
#{self}.$each._p = proc;
#{self}.$each();
self.$each();
return result;
}
14 changes: 14 additions & 0 deletions corelib/opal.rb
Original file line number Diff line number Diff line change
@@ -70,4 +70,18 @@ def self.falsy?(value)
true
end
end

def self.destructure(args)
%x{
if (args.length == 1) {
return args[0];
}
else if (args._isArray) {
return args;
}
else {
return $slice.call(args);
}
}
end
end
5 changes: 5 additions & 0 deletions spec/opal/enumerable/all_break_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe "Enumerable#all?" do
it "breaks out with the proper value" do
[1, 2, 3].all? { break 42 }.should == 42
end
end
5 changes: 5 additions & 0 deletions spec/opal/enumerable/any_break_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe "Enumerable#any?" do
it "breaks out with the proper value" do
[1, 2, 3].any? { break 42 }.should == 42
end
end
13 changes: 13 additions & 0 deletions spec/opal/enumerable/collect_break_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
describe "Enumerable#collect" do
class Test
include Enumerable

def each(&block)
[1, 2, 3].each(&block)
end
end

it "breaks out with the proper value" do
Test.new.collect { break 42 }.should == 42
end
end
5 changes: 5 additions & 0 deletions spec/opal/enumerable/count_break_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe "Enumerable#count" do
it "breaks out with the proper value" do
[1, 2, 3].count { break 42 }.should == 42
end
end
5 changes: 5 additions & 0 deletions spec/opal/enumerable/reduce_break_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe "Enumerable#reduce" do
it "breaks out with the proper value" do
[1, 2, 3].reduce { break 42 }.should == 42
end
end