UNPKG

intl-relativeformat

Version:

Formats JavaScript dates to relative time strings.

3 lines (2 loc) 2.76 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).IntlRelativeFormat=e()}(this,function(){"use strict";var d=6e4,f=864e5;function m(t){var e=new Date(t);return e.setHours(0,0,0,0),e}function v(t,e){t=+t,e=+e;var n=Math.round(e-t),r=Math.round(n/1e3),o=Math.round(r/60),i=Math.round(o/60),a=function(t,e){var n=m(t),r=m(e),o=n.getTime()-n.getTimezoneOffset()*d,i=r.getTime()-r.getTimezoneOffset()*d;return Math.round((o-i)/f)}(e,t),s=Math.round(a/7),u=function(t){return 400*t/146097}(a),h=Math.round(12*u),l=Math.round(u);return{second:r,"second-short":r,"second-narrow":r,minute:o,"minute-short":o,"minute-narrow":o,hour:i,"hour-short":i,"hour-narrow":i,day:a,"day-short":a,"day-narrow":a,week:s,"week-short":s,"week-narrow":s,month:h,"month-short":h,"month-narrow":h,year:l,"year-short":l,"year-narrow":l}}var c=["second","second-short","minute","minute-short","hour","hour-short","day","day-short","month","month-short","year","year-short"];var w=function(u,t){void 0===t&&(t={});var h={style:function(t){if(!t)return"best fit";if("best fit"===t||"numeric"===t)return t;throw new Error('"'+t+"\" is not a valid IntlRelativeFormat 'style' value, it must be one of: 'best fit' or 'numeric'")}(t.style),units:function(t){if(!t||~c.indexOf(t))return!0;if("string"==typeof t){var e=/s$/.test(t)&&t.substr(0,t.length-1);if(e&&~c.indexOf(e))throw new Error('"'+t+"\" is not a valid IntlRelativeFormat 'units' value, did you mean: "+e)}throw new Error('"'+t+"\" is not a valid IntlRelativeFormat 'units' value, it must be one of: "+c.join('", "'))}(t.units)&&t.units},l="best fit"===h.style?"auto":"always",e=new Intl.RelativeTimeFormat(u,{numeric:l});return{format:function(t,e){var n=e&&void 0!==e.now?null===e.now?0:e.now:Date.now();if(void 0===t&&(t=n),!isFinite(n))throw new RangeError("The `now` option provided to IntlRelativeFormat#format() is not in valid range.");if(!isFinite(t))throw new RangeError("The date value provided to IntlRelativeFormat#format() is not in valid range.");var r=v(n,t),o=h.units||function(t){for(var e=c.filter(function(t){return t.indexOf("-short")<1}),n=e[0],r=0,o=e;r<o.length&&(n=o[r],!(Math.abs(t[n])<w.thresholds[n]));r++);return n}(r),i=r[o],a="-short"===o.substring(o.length-6,o.length)?"narrow":"long",s=o.replace("-short","");return new Intl.RelativeTimeFormat(u,{numeric:l,style:a}).format(i,s)},resolvedOptions:function(){return{locale:e.resolvedOptions().locale,style:h.style,units:h.units}}}};return w.defaultLocale="en",w.thresholds={second:45,"second-short":45,minute:45,"minute-short":45,hour:22,"hour-short":22,day:26,"day-short":26,month:11,"month-short":11},w}); //# sourceMappingURL=intl-relativeformat.min.js.map