Comparing version 0.2.3 to 0.2.4
# Arrays | ||
Array methods can be overridden on prototypes. | ||
Array methods can be overridden on instances. | ||
## Minor | ||
## Major | ||
- `#clear` | ||
-- Empties the array in place. | ||
- `#compact` | ||
-- Returns a new array with all `undefined` values removed. | ||
- `#sample` | ||
-- Returns a random value from the array. `undefined` if the array is empty. | ||
- `#size` | ||
-- Returns the length of the array. | ||
- `#uniq` | ||
-- Returns a new array with all duplicate values removed. | ||
## Major | ||
- `#assoc(key)` | ||
-- Returns the first object in the array who has a property matching the key given. | ||
``` | ||
[{a: 1}, {b: 2}, {c: 3, b: 4}].assoc('b') | ||
>> {b: 2} | ||
``` | ||
- `#drop()` | ||
-- is an alias of `Array.protoype.slice()` | ||
- `#fetch(index, substitute)` | ||
-- Finds element at given index - index can be negative to start from the end of the array. Returns the index, unless the index is `undefined`, then the substitute is returned. | ||
```JavaScript | ||
[1,2].fetch(1) | ||
>> 2 | ||
[1, 2].fetch(5, 'foo') | ||
>> 'foo' | ||
``` | ||
- `#reject(fnCallback)` | ||
-- Returns a new array containing each element from the original array that did not pass a given test. | ||
```JavaScript | ||
[1, '2', 3].reject(function (e) {return typeof e === 'number'}); | ||
>> ['2'] | ||
``` | ||
- `#select` | ||
-- is an alias of `Array.prototype.filter()` | ||
- `#take(n)` | ||
-- Returns a new array containing the first *n* elements of the original array. | ||
```JavaScript | ||
[1, 2, 3, 4].take(2) | ||
>> [1, 2] | ||
``` | ||
- `#valuesAt(args*)` | ||
-- Returns a new array containing each value from each suppiled index in the list of arguments. Out of bounds indices give `undefined`. | ||
```JavaScript | ||
[1, 2, 3].valuesAt(0, 2, 10) | ||
>> [1, 3, undefined] | ||
``` | ||
- `#zip()` | ||
-- Returns an array of arrays that is the same length as the original array. Each array contains the values of the original arrays' values at each index. | ||
```JavaScript | ||
[1,2].zip(['a', 'b',], ['x', 'y',], ['shorter']) | ||
>> [ [ 1, 'a', 'x', 'shorter' ], [ 2, 'b', 'y', undefined ] ] | ||
``` |
# Booleans | ||
Boolean methods can be overridden on prototypes. | ||
Boolean methods can **not** be overridden on instances. | ||
## Minor | ||
- `#not` | ||
-- Returns the inverse boolean value. | ||
```JavaScript | ||
true.not | ||
>> false | ||
false.not | ||
>> true | ||
``` | ||
- `#size` | ||
-- Returns `1` if `true`, `0` if `false` | ||
## Major |
# Functions | ||
Function methods can be overridden on prototypes. | ||
Function methods can be overridden on instances. | ||
## Minor | ||
- `#size` | ||
-- Returns the length the specified arguments. | ||
## Major |
# Numbers | ||
Number methods can be overridden on prototypes. | ||
Number methods can **not** be overridden on instances. | ||
## Minor | ||
- `#abs` | ||
-- Returns the absolute value. | ||
- `#abs2` | ||
-- Returns the value sqared. | ||
- `#arg` | ||
-- Returns PI if negative, 0 otherwise. | ||
- `#ceil` | ||
-- Reuturn the largest integer greater than or equal to *self*. | ||
- `#finite` | ||
-- Returns true if number is in the finite range, false otherwise. | ||
- `#floor` | ||
-- Returns the largest integer less than or equal to *self*. | ||
- `#integer` | ||
-- Returns true if value is an integer, false otherwise. | ||
- `#nonzero` | ||
-- Return *self* if | ||
- `#polar` | ||
-- Returns an array containing the absolute value, and either PI if the value was negative, or 0 otherwise. | ||
```JavaScript | ||
(-5).polar | ||
>> [5, 3.141592653589793] | ||
``` | ||
## Major |
# Objects | ||
Object methods can be overridden on prototypes. | ||
Object methods can be overridden on instances. | ||
Object methods extend to all prototypes. | ||
## Minor | ||
## Major | ||
- `#array` | ||
-- Returns `true` if *self* is an array, false otherwise. | ||
- `#bool` | ||
-- Returns `true` if *self* is a boolean, false otherwise. | ||
- `#func` | ||
-- 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. | ||
- `#object` | ||
-- Returns `true` if *self* is an object. | ||
- `#size` | ||
-- For objects, returns the number of *own* properties. | ||
-- Size returns a value specific to each type. See individual documentation. | ||
- `#string` | ||
-- Returns `true` if *self* is a string value, false otherwise. | ||
- `#type` | ||
-- Returns a string representation of *self's* type. | ||
## Major | ||
- `#eql(value)` | ||
-- Returns true if *self* strictly equals the supplied argument. |
@@ -18,3 +18,3 @@ # Documentation | ||
*Minor* methods are methods implemented behind *getters* & *setters*. It would be fair to simply call them getters, but there's generally a lot more going on below the surface than just property retrieval. They can *not* be invoked externally, and as such they never accept any arguments. The are simply called. | ||
*Minor* methods are methods implemented behind *getters* & *setters*. It would be fair to simply call them getters, but there's generally a lot more going on below the surface than just property retrieval. They can *not* be invoked externally, and as such they never accept any arguments. They are simply called. | ||
@@ -21,0 +21,0 @@ |
# Strings | ||
String methods can be overridden on prototypes. | ||
String methods can **not** be overridden on instances. | ||
## Minor | ||
## Major | ||
- `#capitalize` | ||
-- Returns a copy of the string with the first character in uppercase. | ||
- `#chars` | ||
-- Returns an array of all characters in the string. | ||
- `#chop` | ||
-- Returns a new string with the last character removed. | ||
- `#chr` | ||
-- Returns the first character in the string. | ||
- `#downcase` | ||
-- Alias of `String.prototype.toLowerCase()`. | ||
- `#empty` | ||
-- Returns `true` if string length is `0`, `false` otherwise | ||
- `#reverse` | ||
-- Returns a new string with the characters in reverse order. | ||
- `#size` | ||
-- Returns the length of the string. | ||
- `#swapcase` | ||
-- Returns a new string with lowercase character turned uppercase, and uppercase characters turned lowercase. | ||
- `#upcase` | ||
-- Alias of `String.prototype.toUpperCase()`. | ||
## Major | ||
- `#prepend(val)` | ||
-- Returns a new string with *val* added to the beginning of the string. |
{ | ||
"name": "rubyisms", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Prototype extensions.", | ||
@@ -5,0 +5,0 @@ "main": "rubyisms.js", |
107
rubyisms.js
@@ -27,6 +27,7 @@ // Colin 'Oka' Hall-Coates | ||
function minor(prototype, o) { | ||
function minor(prototype, o, callback) { | ||
for (var k in o) { | ||
if (o.hasOwnProperty(k) && !prototype.hasOwnProperty(k)) { | ||
Object.defineProperty(prototype, k, strap(o[k], k)); | ||
if (callback) callback.call(null, k); | ||
} | ||
@@ -36,3 +37,3 @@ } | ||
function major(prototype, o) { | ||
function major(prototype, o, callback) { | ||
for (var k in o) { | ||
@@ -45,2 +46,3 @@ if (!prototype.hasOwnProperty(k)) { | ||
}); | ||
if (callback) callback.call(null, k); | ||
} | ||
@@ -55,5 +57,7 @@ } | ||
compact: function () { | ||
return this.filter(function (e) { | ||
return (typeof e !== 'undefined'); | ||
}); | ||
var o = [], i; | ||
for (i = 0; i < this.length; i++) { | ||
if(typeof this[i] !== 'undefined') o.push(this[i]); | ||
} | ||
return o; | ||
}, | ||
@@ -64,6 +68,37 @@ sample: function () { | ||
uniq: function () { | ||
var c = {}; | ||
return this.filter(function (e) { | ||
return (c.hasOwnProperty(e) ? false : c[e] = true); | ||
}); | ||
var s = {}, b = {}, n = {}, u = {}, o = {}, r = [], c = 0, p, i, k, item, type; | ||
for (i = 0; i < this.length; i++) { | ||
item = this[i]; | ||
type = Object.prototype.toString.call(item); | ||
p = true; | ||
if (type === '[object String]' && !s.hasOwnProperty(item)) { | ||
s[item] = true; r.push(item); | ||
} else if (type === '[object Boolean]' && !b.hasOwnProperty(item)) { | ||
b[item] = true; r.push(item); | ||
} else if (type === '[object Number]' && !n.hasOwnProperty(item)) { | ||
n[item] = true; r.push(item); | ||
} else if (type === '[object Object]' || | ||
type === '[object Function]' || | ||
type === '[object Array]') { | ||
obj: | ||
for (k in o) { | ||
if (o.hasOwnProperty(k) && item === o[k]) { | ||
p = false; | ||
break obj; | ||
} | ||
} | ||
if (p) { | ||
o[c] = item; | ||
r.push(item); | ||
c++; | ||
} | ||
} else if ((type === '[object Undefined]' || | ||
type === '[object Null]') && | ||
!u.hasOwnProperty(item)) { | ||
u[item] = true; r.push(item); | ||
} | ||
} | ||
return r; | ||
} | ||
@@ -135,7 +170,9 @@ }); | ||
size: function () { | ||
if (Array.isArray(this) || | ||
typeof this === 'string' || | ||
typeof this === 'function') return this.length; | ||
else if (typeof this === 'object') return Object.keys(this).length; | ||
else if (typeof this === 'number' && this === this) return this; | ||
var type = Object.prototype.toString.call(this); | ||
if (type === '[object Array]' || | ||
type === '[object String]' || | ||
type === '[object Function]') return this.length; | ||
else if (type === '[object Object]') return Object.keys(this).length; | ||
else if (type === '[object Boolean]') return Number(this); | ||
else if (type === '[object Number]' && this === this) return this; | ||
}, | ||
@@ -149,2 +186,5 @@ string: function () { | ||
} | ||
}, function (key) { | ||
// Unset globals | ||
G[key] = undefined; | ||
}); | ||
@@ -217,10 +257,9 @@ | ||
var a = Array.prototype.slice.call(arguments), | ||
s = this, | ||
o = []; | ||
o = [], i; | ||
a.forEach(function (e) { | ||
if (typeof e !== 'number' || !isFinite(e)) throw new TypeError('Expected index'); | ||
if (e < 0) e = s.length + e; | ||
o.push(s[e]); | ||
}); | ||
for (i = 0; i < a.length; i++) { | ||
if (typeof a[i] !== 'number' || !isFinite(a[i])) throw new TypeError('Expected index'); | ||
if (a[i] < 0) a[i] = this.length + a[i]; | ||
o.push(this[a[i]]); | ||
} | ||
@@ -231,14 +270,11 @@ return o; | ||
var a = Array.prototype.slice.call(arguments), | ||
o = []; | ||
o = [], i, j, q; | ||
this.forEach(function(e, i) { | ||
var q = [e]; | ||
a.forEach(function(e) { | ||
if (!Array.isArray(e)) throw new TypeError('Expected type array'); | ||
q.push(e[i]); | ||
}); | ||
for (i = 0; i < this.length; i++) { | ||
q = [this[i]]; | ||
for(j = 0; j < a.length; j++) { | ||
q.push(a[j][i]); | ||
} | ||
o.push(q); | ||
}); | ||
} | ||
@@ -267,11 +303,2 @@ return o; | ||
// Unset | ||
G.array = undefined; | ||
G.bool = undefined; | ||
G.func = undefined; | ||
G.numeric = undefined; | ||
G.size = undefined; | ||
G.string = undefined; | ||
G.type = undefined; | ||
}((typeof window !== 'undefined' ? window : global))); |
@@ -48,11 +48,45 @@ var chai = require('chai'), | ||
describe('#uniq', function () { | ||
var foo = [1, 2, 2, 3, 3], | ||
bar = foo.uniq; | ||
var f = function () {}, | ||
f2 = f, | ||
a = [], a2 = a, | ||
o = {}, o2 = o, | ||
foo = [f, f2, a, a2, o, o2, 1, 1, '5', '5', true, true, false, false], | ||
bar = foo.uniq; | ||
it('should remove all duplicate values', function () { | ||
assert.deepEqual(bar, [1, 2, 3]); | ||
it('should work with array values', function () { | ||
var a3 = []; | ||
expect([a, a2, a3].uniq).to.deep.equal([a, a3]); | ||
}); | ||
it('should work with function values', function () { | ||
var f3 = function () {}; | ||
expect([f, f2, f3].uniq).to.deep.equal([f, f3]); | ||
}); | ||
it('should work with object values', function () { | ||
var o3 = {}; | ||
expect([o, o2, o3].uniq).to.deep.equal([o, o3]); | ||
}); | ||
it('should work on boolean values', function () { | ||
expect([true, true, false, false].uniq).to.deep.equal([true, false]); | ||
}) | ||
it('should work on string values', function () { | ||
var arr = ['one', 'two', 'two', 'one', 'three']; | ||
expect(arr.uniq).to.deep.equal(['one', 'two', 'three']); | ||
}); | ||
it('should work on number values', function () { | ||
var arr = [1, 2, 2, 1, 3]; | ||
expect(arr.uniq).to.deep.equal([1, 2, 3]); | ||
}); | ||
it('should work on mixed values', function () { | ||
bar.should.deep.equal([f, a, o, 1, '5', true, false]); | ||
}); | ||
it('should not alter the original array', function () { | ||
bar.should.not.equal(foo); | ||
foo.should.deep.equal([f, f2, a, a2, o, o2, 1, 1, '5', '5', true, true, false, false]); | ||
}); | ||
@@ -59,0 +93,0 @@ }); |
@@ -80,2 +80,7 @@ var chai = require('chai'), | ||
it('should return a numeric value for booleans', function () { | ||
expect(true.size).to.equal(1); | ||
expect(false.size).to.equal(0); | ||
}) | ||
it('should return the number of arguments a function has defined', function () { | ||
@@ -82,0 +87,0 @@ expect(f.size).to.equal(2); |
Sorry, the diff of this file is not supported yet
251459
27
806