vue3-timeago
Version:
A vue component used to format date with time ago statement.
2 lines (1 loc) • 6.47 kB
JavaScript
function t(t,n){for(var e=0;e<n.length;e++){var o=n[e];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}function n(t){var n=new Date(t),e=n.getMonth()+1,o=e<10?"0".concat(e):e,r=n.getDate()<10?"0".concat(n.getDate()):n.getDate(),i=n.getHours()<10?"0".concat(n.getHours()):n.getHours(),a=n.getMinutes()<10?"0".concat(n.getMinutes()):n.getMinutes();return"".concat(n.getFullYear(),"-").concat(o,"-").concat(r," ").concat(i,":").concat(a)}function e(t,n){return" ".concat(t," ").concat(n).concat(t>1?"s":""," ago")}function o(t,n){return" ".concat(t," ").concat(n).concat(t>1?"s":""," atrás")}function r(t,n){return" ".concat(t," ").concat(n).concat(t>1?"s":""," atrás")}function i(t,n){return"il y a ".concat(t," ").concat(n).concat(t>1?"s":"")}function a(t,n){return" ".concat(t," ").concat(n).concat(t>1?"s":""," temu")}var c={zh_TW:{short:{now:"剛剛",sec:" 秒",min:" 分",hour:" 小時",day:" 天"},long:{now:"剛剛",sec:" 秒鐘前",min:" 分鐘前",hour:" 小時前",day:" 天前"}},zh_CN:{short:{now:"刚刚",sec:" 秒",min:" 分",hour:" 小时",day:" 天"},long:{now:"刚刚",sec:" 秒钟前",min:" 分钟前",hour:" 小时前",day:" 天前"}},en:{short:{now:"just now",sec:"s",min:"m",hour:"h",day:"d"},long:{now:"just now",sec:function(t){return e(t,"second")},min:function(t){return e(t,"minute")},hour:function(t){return e(t,"hour")},day:function(t){return e(t,"day")}}},jp:{short:{now:"すこし前",sec:" 秒",min:" 分",hour:" 時間",day:" 日"},long:{now:"すこし前",sec:" 秒前",min:" 分前",hour:" 時間前",day:" 日前"}},pt_BR:{short:{now:"agora",sec:"s",min:"m",hour:"h",day:"d"},long:{now:"agora",sec:function(t){return r(t,"segundo")},min:function(t){return r(t,"minuto")},hour:function(t){return r(t,"hora")},day:function(t){return r(t,"dia")}}},es:{short:{now:"ahora mismo",sec:"s",min:"m",hour:"h",day:"d"},long:{now:"ahora mismo",sec:function(t){return o(t,"segundo")},min:function(t){return o(t,"minuto")},hour:function(t){return o(t,"hora")},day:function(t){return o(t,"dia")}}},ar:{short:{now:"قبل قليل",sec:" ثانية",min:" دقيقة",hour:" ساعة",day:" يوم"},long:{now:"قبل قليل",sec:function(t){return" قبل ".concat(t," ثواني")},min:function(t){return" قبل ".concat(t," دقائق")},hour:function(t){return" قبل ".concat(t," ساعات")},day:function(t){return" قبل ".concat(t," أيام")}}},fr:{short:{now:"maintenant",sec:"s",min:"m",hour:"h",day:"j"},long:{now:"maintenant",sec:function(t){return i(t,"seconde")},min:function(t){return i(t,"minute")},hour:function(t){return i(t,"heure")},day:function(t){return i(t,"jour")}}},pl:{short:{now:"przed chwilą",sec:"s",min:"m",hour:"g",day:"d"},long:{now:"przed chwilą",sec:function(t){return a(t,"sekund")},min:function(t){return a(t,"minut")},hour:function(t){return a(t,"godzin")},day:function(t){return a(t,"dni")}}},tr:{short:{now:"şimdi",sec:"sn",min:"dk",hour:"s",day:"g"},long:{now:"şimdi",sec:"saniye",min:"dakika",hour:"saat",day:"gün"}}},u=function(){function e(t,n,o){!function(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")}(this,e),this.dateTime=t,this.locale=n||"en",this.type=o||"short"}var o,r,i;return o=e,(r=[{key:"setLocale",value:function(t){this.locale=t}},{key:"getTimeAgoString",value:function(t,n){return"string"==typeof n?"".concat(t).concat(n):n(t)}},{key:"getTimeAgo",value:function(){var t,e,o=c[this.locale][this.type],r=o.now,i=o.min,a=o.hour,u=o.day,s=(t=this.dateTime,!isNaN(t)||/^\d+$/.test(t)?t:t instanceof Date?t.getTime():(t=(t||"").trim().replace(/\.\d+/,"").replace(/-/,"/").replace(/-/,"/").replace(/(\d)T(\d)/,"$1 $2").replace(/Z/," UTC").replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"),new Date(t).getTime())),d=(new Date).getTime(),h=((e=new Date).setHours(0),e.setMinutes(0),e.setSeconds(0),e.setMilliseconds(0),e.getTime(),function(){var t=new Date;t.setMonth(0),t.setDate(1),t.setHours(0),t.setMinutes(0),t.setSeconds(0),t.setMilliseconds(0),t.getTime()}(),(d-s)/1e3);return{timeago:h<=0||Math.floor(h/60)<=0?r:h<3600?this.getTimeAgoString(Math.round(h/60),i):h>=3600&&Math.round(h/3600)<24?this.getTimeAgoString(Math.round(h/3600),a):h/86400<=31?this.getTimeAgoString(Math.round(h/86400),u):function(t){var n=new Date(t),e=n.getMonth()+1,o=e<10?"0".concat(e):e,r=n.getDate()<10?"0".concat(n.getDate()):n.getDate();return"".concat(n.getFullYear(),"-").concat(o,"-").concat(r)}(s),nowString:n(s)}}}])&&t(o.prototype,r),i&&t(o,i),e}();function s(t,n,e,o,r,i,a,c,u,s){"boolean"!=typeof a&&(u=c,c=a,a=!1);const d="function"==typeof e?e.options:e;let h;if(t&&t.render&&(d.render=t.render,d.staticRenderFns=t.staticRenderFns,d._compiled=!0,r&&(d.functional=!0)),o&&(d._scopeId=o),i?(h=function(t){(t=t||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext)||"undefined"==typeof __VUE_SSR_CONTEXT__||(t=__VUE_SSR_CONTEXT__),n&&n.call(this,u(t)),t&&t._registeredComponents&&t._registeredComponents.add(i)},d._ssrRegister=h):n&&(h=a?function(t){n.call(this,s(t,this.$root.$options.shadowRoot))}:function(t){n.call(this,c(t))}),h)if(d.functional){const t=d.render;d.render=function(n,e){return h.call(e),t(n,e)}}else{const t=d.beforeCreate;d.beforeCreate=t?[].concat(t,h):[h]}return e}const d=s({render:function(){var t=this.$createElement;return(this._self._c||t)("span",[this._t("default",null,{timeago:this.timeago})],2)},staticRenderFns:[]},void 0,{name:"TimeAgo",props:{datetime:{type:[String,Date,Number],default:function(t){return new Date}},locale:{type:String,default:"en"},refresh:{type:[Number,Boolean],default:!1},long:{type:Boolean,default:!1},todo:{type:Function,default:function(t){}},tooltip:{type:[String,Boolean],default:!1}},data:function(){return{timeago:"",nowString:"",intervalId:null}},computed:{options:function(){return{placement:"string"==typeof this.tooltip?this.tooltip:"top",content:this.nowString}}},methods:{reloadTime:function(){var t,n,e,o=(t=this.datetime,n=this.locale,e=this.long?"long":"short",new u(t,n,e).getTimeAgo()),r=o.timeago,i=o.nowString;this.timeago=r,this.nowString=i,this.intervalId&&this.todo()}},mounted:function(){var t=this;this.$nextTick((function(){if(t.reloadTime(),t.refresh){!0===t.refresh||t.refresh;t.intervalId=setInterval(t.reloadTime,1e3*t.refresh)}}))},destroyed:function(){this.intervalId&&clearInterval(this.intervalId)}},void 0,!1,void 0,!1,void 0,void 0,void 0);export default d;