windy 6 years ago
parent
commit
0a90731d33
  1. 38
      src/core/proto/array.js
  2. 65
      src/core/proto/date.i18n.js
  3. 216
      src/core/proto/date.js
  4. 21
      src/core/proto/function.js
  5. 116
      src/core/proto/string.js

38
src/core/proto/array.js

@ -1,38 +0,0 @@
/**
* 对数组对象的扩展
* @class Array
*/
_.extend(Array.prototype, {
contains: function (o) {
return this.indexOf(o) > -1;
},
/**
* 从数组中移除指定的值如果值不在数组中则不产生任何效果
* @param {Object} o 要移除的值
* @return {Array} 移除制定值后的数组
*/
remove: function (o) {
var index = this.indexOf(o);
if (index !== -1) {
this.splice(index, 1);
}
return this;
},
pushArray: function (array) {
for (var i = 0; i < array.length; i++) {
this.push(array[i]);
}
},
pushDistinct: function (obj) {
if (!this.contains(obj)) {
this.push(obj);
}
},
pushDistinctArray: function (array) {
for (var i = 0, len = array.length; i < len; i++) {
this.pushDistinct(array[i]);
}
}
});

65
src/core/proto/date.i18n.js

@ -1,65 +0,0 @@
BI.prepares.push(function () {
// 牵扯到国际化这些常量在页面加载后再生效
// full day names
Date._DN = [BI.i18nText("BI-Basic_Sunday"),
BI.i18nText("BI-Basic_Monday"),
BI.i18nText("BI-Basic_Tuesday"),
BI.i18nText("BI-Basic_Wednesday"),
BI.i18nText("BI-Basic_Thursday"),
BI.i18nText("BI-Basic_Friday"),
BI.i18nText("BI-Basic_Saturday"),
BI.i18nText("BI-Basic_Sunday")];
// short day names
Date._SDN = [BI.i18nText("BI-Basic_Simple_Sunday"),
BI.i18nText("BI-Basic_Simple_Monday"),
BI.i18nText("BI-Basic_Simple_Tuesday"),
BI.i18nText("BI-Basic_Simple_Wednesday"),
BI.i18nText("BI-Basic_Simple_Thursday"),
BI.i18nText("BI-Basic_Simple_Friday"),
BI.i18nText("BI-Basic_Simple_Saturday"),
BI.i18nText("BI-Basic_Simple_Sunday")];
// Monday first, etc.
Date._FD = 1;
// full month namesdat
Date._MN = [
BI.i18nText("BI-Basic_January"),
BI.i18nText("BI-Basic_February"),
BI.i18nText("BI-Basic_March"),
BI.i18nText("BI-Basic_April"),
BI.i18nText("BI-Basic_May"),
BI.i18nText("BI-Basic_June"),
BI.i18nText("BI-Basic_July"),
BI.i18nText("BI-Basic_August"),
BI.i18nText("BI-Basic_September"),
BI.i18nText("BI-Basic_October"),
BI.i18nText("BI-Basic_November"),
BI.i18nText("BI-Basic_December")];
// short month names
Date._SMN = [0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11];
Date._QN = ["", BI.i18nText("BI-Quarter_1"),
BI.i18nText("BI-Quarter_2"),
BI.i18nText("BI-Quarter_3"),
BI.i18nText("BI-Quarter_4")];
/** Adds the number of days array to the Date object. */
Date._MD = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// 实际上无论周几作为一周的第一天,周初周末都是在-6-0间做偏移,用一个数组就可以
Date._OFFSET = [0, -1, -2, -3, -4, -5, -6];
});

216
src/core/proto/date.js

