diff --git a/Foundation/CPNumber.j b/Foundation/CPNumber.j index 33bcb73ac..52519091b 100644 --- a/Foundation/CPNumber.j +++ b/Foundation/CPNumber.j @@ -239,6 +239,7 @@ FIXME: Do we need this? { if (typeof self == "boolean") return self ? 1 : 0; + return self; } @@ -246,6 +247,7 @@ FIXME: Do we need this? { if (typeof self == "boolean") return self ? 1 : 0; + return self; } @@ -253,6 +255,15 @@ FIXME: Do we need this? { if (typeof self == "boolean") return self ? 1 : 0; + + return self; +} + +- (int)integerValue +{ + if (typeof self == "boolean") + return self ? 1 : 0; + return self; } @@ -260,6 +271,7 @@ FIXME: Do we need this? { if (typeof self == "boolean") return self ? 1 : 0; + return self; } @@ -267,6 +279,7 @@ FIXME: Do we need this? { if (typeof self == "boolean") return self ? 1 : 0; + return self; } @@ -274,6 +287,7 @@ FIXME: Do we need this? { if (typeof self == "boolean") return self ? 1 : 0; + return self; } @@ -291,6 +305,7 @@ FIXME: Do we need this? { if (typeof self == "boolean") return self ? 1 : 0; + return self; } /* @@ -304,6 +319,7 @@ FIXME: Do we need this? { if (typeof self == "boolean") return self ? 1 : 0; + return self; } @@ -311,6 +327,7 @@ FIXME: Do we need this? { if (typeof self == "boolean") return self ? 1 : 0; + return self; } diff --git a/Foundation/CPString.j b/Foundation/CPString.j index 69e9caf8a..d87c4efd8 100644 --- a/Foundation/CPString.j +++ b/Foundation/CPString.j @@ -731,6 +731,14 @@ var CPStringNull = [CPNull null]; return parseInt(self, 10); } +/*! + Returns the text as an integer +*/ +- (int)integerValue +{ + return parseInt(self, 10); +} + /*! Returns an the path components of this string. This method assumes that the string's content is a '/' diff --git a/Tests/Foundation/CPNumberTest.j b/Tests/Foundation/CPNumberTest.j index 5939a2bc8..4047ea5f4 100644 --- a/Tests/Foundation/CPNumberTest.j +++ b/Tests/Foundation/CPNumberTest.j @@ -12,4 +12,23 @@ [self assertThrows:function () { [34 compare:[CPNull null]] }]; } +- (void)testintValue +{ + var testStrings = [ +// [090, 90], // Removed cause Rhino does not support numbers starting with '0' + [-1, -1], + [3.1415, 3], + [2.7183, 2], + [-0, 0], + [00, 0], + [-00, 0], + [+001, 1], + ]; + + for (var i = 0; i < testStrings.length; i++) + [self assert:[testStrings[i][0] intValue] equals:testStrings[i][1]]; + for (var i = 0; i < testStrings.length; i++) + [self assert:[testStrings[i][0] integerValue] equals:testStrings[i][1]]; +} + @end diff --git a/Tests/Foundation/CPStringTest.j b/Tests/Foundation/CPStringTest.j index 326c781db..5443003c0 100644 --- a/Tests/Foundation/CPStringTest.j +++ b/Tests/Foundation/CPStringTest.j @@ -165,6 +165,25 @@ [self assert:[testStrings[i][0] boolValue] equals:testStrings[i][1]]; } +- (void)testintValue +{ + var testStrings = [ + [" 090", 90], + [" -1", -1], + [" 3.1415", 3], + [" 2.7183", 2], + [" -0", 0], + [" 00", 0], + [" -00", 0], + [" +001", 1], + ]; + + for (var i = 0; i < testStrings.length; i++) + [self assert:[testStrings[i][0] intValue] equals:testStrings[i][1]]; + for (var i = 0; i < testStrings.length; i++) + [self assert:[testStrings[i][0] integerValue] equals:testStrings[i][1]]; +} + - (void)testCommonPrefixWithString { var testStringsCase = [