481 lines
15 KiB
JavaScript
481 lines
15 KiB
JavaScript
/***
|
||
* 扩展String对象
|
||
*/
|
||
;(function(){
|
||
|
||
Object.is = function(obj){
|
||
return Object.prototype.toString.call(obj) == "[object Object]";
|
||
}
|
||
|
||
String.is = function(obj){
|
||
return Object.prototype.toString.call(obj) == "[object String]";
|
||
}
|
||
|
||
String.prototype.getBytes = function(charset){
|
||
charset = charset||"UTF-8";
|
||
return new TextEncoder(charset).encode(this);
|
||
}
|
||
|
||
String.ofBytes = function(arr,charset){
|
||
charset = charset||"UTF-8";
|
||
var uint = arr instanceof Uint8Array?arr:new Uint8Array(arr);
|
||
return new TextDecoder(charset).decode(uint);
|
||
}
|
||
|
||
String.prototype.startWith = function(str){
|
||
if(str==undefined) return false;
|
||
if(str.length > this.length) return false;
|
||
for (let i = 0; i < str.length; i++) {
|
||
if(str.charCodeAt(i)!=this.charCodeAt(i)){
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
String.prototype.endWith = function(str){
|
||
if(str==undefined) return false;
|
||
if(str.length > this.length) return false;
|
||
var j = this.length - str.length;
|
||
for (let i = 0; i < str.length; i++) {
|
||
if(str.charCodeAt(i)!=this.charCodeAt(i+j)){
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
String.prototype.eq = function(str){
|
||
return (this+'')===str;
|
||
}
|
||
})();
|
||
|
||
/**
|
||
* 扩展Number对象
|
||
*/
|
||
;(function(){
|
||
Number.is = function(obj){
|
||
return Object.prototype.toString.call(obj) == "[object Number]";
|
||
}
|
||
|
||
Number.format = function(num,fmt){
|
||
var fmts = fmt.split(".");
|
||
var format = {
|
||
int:fmts[0].split("").reverse(),
|
||
scale:fmts[1]!=undefined?fmts[1].split(""):undefined
|
||
}
|
||
var numsp = (num+"").split(".");
|
||
var numinfo = {
|
||
int:numsp[0].split("").reverse(),
|
||
scale:numsp[1]!=undefined?numsp[1].split(""):undefined
|
||
}
|
||
var res = {
|
||
int:[],
|
||
scale:undefined
|
||
}
|
||
var ni = 0;
|
||
for (var i = 0; i < format.int.length; i++) {
|
||
var item = format.int[i];
|
||
if(item=="#" && ni < numinfo.int.length){
|
||
res.int.push(numinfo.int[ni]);
|
||
ni++;
|
||
}else if(item=="0"){
|
||
if(ni < numinfo.int.length){
|
||
res.int.push(numinfo.int[ni]);
|
||
ni++;
|
||
}else{
|
||
res.int.push(item);
|
||
}
|
||
}else if(item==","){
|
||
if(ni < numinfo.int.length){
|
||
res.int.push(",");
|
||
}else{
|
||
continue;
|
||
}
|
||
}else if(item!="#"){
|
||
res.int.push(item);
|
||
}
|
||
}
|
||
|
||
if(format.scale!=undefined){
|
||
res.scale = [];
|
||
for (var j = 0; j < format.scale.length; j++) {
|
||
var item = format.scale[j];
|
||
if((item=="0" || item=="#") && numinfo.scale!=undefined && j < numinfo.scale.length){
|
||
res.scale.push(numinfo.scale[j]);
|
||
}else if(item!="#"){
|
||
res.scale.push(item);
|
||
}
|
||
}
|
||
|
||
var numL = 0;
|
||
for (var k = 0; k < res.scale.length; k++) {
|
||
var item = res.scale[k];
|
||
if(/\b/.test(item)){
|
||
numL++;
|
||
}
|
||
}
|
||
|
||
if(numinfo.scale!=undefined && numinfo.scale.length > numL){
|
||
var nz = res.scale[numL-1];
|
||
res.scale[numL-1] = parseInt(numinfo.scale[numL])>=5?parseInt(nz)+1:nz;
|
||
}
|
||
}
|
||
var reStr = "";
|
||
reStr += res.int.reverse().join("");
|
||
if(res.scale!=undefined){
|
||
reStr+= ".";
|
||
reStr+= res.scale.join("");
|
||
}
|
||
return reStr.replace("-,", "-");
|
||
}
|
||
|
||
Number.prototype.format = function(fmt){
|
||
if(fmt==undefined) throw new Error("参数格式化表达式不能为空!");
|
||
return Number.format(this,fmt);
|
||
}
|
||
|
||
Number.parse = function(str){
|
||
if(str==undefined) return str;
|
||
str += "";
|
||
if(/^-?\d+[\.\d]*$/.test(str)){
|
||
return new Number(str);
|
||
}
|
||
var sps = str.split("");
|
||
var res = [];
|
||
for (var i = 0; i < sps.length; i++) {
|
||
var item = sps[i];
|
||
if(item=="-" || item=="." || /[0-9]/.test(item)){
|
||
res.push(item);
|
||
}
|
||
}
|
||
return new Number(res.join(""));
|
||
}
|
||
})();
|
||
|
||
/**
|
||
* 扩展日期对象
|
||
*/
|
||
;(function(){
|
||
Date.SECOND = {
|
||
MINUTES:60000,
|
||
HOURS:3600000,
|
||
DAY:86400000,
|
||
WEEK:604800000,
|
||
}
|
||
|
||
Date.Dictionary = {
|
||
Holiday:{
|
||
"0101":"元旦",
|
||
"0214":"情人节",
|
||
"0308":"妇女节",
|
||
"0312":"植树节",
|
||
"0315":"消费者权益日",
|
||
"0401":"愚人节",
|
||
"0501":"劳动节",
|
||
"0504":"青年节",
|
||
"0512":"护士节",
|
||
"0601":"儿童节",
|
||
"0701":"建党节",
|
||
"0801":"建军节",
|
||
"0910":"教师节",
|
||
"0928":"孔子诞辰",
|
||
"1001":"国庆节",
|
||
"1006":"老人节",
|
||
"1024":"联合国日",
|
||
"1224":"平安夜",
|
||
"1225":"圣诞节",
|
||
},
|
||
LunarHoliday:{
|
||
"0101":"春节",
|
||
"0115":"元宵节",
|
||
"0505":"端午节",
|
||
"0707":"七夕情人节",
|
||
"0715":"中元节",
|
||
"0815":"中秋节",
|
||
"0909":"重阳节",
|
||
"1208":"腊八节",
|
||
"1224":"小年"
|
||
},
|
||
Week:{
|
||
"0" : "日",
|
||
"1" : "一",
|
||
"2" : "二",
|
||
"3" : "三",
|
||
"4" : "四",
|
||
"5" : "五",
|
||
"6" : "六"
|
||
},
|
||
timeDistance:{
|
||
yesterday:"昨天",
|
||
daysAgo:"{0}天前",
|
||
hoursAgo:"{0}小时前",
|
||
minutesAgo:"{0}分钟前",
|
||
justNow:"刚刚"
|
||
}
|
||
}
|
||
|
||
Date.is = function(obj){
|
||
return Object.prototype.toString.call(obj) == "[object Date]";
|
||
}
|
||
|
||
Date.valueOf = function(val,fmt){
|
||
if(val == undefined) return;
|
||
if(Date.is(val)){
|
||
return val;
|
||
}
|
||
if(String.is(val)){
|
||
if (/^\d+$/.test(val)) {
|
||
return new Date(parseInt(val));
|
||
}else if(fmt!=undefined){
|
||
var fmts = fmt.split("");
|
||
var conf = {
|
||
y:{start:undefined,end:undefined},
|
||
M:{start:undefined,end:undefined},
|
||
d:{start:undefined,end:undefined},
|
||
H:{start:undefined,end:undefined},
|
||
h:{start:undefined,end:undefined},
|
||
m:{start:undefined,end:undefined},
|
||
s:{start:undefined,end:undefined},
|
||
};
|
||
for (var i = 0; i < fmts.length; i++) {
|
||
var item = conf[fmts[i]];
|
||
if(item!=undefined){
|
||
if(item.start==undefined){
|
||
item.start = i;
|
||
}else{
|
||
item.end = i;
|
||
}
|
||
}
|
||
}
|
||
var keys = Object.keys(conf);
|
||
var parse = {};
|
||
for (var j = 0; j < keys.length; j++) {
|
||
var key = keys[j];
|
||
if(conf[key].start!=undefined && conf[key].end!=undefined){
|
||
parse[key] = parseInt(val.substr(conf[key].start,conf[key].end+1));
|
||
}
|
||
}
|
||
return new Date(parse.y||1975,(parse.M||1)-1,parse.d||1,parse.H||parse.h||0,parse.m||0,parse.s||0);
|
||
}else{
|
||
return new Date(val.replace(/-/g, "/"));
|
||
}
|
||
}else if(Number.is(val)){
|
||
return new Date(val);
|
||
}
|
||
return undefined;
|
||
}
|
||
|
||
|
||
|
||
Date.prototype.format = function(fmt){
|
||
fmt = fmt || "yyyy-MM-dd HH:mm:ss";
|
||
var o = {
|
||
"M+" : this.getMonth()+1, //月份
|
||
"d+" : this.getDate(), //日
|
||
"h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时
|
||
"H+" : this.getHours(), //小时
|
||
"m+" : this.getMinutes(), //分
|
||
"s+" : this.getSeconds(), //秒
|
||
"q+" : Math.floor((this.getMonth()+3)/3), //季度
|
||
"S" : this.getMilliseconds() //毫秒
|
||
};
|
||
if(/(y+)/.test(fmt)){
|
||
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
|
||
}
|
||
if(/(E+)/.test(fmt)){
|
||
fmt=fmt.replace(RegExp.$1,((RegExp.$1.length>1)?(RegExp.$1.length==3?"星期":"周"):"")+this.i18n("Date.Week."+this.getDay()));
|
||
}
|
||
for(var k in o){
|
||
if(new RegExp("("+ k +")").test(fmt)){
|
||
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
|
||
}
|
||
}
|
||
return fmt;
|
||
}
|
||
|
||
Date.prototype.toString = function(){
|
||
return this.format();
|
||
}
|
||
|
||
Date.prototype.getMonthInfo = function(){
|
||
return {
|
||
first:new Date(this.getFullYear(),this.getMonth(),1),
|
||
last:new Date(this.getFullYear(),this.getMonth()+1,0),
|
||
month:this.getMonth(),
|
||
name:this.format("yyyy-MM"),
|
||
monthIndex:this.getMonthIndex()
|
||
}
|
||
}
|
||
|
||
Date.prototype.getMonthIndex = function(){
|
||
return parseInt(this.format("yyyyMM"));
|
||
}
|
||
|
||
Date.prototype.getYearInfo = function(){
|
||
return {
|
||
first:new Date(this.getFullYear(),0,1),
|
||
last:new Date(this.getFullYear(),12,0),
|
||
name:this.format("yyyy")
|
||
}
|
||
}
|
||
|
||
Date.prototype.getWeekOpt = function(){
|
||
var yearFirst = new Date(this.getFullYear(),0,1);
|
||
var d = Math.round((this.valueOf() - yearFirst.valueOf()) / Date.SECOND.DAY);
|
||
return {
|
||
first:new Date(this.getTime() - this.getDay()* Date.SECOND.DAY),
|
||
last:new Date(this.getTime() + (7 - this.getDay())* Date.SECOND.DAY),
|
||
monthIndex:Math.ceil((this.getDate() + 6 - this.getDay()) / 7),
|
||
yearIndex:Math.ceil((d + ((yearFirst.getDay() + 1) - 1)) / 7),
|
||
name:this.format("EE"),
|
||
fullName:this.format("yyyy-MM EE")
|
||
}
|
||
}
|
||
|
||
Date.prototype.copy = function(){
|
||
return new Date(this.getFullYear(),this.getMonth(),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
|
||
}
|
||
|
||
Date.prototype.getDayIndex = function(){
|
||
var yearFirst = new Date(this.getFullYear(),0,1);
|
||
return parseInt((this.valueOf() - yearFirst.valueOf())/Date.SECOND.DAY)+1
|
||
}
|
||
|
||
Date.prototype.getDayName = function(){
|
||
return this.format("yyyy-MM-dd");
|
||
}
|
||
|
||
|
||
Date.prototype.next = function(){
|
||
if(arguments.length==0){
|
||
return new Date(this.getTime()+Date.SECOND.DAY);
|
||
}
|
||
if(arguments.length > 0){
|
||
var type = arguments[0];
|
||
if(Object.prototype.toString.call(type) == "[object Number]" ||
|
||
(Object.prototype.toString.call(type) == "[object String]" && /^-?\d+$/.test(type))){
|
||
return new Date(this.getTime()+Date.SECOND.DAY*parseInt(type));
|
||
}
|
||
var step = parseInt(arguments[1]) || 1;
|
||
var res = undefined;
|
||
switch (type) {
|
||
case "y":
|
||
res = new Date(this.getTime());
|
||
res.setFullYear(this.getFullYear()+step);
|
||
break;
|
||
case "M":
|
||
res = new Date(this.getTime());
|
||
res.setDate(1);
|
||
var y = step%12;
|
||
res.setFullYear(this.getFullYear()+parseInt(step/12));
|
||
var month = res.getMonth()+y;
|
||
if(month>11){
|
||
res.setFullYear(res.getFullYear()+1);
|
||
res.setMonth(month-12);
|
||
}else if(month < 0){
|
||
res.setFullYear(res.getFullYear()-1);
|
||
res.setMonth(month+12);
|
||
}else{
|
||
res.setMonth(month);
|
||
}
|
||
var conf = res.getMonthInfo();
|
||
if(this.getDate() < conf.last.getDate()){
|
||
res.setDate(this.getDate());
|
||
}else{
|
||
res.setDate(conf.last.getDate());
|
||
}
|
||
break;
|
||
case "w": res = new Date(this.getTime()+Date.SECOND.WEEK*step); break;
|
||
case "d": res = new Date(this.getTime()+Date.SECOND.DAY*step); break;
|
||
case "H": res = new Date(this.getTime()+Date.SECOND.HOURS*step); break;
|
||
case "m": res = new Date(this.getTime()+Date.SECOND.MINUTES*step); break;
|
||
case "s": res = new Date(this.getTime()+1000*step); break;
|
||
default:
|
||
res = new Date(this.getTime()+Date.SECOND.DAY*step);
|
||
break;
|
||
}
|
||
return res;
|
||
}
|
||
}
|
||
|
||
Date.prototype.i18n = function(fmt,args){
|
||
var str = undefined;
|
||
if($i18n){
|
||
var val = $i18n.t(fmt);
|
||
str =val==fmt?undefined:val;
|
||
}
|
||
if(str==undefined){
|
||
var obj = Date.Dictionary;
|
||
var fmts = fmt.split('.');
|
||
for (let i = 1; i < fmts.length; i++) {
|
||
const item = fmts[i];
|
||
obj = obj[item];
|
||
if(obj==undefined){
|
||
break;
|
||
}
|
||
}
|
||
str = obj;
|
||
}
|
||
if(str==undefined) return str;
|
||
if(args!=undefined){
|
||
for (var i = 0; i < args.length; i++) {
|
||
args[i] = args[i] || "";
|
||
var reg = new RegExp("({)" + i + "(})", "g");
|
||
str = str.replace(reg, args[i]);
|
||
}
|
||
}
|
||
str = str.replace(/^\{\d+\}$/g, "");
|
||
return str;
|
||
}
|
||
|
||
Date.prototype.getHolidayName = function(){
|
||
return this.i18n("Date.Holiday."+this.format("MMdd"));
|
||
}
|
||
|
||
Date.prototype.getLunarHolidayName = function(fmt){
|
||
return this.i18n("Date.LunarHoliday."+fmt);
|
||
}
|
||
|
||
Date.prototype.getTimeDistance = function(){
|
||
var date_now = new Date();
|
||
var time = this.getTime();
|
||
var distance = date_now.getTime() - time;
|
||
|
||
var days = parseInt(distance / (1000 * 60 * 60 * 24));
|
||
if (days == 1) {
|
||
return this.i18n('Date.timeDistance.yesterday');
|
||
} else if (days > 1 && days < 4) {
|
||
return this.i18n('Date.timeDistance.daysAgo',[days]);//days ago
|
||
} else if (days > 3) {
|
||
// 超过3天的,返回日期,如 2018-12-05
|
||
// 如果是今年的,就省去年份,返回 12-05
|
||
var year = this.getFullYear();
|
||
var month = this.getMonth() + 1;
|
||
if (month < 10) {
|
||
month = "0" + month;
|
||
}
|
||
var day = this.getDate();
|
||
if (day < 10) {
|
||
day = "0" + day;
|
||
}
|
||
if (date_now.getFullYear() == year) {
|
||
return month + "-" + day;
|
||
} else {
|
||
return year + "-" + month + "-" + day;
|
||
}
|
||
}
|
||
|
||
var hours = parseInt((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
|
||
if (hours > 0) {
|
||
return this.i18n('Date.timeDistance.hoursAgo',[hours]);
|
||
}
|
||
|
||
var minutes = parseInt((distance % (1000 * 60 * 60)) / (1000 * 60));
|
||
if (minutes > 0) {
|
||
return this.i18n('Date.timeDistance.minutesAgo',[minutes]);
|
||
};
|
||
return this.i18n('Date.timeDistance.justNow');
|
||
}
|
||
|
||
})(); |