price-to-words-pl
Version:
Converting a price value into a word representation with a currency.
1 lines • 10.4 kB
JavaScript
!function(t,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports["price-to-words-pl"]=r():t["price-to-words-pl"]=r()}(this,(function(){return(()=>{"use strict";var t={d:(r,e)=>{for(var n in e)t.o(e,n)&&!t.o(r,n)&&Object.defineProperty(r,n,{enumerable:!0,get:e[n]})},o:(t,r)=>Object.prototype.hasOwnProperty.call(t,r),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},r={};return((t,r,e)=>{function n(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e<r;e++)n[e]=t[e];return n}function o(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}function i(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}e.r(r),e.d(r,{CONVERTING_FORMATS:()=>a,default:()=>w});var a={typeA:"zl-words zl-full and gr-words gr-full",typeB:"zl-words zl gr-words gr",typeC:"zl-words zl gr-short",typeD:"zl-number zl gr-number gr"},u=function(){function t(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:a;o(this,t),this.formats=r}var r,e;return r=t,(e=[{key:"addFormats",value:function(t){Object.assign(this.formats,t)}},{key:"has",value:function(t){return(r=Object.entries(t),e=1,function(t){if(Array.isArray(t))return t}(r)||function(t,r){var e=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=e){var n,o,i=[],a=!0,u=!1;try{for(e=e.call(t);!(a=(n=e.next()).done)&&(i.push(n.value),!r||i.length!==r);a=!0);}catch(t){u=!0,o=t}finally{try{a||null==e.return||e.return()}finally{if(u)throw o}}return i}}(r,e)||function(t,r){if(t){if("string"==typeof t)return n(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?n(t,r):void 0}}(r,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.")}())[0]in this.formats;var r,e}},{key:"_formatRulesToMethodName",value:function(t){return t.match(/[-a-z]+/gi).map((function(t){return t.replace(/(-)([a-z])/gi,(function(t,r,e){return e.toUpperCase()}))}))}},{key:"getPriceFormat",value:function(t){var r=this._formatRulesToMethodName(this.formats.typeA);if("string"==typeof t)if(1===t.length){var e="type".concat(t.toUpperCase());this.formats.hasOwnProperty(e)&&(r=this._formatRulesToMethodName(this.formats[e]))}else{var n=this._formatRulesToMethodName(t);r=n.length?n:r}else void 0!==t&&(r=[""]);return r}}])&&i(r.prototype,e),t}();function c(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e<r;e++)n[e]=t[e];return n}function l(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}var s=function(){function t(r){!function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,t),this.dictionary=r.translations}var r,e;return r=t,(e=[{key:"_addTails",value:function(t){return t.replace(/(n |[ay] |[ćm] )(tysiąc|milion|miliard)/g,(function(t,r,e){if("tysiąc"===e)switch(r){case"n ":return e;case"a ":case"y ":return"".concat(r,"tysiące");case"ć ":case"m ":return"".concat(r,"tysięcy")}else switch(r){case"n ":return e;case"a ":case"y ":return"".concat(r).concat(e,"y");case"ć ":case"m ":return"".concat(r).concat(e,"ów")}}))}},{key:"convert",value:function(t){var r=this;if(!(t=+t)||t<=0)return"zero";var e=this.dictionary.reduce((function(e,n){var o,i,a=(i=2,function(t){if(Array.isArray(t))return t}(o=n)||function(t,r){var e=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=e){var n,o,i=[],a=!0,u=!1;try{for(e=e.call(t);!(a=(n=e.next()).done)&&(i.push(n.value),!r||i.length!==r);a=!0);}catch(t){u=!0,o=t}finally{try{a||null==e.return||e.return()}finally{if(u)throw o}}return i}}(o,i)||function(t,r){if(t){if("string"==typeof t)return c(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?c(t,r):void 0}}(o,i)||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.")}()),u=a[0],l=a[1],s=~~(t/l);return s?(t%=l,e+(l>=1e3?r.convert(s)+" ":"")+u+(t?" ":"")):e}),"");return this._addTails(e)}}])&&l(r.prototype,e),t}();function f(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}var y=function(){function t(r,e){!function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,t),this.price=r,this.numberToWords=new s(e)}var r,e;return r=t,(e=[{key:"zlNumber",value:function(){return"".concat(this.price[0])}},{key:"grNumber",value:function(){return this.price[1]<10?"0".concat(this.price[1]):"".concat(this.price[1])}},{key:"zlWords",value:function(){return this.numberToWords.convert(this.price[0])}},{key:"grWords",value:function(){return this.numberToWords.convert(this.price[1])}},{key:"zl",value:function(){return"zł"}},{key:"gr",value:function(){return"gr"}},{key:"grShort",value:function(){return"".concat(this.grNumber(this.price),"/100")}},{key:"zlFull",value:function(){if(1===this.price[0])return"złoty";if(this.price[0]>=5&&this.price[0]<21)return"złotych";var t=this.price[0].toString().split("").slice(-1);return/[234]/.test(t)?"złote":"złotych"}},{key:"grFull",value:function(){if(1===this.price[1])return"grosz";if(0===this.price[0]&&this.price[1]>=5&&this.price[1]<21)return"groszy";var t=this.price[1].toString().split("").slice(-1);return/[234]/.test(t)?"grosze":"groszy"}},{key:"and",value:function(){return"i"}}])&&f(r.prototype,e),t}();const d=JSON.parse('{"translations":[["miliard",1000000000],["milion",1000000],["tysiąc",1000],["dziewięćset",900],["osiemset",800],["siedemset",700],["sześćset",600],["pięćset",500],["czterysta",400],["trzysta",300],["dwieście",200],["sto",100],["dziewięćdziesiąt",90],["osiemdziesiąt",80],["siedemdziesiąt",70],["sześćdziesiąt",60],["pięćdziesiąt",50],["czterdzieści",40],["trzydzieści",30],["dwadzieścia",20],["dziewiętnaście",19],["osiemnaście",18],["siedemnaście",17],["szesnaście",16],["piętnaście",15],["czternaście",14],["trzynaście",13],["dwanaście",12],["jedenaście",11],["dziesięć",10],["dziewięć",9],["osiem",8],["siedem",7],["sześć",6],["pięć",5],["cztery",4],["trzy",3],["dwa",2],["jeden",1]]}');function p(t,r){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!e){if(Array.isArray(t)||(e=m(t))||r&&t&&"number"==typeof t.length){e&&(t=e);var n=0,o=function(){};return{s:o,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,a=!0,u=!1;return{s:function(){e=e.call(t)},n:function(){var t=e.next();return a=t.done,t},e:function(t){u=!0,i=t},f:function(){try{a||null==e.return||e.return()}finally{if(u)throw 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,r){if(t){if("string"==typeof t)return v(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?v(t,r):void 0}}function v(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e<r;e++)n[e]=t[e];return n}function b(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}function g(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}var w=function(){function t(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=r.formatManger,n=void 0===e?new u:e,o=r.dictionary,i=void 0===o?d:o,a=r.format;b(this,t),this.formatManger=n,this.dictionary=i,this._defautlFormat=a}var r,e;return r=t,e=[{key:"_priceToArray",value:function(t){if("number"!=typeof t||isNaN(t))throw new TypeError("Invalid price value");var r,e,n=t.toString(),o=(r=(n="0"===n?"0.0":n).split(/[,.]/),e=2,function(t){if(Array.isArray(t))return t}(r)||function(t,r){var e=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=e){var n,o,i=[],a=!0,u=!1;try{for(e=e.call(t);!(a=(n=e.next()).done)&&(i.push(n.value),!r||i.length!==r);a=!0);}catch(t){u=!0,o=t}finally{try{a||null==e.return||e.return()}finally{if(u)throw o}}return i}}(r,e)||m(r,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.")}()),i=o[0],a=o[1];if([i,a=(a||"").padEnd(2,"0")].map((function(t){return+t})),isNaN(i)||isNaN(a))throw new Error("Parsing error");return[~~i,~~a]}},{key:"addFormats",value:function(t){this.formatManger.addFormats(t)}},{key:"format",set:function(t){this.formatManger.has(t)&&this.addFormats(t),this._defautlFormat=Object.values(t)[0]}},{key:"convert",value:function(t){var r,e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this._defautlFormat;"object"===h(e)&&(e=null===(r=Object.values(e))||void 0===r?void 0:r[0]),this.priceArray=null!=t?this._priceToArray(t):null,e=this.formatManger.getPriceFormat(e);var n=new y(this.priceArray,this.dictionary),o="";if(!this.priceArray)throw new TypeError("Invalid input format");var i,a=p(e);try{for(a.s();!(i=a.n()).done;){var u=i.value;try{o+=n[u]()+" "}catch(t){throw new TypeError("Invalid format!")}}}catch(t){a.e(t)}finally{a.f()}return o.trim()}}],e&&g(r.prototype,e),t}()})(0,r,t),r})()}));