Created
October 27, 2010 12:33
-
-
Save taiju/648941 to your computer and use it in GitHub Desktop.
配列操作の俺々ライブラリ。まだ途中。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var _Ary = function() { | |
var self = this; | |
return function(data) { | |
self.data = data instanceof Array ? data : Array.prototype.slice.call(data); | |
return self; | |
}; | |
}; | |
_Ary.prototype = { | |
each: function(callback) { | |
if (Array.prototype.forEach) { | |
this.data.forEach(callback); | |
return this; | |
} | |
else { | |
for (var i = 0, l = this.data.length; i < l; i++) { | |
callback(this.data[i], i, this.data); | |
return this; | |
} | |
} | |
}, | |
map: function(callback) { | |
if (Array.prototype.map) { | |
this.data = this.data.map(callback); | |
return this; | |
} | |
else { | |
for (var i = 0, l = this.data.length; i < l; i++) { | |
this.data[i] = callback(this.data[i], i, this.data); | |
} | |
return this; | |
} | |
}, | |
reduce: function(callback, init) { | |
if (Array.prototype.reduce) { | |
this.data = (init) ? this.data.reduce(callback, init) : this.data.reduce(callback); | |
return this; | |
} | |
else { | |
var data; | |
if (init) { | |
this.data.unshift(init); | |
} | |
data = this.data[0]; | |
for (var i = 1, l = this.data.length; i < l; i++) { | |
data = callback(data, this.data[i], i, this.data); | |
} | |
this.data = data; | |
return this; | |
} | |
}, | |
get: function(callback) { | |
return callback ? callback(this.data) : this.data; | |
}, | |
set: function(data) { | |
this.data = data; | |
return this; | |
}, | |
eq: function(index, callback) { | |
return callback ? callback(this.data[index]) : this.data[index]; | |
}, | |
push: function() { | |
var args = Array.prototype.slice.call(arguments); | |
for (var i = 0, l = args.length; i < l; i++) { | |
this.data.push(args[i]); | |
} | |
return this; | |
}, | |
unshift: function() { | |
var args = Array.prototype.slice.call(arguments); | |
for (var i = 0, l = args.length; i < l; i++) { | |
this.data.unshift(args[i]); | |
} | |
return this; | |
}, | |
pop: function(callback) { | |
var data = this.data.pop(); | |
return callback ? callback(data, this.data) : data; | |
}, | |
shift: function(callback) { | |
var data = this.data.shift(); | |
return callback ? callback(data, this.data) : data; | |
}, | |
}; | |
window.Ary = new _Ary(); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ary([1,2,3,4,5]).each(function(val) { | |
console.log(val); | |
}); | |
Ary([1,2,3,4,5]).map(function(val) { | |
return val * 10; | |
}); | |
Ary([1,2,3,4,5]).reduce(function(x, y) { | |
return x + y; | |
}); | |
'<ul><li>' + Ary(document.getElementsByTagName('a')).reduce(function(x, y, i) { | |
return i === 0 ? x + y + '</li>' : x + '<li>' + y + '</li>'; | |
}).get() + '</ul>'; | |
Ary([1,2,3,4,5]).each(function(val) { | |
console.log(val); | |
}).set([6,7,8,9]).each(function(val) { | |
console.log(val); | |
}); | |
Ary([1,2,3,4,5]).eq(4); | |
Ary([1,2,3]).push(4).push(5,6,7).push(8,9).get(); | |
Ary([7,8,9]).unshift(6).unshift(5,4,3,2,1).get(); | |
Ary([1,2,3]).pop(); | |
Ary([1,2,3]).shift(); | |
Ary([1,2,3]).pop(function(val, arr) { | |
console.log('pop: ' + val); | |
console.log('残り: ' + arr.toSource()); | |
}); | |
Ary([1,2,3]).shift(function(val, arr) { | |
console.log('shift: ' + val); | |
console.log('残り: ' + arr.toSource()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment