UNPKG

currency-to-words

Version:
263 lines (237 loc) 7.83 kB
module.exports = /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); /******/ } /******/ }; /******/ /******/ // define __esModule on exports /******/ __webpack_require__.r = function(exports) { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ /******/ // create a fake namespace object /******/ // mode & 1: value is a module id, require it /******/ // mode & 2: merge all properties of value into the ns /******/ // mode & 4: return value when already ns object /******/ // mode & 8|1: behave like require /******/ __webpack_require__.t = function(value, mode) { /******/ if(mode & 1) value = __webpack_require__(value); /******/ if(mode & 8) return value; /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; /******/ var ns = Object.create(null); /******/ __webpack_require__.r(ns); /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); /******/ return ns; /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; // ESM COMPAT FLAG __webpack_require__.r(__webpack_exports__); // EXPORTS __webpack_require__.d(__webpack_exports__, "CurrencyToWords", function() { return /* reexport */ CurrencyToWords; }); // CONCATENATED MODULE: ./src/converter.jsx const oneDigit = (digit) => { switch (digit) { case 1: return "one"; case 2: return "two"; case 3: return "three"; case 4: return "four"; case 5: return "five"; case 6: return "six"; case 7: return "seven"; case 8: return "eight"; case 9: return "nine"; default: return ""; } }; const twoDigits = (twoDigitNumber) => { switch (twoDigitNumber) { case 10: return "ten"; case 11: return "eleven"; case 12: return "twelve"; case 13: return "thirteen"; case 14: return "fourteen"; case 15: return "fifteen"; case 16: return "sixteen"; case 17: return "seventeen"; case 18: return "eighteen"; case 19: return "nineteen"; case 20: return "twenty"; case 30: return "thirty"; case 40: return "forty"; case 50: return "fifty"; case 60: return "sixty"; case 70: return "seventy"; case 80: return "eighty"; case 90: return "ninety"; default: //for numbers like 21 const firstPart = twoDigitNumber % 10; const secondPart = twoDigitNumber - firstPart; return `${twoDigits(secondPart)}-${oneDigit(firstPart)}`; } }; const threeDigits = (threeDigits) => { const firstPart = oneDigit(Math.floor(threeDigits / 100)) + " hundred "; const secondPart = threeDigits % 100; return ( firstPart + (secondPart > 9 ? twoDigits(secondPart) : oneDigit(secondPart)) ); //for numbers like 101 }; const GetNumericGroupTitle = (numericGroup) => { switch (numericGroup) { default: case 1: //hundreds doesn't need return ""; case 2: return "thousand"; case 3: return "million"; } }; /* harmony default export */ var converter = ({ oneDigit, twoDigits, threeDigits, GetNumericGroupTitle }); // CONCATENATED MODULE: ./src/currencyToWords.jsx const convertToWords = (currency) => { if (currency !=0 && !currency) return "invalid input"; let numericGroup = 1; // thousands , millions let result = ""; try { while (currency != 0) { let tempResult = ""; let remain = currency % 1000; if (remain < 10) tempResult = oneDigit(remain); else if (remain < 100) tempResult = twoDigits(remain); else tempResult = threeDigits(remain); if (tempResult) //for numbers like 1,000,001 that thousands part is empty result = `${tempResult} ${GetNumericGroupTitle(numericGroup)} ${result}`.trim(); currency = Math.floor(currency / 1000); numericGroup++; } return result; } catch (e) { throw new Error("Exception in parsing"); } }; const completeDollarWords = (dollars, dollarAmountWord = "dollar") => { if (!dollars) dollars = "zero"; return dollars + (dollars === "one" ? ` ${dollarAmountWord}` : ` ${dollarAmountWord}s`); }; const completeCentsWords = (cents, centAmountWord = "cent") => { if (!cents) return ""; return ` and ${cents} ` + (cents === "one" ? `${centAmountWord}` : `${centAmountWord}s`); }; const CurrencyToWords = (value, dollarText = "dollar", centText = "cent") => { const currency = (value + "").replace(" ", "").split("."); const dollarInWords = convertToWords(Number.parseInt(currency[0])); const centsInWords = currency.length == 2 ? convertToWords( Number.parseInt( currency[1].length == 1 ? `${currency[1]}0` : currency[1] ) ) : ""; return completeDollarWords(dollarInWords, dollarText) + completeCentsWords(centsInWords, centText); }; /* harmony default export */ var currencyToWords = (CurrencyToWords); // CONCATENATED MODULE: ./index.jsx /***/ }) /******/ ]);