📄 extra.js
字号:
/*
Array extra support (ver 1.0 / 2005-09-13)
by ma.la http://ma.la/
Licence: GPL
http://www.gnu.org/copyleft/gpl.html
*/
Array.prototype.forEach = function(callback,thisObject){
for(var i=0,len=this.length;i<len;i++)
callback.call(thisObject,this[i],i,this)
}
Array.prototype.map = function(callback,thisObject){
for(var i=0,res=[],len=this.length;i<len;i++)
res[i] = callback.call(thisObject,this[i],i,this);
return res
}
Array.prototype.filter = function(callback,thisObject){
for(var i=0,res=[],len=this.length;i<len;i++)
callback.call(thisObject,this[i],i,this) && res.push(this[i]);
return res
}
Array.prototype.indexOf = function(searchElement,fromIndex){
var i = (fromIndex < 0) ? this.length+fromIndex : fromIndex || 0;
for(;i<this.length;i++)
if(searchElement === this[i]) return i;
return -1
}
Array.prototype.lastIndexOf = function(searchElement,fromIndex){
var max = this.length-1;
var i = (fromIndex < 0) ? Math.max(max+1 + fromIndex,0) :
(fromIndex > max) ? max :
max-(fromIndex||0) || max;
for(;i>=0;i--)
if(searchElement === this[i]) return i;
return -1
}
Array.prototype.every = function(callback,thisObject){
for(var i=0,len=this.length;i<len;i++)
if(!callback.call(thisObject,this[i],i,this)) return false;
return true
}
Array.prototype.some = function(callback,thisObject){
for(var i=0,len=this.length;i<len;i++)
if(callback.call(thisObject,this[i],i,this)) return true;
return false
}
/*
and more extra methods
*/
Array.prototype.clone = function(){
return Array.apply(null,this)
};
(function(){
var native_sort = Array.prototype.sort;
Array.prototype.sortIt = function(){return native_sort.apply(this,arguments)}
var native_reverse = Array.prototype.reverse;
Array.prototype.reverseIt = function(){return native_reverse.apply(this,arguments)}
})();
Array.prototype.sort = function(){
var tmp = this.clone();
return tmp.sortIt.apply(tmp,arguments)
}
Array.prototype.reverse = function(){
var tmp = this.clone();
return tmp.reverseIt.apply(tmp,arguments)
}
Array.prototype.last = function(){
return this[this.length-1]
}
Array.prototype.compact = function(){
return this.remove("");
}
Array.prototype.uniq = function(){
var tmp = {};
var len = this.length;
for(var i=0;i<this.length;i++){
if(tmp.hasOwnProperty(this[i])){
this.splice(i,1);
if(this.length == i){break}
i--;
}else{
tmp[this[i]] = true;
}
}
return this;
}
Array.prototype.select = function(func){
var result = [];
for(var i=0;i<this.length;i++)
func(this[i],i) && result.push(this[i]);
return result;
}
// 遅延検索。numには取り急ぎ欲しい件数、hit時に実行する関数
Array.prototype.lazyselect = function(func,num,callback){
var result = [];
var count = 0;
var self = this;
for(var i=0;i<this.length;i++){
if(func(this[i],i)){
count++;
result.push(this[i])
}
if(count == num){i++; break;}
}
(function(){
if(i == self.length){arguments.callee.kill();return;}
if(func(self[i],i)){
result.push(self[i]);
callback({
result : result,
thread : arguments.callee
});
window.status = "追加"+i;
}
i++
}).bg(10);
return result;
}
Array.prototype.append = function(val){
this.push(val);
return this
}
Array.prototype.remove = function(to_remove){
return this.select(function(val){
return val != to_remove ? true : false
});
}
/*
配列を条件で分割する、trueになったら新しい行に。
*/
Array.prototype.splitter = function(callback,thisObject){
var res = [];
var tmp = [];
for(var i=0,len=this.length;i<len;i++)
callback.call(thisObject,this[i],i,this) ?
(tmp.push(this[i]),res.push(tmp),tmp=[]) :
tmp.push(this[i]);
tmp.length ? res.push(tmp) : 0;
return res;
}
// n個数ずつに分割する
// [1,2,3,4,5].splitPer(2) = [[1,2],[3,4],[5]];
Array.prototype.splitPer = function(num){
return this.splitter(function(v,i){return i%num==num-1});
}
// n個数に分割する
// [1,2,3,4,5].splitTo(2) => [[1,2,3],[4,5]]
Array.prototype.splitTo = function(num){
var per = Math.ceil(this.length / num);
return this.splitter(function(v,i){return i%per==per-1});
}
/*
date_extra.js
*/
if(typeof Locale == "undefined") Locale = {};
Locale.jp = {
abday : ["日", "月", "火", "水", "木", "金", "土"],
day : ["日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"],
abmon : [" 1月", " 2月", " 3月", " 4月", " 5月", " 6月", " 7月", " 8月", " 9月", "10月", "11月", "12月"],
mon : ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
d_t_fmt : "%Y年%m月%d日 %H時%M分%S秒",
d_fmt : "%Y年%m月%d日",
t_fmt : "%H時%M分%S秒",
am_pm : ["午前", "午後"],
t_fmt_ampm : "%p%H時%M分%S秒"
}
Locale.en = {
abday : ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
day : ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
abmon : ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
mon : ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
d_t_fmt : "%a %b %d %Y",
d_fmt : "%d/%m/%Y",
t_fmt : "%H:%M:%S",
am_pm : ["AM", "PM"]
}
// locale指定、グローバル
Date.setLocale = function(lc){
Date.prototype.locale = lc
}
// locale指定、そのオブジェクトに対してのみ
Date.prototype.setLocale = function(lc){
this.locale = lc;
return this;
}
// うるう年を判別する
Date.isleap = function(year){
return ((year % 400) == 0) ? 1 :
((year % 100) == 0) ? 0 :
((year % 4) == 0) ? 1 :
0;
}
Date.prototype.isleap = function(){
return Date.isleap(this.getFullYear())
}
Date.month_yday = [
[0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365],
[0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]
];
/*
tm object like ruby
mon : 1-12
yday: 1-366
*/
Date.prototype.tm = function(){
with(this){return {
year : getFullYear(),
mon : getMonth() + 1,
mday : getDate(),
yday : Date.month_yday[isleap()][getMonth()] + getDate(),
wday : getDay(),
hour : getHours(),
min : getMinutes(),
sec : getSeconds()
}}
}
/*
http://www.microsoft.com/japan/msdn/library/default.asp?url=/japan/msdn/library/ja/cpref/html/frlrfSystemGlobalizationDateTimeFormatInfoClassTopic.asp
*/
Date.prototype.toObject = function(){
var zerofill = function(keta){
return function(){return ("0".x(keta) + this).slice(-keta)}
}
with(this.tm()){
var o = {
yyyy : year,
M : mon,
d : mday,
t : Locale[this.locale].am_pm[(hour < 12 ) ? 0 : 1],
h : hour % 12 || 12,
H : hour,
m : min,
s : sec
}}
o.yy = this.getYear().format(zerofill(2));
"M d H h m s".split(" ").forEach(function(v){
o[v+v] = o[v].format(zerofill(2))
});
return o;
}
Date.prototype.strfObject = function(){
var self = this;
var tm = this.tm();
var o = this.toObject();
var lc = Locale[this.locale];
var lazy = function(f){
var tmp = {};
tmp.toString = function(){return self.strftime(f)}
return tmp;
}
return {
"a" : lc.abday[tm.wday],
"A" : lc.day[tm.wday],
"b" : lc.abmon[tm.mon-1],
"B" : lc.mon[tm.mon-1],
"c" : lazy(lc.d_t_fmt),
"d" : o.dd,
"H" : o.HH,
"I" : o.hh,
"j" : tm.yday,
"M" : o.mm,
"m" : o.MM,
"p" : o.t,
"S" : o.ss,
"U" : "",
"w" : tm.wday,
"W" : "",
"x" : lazy(lc.d_fmt),
"X" : lazy(lc.t_fmt),
"Y" : o.yyyy,
"y" : o.yy,
"z" : "",
"Z" : ""
}
}
Date.prototype.strftime = function(format){
var obj = this.strfObject();
return format.replace(/%(\w{1})/g,function(){
var arg = arguments;
return obj[arg[1]]
})
}
Date.prototype.format = function(){
};
/*
Function.prototype
*/
Function.prototype.isFunction = true;
/*
インスタンス生成
*/
Function.prototype.New =
Function.prototype.newInstance = function(){
for(var i=0,arg=[];i<arguments.length;i++) arg.push("arguments["+i+"]");
eval("var ins = new this("+arg.join(",")+")");
return ins;
}
/*
関数変形
*/
Function.prototype.getbody = function(){
var m;
return (m=this.toString().match(RE.func_body)) ? m[1] : "";
}
Function.prototype.getname = function(){
var m;
return (m=this.toString().match(RE.func_name)) ? m[1] : "";
}
Function.prototype.getargs = function(){
var m;
return (m=this.toString().match(RE.func_args)) ? m[1].split(/\s*,\s*/) : [];
}
// 使われている変数名を取得する
Function.prototype.getvars = function(){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -