Comparing version 0.2.1 to 0.2.2
# Documentation | ||
- Arrays | ||
- Booleans | ||
- Functions | ||
- Numbers | ||
- Objects | ||
- Strings | ||
- [Arrays](arrays) | ||
- [Booleans](booleans) | ||
- [Functions](functions) | ||
- [Numbers](numbers) | ||
- [Objects](objects) | ||
- [Strings](strings) |
{ | ||
"name": "rubyisms", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Prototype extensions.", | ||
@@ -5,0 +5,0 @@ "main": "rubyisms.js", |
<p align="center"> | ||
<img src="http://i.imgur.com/Gx7OFGO.png" /> | ||
<a href="https://www.npmjs.com/package/rubyisms"> | ||
<img src="http://i.imgur.com/Gx7OFGO.png" /> | ||
</a> | ||
</p> | ||
# rubyisms | ||
# Rubyisms | ||
**Ruby style ES5 prototype extensions** | ||
## info | ||
## Info | ||
A highly experimental set of prototype extensions for types in ES5 JavaScript. | ||
A highly experimental set of prototype extensions for native types in ES5 JavaScript. | ||
## install | ||
## Install | ||
Using [npm](npm). | ||
Using [npm](https://www.npmjs.com/). | ||
npm install rubyisms | ||
[npm]: https://www.npmjs.com/ | ||
## Documentation | ||
~~Full~~ list of methods [here](https://github.com/Oka-/rubyisms/tree/master/docs). | ||
## Issues | ||
### `Object.prototype` | ||
Rubyisms defines properties on `Object.prototype`. A side effect is the creation of globally scoped *constants*. The following should be treated as reserved keywords in the global scope, and are not writable. | ||
- `array` | ||
- `bool` | ||
- `func` | ||
- `numeric` | ||
- `object` | ||
- `size` | ||
- `type` | ||
*Everything* created will contain these as prototype properties. To override these properties on objects you must define them during construction, or explicitly define them with `Object.defineProperty`. | ||
No good. | ||
```JavaScript | ||
var o = {}; | ||
o.size = 5 // Will not write | ||
o.size // 0 | ||
function Constructor () { | ||
this.size = 5; // Will not write | ||
} | ||
new Constructor().size // 0 | ||
``` | ||
Better. | ||
```JavaScript | ||
var o = {size: 5}; | ||
o.size // 5 | ||
function Constructor () { | ||
Object.defineProperty(this, 'size', { | ||
value: 5, | ||
writable: true | ||
}); | ||
} | ||
new Constructor().size // 5 | ||
``` | ||
### `.type` | ||
Return values from calling `.type` on the global object will differ. In V8 (Chrome, Node), the return string is `'global'`, whereas in other browsers it is `'window'`. |
@@ -30,2 +30,5 @@ // Colin 'Oka' Hall-Coates | ||
}), | ||
sample: $(function () { | ||
return this[Math.floor(Math.random() * this.length)]; | ||
}), | ||
uniq: $(function () { | ||
@@ -51,2 +54,11 @@ var c = {}; | ||
}), | ||
abs2: $(function () { | ||
return this * this; | ||
}), | ||
arg: $(function () { | ||
return (this < 0 ? Math.PI : 0); | ||
}), | ||
ceil: $(function () { | ||
return Math.ceil(this); | ||
}), | ||
finite: $(function () { | ||
@@ -61,2 +73,5 @@ return isFinite(this); | ||
}), | ||
nonzero: $(function () { | ||
return (this !== 0 ? this: null); | ||
}), | ||
polar: $(function () { | ||
@@ -69,2 +84,5 @@ return (isFinite(this) && | ||
return Math.round(this); | ||
}), | ||
zero: $(function () { | ||
return this === 0; | ||
}) | ||
@@ -100,18 +118,4 @@ }); | ||
type: $(function () { | ||
var t = OP.toString.call(this) | ||
switch(t) { | ||
case '[object Array]': | ||
return 'array'; | ||
case '[object Boolean]': | ||
return 'boolean'; | ||
case '[object Function]': | ||
return 'function'; | ||
case '[object Number]': | ||
if (this === this) return 'number'; | ||
else return 'NaN'; | ||
case '[object Object]': | ||
return 'object'; | ||
case '[object String]': | ||
return 'string'; | ||
} | ||
var t = OP.toString.call(this).match(/\w+(?=\])/)[0].toLowerCase(); | ||
return (t === 'number' && this !== this ? 'NaN' : t); | ||
}) | ||
@@ -127,2 +131,5 @@ }); | ||
}), | ||
chop: $(function () { | ||
return this.slice(0, this.length - 1); | ||
}), | ||
chr: $(function () { | ||
@@ -142,2 +149,11 @@ return this.substring(0, 1); | ||
}), | ||
swapcase: $(function () { | ||
var s = '', i, c; | ||
for (i = 0; i < this.length; i++) { | ||
c = this[i].toUpperCase(); | ||
if (c === this[i]) c = this[i].toLowerCase(); | ||
s += c; | ||
} | ||
return s; | ||
}), | ||
upcase: $(function () { | ||
@@ -152,2 +168,8 @@ return this.toUpperCase(); | ||
_p: AP, | ||
assoc: function (key) { | ||
for (var i = 0; i < this.length; i++) { | ||
if (OP.toString.call(this[i]) !== '[object Object]') continue; | ||
if (this[i].hasOwnProperty(key)) return this[i]; | ||
} | ||
}, | ||
drop: AP.slice, | ||
@@ -160,2 +182,10 @@ fetch: function(n, d) { | ||
}, | ||
reject: function (f) { | ||
var a = [], i; | ||
for (i = 0; i < this.length; i++) { | ||
if (!f.call(null, this[i], i)) a.push(this[i]); | ||
} | ||
return a; | ||
}, | ||
select: AP.filter, | ||
take: function (n) { | ||
@@ -162,0 +192,0 @@ return this.slice().splice(0, n); |
@@ -34,5 +34,18 @@ var chai = require('chai'), | ||
describe('#sample', function () { | ||
var foo = ['a', 'b'], | ||
bar = foo.sample; | ||
it('should return a random value from the array', function () { | ||
expect(bar).to.be.a('string'); | ||
}); | ||
it('should not alter the original array', function () { | ||
assert.deepEqual(foo, ['a', 'b']); | ||
}); | ||
}); | ||
describe('#uniq', function () { | ||
var foo = [1, 2, 2, 3, 3], | ||
bar = foo.uniq; | ||
bar = foo.uniq; | ||
@@ -50,2 +63,15 @@ it('should remove all duplicate values', function () { | ||
describe('#assoc()', function () { | ||
var foo = [{a: 1}, 'bar', {b: 2}, {b: 4}], | ||
bar = foo.assoc('b'); | ||
it('should return the first object that has the given property', function () { | ||
bar.should.equal(foo[2]); | ||
}); | ||
it('should not alter the original array', function () { | ||
assert.deepEqual(foo, [{a: 1}, 'bar', {b: 2}, {b: 4}]); | ||
}); | ||
}); | ||
describe('#drop()', function () { | ||
@@ -55,3 +81,3 @@ var foo = [1, 2, 3], | ||
it('should return a new array starting from the index', function () { | ||
it('should return a new array starting from the given index', function () { | ||
assert.deepEqual(bar, [3]); | ||
@@ -61,3 +87,2 @@ }); | ||
it('should not alter the original array', function () { | ||
expect(foo.drop).to.equal(foo.slice); | ||
bar.should.not.equal(foo); | ||
@@ -91,2 +116,26 @@ }) | ||
describe('#reject()', function () { | ||
var foo = [1, 'baz', 3], | ||
bar = foo.reject(function (e) { | ||
return typeof e === 'number'; | ||
}); | ||
it('should return an array of elements that fail a given test', function () { | ||
assert.deepEqual(bar, ['baz']); | ||
}); | ||
it('should not alter the original array', function () { | ||
assert.deepEqual(foo, [1, 'baz', 3]); | ||
}); | ||
}); | ||
describe('#select()', function () { | ||
var foo = [].select, | ||
bar = [].filter; | ||
it('should be the same as #filter()', function () { | ||
foo.should.equal(bar); | ||
}); | ||
}); | ||
describe('#take()', function () { | ||
@@ -93,0 +142,0 @@ var foo = [1, 'baz', 3], |
@@ -0,0 +0,0 @@ var chai = require('chai'), |
@@ -0,0 +0,0 @@ var chai = require('chai'), |
@@ -27,2 +27,29 @@ var chai = require('chai'), | ||
describe('#abs2', function () { | ||
it('should return the value of the number squared', function () { | ||
expect(zero.abs2).to.equal(0); | ||
expect(one.abs2).to.equal(1); | ||
expect(neg.abs2).to.equal(25); | ||
expect(pos.abs2).to.equal(49); | ||
}); | ||
}); | ||
describe('#arg', function () { | ||
it('should return PI if negative, 0 otherwise', function () { | ||
expect(zero.arg).to.equal(0); | ||
expect(pos.arg).to.equal(0); | ||
expect(neg.arg).to.equal(Math.PI); | ||
expect(negf.arg).to.equal(Math.PI); | ||
}); | ||
}); | ||
describe('#ceil', function () { | ||
it('should return the largest integer greater than or equal to self', function () { | ||
expect(zero.ceil).to.equal(0); | ||
expect(one.ceil).to.equal(1); | ||
expect(posf.ceil).to.equal(13); | ||
expect(negf.ceil).to.equal(-3); | ||
}); | ||
}); | ||
describe('#finite', function () { | ||
@@ -61,2 +88,10 @@ it('should return true or false if number is finite', function () { | ||
describe('#nonzero', function () { | ||
it('should return self if not 0, null otherwise', function () { | ||
expect(zero.nonzero).to.equal(null); | ||
expect(one.nonzero).to.equal(one); | ||
expect(negf.nonzero).to.equal(negf); | ||
}); | ||
}); | ||
describe('#polar', function () { | ||
@@ -83,3 +118,14 @@ it('should return [absolute value of number, (PI if negative, 0 otherwise)]', function () { | ||
describe('#zero', function () { | ||
it('should return true if value is zero, false otherwise', function () { | ||
expect(zero.zero).to.be.true; | ||
expect(one.zero).to.be.false; | ||
expect(pos.zero).to.be.false; | ||
expect(negf.zero).to.be.false; | ||
expect(NaN.zero).to.be.false; | ||
expect(Infinity.zero).to.be.false; | ||
}); | ||
}); | ||
// Major methods | ||
}); |
@@ -0,0 +0,0 @@ var chai = require('chai'), |
@@ -37,2 +37,16 @@ var chai = require('chai'), | ||
describe('#chop', function () { | ||
var foo = 'hello', | ||
bar = foo.chop; | ||
it('should return a string with the last character removed', function () { | ||
bar.should.equal('hell'); | ||
expect(''.chop).to.equal(''); | ||
}); | ||
it('should not affect the original', function () { | ||
foo.should.equal('hello'); | ||
}); | ||
}); | ||
describe('#chr', function () { | ||
@@ -94,2 +108,15 @@ var foo = 'hello', | ||
describe('#swapcase', function () { | ||
var foo = 'HeLlO', | ||
bar = foo.swapcase | ||
it('should return a string with each character swapped in case', function () { | ||
bar.should.equal('hElLo'); | ||
}); | ||
it('should not affect the original', function () { | ||
foo.should.equal('HeLlO'); | ||
}); | ||
}); | ||
describe('#upcase', function () { | ||
@@ -96,0 +123,0 @@ var foo = 'hello', |
Sorry, the diff of this file is not supported yet
53957
19
730
75