UNPKG

relative-time-format

Version:

A convenient Intl.RelativeTimeFormat polyfill

3 lines (2 loc) 14.5 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).RelativeTimeFormat=e()}(this,(function(){"use strict";var t="en",e={},r={};function n(){return t}function o(e){t=e}function i(t){if(!t)throw new Error("No locale data passed");e[t.locale]=t,r[t.locale.toLowerCase()]=t.locale}function a(t){return e[t]?t:r[t.toLowerCase()]?r[t.toLowerCase()]:void 0}function u(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=e.localeMatcher||"lookup";switch(r){case"lookup":case"best fit":return l(t);default:throw new RangeError('Invalid "localeMatcher" option: '.concat(r))}}function l(t){var e=a(t);if(e)return e;for(var r=t.split("-");t.length>1;){r.pop();var n=a(t=r.join("-"));if(n)return n}}var c={af:function(t){return 1==t?"one":"other"},ak:function(t){return 0==t||1==t?"one":"other"},am:function(t){return t>=0&&t<=1?"one":"other"},ar:function(t){var e=String(t).split("."),r=Number(e[0])==t&&e[0].slice(-2);return 0==t?"zero":1==t?"one":2==t?"two":r>=3&&r<=10?"few":r>=11&&r<=99?"many":"other"},ast:function(t){var e=!String(t).split(".")[1];return 1==t&&e?"one":"other"},be:function(t){var e=String(t).split("."),r=Number(e[0])==t,n=r&&e[0].slice(-1),o=r&&e[0].slice(-2);return 1==n&&11!=o?"one":n>=2&&n<=4&&(o<12||o>14)?"few":r&&0==n||n>=5&&n<=9||o>=11&&o<=14?"many":"other"},blo:function(t){return 0==t?"zero":1==t?"one":"other"},br:function(t){var e=String(t).split("."),r=Number(e[0])==t,n=r&&e[0].slice(-1),o=r&&e[0].slice(-2),i=r&&e[0].slice(-6);return 1==n&&11!=o&&71!=o&&91!=o?"one":2==n&&12!=o&&72!=o&&92!=o?"two":(3==n||4==n||9==n)&&(o<10||o>19)&&(o<70||o>79)&&(o<90||o>99)?"few":0!=t&&r&&0==i?"many":"other"},bs:function(t){var e=String(t).split("."),r=e[0],n=e[1]||"",o=!e[1],i=r.slice(-1),a=r.slice(-2),u=n.slice(-1),l=n.slice(-2);return o&&1==i&&11!=a||1==u&&11!=l?"one":o&&i>=2&&i<=4&&(a<12||a>14)||u>=2&&u<=4&&(l<12||l>14)?"few":"other"},ca:function(t){var e=String(t).split("."),r=e[0],n=!e[1],o=r.slice(-6);return 1==t&&n?"one":0!=r&&0==o&&n?"many":"other"},ceb:function(t){var e=String(t).split("."),r=e[0],n=e[1]||"",o=!e[1],i=r.slice(-1),a=n.slice(-1);return o&&(1==r||2==r||3==r)||o&&4!=i&&6!=i&&9!=i||!o&&4!=a&&6!=a&&9!=a?"one":"other"},cs:function(t){var e=String(t).split("."),r=e[0],n=!e[1];return 1==t&&n?"one":r>=2&&r<=4&&n?"few":n?"other":"many"},cy:function(t){return 0==t?"zero":1==t?"one":2==t?"two":3==t?"few":6==t?"many":"other"},da:function(t){var e=String(t).split("."),r=e[0],n=Number(e[0])==t;return 1!=t&&(n||0!=r&&1!=r)?"other":"one"},dsb:function(t){var e=String(t).split("."),r=e[0],n=e[1]||"",o=!e[1],i=r.slice(-2),a=n.slice(-2);return o&&1==i||1==a?"one":o&&2==i||2==a?"two":o&&(3==i||4==i)||3==a||4==a?"few":"other"},dz:function(t){return"other"},es:function(t){var e=String(t).split("."),r=e[0],n=!e[1],o=r.slice(-6);return 1==t?"one":0!=r&&0==o&&n?"many":"other"},ff:function(t){return t>=0&&t<2?"one":"other"},fr:function(t){var e=String(t).split("."),r=e[0],n=!e[1],o=r.slice(-6);return t>=0&&t<2?"one":0!=r&&0==o&&n?"many":"other"},ga:function(t){var e=String(t).split("."),r=Number(e[0])==t;return 1==t?"one":2==t?"two":r&&t>=3&&t<=6?"few":r&&t>=7&&t<=10?"many":"other"},gd:function(t){var e=String(t).split("."),r=Number(e[0])==t;return 1==t||11==t?"one":2==t||12==t?"two":r&&t>=3&&t<=10||r&&t>=13&&t<=19?"few":"other"},he:function(t){var e=String(t).split("."),r=e[0],n=!e[1];return 1==r&&n||0==r&&!n?"one":2==r&&n?"two":"other"},is:function(t){var e=String(t).split("."),r=e[0],n=(e[1]||"").replace(/0+$/,""),o=Number(e[0])==t,i=r.slice(-1),a=r.slice(-2);return o&&1==i&&11!=a||n%10==1&&n%100!=11?"one":"other"},lt:function(t){var e=String(t).split("."),r=e[1]||"",n=Number(e[0])==t,o=n&&e[0].slice(-1),i=n&&e[0].slice(-2);return 1==o&&(i<11||i>19)?"one":o>=2&&o<=9&&(i<11||i>19)?"few":0!=r?"many":"other"},lv:function(t){var e=String(t).split("."),r=e[1]||"",n=r.length,o=Number(e[0])==t,i=o&&e[0].slice(-1),a=o&&e[0].slice(-2),u=r.slice(-2),l=r.slice(-1);return o&&0==i||a>=11&&a<=19||2==n&&u>=11&&u<=19?"zero":1==i&&11!=a||2==n&&1==l&&11!=u||2!=n&&1==l?"one":"other"},mk:function(t){var e=String(t).split("."),r=e[0],n=e[1]||"",o=!e[1],i=r.slice(-1),a=r.slice(-2),u=n.slice(-1),l=n.slice(-2);return o&&1==i&&11!=a||1==u&&11!=l?"one":"other"},mt:function(t){var e=String(t).split("."),r=Number(e[0])==t&&e[0].slice(-2);return 1==t?"one":2==t?"two":0==t||r>=3&&r<=10?"few":r>=11&&r<=19?"many":"other"},pl:function(t){var e=String(t).split("."),r=e[0],n=!e[1],o=r.slice(-1),i=r.slice(-2);return 1==t&&n?"one":n&&o>=2&&o<=4&&(i<12||i>14)?"few":n&&1!=r&&(0==o||1==o)||n&&o>=5&&o<=9||n&&i>=12&&i<=14?"many":"other"},pt:function(t){var e=String(t).split("."),r=e[0],n=!e[1],o=r.slice(-6);return 0==r||1==r?"one":0!=r&&0==o&&n?"many":"other"},ro:function(t){var e=String(t).split("."),r=!e[1],n=Number(e[0])==t&&e[0].slice(-2);return 1==t&&r?"one":!r||0==t||1!=t&&n>=1&&n<=19?"few":"other"},ru:function(t){var e=String(t).split("."),r=e[0],n=!e[1],o=r.slice(-1),i=r.slice(-2);return n&&1==o&&11!=i?"one":n&&o>=2&&o<=4&&(i<12||i>14)?"few":n&&0==o||n&&o>=5&&o<=9||n&&i>=11&&i<=14?"many":"other"},se:function(t){return 1==t?"one":2==t?"two":"other"},si:function(t){var e=String(t).split("."),r=e[0],n=e[1]||"";return 0==t||1==t||0==r&&1==n?"one":"other"},sl:function(t){var e=String(t).split("."),r=e[0],n=!e[1],o=r.slice(-2);return n&&1==o?"one":n&&2==o?"two":n&&(3==o||4==o)||!n?"few":"other"}};function f(t){return"pt-PT"===t?t:function(t){var e=t.match(s);if(!e)throw new TypeError("Invalid locale: ".concat(t));return e[1]}(t)}c.as=c.am,c.az=c.af,c.bal=c.af,c.bg=c.af,c.bn=c.am,c.brx=c.af,c.ce=c.af,c.chr=c.af,c.de=c.ast,c.doi=c.am,c.ee=c.af,c.el=c.af,c.en=c.ast,c.et=c.ast,c.eu=c.af,c.fa=c.am,c.fi=c.ast,c.fil=c.ceb,c.fo=c.af,c.fur=c.af,c.fy=c.ast,c.gl=c.ast,c.gu=c.am,c.ha=c.af,c.hi=c.am,c.hr=c.bs,c.hsb=c.dsb,c.hu=c.af,c.hy=c.ff,c.ia=c.ast,c.id=c.dz,c.ig=c.dz,c.ii=c.dz,c.it=c.ca,c.ja=c.dz,c.jgo=c.af,c.jv=c.dz,c.ka=c.af,c.kea=c.dz,c.kk=c.af,c.kl=c.af,c.km=c.dz,c.kn=c.am,c.ko=c.dz,c.ks=c.af,c.ksh=c.blo,c.ku=c.af,c.ky=c.af,c.lb=c.af,c.lkt=c.dz,c.lo=c.dz,c.ml=c.af,c.mn=c.af,c.mr=c.af,c.ms=c.dz,c.my=c.dz,c.nb=c.af,c.ne=c.af,c.nl=c.ast,c.nn=c.af,c.no=c.af,c.nqo=c.dz,c.om=c.af,c.or=c.af,c.pa=c.ak,c.pcm=c.am,c.ps=c.af,c.rm=c.af,c.sah=c.dz,c.sc=c.ast,c.sd=c.af,c.sk=c.cs,c.so=c.af,c.sq=c.af,c.sr=c.bs,c.su=c.dz,c.sv=c.ast,c.sw=c.ast,c.syr=c.af,c.ta=c.af,c.te=c.af,c.th=c.dz,c.ti=c.ak,c.tk=c.af,c.tn=c.af,c.to=c.dz,c.tr=c.af,c.ug=c.af,c.uk=c.ru,c.ur=c.ast,c.uz=c.af,c.vec=c.ca,c.vi=c.dz,c.wae=c.af,c.wo=c.dz,c.xh=c.af,c.yi=c.ast,c.yo=c.dz,c.yue=c.dz,c.zh=c.dz,c.zu=c.am;var s=/^([a-z0-9]+)/i;function h(t){return h="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},h(t)}function m(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,p(n.key),n)}}function p(t){var e=function(t,e){if("object"!=h(t)||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,e||"default");if("object"!=h(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==h(e)?e:e+""}var y=function(){function t(e,r){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t);var n=t.supportedLocalesOf(e);if(0===n.length)throw new RangeError("Unsupported locale: "+e);if(r&&"cardinal"!==r.type)throw new RangeError('Only "cardinal" "type" is supported');this.$=c[f(n[0])]}var e,r,n;return e=t,n=[{key:"supportedLocalesOf",value:function(t){return"string"==typeof t&&(t=[t]),t.filter((function(t){return c[f(t)]}))}}],(r=[{key:"select",value:function(t){return this.$(t)}}])&&m(e.prototype,r),n&&m(e,n),Object.defineProperty(e,"prototype",{writable:!1}),t}();function b(t){return b="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},b(t)}function v(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function d(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?v(Object(r),!0).forEach((function(e){g(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):v(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}function g(t,e,r){return(e=z(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function w(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i,a,u=[],l=!0,c=!1;try{if(i=(r=r.call(t)).next,0===e){if(Object(r)!==r)return;l=!1}else for(;!(l=(n=i.call(r)).done)&&(u.push(n.value),u.length!==e);l=!0);}catch(t){c=!0,o=t}finally{try{if(!l&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(c)throw o}}return u}}(t,e)||function(t,e){if(t){if("string"==typeof t)return S(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?S(t,e):void 0}}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function S(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function O(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function k(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,z(n.key),n)}}function z(t){var e=function(t,e){if("object"!=b(t)||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,e||"default");if("object"!=b(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==b(e)?e:e+""}var j=["second","minute","hour","day","week","month","quarter","year"],N=["auto","always"],P=["long","short","narrow"],E=["lookup","best fit"],T=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};O(this,t);var o=r.numeric,i=r.style,a=r.localeMatcher;if(this.numeric="always",this.style="long",this.localeMatcher="lookup",void 0!==o){if(N.indexOf(o)<0)throw new RangeError('Invalid "numeric" option: '.concat(o));this.numeric=o}if(void 0!==i){if(P.indexOf(i)<0)throw new RangeError('Invalid "style" option: '.concat(i));this.style=i}if(void 0!==a){if(E.indexOf(a)<0)throw new RangeError('Invalid "localeMatcher" option: '.concat(a));this.localeMatcher=a}if("string"==typeof e&&(e=[e]),e.push(n()),this.locale=t.supportedLocalesOf(e,{localeMatcher:this.localeMatcher})[0],!this.locale)throw new Error("Unsupported locale");y.supportedLocalesOf(this.locale).length>0?this.pluralRules=new y(this.locale):console.warn('"'.concat(this.locale,'" locale is not supported')),"undefined"!=typeof Intl&&Intl.NumberFormat?(this.numberFormat=new Intl.NumberFormat(this.locale),this.numberingSystem=this.numberFormat.resolvedOptions().numberingSystem):this.numberingSystem="latn",this.locale=u(this.locale,{localeMatcher:this.localeMatcher})}var r,o,i;return r=t,o=[{key:"format",value:function(){var t=F(arguments),e=w(t,2),r=e[0],n=e[1];return this.getRule(r,n).replace("{0}",this.formatNumber(Math.abs(r)))}},{key:"formatToParts",value:function(){var t=F(arguments),e=w(t,2),r=e[0],n=e[1],o=this.getRule(r,n),i=o.indexOf("{0}");if(i<0)return[{type:"literal",value:o}];var a=[];return i>0&&a.push({type:"literal",value:o.slice(0,i)}),a=a.concat(this.formatNumberToParts(Math.abs(r)).map((function(t){return d(d({},t),{},{unit:n})}))),i+"{0}".length<o.length-1&&a.push({type:"literal",value:o.slice(i+"{0}".length)}),a}},{key:"getRule",value:function(t,r){var n,o=(n=this.locale,e[n])[this.style][r];if("string"==typeof o)return o;if("auto"===this.numeric)if(-2===t||-1===t){var i=o["previous".concat(-1===t?"":"-"+Math.abs(t))];if(i)return i}else if(1===t||2===t){var a=o["next".concat(1===t?"":"-"+Math.abs(t))];if(a)return a}else if(0===t&&o.current)return o.current;var u,l=o[(u=t,u<0||0===u&&function(t){return 1/t==-1/0}(u)?"past":"future")];return"string"==typeof l?l:l[this.pluralRules&&this.pluralRules.select(Math.abs(t))||"other"]||l.other}},{key:"formatNumber",value:function(t){return this.numberFormat?this.numberFormat.format(t):String(t)}},{key:"formatNumberToParts",value:function(t){return this.numberFormat&&this.numberFormat.formatToParts?this.numberFormat.formatToParts(t):[{type:"integer",value:this.formatNumber(t)}]}},{key:"resolvedOptions",value:function(){return{locale:this.locale,style:this.style,numeric:this.numeric,numberingSystem:this.numberingSystem}}}],o&&k(r.prototype,o),i&&k(r,i),Object.defineProperty(r,"prototype",{writable:!1}),t}();T.supportedLocalesOf=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("string"==typeof t)t=[t];else if(!Array.isArray(t))throw new TypeError('Invalid "locales" argument');return t.filter((function(t){return u(t,e)}))},T.addLocale=i,T.addDefaultLocale=function(t){i(t),o(t.locale)},T.setDefaultLocale=o,T.getDefaultLocale=n,T.PluralRules=y;var R='Invalid "unit" argument';function M(t){if("symbol"===b(t))throw new TypeError(R);if("string"!=typeof t)throw new RangeError("".concat(R,": ").concat(t));if("s"===t[t.length-1]&&(t=t.slice(0,t.length-1)),j.indexOf(t)<0)throw new RangeError("".concat(R,": ").concat(t));return t}function I(t){if(t=Number(t),Number.isFinite&&!Number.isFinite(t))throw new RangeError("".concat('Invalid "number" argument',": ").concat(t));return t}function F(t){if(t.length<2)throw new TypeError('"unit" argument is required');return[I(t[0]),M(t[1])]}return T})); //# sourceMappingURL=polyfill.js.map