Permalink
Browse files

fix Array#flatten. do not typecast elements.

  • Loading branch information...
1 parent fabe133 commit 6916a065216204b22e37fe58774cb45be687bf02 @hasclass hasclass committed Feb 26, 2013
View
@@ -740,16 +740,16 @@ class RubyJS.Array extends RubyJS.Object
# @todo do not typecast elements!
#
flatten: (recursion = -1) ->
- recursion = R(recursion)
+ recursion = CoerceProto.to_int_native(recursion)
arr = new R.Array([])
- @each (el) ->
- el = R(el)
- if el?.to_ary? && !recursion.equals(0)
- el.to_ary().flatten(recursion.minus(1)).each (e) -> arr.push(e)
+ @each (element) ->
+ el = R(element)
+ if el?.to_ary? && !(recursion == 0)
+ el.to_ary().flatten(recursion - 1).each (e) -> arr.push(e)
else
- arr.push(el)
+ arr.push(element)
arr
# Inserts the given values before the element with the given index (which
View
@@ -10,7 +10,7 @@ class RubyJS.Hash extends RubyJS.Object
new R.Hash()
constructor: (hsh, default_value) ->
- @__native__ = hsh
+ @__native__ = hsh
@__default__ = default_value
# ---- RubyJSism ------------------------------------------------------------
@@ -271,7 +271,7 @@ class RubyJS.Hash extends RubyJS.Object
else if default_value?.call? || arguments[2]?.call?
(arguments[2] || default_value)(key)
else if default_value != undefined
- default_value
+ default_value
else
throw R.KeyError.new()
@@ -359,14 +359,14 @@ class RubyJS.Hash extends RubyJS.Object
# @alias #index
#
key: (value) ->
- value = R(value)
+ # value = R(value)
if value.rubyjs?
for own k, v of @__native__
return k if value.equals(v)
else
for own k, v of @__native__
- return k if v == value
+ return k if v.valueOf() == value
null
@@ -528,6 +528,15 @@ class RubyJS.Hash extends RubyJS.Object
@__native__[key] = value
+ flatten: (recursion = 1) ->
+ recursion = CoerceProto.to_int_native(recursion)
+ @to_a().flatten(recursion)
+
+ sort: (block) ->
+ @to_a().sort(block)
+
+
+
store: @prototype.set
# Returns the number of key-value pairs in the hash.
@@ -0,0 +1,51 @@
+describe "Hash#flatten", ->
+
+ beforeEach ->
+ @h = R.h({
+ plato: 'greek',
+ witgenstein: ['austrian', 'british'],
+ russell: 'welsh'
+ })
+
+ it "returns an Array", ->
+ expect( R.h({}).flatten() ).toBeInstanceOf(R.Array)
+
+ it "returns an empty Array for an empty Hash", ->
+ expect( R.h({}).flatten() ).toEqual R([])
+
+ it "sets each even index of the Array to a key of the Hash", ->
+ a = @h.flatten().to_native()
+ expect( a[0] ).toEqual 'plato'
+ expect( a[2] ).toEqual 'witgenstein'
+ expect( a[4] ).toEqual 'russell'
+
+ it "sets each odd index of the Array to the value corresponding to the previous element", ->
+ a = @h.flatten().to_native()
+ expect( a[1] ).toEqual 'greek'
+ expect( a[3] ).toEqual ['austrian', 'british']
+ expect( a[5] ).toEqual 'welsh'
+
+ it "does not recursively flatten Array values when called without arguments", ->
+ a = @h.flatten().to_native()
+ expect( a[3] ).toEqual ['austrian', 'british']
+
+ # it "does not recursively flatten Hash values when called without arguments", ->
+ # @h[:russell] = {:born => :wales, :influenced_by => :mill }
+ # a = @h.flatten().to_native()
+ # a[5].should_not == {:born => :wales, :influenced_by => :mill }.flatten
+
+ # it "recursively flattens Array values when called with an argument >= 2", ->
+ # a = @h.flatten(2)
+ # expect( a[3] ).toEqual 'austrian'
+ # expect( a[4] ).toEqual 'british'
+
+ # it "recursively flattens Array values to the given depth", ->
+ # @h[:russell] = [[:born, :wales], [:influenced_by, :mill]]
+ # a = @h.flatten(2)
+ # expect( a[6] ).toEqual [:born, :wales]
+ # expect( a[7] ).toEqual [:influenced_by, :mill]
+
+ # it "raises an TypeError if given a non-Integer argument", ->
+ # lambda do
+ # @h.flatten(Object.new)
+ # end.should raise_error(TypeError)
@@ -1,64 +0,0 @@
-require File.expand_path('../../../spec_helper', __FILE__)
-
-ruby_version_is "1.9" do
- describe "Hash#flatten" do
-
- before(:each) do
- @h = {:plato => :greek,
- :witgenstein => [:austrian, :british],
- :russell => :welsh}
- end
-
- it "returns an Array" do
- new_hash.flatten.should be_an_instance_of(Array)
- end
-
- it "returns an empty Array for an empty Hash" do
- new_hash.flatten.should == []
- end
-
- it "sets each even index of the Array to a key of the Hash" do
- a = @h.flatten
- a[0].should == :plato
- a[2].should == :witgenstein
- a[4].should == :russell
- end
-
- it "sets each odd index of the Array to the value corresponding to the previous element" do
- a = @h.flatten
- a[1].should == :greek
- a[3].should == [:austrian, :british]
- a[5].should == :welsh
- end
-
- it "does not recursively flatten Array values when called without arguments" do
- a = @h.flatten
- a[3].should == [:austrian, :british]
- end
-
- it "does not recursively flatten Hash values when called without arguments" do
- @h[:russell] = {:born => :wales, :influenced_by => :mill }
- a = @h.flatten
- a[5].should_not == {:born => :wales, :influenced_by => :mill }.flatten
- end
-
- it "recursively flattens Array values when called with an argument >= 2" do
- a = @h.flatten(2)
- a[3].should == :austrian
- a[4].should == :british
- end
-
- it "recursively flattens Array values to the given depth" do
- @h[:russell] = [[:born, :wales], [:influenced_by, :mill]]
- a = @h.flatten(2)
- a[6].should == [:born, :wales]
- a[7].should == [:influenced_by, :mill]
- end
-
- it "raises an TypeError if given a non-Integer argument" do
- lambda do
- @h.flatten(Object.new)
- end.should raise_error(TypeError)
- end
- end
-end
@@ -0,0 +1,10 @@
+describe "Hash#sort", ->
+ it "converts self to a nested array of [key, value] arrays and sort with Array#sort", ->
+ hsh = R.h(a: 'b', 1: '2', b: 'a')
+ expect( hsh.sort() ).toEqual R([["1", "2"], ["a", "b"], ["b", "a"]])
+
+ xit "works when some of the keys are themselves arrays", ->
+ # R.h([1,2]: 5, [1,1]: 5).sort.should == [[[1,1],5], [[1,2],5]]
+
+ it "uses block to sort array if passed a block", ->
+ expect( R.h(1: 2, 2: 9, 3: 4).sort( (a,b) -> R(b).cmp(a) ) ).toEqual R([['3', 4], ['2', 9], ['1', 2]])
@@ -1,17 +0,0 @@
-require File.expand_path('../../../spec_helper', __FILE__)
-require File.expand_path('../fixtures/classes', __FILE__)
-
-describe "Hash#sort" do
- it "converts self to a nested array of [key, value] arrays and sort with Array#sort" do
- new_hash('a' => 'b', '1' => '2', 'b' => 'a').sort.should ==
- [["1", "2"], ["a", "b"], ["b", "a"]]
- end
-
- it "works when some of the keys are themselves arrays" do
- new_hash([1,2] => 5, [1,1] => 5).sort.should == [[[1,1],5], [[1,2],5]]
- end
-
- it "uses block to sort array if passed a block" do
- new_hash(1 => 2, 2 => 9, 3 => 4).sort { |a,b| b <=> a }.should == [[3, 4], [2, 9], [1, 2]]
- end
-end

0 comments on commit 6916a06

Please sign in to comment.