moment-duration-format.min.js 3.9 KB

1
  1. !function(a,b){function c(a){var b="";if(a=parseInt(a,10),!a||1>a)return b;for(;a;)b+="0",a-=1;return b}function d(a,b,d){return null==a&&(a=""),a=""+a,(d?a:"")+c(b-a.length)+(d?"":a)}function e(a){return"[object Array]"===Object.prototype.toString.call(a)}function f(a){return"[object Object]"===Object.prototype.toString.call(a)}function g(a,b){for(var c=a.length;c-=1;)if(b(a[c]))return a[c]}function h(a,b){var e,c=0,d=a.length;for("function"!=typeof b&&(e=b,b=function(a){return a===e});d>c;){if(b(a[c]))return a[c];c+=1}}function i(a,b){var c=0,d=a.length;if(a&&d)for(;d>c;){if(b(a[c],c)===!1)return;c+=1}}function j(a,b){var c=0,d=a.length,e=[];if(!a||!d)return e;for(;d>c;)e[c]=b(a[c],c),c+=1;return e}function k(a,b){return j(a,function(a){return a[b]})}function l(a){var b=[];return i(a,function(a){a&&b.push(a)}),b}function m(a){var b=[];return i(a,function(a){h(b,a)||b.push(a)}),b}function n(a,b){var c=[];return i(a,function(a){i(b,function(b){a===b&&c.push(a)})}),m(c)}function o(a,b){var c=[];return i(a,function(d,e){return b(d)?void 0:(c=a.slice(e),!1)}),c}function p(a,b){var c=a.slice().reverse();return o(c,b).reverse()}function q(a,b){for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a}var r;if("function"==typeof require)try{r=require("moment")}catch(s){}if(!r&&a.moment&&(r=a.moment),!r)throw"Moment Duration Format cannot find Moment.js";r.duration.fn.format=function(){var a,b,g,s,t,u,w=[].slice.call(arguments),x=q({},this.format.defaults),y=r.duration(this);return x.duration=this,i(w,function(a){return"string"==typeof a||"function"==typeof a?void(x.template=a):"number"==typeof a?void(x.precision=a):void(f(a)&&q(x,a))}),g=x.types=e(x.types)?x.types:x.types.split(" "),"function"==typeof x.template&&(x.template=x.template.apply(x)),a=new RegExp(j(g,function(a){return x[a].source}).join("|"),"g"),s=function(a){return h(g,function(b){return x[b].test(a)})},b=j(x.template.match(a),function(a,b){var c=s(a),d=a.length;return{index:b,length:d,token:"escape"===c?a.replace(x.escape,"$1"):a,type:"escape"===c||"general"===c?null:c}},this),t=n(g,m(l(k(b,"type")))),t.length?(i(t,function(a,c){var d,e,f,g,h;d=y.as(a),e=d>0?Math.floor(d):Math.ceil(d),f=d-e,g=c+1===t.length,h=!c,i(b,function(b){b.type===a&&(q(b,{value:d,wholeValue:e,decimalValue:f,isLeast:g,isMost:h}),h&&null==x.forceLength&&b.length>1&&(x.forceLength=!0))}),y.subtract(e,a)}),x.trim&&(b=("left"===x.trim?o:p)(b,function(a){return!(a.isLeast||null!=a.type&&a.wholeValue)})),u=!1,"right"===x.trim&&b.reverse(),b=j(b,function(a){var b,e;if(!a.type)return a.token;if(b=a.isLeast&&x.precision<0?(Math.floor(a.wholeValue*Math.pow(10,x.precision))*Math.pow(10,-x.precision)).toString():a.wholeValue.toString(),b=b.replace(/^\-/,""),a.length>1&&(u||a.isMost||x.forceLength)&&(b=d(b,a.length)),a.isLeast&&x.precision>0)switch(e=a.decimalValue.toString().replace(/^\-/,"").split(/\.|e\-/),e.length){case 1:b+="."+d(e[0],x.precision,!0).slice(0,x.precision);break;case 2:b+="."+d(e[1],x.precision,!0).slice(0,x.precision);break;case 3:b+="."+d(c(+e[2]-1)+(e[0]||"0")+e[1],x.precision,!0).slice(0,x.precision);break;default:throw"Moment Duration Format: unable to parse token decimal value."}return a.isMost&&a.value<0&&(b="-"+b),u=!0,b}),"right"===x.trim&&b.reverse(),b.join("")):k(b,"token").join("")},r.duration.fn.format.defaults={escape:/\[(.+?)\]/,years:/[Yy]+/,months:/M+/,weeks:/[Ww]+/,days:/[Dd]+/,hours:/[Hh]+/,minutes:/m+/,seconds:/s+/,milliseconds:/S+/,general:/.+?/,types:"escape years months weeks days hours minutes seconds milliseconds general",trim:"left",precision:0,forceLength:null,template:function(){var a=this.types,b=this.duration,c=g(a,function(a){return b._data[a]});switch(c){case"seconds":return"h:mm:ss";case"minutes":return"d[d] h:mm";case"hours":return"d[d] h[h]";case"days":return"M[m] d[d]";case"weeks":return"y[y] w[w]";case"months":return"y[y] M[m]";case"years":return"y[y]";default:return"y[y] M[m] d[d] h:mm:ss"}}}}(this);