Comparing version 0.2.4 to 0.2.5
@@ -15,3 +15,3 @@ # Numbers | ||
- `#arg` | ||
-- Returns PI if negative, 0 otherwise. | ||
-- Returns PI if negative, `0` otherwise. | ||
@@ -21,4 +21,7 @@ - `#ceil` | ||
- `#even` | ||
-- Returns `true` if number is even, `false` otherwise. | ||
- `#finite` | ||
-- Returns true if number is in the finite range, false otherwise. | ||
-- Returns `true` if number is in the finite range, `false` otherwise. | ||
@@ -29,9 +32,9 @@ - `#floor` | ||
- `#integer` | ||
-- Returns true if value is an integer, false otherwise. | ||
-- Returns `true` if value is an integer, `false` otherwise. | ||
- `#nonzero` | ||
-- Return *self* if | ||
-- Return *self* if value is not `0`, `false` otherwise. | ||
- `#polar` | ||
-- Returns an array containing the absolute value, and either PI if the value was negative, or 0 otherwise. | ||
-- Returns an array containing the absolute value, and either PI if the value was negative, or `0` otherwise. | ||
@@ -43,3 +46,11 @@ ```JavaScript | ||
## Major | ||
## Major | ||
- `#downto(limit, fn, after)` | ||
-- Iterates the given function, passing in integer values from *self* down to and including limit.. Optional second parameter: if *after* is a function, the return value from `#times` is the return value of invoking that function. If *after* is anything else, that is the return value. | ||
- `#times(fn, after)` | ||
-- Loops `1`-*self* times, invoking `fn` each time. First parameter passed to `fn` is is the current loop number. Optional second parameter: if *after* is a function, the return value from `#times` is the return value of invoking that function. If *after* is anything else, that is the return value. | ||
- `#upto(limit, fn, after)` | ||
-- Iterates the given block, passing in integer values from *self* up to and including limit. Optional second parameter: if *after* is a function, the return value from `#times` is the return value of invoking that function. If *after* is anything else, that is the return value. |
@@ -10,12 +10,12 @@ # Objects | ||
- `#array` | ||
-- Returns `true` if *self* is an array, false otherwise. | ||
-- Returns `true` if *self* is an array, `false` otherwise. | ||
- `#bool` | ||
-- Returns `true` if *self* is a boolean, false otherwise. | ||
-- Returns `true` if *self* is a boolean, `false` otherwise. | ||
- `#func` | ||
-- Returns `true` if *self* is a function, false otherwise. | ||
-- Returns `true` if *self* is a function, `false` otherwise. | ||
- `#numeric` | ||
-- Returns `true` if *self* is a numeric value, false otherwise. `NaN` is *not* numeric, `Infinity` is. | ||
-- Returns `true` if *self* is a numeric value, `false` otherwise. `NaN` is *not* numeric, `Infinity` is. | ||
@@ -30,3 +30,3 @@ - `#object` | ||
- `#string` | ||
-- Returns `true` if *self* is a string value, false otherwise. | ||
-- Returns `true` if *self* is a string value, `false` otherwise. | ||
@@ -40,2 +40,2 @@ - `#type` | ||
- `#eql(value)` | ||
-- Returns true if *self* strictly equals the supplied argument. | ||
-- Returns `true` if *self* strictly equals the supplied argument, `false` otherise. |
@@ -21,3 +21,3 @@ # Strings | ||
- `#downcase` | ||
-- Alias of `String.prototype.toLowerCase()`. | ||
-- Returns string with all character to lowercase. | ||
@@ -37,7 +37,10 @@ - `#empty` | ||
- `#upcase` | ||
-- Alias of `String.prototype.toUpperCase()`. | ||
-- Returns string with all characters to uppercase; | ||
## Major | ||
- `#each(fn, after)` | ||
-- Iterates through the string. *fn* is passed the current character, and the index. Optional second parameter: if *after* is a function, the return value from `#each` is the return value of invoking that function. If *after* is anything else, that is the return value. | ||
- `#prepend(val)` | ||
-- Returns a new string with *val* added to the beginning of the string. |
{ | ||
"name": "rubyisms", | ||
"version": "0.2.4", | ||
"description": "Prototype extensions.", | ||
"version": "0.2.5", | ||
"description": "Ruby style ES5 prototype extensions.", | ||
"keywords": [ | ||
"ES5", | ||
"extensions", | ||
"javascript", | ||
"native", | ||
"prototype", | ||
"rubyisms" | ||
], | ||
"main": "rubyisms.js", | ||
@@ -6,0 +14,0 @@ "repository": { |
@@ -123,2 +123,5 @@ // Colin 'Oka' Hall-Coates | ||
}, | ||
even: function () { | ||
return this.toFixed() == this && this % 2 === 0; | ||
}, | ||
finite: function () { | ||
@@ -133,5 +136,15 @@ return isFinite(this); | ||
}, | ||
next: function () { | ||
if (this.toFixed() != this || this === Infinity) { | ||
throw new TypeError('Self must be integer'); | ||
} | ||
return this + 1; | ||
}, | ||
nonzero: function () { | ||
return (this !== 0 ? this: null); | ||
}, | ||
odd: function () { | ||
return this.toFixed() == this && this % 2 !== 0; | ||
}, | ||
polar: function () { | ||
@@ -142,2 +155,9 @@ return (isFinite(this) && | ||
}, | ||
pred: function () { | ||
if (this.toFixed() != this || this === Infinity) { | ||
throw new TypeError('Self must be integer'); | ||
} | ||
return this - 1; | ||
}, | ||
round: function () { | ||
@@ -283,3 +303,39 @@ return Math.round(this); | ||
major(Number.prototype, {}); | ||
major(Number.prototype, { | ||
downto: function (n, fn, after) { | ||
var type = Object.prototype.toString.call(n); | ||
if (type !== '[object Number]' || n.toFixed() != n) { | ||
throw new TypeError('Integer value required'); | ||
} | ||
if (this.toFixed() != this || this === Infinity) { | ||
throw new TypeError('Self is not an integer'); | ||
} | ||
if (typeof fn !== 'function') throw new TypeError('Expected function'); | ||
for (var i = this; i >= n; i--) { | ||
fn.call(this, i); | ||
} | ||
return (typeof after === 'function'? after.call(this) : after); | ||
}, | ||
times: function (fn, after) { | ||
if (typeof fn !== 'function') throw new TypeError('Expected function'); | ||
for (var i = 1; i <= this; i++) { | ||
fn.call(this, i); | ||
} | ||
return (typeof after === 'function'? after.call(this) : after); | ||
}, | ||
upto: function (n, fn, after) { | ||
var type = Object.prototype.toString.call(n); | ||
if (type !== '[object Number]' || n.toFixed() != n) { | ||
throw new TypeError('Integer value required'); | ||
} | ||
if (this.toFixed() != this || this === Infinity) { | ||
throw new TypeError('Self is not an integer'); | ||
} | ||
if (typeof fn !== 'function') throw new TypeError('Expected function'); | ||
for (var i = this; i <= n; i++) { | ||
fn.call(this, i); | ||
} | ||
return (typeof after === 'function'? after.call(this) : after); | ||
} | ||
}); | ||
@@ -293,2 +349,9 @@ major(Object.prototype, { | ||
major(String.prototype, { | ||
each: function (fn, after) { | ||
if (typeof fn !== 'function') throw new TypeError('Expected function'); | ||
for (var i = 0; i < this.length; i++) { | ||
fn.call(this, this[i], i); | ||
} | ||
return (typeof after === 'function'? after.call(this) : after); | ||
}, | ||
prepend: function (o) { | ||
@@ -295,0 +358,0 @@ return o + this; |
@@ -16,3 +16,5 @@ var chai = require('chai'), | ||
pos = 7, | ||
posf = 12.4; | ||
posf = 12.4, | ||
even = 10, | ||
odd = 11; | ||
@@ -55,2 +57,10 @@ describe('#abs', function () { | ||
describe('#even', function () { | ||
it('should return true if value is even, false otherwise', function () { | ||
expect(even.even).to.be.true; | ||
expect(odd.even).to.be.false; | ||
expect(posf.even).to.be.false; | ||
}); | ||
}); | ||
describe('#finite', function () { | ||
@@ -89,2 +99,20 @@ it('should return true or false if number is finite', function () { | ||
describe('#next', function () { | ||
var i = 5, | ||
f = 5.5; | ||
it('should return self + 1, if self is integer value', function () { | ||
expect(i.next).to.equal(6); | ||
}); | ||
it('should throw TypeError if self is not integer value', function () { | ||
expect(function () {f.next}).to.throw(TypeError); | ||
expect(function () {NaN.next}).to.throw(TypeError); | ||
expect(function () {Infinity.next}).to.throw(TypeError); | ||
}); | ||
it('should not effect the original value', function () { | ||
i.should.equal(5); | ||
}); | ||
}); | ||
describe('#nonzero', function () { | ||
@@ -98,2 +126,10 @@ it('should return self if not 0, null otherwise', function () { | ||
describe('#odd', function () { | ||
it('should return true if value is odd, false otherwise', function () { | ||
expect(odd.odd).to.be.true; | ||
expect(even.odd).to.be.false; | ||
expect(posf.odd).to.be.false; | ||
}); | ||
}); | ||
describe('#polar', function () { | ||
@@ -110,2 +146,20 @@ it('should return [absolute value of number, (PI if negative, 0 otherwise)]', function () { | ||
describe('#pred', function () { | ||
var i = 5, | ||
f = 5.5; | ||
it('should return self + 1, if self is integer value', function () { | ||
expect(i.pred).to.equal(4); | ||
}); | ||
it('should throw TypeError if self is not integer value', function () { | ||
expect(function () {f.pred}).to.throw(TypeError); | ||
expect(function () {NaN.pred}).to.throw(TypeError); | ||
expect(function () {Infinity.pred}).to.throw(TypeError); | ||
}); | ||
it('should not effect the original value', function () { | ||
i.should.equal(5); | ||
}); | ||
}); | ||
describe('#round', function () { | ||
@@ -133,2 +187,102 @@ it('should return the value rounded to the nearest integer', function () { | ||
// Major methods | ||
describe('#downto()', function () { | ||
var i = 20, | ||
l = 15; | ||
it('should loop from self to limit, invoking its callback function', function () { | ||
var out = []; | ||
i.downto(l, function (n) { | ||
out.push(n); | ||
}); | ||
out.should.deep.equal([20, 19, 18, 17, 16, 15]); | ||
}); | ||
it('should return second parameter', function () { | ||
expect(i.downto(l, function (i) { | ||
return i; | ||
}, 'hello!')).to.equal('hello!'); | ||
}); | ||
it('should invoke and return second parameter if function', function () { | ||
expect(i.downto(l, function (i) { | ||
return i; | ||
}, function () { | ||
return 'hello!'; | ||
})).to.equal('hello!'); | ||
}); | ||
it('should not affect own value', function () { | ||
i.should.equal(20); | ||
}); | ||
it('should not effect limit value', function () { | ||
l.should.equal(15); | ||
}); | ||
}); | ||
describe('#times()', function () { | ||
var i = 5; | ||
it('should loop n times invoking its callback function', function () { | ||
var out = []; | ||
i.times(function (i) { | ||
out.push(i); | ||
}); | ||
out.should.deep.equal([1, 2, 3, 4, 5]); | ||
}); | ||
it('should return second parameter', function () { | ||
expect(i.times(function (i) { | ||
return i; | ||
}, 'hello!')).to.equal('hello!'); | ||
}); | ||
it('should invoke and return second parameter if function', function () { | ||
expect(i.times(function (i) { | ||
return i; | ||
}, function () { | ||
return 'hello!'; | ||
})).to.equal('hello!'); | ||
}); | ||
it('should not affect own value', function () { | ||
i.should.equal(5); | ||
}); | ||
}); | ||
describe('#upto()', function () { | ||
var i = 5, | ||
l = 10; | ||
it('should loop from self to limit, invoking its callback function', function () { | ||
var out = []; | ||
i.upto(l, function (n) { | ||
out.push(n); | ||
}); | ||
out.should.deep.equal([5, 6, 7, 8, 9, 10]); | ||
}); | ||
it('should return second parameter', function () { | ||
expect(i.upto(l, function (i) { | ||
return i; | ||
}, 'hello!')).to.equal('hello!'); | ||
}); | ||
it('should invoke and return second parameter if function', function () { | ||
expect(i.upto(l, function (i) { | ||
return i; | ||
}, function () { | ||
return 'hello!'; | ||
})).to.equal('hello!'); | ||
}); | ||
it('should not affect own value', function () { | ||
i.should.equal(5); | ||
}); | ||
it('should not effect limit value', function () { | ||
l.should.equal(10); | ||
}); | ||
}); | ||
}); |
@@ -135,2 +135,19 @@ var chai = require('chai'), | ||
describe('#each()', function () { | ||
var foo = 'hello', | ||
out = []; | ||
foo.each(function (e) { | ||
out.push(e); | ||
}); | ||
it('should it iterate through the string', function () { | ||
expect(out.join('')).to.equal(foo); | ||
out.should.deep.equal(['h', 'e', 'l', 'l', 'o']); | ||
}); | ||
it('should not affect the original', function () { | ||
foo.should.equal('hello'); | ||
}) | ||
}); | ||
describe('#prepend()', function () { | ||
@@ -137,0 +154,0 @@ var foo = 'world', |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
260164
1010