intl-relative-time-format
Version:
A fully spec-compliant polyfill for 'Intl.RelativeTimeFormat'
3 lines (2 loc) • 12.5 kB
JavaScript
function _typeof3(a){return _typeof3="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof3(a)}(function(){'use strict';function a(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function b(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function c(a,c,d){return c&&b(a.prototype,c),d&&b(a,d),a}function d(a){if("string"!=typeof a)throw new TypeError("unit: '".concat(a,"' must be a string"));if("seconds"===a)return"second";if("minutes"===a)return"minute";if("hours"===a)return"hour";if("days"===a)return"day";if("weeks"===a)return"week";if("months"===a)return"month";if("quarters"===a)return"quarter";if("years"===a)return"year";if(!T.some(function(b){return b===a}))throw new RangeError("Unit: '".concat(a,"' must be one of: ").concat(T.map(function(a){return"\"".concat(a,"\"")}).join(", ")));return a}function e(a){return e="function"==typeof Symbol&&"symbol"===_typeof3(Symbol.iterator)?function(a){return _typeof3(a)}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":_typeof3(a)},e(a)}function f(a){return f="function"==typeof Symbol&&"symbol"===e(Symbol.iterator)?function(a){return e(a)}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":e(a)},f(a)}function g(a){if(null==a)throw new TypeError("Argument ".concat(a," cannot be converted to an Object"));return"boolean"==typeof a?new Boolean(a):"number"==typeof a?new Number(a):"string"==typeof a?new String(a):"symbol"===f(a)?Object(a):a}function h(a){return a+""}function i(a){return a.replace(U,"")}function j(a,b){if(2!==b.length)throw new TypeError("Could not get UnicodeExtensionValue: The given key: '".concat(b,"' must have a length of 2"));var c=b.length,d="-".concat(b,"-"),f=String.prototype.indexOf.call(a,d);if(-1!==f){for(var g=f+4,h=g,i=g,j=!1;!j;){var l=String.prototype.indexOf.call(a,"-",i),e=-1===l?c-i:l-i;2===e?j=!0:-1===l?(h=c,j=!0):(h=l,i=l+1)}return a.slice(g,h)}return d="-".concat(b),f=String.prototype.indexOf.call(a,d),-1!==f&&f+3===c?"":void 0}function k(a,b){for(var c=b;;){if(a.includes(c))return c;var d=c.lastIndexOf("-");if(-1===d)return;2<=d&&"-"===c.charAt(d-2)&&(d-=2),c=c.slice(0,d)}}function l(a){O=a}function m(){return O}function n(){if(null==O)throw new ReferenceError("Could not determine locale: No default locale has been configured");return O}function o(a){var b=a.availableLocales,c=a.requestedLocales,d={},e=!0,f=!1,g=void 0;try{for(var h,j=c[Symbol.iterator]();!(e=(h=j.next()).done);e=!0){var l=h.value,m=i(l),o=k(b,m);if(void 0!==o){if(d.locale=o,l!==m){var p=l.match(U);d.extension=null==p?"":p[0]}return d}}}catch(a){f=!0,g=a}finally{try{e||null==j.return||j.return()}finally{if(f)throw g}}var q=n();return d.locale=q,d}function p(a){return o(a)}function q(a){return"[object Object]"===Object.prototype.toString.call(a)}function s(a){return Array.isArray(a)||q(a)}function t(a,b){if("number"==typeof a)throw new TypeError("First argument 'x' must not be a number");if(f(a)!==f(b))throw new TypeError("The given arguments must have the same type");return void 0===a||null===a||("string"==typeof a?a===b:"boolean"==typeof a?a===b:"symbol"===f(a)?a.valueOf()===b.valueOf():a===b)}function u(a,b){return!(f(a)!==f(b))&&("number"==typeof a?!!(isNaN(a)&&isNaN(b))||!(Object.is(a,0)&&Object.is(b,-0))&&a===b:t(a,b))}function v(a,b,c,d,e){var f=c.localeMatcher,g="lookup"===f?o({availableLocales:a,requestedLocales:b}):p({availableLocales:a,requestedLocales:b}),h=g.locale,i={};i.dataLocale=h;var k="-u",l=!0,m=!1,n=void 0;try{for(var r,t=d[Symbol.iterator]();!(l=(r=t.next()).done);l=!0){var v=r.value,w=e[h];if(!q(w))throw new TypeError("LocaleData for locale: '".concat(h,"' must be an object"));var x=w[v];if(!s(x))throw new TypeError("key: '".concat(v,"' in LocaleData for locale: '").concat(h,"' must be indexable"));var y=x[0];if("string"!=typeof y&&null!==y)throw new TypeError("value: '".concat(y,"' for key: '").concat(v,"' in LocaleData for locale: '").concat(h,"' must be a string or null"));var z="";if("extension"in g){var A=j(g.extension,v);void 0!==A&&(""===A?x.includes("true")&&(y="true"):x.includes(A)&&(y=A,z="-".concat(v,"-").concat(y)))}if("key"in c){var B=c.key;if("string"!=typeof B&&null!=B)throw new TypeError("options value: '".concat(B,"' must be a string, undefined, or null"));void 0!==B&&x.includes(B)&&!u(B,y)&&(y=B,z="")}i[v]=y,k+=z}}catch(a){m=!0,n=a}finally{try{l||null==t.return||t.return()}finally{if(m)throw n}}if(2<k.length){var C=String.prototype.indexOf.call(h,"-x-");if(-1===C)h="".concat(h).concat(k);else{var D=h.slice(0,C),E=h.slice(C);h="".concat(D).concat(k).concat(E)}h=Intl.getCanonicalLocales(h)[0]}return i.locale=h,i}function w(a,b){var c=[],d=!0,e=!1,f=void 0;try{for(var g,h=b[Symbol.iterator]();!(d=(g=h.next()).done);d=!0){var j=g.value,l=i(j),m=k(a,l);m!==void 0&&c.push(j)}}catch(a){e=!0,f=a}finally{try{d||null==h.return||h.return()}finally{if(e)throw f}}return c}function x(a,b){return w(a,b)}function y(a){return!("string"!=typeof a)||!("symbol"!==f(a))}function z(a,b){if("object"!==f(a))throw new TypeError("Given argument ".concat(a," must be of type Object"));if(!y(b))throw new TypeError("Given argument ".concat(b," must be a PropertyKey"));return a[b]}function A(a){return!!a}function B(a,b,c,d,e){var f=z(a,b);if(void 0!==f){if("boolean"!==c&&"string"!==c)throw new TypeError("Expected type ".concat(c," to be 'boolean' or 'string"));if("boolean"===c&&(f=A(f)),"string"===c&&(f=h(f)),void 0!==d&&!d.includes(f))throw new RangeError("Value ".concat(f," out of range for options property ").concat(b));return f}return e}function C(a,b,c){var d;return void 0===c?d="best fit":(c=g(c),d=B(c,"localeMatcher","string",Q,"best fit")),"best fit"===d?x(a,b):w(a,b)}function D(a,b,c){var d=V.get(a);null==d&&(d=Object.create(null),V.set(a,d)),d[b]=c}function E(a,b){var c=V.get(a);if(null==c)throw new ReferenceError("No internal slots has been allocated for the given instance of RelativeTimeFormat");return c[b]}function F(a,b){var c=V.get(a);return null!=c&&b in c}function G(a,b){if(!F(a,"pluralRules"))throw new TypeError("Given instance of of Intl.RelativeTimeFormat must have an [[InitializedPluralRules]] internal slot");if("number"!=typeof b)throw new TypeError("Argument 'n' must be a number");if(!isFinite(b))return"other";var c=E(a,"pluralRules");return c.select(b)}function H(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function I(a){for(var b=1;b<arguments.length;b++)if(b%2){var c=null==arguments[b]?{}:arguments[b],d=Object.keys(c);"function"==typeof Object.getOwnPropertySymbols&&(d=d.concat(Object.getOwnPropertySymbols(c).filter(function(a){return Object.getOwnPropertyDescriptor(c,a).enumerable}))),d.forEach(function(b){H(a,b,c[b])})}else Object.defineProperties(a,Object.getOwnPropertyDescriptors(arguments[b]));return a}function J(a,b,c){for(var d=[],e=String.prototype.indexOf.call(a,"{",0),f=0,g=0,h=a.length;void 0!==a[e];){if(f=String.prototype.indexOf.call(a,"}",e),-1===f)throw new RangeError("The pattern: '".concat(a,"' is malformed"));if(e>g){var i=a.slice(g,e);d.push({type:"literal",value:i})}var j=a.slice(e+1,f);if("0"!==j)throw new TypeError("Expected ".concat(j," to be \"0\""));var k=!0,l=!1,m=void 0;try{for(var n,o,p=c[Symbol.iterator]();!(k=(n=p.next()).done);k=!0)o=n.value,"literal"===o.type?d.push(I({},o,{type:o.type})):d.push(I({},o,{unit:b}))}catch(a){l=!0,m=a}finally{try{k||null==p.return||p.return()}finally{if(l)throw m}}g=f+1,e=String.prototype.indexOf.call(a,"{",g)}if(g<h){var q=a.slice(g,h);d.push({type:"literal",value:q})}return d}function K(a,b,c){if(!F(a,"initializedRelativeTimeFormat"))throw new TypeError("Internal function called on incompatible receiver ".concat(a.toString()));if("number"!=typeof b)throw new TypeError("Argument: 'value' must be a number");if("string"!=typeof c)throw new TypeError("Argument: 'unit' must be a string");if(isNaN(b)||b===1/0||b===-Infinity)throw new RangeError("Value need to be finite number");c=d(c);var e=E(a,"fields"),f=E(a,"style"),g="short"===f?"".concat(c,"-short"):"narrow"===f?"".concat(c,"-narrow"):c,i=g in e;i||(g=c);var j=e[g];if(null==j)throw new TypeError("Could not match entry: '".concat(g,"' inside fields for locale: '").concat(E(a,"locale"),"'"));var k=E(a,"numeric");if("auto"===k&&(i=h(b)in j,i)){var l=j[h(b)];return[{type:"literal",value:l}]}var m=Object.is(b,-0)||0>b?"past":"future",n=j[m],o=E(a,"numberFormat").formatToParts(Math.abs(b)),p=G(a,b),q=n[p];return J(q,c,o)}function L(a,b,c){var d=K(a,b,c),e="",f=!0,g=!1,h=void 0;try{for(var i,j,k=d[Symbol.iterator]();!(f=(i=k.next()).done);f=!0)j=i.value,e+=j.value}catch(a){g=!0,h=a}finally{try{f||null==k.return||k.return()}finally{if(g)throw h}}return e}function M(a,b,c){return K(a,b,c)}function N(a){return+a}var O,P="RelativeTimeFormat"in Intl,Q=["lookup","best fit"],R=["long","short","narrow"],S=["always","auto"],T=["second","minute","hour","day","week","month","quarter","year"],U=/-u(?:-[0-9a-z]{2,8})+/gi,V=new WeakMap,W={relevantExtensionKeys:["nu"],localeData:{},availableLocales:[]},X=function(){function b(){a(this,b);var c=arguments[0],d=arguments[1];if(void 0===(this instanceof b?this.constructor:void 0))throw new TypeError("Constructor Intl.RelativeTimeFormat requires 'new'");var e=Intl.getCanonicalLocales(c);d=void 0===d?Object.create(null):g(d);var f=Object.create(null),h=B(d,"localeMatcher","string",Q,"best fit");f.localeMatcher=h;var i=W.localeData,j=v(W.availableLocales,e,f,W.relevantExtensionKeys,i),k=j.locale;D(this,"locale",k),D(this,"numberingSystem",j.nu);var l=j.dataLocale,m=B(d,"style","string",R,"long");D(this,"style",m);var n=B(d,"numeric","string",S,"always");D(this,"numeric",n);var o=i[l];if(!(o instanceof Object))throw new TypeError("Expected the LocaleDataEntry for locale: '".concat(l,"' to be an Object"));D(this,"fields",o),D(this,"numberFormat",new Intl.NumberFormat(k)),D(this,"pluralRules",new Intl.PluralRules(k)),D(this,"initializedRelativeTimeFormat",this)}return c(b,[{key:"format",value:function(a,b){var c=this;if(!(c instanceof Object))throw new TypeError("Method Intl.RelativeTimeFormat.prototype.format called on incompatible receiver ".concat(this.toString()));if(!F(c,"initializedRelativeTimeFormat"))throw new TypeError("Method Intl.RelativeTimeFormat.prototype.format called on incompatible receiver ".concat(this.toString()));return a=N(a),b=h(b),L(c,a,b)}},{key:"formatToParts",value:function(a,b){var c=this;if(!(c instanceof Object))throw new TypeError("Method Intl.RelativeTimeFormat.prototype.formatToParts called on incompatible receiver ".concat(this.toString()));if(!F(c,"initializedRelativeTimeFormat"))throw new TypeError("Method Intl.RelativeTimeFormat.prototype.formatToParts called on incompatible receiver ".concat(this.toString()));return a=N(a),b=h(b),M(c,a,b)}},{key:"resolvedOptions",value:function(){var a=this;if(!(a instanceof Object))throw new TypeError("Method Intl.RelativeTimeFormat.prototype.resolvedOptions called on incompatible receiver ".concat(this.toString()));if(!F(a,"initializedRelativeTimeFormat"))throw new TypeError("Method Intl.RelativeTimeFormat.prototype.resolvedOptions called on incompatible receiver ".concat(this.toString()));var b=E(this,"locale"),c=E(this,"style"),d=E(this,"numeric"),e=E(this,"numberingSystem");return{locale:b,style:c,numeric:d,numberingSystem:e}}}],[{key:"supportedLocalesOf",value:function(a){var b=arguments[1],c=W.availableLocales,d=Intl.getCanonicalLocales(a);return C(c,d,b)}},{key:"__addLocaleData",value:function(a){var b=a.data,c=a.locale,d=m();null==d&&l(c),W.localeData[c]=b,W.availableLocales.includes(c)||W.availableLocales.push(c)}}]),b}();Object.defineProperty(X.prototype,Symbol.toStringTag,{writable:!1,enumerable:!1,value:"Intl.RelativeTimeFormat",configurable:!0}),P||function(){if("undefined"==typeof Intl)throw new TypeError("Could not define Intl.RelativeTimeFormat: Expected 'Intl' to exist. Remember to include polyfills for Intl.NumberFormat, Intl.getCanonicalLocales, and Intl.PluralRules before applying this polyfill");Intl.RelativeTimeFormat=X}()})();
//# sourceMappingURL=index.min.js.map