dayjs-ext
Version:
2KB immutable date time library alternative to Moment.js with the same modern API
3 lines (2 loc) • 1.81 kB
JavaScript
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r(require("fast-plural-rules")):"function"==typeof define&&define.amd?define(["fast-plural-rules"],r):e.dayjs_plugin_relativeTime=r(e.fastPluralRules)}(this,function(g){"use strict";return function(e,r,v){var t=r.prototype;function M(e){var i,t,n,a,u,o;e.s&&(t=(i=e).future,n=i.past,a=Object.keys(i).reduce(function(e,r){var t=r.length;if(t<=2){var n=i[r];if(1===t){e[r]=n;var a=r+r,u=e[a];u||(u=e[a]=[]),u.unshift(n)}else{var o=e[r];o||(o=e[r]=[]),o.push(n)}}return delete i[r],e},{}),u={},o={},Object.keys(a).forEach(function(e){var r=a[e];o[e]="string"==typeof r?(u[e]=t.replace("%s",r),n.replace("%s",r)):(u[e]=r.map(function(e){return t.replace("%s",e)}),r.map(function(e){return n.replace("%s",e)}))}),i.duration=a,i.future=u,i.past=o)}v.en.relativeTime={future:"in %s",past:"%s ago",s:"a few seconds",m:"a minute",mm:"%d minutes",h:"an hour",hh:"%d hours",d:"a day",dd:"%d days",M:"a month",MM:"%d months",y:"a year",yy:"%d years"};var n=function(e,r,t,n){var a,u,o=t.$locale(),i=o.relativeTime,l=[{l:"s",r:44,d:"second"},{l:"m",r:89},{l:"mm",r:44,d:"minute"},{l:"h",r:89},{l:"hh",r:21,d:"hour"},{l:"d",r:35},{l:"dd",r:25,d:"day"},{l:"M",r:45},{l:"MM",r:10,d:"month"},{l:"y",r:17},{l:"yy",d:"year"}],s=l.length;M(i);for(var f=0;f<s;f+=1){var d=l[f],c=d.d;c&&(a=n?v(e).diff(t,c,!0):t.diff(e,c,!0));var h=Math.ceil(Math.abs(a)),m=d.r;if(h<=m||!m){var p=void 0;p=r?i.duration:0<a?i.future:i.past;var y=d.l;u=1===y.length?p[y]:p[y][g.getPluralFormForCardinalByLocale(o.name,h)].replace("%d",h);break}}return u};t.to=function(e,r){return n(e,r,this,!0)},t.from=function(e,r){return n(e,r,this)},t.toNow=function(e){return this.to(v(),e)},t.fromNow=function(e){return this.from(v(),e)}}});
//# sourceMappingURL=relativeTime.js.map