Skip to content

Commit c885033

Browse files
committedJan 21, 2015
Add Date#<=> with specs
1 parent d2e435b commit c885033

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed
 

‎CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## edge (upcoming 0.7)
22

3+
* Add `Date#<=>` with specs.
4+
35
* Show extended info and context upon parsing, compiling and building errors
46

57
* Support keyword arguments in method calls and definitions.

‎spec/opal/core/date_spec.rb

+14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@
3434
end
3535
end
3636

37+
describe '<=>' do
38+
it 'returns -1 when self is less than other' do
39+
(Date.new(2015, 1, 1) <=> Date.new(2015, 1, 2)).should == -1
40+
end
41+
42+
it 'returns 0 when self is equal to other' do
43+
(Date.new(2015, 1, 1) <=> Date.new(2015, 1, 1)).should == 0
44+
end
45+
46+
it 'returns 1 when self is greater than other' do
47+
(Date.new(2015, 1, 2) <=> Date.new(2015, 1, 1)).should == 1
48+
end
49+
end
50+
3751
describe "#==" do
3852
it "returns true if self is equal to other date" do
3953
(Date.new(2013, 9, 13) == Date.new(2013, 9, 13)).should == true

‎stdlib/date.rb

+18
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ def >=(other)
8787
}
8888
end
8989

90+
def <=>(other)
91+
%x{
92+
var a = #@date, b = #{other}.date;
93+
a.setHours(0, 0, 0, 0);
94+
b.setHours(0, 0, 0, 0);
95+
96+
if (a < b) {
97+
return -1;
98+
}
99+
else if (a > b) {
100+
return 1;
101+
}
102+
else {
103+
return 0;
104+
}
105+
}
106+
end
107+
90108
def ==(other)
91109
%x{
92110
var a = #@date, b = other.date;

0 commit comments

Comments
 (0)
Please sign in to comment.