@ -1,216 +0,0 @@
/** Constants used for time computations */
Date.SECOND = 1000;
Date.MINUTE = 60 * Date.SECOND;
Date.HOUR = 60 * Date.MINUTE;
Date.DAY = 24 * Date.HOUR;
Date.WEEK = 7 * Date.DAY;
/**
* 获取时区
* @returns {String}
*/
Date.prototype.getTimezone = function () {
return this.toString().replace(/^.* (?:\((.*)\)|([A-Z]{1,4})(?:[\-+][0-9]{4})?(?: -?\d+)?)$/, "$1$2").replace(/[^A-Z]/g, "");
};
/** Returns the number of days in the current month */
Date.prototype.getMonthDays = function (month) {
var year = this.getFullYear();
if (typeof month === "undefined") {
month = this.getMonth();
}
if (((0 == (year % 4)) && ( (0 != (year % 100)) || (0 == (year % 400)))) && month == 1) {
return 29;
}
return Date._MD[month];
};
/**
* 获取每月的最后一天
* @returns {Date}
*/
Date.prototype.getLastDateOfMonth = function () {
return BI.getDate(this.getFullYear(), this.getMonth(), this.getMonthDays());
};
/** Returns the number of day in the year. */
Date.prototype.getDayOfYear = function () {
var now = BI.getDate(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
var then = BI.getDate(this.getFullYear(), 0, 0, 0, 0, 0);
var time = now - then;
return Math.floor(time / Date.DAY);
};
/** Returns the number of the week in year, as defined in ISO 8601. */
Date.prototype.getWeekNumber = function () {
var d = BI.getDate(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
var week = d.getDay();
var startOfWeek = BI.StartOfWeek % 7;
if (this.getMonth() === 0 && this.getDate() <= week) {
return 1;
}
d.setDate(this.getDate() - (week < startOfWeek ? (7 + week - startOfWeek) : (week - startOfWeek)));
var ms = d.valueOf(); // GMT
d.setMonth(0);
d.setDate(1);
var offset = Math.floor((ms - d.valueOf()) / (7 * 864e5)) + 1;
if (d.getDay() !== startOfWeek) {
offset++;
}
return offset;
};
Date.prototype.getQuarter = function () {
return Math.floor(this.getMonth() / 3) + 1;
};
// 离当前时间多少天的时间
Date.prototype.getOffsetDate = function (offset) {
return BI.getDate(BI.getTime(this.getFullYear(), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds()) + offset * 864e5);
};
Date.prototype.getOffsetQuarter = function (n) {
var dt = BI.getDate(BI.getTime(this.getFullYear(), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds()));
var day = dt.getDate();
var monthDay = BI.getDate(dt.getFullYear(), dt.getMonth() + BI.parseInt(n) * 3, 1).getMonthDays();
if (day > monthDay) {
day = monthDay;
}
dt.setDate(day);
dt.setMonth(dt.getMonth() + parseInt(n) * 3);
return dt;
};
// 得到本季度的起始月份
Date.prototype.getQuarterStartMonth = function () {
var quarterStartMonth = 0;
var nowMonth = this.getMonth();
if (nowMonth < 3) {
quarterStartMonth = 0;
}
if (2 < nowMonth && nowMonth < 6) {
quarterStartMonth = 3;
}
if (5 < nowMonth && nowMonth < 9) {
quarterStartMonth = 6;
}
if (nowMonth > 8) {
quarterStartMonth = 9;
}
return quarterStartMonth;
};
// 获得本季度的起始日期
Date.prototype.getQuarterStartDate = function () {
return BI.getDate(this.getFullYear(), this.getQuarterStartMonth(), 1);
};
// 得到本季度的结束日期
Date.prototype.getQuarterEndDate = function () {
var quarterEndMonth = this.getQuarterStartMonth() + 2;
return BI.getDate(this.getFullYear(), quarterEndMonth, this.getMonthDays(quarterEndMonth));
};
// 指定日期n个月之前或之后的日期
Date.prototype.getOffsetMonth = function (n) {
var dt = BI.getDate(BI.getTime(this.getFullYear(), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds()));
var day = dt.getDate();
var monthDay = BI.getDate(dt.getFullYear(), dt.getMonth() + parseInt(n), 1).getMonthDays();
if (day > monthDay) {
day = monthDay;
}
dt.setDate(day);
dt.setMonth(dt.getMonth() + parseInt(n));
return dt;
};
// 获得本周的起始日期
Date.prototype.getWeekStartDate = function () {
var w = this.getDay();
var startOfWeek = BI.StartOfWeek % 7;
return this.getOffsetDate(Date._OFFSET[w < startOfWeek ? (7 + w - startOfWeek) : (w - startOfWeek)]);
};
// 得到本周的结束日期
Date.prototype.getWeekEndDate = function () {
var w = this.getDay();
var startOfWeek = BI.StartOfWeek % 7;
return this.getOffsetDate(Date._OFFSET[w < startOfWeek ? (7 + w - startOfWeek) : (w - startOfWeek)] + 6);
};
// 格式化打印日期
Date.prototype.print = function (str) {
var m = this.getMonth();
var d = this.getDate();
var y = this.getFullYear();
var yWith4number = y + "";
while (yWith4number.length < 4) {
yWith4number = "0" + yWith4number;
}
var wn = this.getWeekNumber();
var qr = this.getQuarter();
var w = this.getDay();
var s = {};
var hr = this.getHours();
var pm = (hr >= 12);
var ir = (pm) ? (hr - 12) : hr;
var dy = this.getDayOfYear();
if (ir == 0) {
ir = 12;
}
var min = this.getMinutes();
var sec = this.getSeconds();
s["%a"] = Date._SDN[w]; // abbreviated weekday name [FIXME: I18N]
s["%A"] = Date._DN[w]; // full weekday name
s["%b"] = Date._SMN[m]; // abbreviated month name [FIXME: I18N]
s["%B"] = Date._MN[m]; // full month name
// FIXME: %c : preferred date and time representation for the current locale
s["%C"] = 1 + Math.floor(y / 100); // the century number
s["%d"] = (d < 10) ? ("0" + d) : d; // the day of the month (range 01 to 31)
s["%e"] = d; // the day of the month (range 1 to 31)
// FIXME: %D : american date style: %m/%d/%y
// FIXME: %E, %F, %G, %g, %h (man strftime)
s["%H"] = (hr < 10) ? ("0" + hr) : hr; // hour, range 00 to 23 (24h format)
s["%I"] = (ir < 10) ? ("0" + ir) : ir; // hour, range 01 to 12 (12h format)
s["%j"] = (dy < 100) ? ((dy < 10) ? ("00" + dy) : ("0" + dy)) : dy; // day of the year (range 001 to 366)
s["%k"] = hr; // hour, range 0 to 23 (24h format)
s["%l"] = ir; // hour, range 1 to 12 (12h format)
s["%X"] = (m < 9) ? ("0" + (1 + m)) : (1 + m); // month, range 01 to 12
s["%x"] = m + 1; // month, range 1 to 12
s["%M"] = (min < 10) ? ("0" + min) : min; // minute, range 00 to 59
s["%n"] = "\n"; // a newline character
s["%p"] = pm ? "PM" : "AM";
s["%P"] = pm ? "pm" : "am";
// FIXME: %r : the time in am/pm notation %I:%M:%S %p
// FIXME: %R : the time in 24-hour notation %H:%M
s["%s"] = Math.floor(this.getTime() / 1000);
s["%S"] = (sec < 10) ? ("0" + sec) : sec; // seconds, range 00 to 59
s["%t"] = "\t"; // a tab character
// FIXME: %T : the time in 24-hour notation (%H:%M:%S)
s["%U"] = s["%W"] = s["%V"] = (wn < 10) ? ("0" + wn) : wn;
s["%u"] = w + 1; // the day of the week (range 1 to 7, 1 = MON)
s["%w"] = w; // the day of the week (range 0 to 6, 0 = SUN)
// FIXME: %x : preferred date representation for the current locale without the time
// FIXME: %X : preferred time representation for the current locale without the date
s["%y"] = yWith4number.substr(2, 2); // year without the century (range 00 to 99)
s["%Y"] = yWith4number; // year with the century
s["%%"] = "%"; // a literal '%' character
s["%Q"] = qr;
var re = /%./g;
if (!BI.isKhtml()) {
return str.replace(re, function (par) {
return s[par] || par;
});
}
var a = str.match(re);
for (var i = 0; i < a.length; i++) {
var tmp = s[a[i]];
if (tmp) {
re = new RegExp(a[i], "g");
str = str.replace(re, tmp);
}
}
return str;
};

21
src/core/proto/function.js

@ -1,21 +0,0 @@
Function.prototype.before = function (func) {
var __self = this;
return function () {
if (func.apply(this, arguments) === false) {
return false;
}
return __self.apply(this, arguments);
};
};
Function.prototype.after = function (func) {
var __self = this;
return function () {
var ret = __self.apply(this, arguments);
if (ret === false) {
return false;
}
func.apply(this, arguments);
return ret;
};
};

116
src/core/proto/string.js

@ -1,116 +0,0 @@
/**
* 对字符串对象的扩展
* @class String
*/
_.extend(String.prototype, {
/**
* 判断字符串是否已指定的字符串开始
* @param {String} startTag 指定的开始字符串
* @return {Boolean} 如果字符串以指定字符串开始则返回true否则返回false
*/
startWith: function (startTag) {
if (startTag == null || startTag == "" || this.length === 0 || startTag.length > this.length) {
return false;
}
return this.substr(0, startTag.length) == startTag;
},
/**
* 判断字符串是否以指定的字符串结束
* @param {String} endTag 指定的字符串
* @return {Boolean} 如果字符串以指定字符串结束则返回true否则返回false
*/
endWith: function (endTag) {
if (endTag == null || endTag == "" || this.length === 0 || endTag.length > this.length) {
return false;
}
return this.substring(this.length - endTag.length) == endTag;
},
/**
* 获取url中指定名字的参数
* @param {String} name 参数的名字
* @return {String} 参数的值
*/
getQuery: function (name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = this.substr(this.indexOf("?") + 1).match(reg);
if (r) {
return unescape(r[2]);
}
return null;
},
/**
* 给url加上给定的参数
* @param {Object} paras 参数对象是一个键值对对象
* @return {String} 添加了给定参数的url
*/
appendQuery: function (paras) {
if (!paras) {
return this;
}
var src = this;
// 没有问号说明还没有参数
if (src.indexOf("?") === -1) {
src += "?";
}
// 如果以问号结尾,说明没有其他参数
if (src.endWith("?") !== false) {
} else {
src += "&";
}
_.each(paras, function (value, name) {
if (typeof(name) === "string") {
src += name + "=" + value + "&";
}
});
src = src.substr(0, src.length - 1);
return src;
},
/**
* 将所有符合第一个字符串所表示的字符串替换成为第二个字符串
* @param {String} s1 要替换的字符串的正则表达式
* @param {String} s2 替换的结果字符串
* @returns {String} 替换后的字符串
*/
replaceAll: function (s1, s2) {
return this.replace(new RegExp(s1, "gm"), s2);
},
/**
* 总是让字符串以指定的字符开头
* @param {String} start 指定的字符
* @returns {String} 以指定字符开头的字符串
*/
perfectStart: function (start) {
if (this.startWith(start)) {
return this;
}
return start + this;
},
/**
* 获取字符串中某字符串的所有项位置数组
* @param {String} sub 子字符串
* @return {Number[]} 子字符串在父字符串中出现的所有位置组成的数组
*/
allIndexOf: function (sub) {
if (typeof sub !== "string") {
return [];
}
var str = this;
var location = [];
var offset = 0;
while (str.length > 0) {
var loc = str.indexOf(sub);
if (loc === -1) {
break;
}
location.push(offset + loc);
str = str.substring(loc + sub.length, str.length);
offset += loc + sub.length;
}
return location;
}
});
Loading…
Cancel
Save