relative-time-format
Version:
A convenient Intl.RelativeTimeFormat polyfill
3 lines (2 loc) • 11.9 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).RelativeTimeFormat=t()}(this,function(){"use strict";var e="en",t={},r={};function n(){return e}function o(e){return t[e]?e:r[e.toLowerCase()]?r[e.toLowerCase()]:void 0}function a(e){var t=(arguments.length>1&&void 0!==arguments[1]?arguments[1]:{}).localeMatcher||"lookup";switch(t){case"lookup":case"best fit":return i(e);default:throw new RangeError('Invalid "localeMatcher" option: '.concat(t))}}function i(e){var t=o(e);if(t)return t;for(var r=e.split("-");e.length>1;){r.pop();var n=o(e=r.join("-"));if(n)return n}}var u={af:function(e){return 1==e?"one":"other"},am:function(e){return e>=0&&e<=1?"one":"other"},ar:function(e){var t=String(e).split("."),r=Number(t[0])==e&&t[0].slice(-2);return 0==e?"zero":1==e?"one":2==e?"two":r>=3&&r<=10?"few":r>=11&&r<=99?"many":"other"},ast:function(e){var t=!String(e).split(".")[1];return 1==e&&t?"one":"other"},be:function(e){var t=String(e).split("."),r=Number(t[0])==e,n=r&&t[0].slice(-1),o=r&&t[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"},br:function(e){var t=String(e).split("."),r=Number(t[0])==e,n=r&&t[0].slice(-1),o=r&&t[0].slice(-2),a=r&&t[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!=e&&r&&0==a?"many":"other"},bs:function(e){var t=String(e).split("."),r=t[0],n=t[1]||"",o=!t[1],a=r.slice(-1),i=r.slice(-2),u=n.slice(-1),l=n.slice(-2);return o&&1==a&&11!=i||1==u&&11!=l?"one":o&&a>=2&&a<=4&&(i<12||i>14)||u>=2&&u<=4&&(l<12||l>14)?"few":"other"},cs:function(e){var t=String(e).split("."),r=t[0],n=!t[1];return 1==e&&n?"one":r>=2&&r<=4&&n?"few":n?"other":"many"},cy:function(e){return 0==e?"zero":1==e?"one":2==e?"two":3==e?"few":6==e?"many":"other"},da:function(e){var t=String(e).split("."),r=t[0],n=Number(t[0])==e;return 1!=e&&(n||0!=r&&1!=r)?"other":"one"},dsb:function(e){var t=String(e).split("."),r=t[0],n=t[1]||"",o=!t[1],a=r.slice(-2),i=n.slice(-2);return o&&1==a||1==i?"one":o&&2==a||2==i?"two":o&&(3==a||4==a)||3==i||4==i?"few":"other"},dz:function(e){return"other"},fil:function(e){var t=String(e).split("."),r=t[0],n=t[1]||"",o=!t[1],a=r.slice(-1),i=n.slice(-1);return o&&(1==r||2==r||3==r)||o&&4!=a&&6!=a&&9!=a||!o&&4!=i&&6!=i&&9!=i?"one":"other"},fr:function(e){return e>=0&&e<2?"one":"other"},ga:function(e){var t=String(e).split("."),r=Number(t[0])==e;return 1==e?"one":2==e?"two":r&&e>=3&&e<=6?"few":r&&e>=7&&e<=10?"many":"other"},gd:function(e){var t=String(e).split("."),r=Number(t[0])==e;return 1==e||11==e?"one":2==e||12==e?"two":r&&e>=3&&e<=10||r&&e>=13&&e<=19?"few":"other"},he:function(e){var t=String(e).split("."),r=t[0],n=!t[1],o=Number(t[0])==e,a=o&&t[0].slice(-1);return 1==e&&n?"one":2==r&&n?"two":n&&(e<0||e>10)&&o&&0==a?"many":"other"},is:function(e){var t=String(e).split("."),r=t[0],n=Number(t[0])==e,o=r.slice(-1),a=r.slice(-2);return n&&1==o&&11!=a||!n?"one":"other"},ksh:function(e){return 0==e?"zero":1==e?"one":"other"},lt:function(e){var t=String(e).split("."),r=t[1]||"",n=Number(t[0])==e,o=n&&t[0].slice(-1),a=n&&t[0].slice(-2);return 1==o&&(a<11||a>19)?"one":o>=2&&o<=9&&(a<11||a>19)?"few":0!=r?"many":"other"},lv:function(e){var t=String(e).split("."),r=t[1]||"",n=r.length,o=Number(t[0])==e,a=o&&t[0].slice(-1),i=o&&t[0].slice(-2),u=r.slice(-2),l=r.slice(-1);return o&&0==a||i>=11&&i<=19||2==n&&u>=11&&u<=19?"zero":1==a&&11!=i||2==n&&1==l&&11!=u||2!=n&&1==l?"one":"other"},mk:function(e){var t=String(e).split("."),r=t[0],n=t[1]||"",o=!t[1],a=r.slice(-1),i=r.slice(-2),u=n.slice(-1),l=n.slice(-2);return o&&1==a&&11!=i||1==u&&11!=l?"one":"other"},mt:function(e){var t=String(e).split("."),r=Number(t[0])==e&&t[0].slice(-2);return 1==e?"one":0==e||r>=2&&r<=10?"few":r>=11&&r<=19?"many":"other"},pa:function(e){return 0==e||1==e?"one":"other"},pl:function(e){var t=String(e).split("."),r=t[0],n=!t[1],o=r.slice(-1),a=r.slice(-2);return 1==e&&n?"one":n&&o>=2&&o<=4&&(a<12||a>14)?"few":n&&1!=r&&(0==o||1==o)||n&&o>=5&&o<=9||n&&a>=12&&a<=14?"many":"other"},pt:function(e){var t=String(e).split(".")[0];return 0==t||1==t?"one":"other"},ro:function(e){var t=String(e).split("."),r=!t[1],n=Number(t[0])==e&&t[0].slice(-2);return 1==e&&r?"one":!r||0==e||1!=e&&n>=1&&n<=19?"few":"other"},ru:function(e){var t=String(e).split("."),r=t[0],n=!t[1],o=r.slice(-1),a=r.slice(-2);return n&&1==o&&11!=a?"one":n&&o>=2&&o<=4&&(a<12||a>14)?"few":n&&0==o||n&&o>=5&&o<=9||n&&a>=11&&a<=14?"many":"other"},se:function(e){return 1==e?"one":2==e?"two":"other"},si:function(e){var t=String(e).split("."),r=t[0],n=t[1]||"";return 0==e||1==e||0==r&&1==n?"one":"other"},sl:function(e){var t=String(e).split("."),r=t[0],n=!t[1],o=r.slice(-2);return n&&1==o?"one":n&&2==o?"two":n&&(3==o||4==o)||!n?"few":"other"}};function l(e){return"pt-PT"===e?e:function(e){var t=e.match(s);if(!t)throw new TypeError("Invalid locale: ".concat(e));return t[1]}(e)}u.as=u.am,u.az=u.af,u.bg=u.af,u.bn=u.am,u.ca=u.ast,u.ce=u.af,u.chr=u.af,u.de=u.ast,u.ee=u.af,u.el=u.af,u.en=u.ast,u.es=u.af,u.et=u.ast,u.eu=u.af,u.fa=u.am,u.fi=u.ast,u.fo=u.af,u.fur=u.af,u.fy=u.ast,u.gl=u.ast,u.gu=u.am,u.hi=u.am,u.hr=u.bs,u.hsb=u.dsb,u.hu=u.af,u.hy=u.fr,u.ia=u.ast,u.id=u.dz,u.it=u.ast,u.ja=u.dz,u.jgo=u.af,u.jv=u.dz,u.ka=u.af,u.kea=u.dz,u.kk=u.af,u.kl=u.af,u.km=u.dz,u.kn=u.am,u.ko=u.dz,u.ku=u.af,u.ky=u.af,u.lb=u.af,u.lkt=u.dz,u.lo=u.dz,u.ml=u.af,u.mn=u.af,u.mr=u.am,u.ms=u.dz,u.my=u.dz,u.nb=u.af,u.ne=u.af,u.nl=u.ast,u.nn=u.af,u.or=u.af,u.ps=u.af,u["pt-PT"]=u.ast,u.sah=u.dz,u.sd=u.af,u.sk=u.cs,u.so=u.af,u.sq=u.af,u.sr=u.bs,u.sv=u.ast,u.sw=u.ast,u.ta=u.af,u.te=u.af,u.th=u.dz,u.ti=u.pa,u.tk=u.af,u.to=u.dz,u.tr=u.af,u.ug=u.af,u.uk=u.ru,u.ur=u.ast,u.uz=u.af,u.vi=u.dz,u.wae=u.af,u.yi=u.ast,u.yue=u.dz,u.zh=u.dz,u.zu=u.am;var s=/^([a-z0-9]+)/i;function c(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}var f=function(){function e(t,r){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e);var n=e.supportedLocalesOf(t);if(0===n.length)throw new RangeError("Unsupported locale: "+t);if(r&&"cardinal"!==r.type)throw new RangeError('Only "cardinal" "type" is supported');this.$=u[l(n[0])]}var t,r,n;return t=e,n=[{key:"supportedLocalesOf",value:function(e){return"string"==typeof e&&(e=[e]),e.filter(function(e){return u[l(e)]})}}],(r=[{key:"select",value:function(e){return this.$(e)}}])&&c(t.prototype,r),n&&c(t,n),e}();function h(e){return(h="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function m(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=[],n=!0,o=!1,a=void 0;try{for(var i,u=e[Symbol.iterator]();!(n=(i=u.next()).done)&&(r.push(i.value),!t||r.length!==t);n=!0);}catch(e){o=!0,a=e}finally{try{n||null==u.return||u.return()}finally{if(o)throw a}}return r}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function p(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function y(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var v=["second","minute","hour","day","week","month","quarter","year"],b=["auto","always"],g=["long","short","narrow"],d=["lookup","best fit"],w=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),y(this,"numeric","always"),y(this,"style","long"),y(this,"localeMatcher","lookup");var o=r.numeric,i=r.style,u=r.localeMatcher;if(void 0!==o){if(b.indexOf(o)<0)throw new RangeError('Invalid "numeric" option: '.concat(o));this.numeric=o}if(void 0!==i){if(g.indexOf(i)<0)throw new RangeError('Invalid "style" option: '.concat(i));this.style=i}if(void 0!==u){if(d.indexOf(u)<0)throw new RangeError('Invalid "localeMatcher" option: '.concat(u));this.localeMatcher=u}if("string"==typeof t&&(t=[t]),t.push(n()),this.locale=e.supportedLocalesOf(t,{localeMatcher:this.localeMatcher})[0],!this.locale)throw new Error("No supported locale was found");f.supportedLocalesOf(this.locale).length>0?this.pluralRules=new f(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=a(this.locale,{localeMatcher:this.localeMatcher})}var r,o,i;return r=e,(o=[{key:"format",value:function(){var e=m(O(arguments),2),t=e[0],r=e[1];return this.getRule(t,r).replace("{0}",this.formatNumber(Math.abs(t)))}},{key:"formatToParts",value:function(){var e=m(O(arguments),2),t=e[0],r=e[1],n=this.getRule(t,r),o=n.indexOf("{0}");if(o<0)return[{type:"literal",value:n}];var a=[];return o>0&&a.push({type:"literal",value:n.slice(0,o)}),a=a.concat(this.formatNumberToParts(Math.abs(t)).map(function(e){return function(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter(function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable}))),n.forEach(function(t){y(e,t,r[t])})}return e}({},e,{unit:r})})),o+"{0}".length<n.length-1&&a.push({type:"literal",value:n.slice(o+"{0}".length)}),a}},{key:"getRule",value:function(e,r){var n,o=(n=this.locale,t[n])[this.style][r];if("auto"===this.numeric)if(-2===e||-1===e){var a=o["previous".concat(-1===e?"":"-"+Math.abs(e))];if(a)return a}else if(1===e||2===e){var i=o["next".concat(1===e?"":"-"+Math.abs(e))];if(i)return i}else if(0===e&&o.current)return o.current;var u,l=o[(u=e,u<0||0===u&&function(e){return 1/e==-1/0}(u)?"past":"future")];return"string"==typeof l?l:l[this.pluralRules&&this.pluralRules.select(Math.abs(e))||"other"]||l.other}},{key:"formatNumber",value:function(e){return this.numberFormat?this.numberFormat.format(e):String(e)}},{key:"formatNumberToParts",value:function(e){return this.numberFormat&&this.numberFormat.formatToParts?this.numberFormat.formatToParts(e):[{type:"integer",value:this.formatNumber(e)}]}},{key:"resolvedOptions",value:function(){return{locale:this.locale,style:this.style,numeric:this.numeric,numberingSystem:this.numberingSystem}}}])&&p(r.prototype,o),i&&p(r,i),e}();w.supportedLocalesOf=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("string"==typeof e)e=[e];else if(!Array.isArray(e))throw new TypeError('Invalid "locales" argument');return e.filter(function(e){return a(e,t)})},w.addLocale=function(e){if(!e)throw new Error("No locale data passed");t[e.locale]=e,r[e.locale.toLowerCase()]=e.locale},w.setDefaultLocale=function(t){e=t},w.getDefaultLocale=n,w.PluralRules=f;var S='Invalid "unit" argument';function k(e){if("symbol"===h(e))throw new TypeError(S);if("string"!=typeof e)throw new RangeError("".concat(S,": ").concat(e));if("s"===e[e.length-1]&&(e=e.slice(0,e.length-1)),v.indexOf(e)<0)throw new RangeError("".concat(S,": ").concat(e));return e}var z='Invalid "number" argument';function N(e){if(e=Number(e),Number.isFinite&&!Number.isFinite(e))throw new RangeError("".concat(z,": ").concat(e));return e}function O(e){if(e.length<2)throw new TypeError('"unit" argument is required');return[N(e[0]),k(e[1])]}return w});
//# sourceMappingURL=relative-time-format.js.map