UNPKG

n2words

Version:

Convert numbers to words in 70+ languages with zero dependencies. Supports BigInt, decimals, and browser/Node.js environments.

3 lines (2 loc) 5.97 kB
/*! n2words/fil-PH v5.1.2 | MIT License | github.com/forzagreen/n2words */ var n,e;n=this,e=function(n){"use strict";function e(n){let[e,t]=n.toLowerCase().split("e");const i=parseInt(t,10);let r="";"-"===e[0]&&(r="-",e=e.slice(1));const o=e.indexOf("."),s=-1===o?e:e.slice(0,o)+e.slice(o+1),a=(-1===o?e.length:o)+i;return a>=s.length?r+s+"0".repeat(a-s.length):a<=0?r+"0."+"0".repeat(-a)+s:r+s.slice(0,a)+"."+s.slice(a)}function t(n){const t=n.toString();return i(t)?e(t):t}function i(n){return n.includes("e")||n.includes("E")}function r(n){const e="-"===n[0];e&&(n=n.slice(1));const t=n.indexOf(".");if(-1===t)return{isNegative:e,integerPart:BigInt(n)};const i=n.slice(0,t)||"0",r=n.slice(t+1);return{isNegative:e,integerPart:BigInt(i),decimalPart:r}}function o(n){let t=n.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid currency format: "${n}"`);i(t)&&(t=e(t));const r="-"===t[0];r&&(t=t.slice(1));const o=t.indexOf(".");if(-1===o)return{isNegative:r,dollars:BigInt(t),cents:0n};const s=t.slice(0,o)||"0",a=(t.slice(o+1)+"00").slice(0,2);return{isNegative:r,dollars:BigInt(s),cents:BigInt(a)}}function s(n,e,t){if(null===e)return;if(n<e)return;const i=e.toString().length-1,r=e===10n**BigInt(i)?`10^${i} - 1`:""+(e-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${r}`)}function a(n){return 10n**BigInt(3*(n+1))}const u=["","isa","dalawa","tatlo","apat","lima","anim","pito","walo","siyam"],l=["sampu","labinisa","labindalawa","labintatlo","labinapat","labinlima","labinanum","labimpito","labingwalo","labinsiyam"],c=["","","dalawampu","tatlumpu","apatnapu","limampu","animnapu","pitumpu","walumpu","siyamnapu"],f="zero",g="negatibo",d=["","libong","milyong","bilyong","trilyong"],m=a(d.length-1),p=a(d.length-1),b=a(d.length-1),w="piso",h="sentimo",v=["a","e","i","o","u"];function y(n){return v.includes(n[n.length-1])?n+"ng":n+" na"}function N(n){if(0===n)return"";const e=n%10,t=Math.trunc(n/10)%10,i=Math.trunc(n/100),r=[];if(i>0){const n=y(u[i]);r.push(n+" daang")}const o=n%100;return 0===o||r.push(o<10?u[e]:o<20?l[e]:0===e?c[t]:5===t?c[t]+"ng "+u[e]:c[t]+" "+u[e]),r.join(" ")}function I(n){const e=N(n);if(!e)return"";const t=e.lastIndexOf(" ");if(-1===t)return"g"===e[e.length-1]&&e.endsWith("ng")?e:y(e);const i=e.slice(0,t+1),r=e.slice(t+1);return r.endsWith("ng")?e:i+y(r)}function E(n){return 0n===n?f:n<1000n?N(Number(n)):function(n){const e=[];let t=n;for(;t>0n;)e.push(Number(t%1000n)),t/=1000n;let i="";for(let n=e.length-1;n>=0;n--){const t=e[n];if(0===t)continue;const r=d[n]||"";i&&(i+=" "),i+=0===n?N(t):I(t)+" "+r}return i}(n)}const R={filPH:function(n){const t=function(n){const t=typeof n;if("bigint"==typeof n){if(n<=0n)throw new RangeError("Ordinals must be positive integers");return n}if("number"==typeof n){if(!Number.isFinite(n))throw new RangeError("Ordinals must be finite numbers");if(!Number.isInteger(n))throw new RangeError("Ordinals must be whole numbers");if(n<=0)throw new RangeError("Ordinals must be positive integers");return BigInt(n)}if("string"==typeof n)return function(n){const t=n.trim();if(0===t.length)throw new RangeError("Ordinals cannot be empty strings");if("-"===t[0])throw new RangeError("Ordinals cannot be negative");if(t.includes("."))throw new RangeError("Ordinals must be whole numbers");if(i(t)){const n=e(t);if(n.includes("."))throw new RangeError("Ordinals must be whole numbers");const i=BigInt(n);if(i<=0n)throw new RangeError("Ordinals must be positive integers");return i}try{const n=BigInt(t);if(n<=0n)throw new RangeError("Ordinals must be positive integers");return n}catch(e){if(e instanceof RangeError)throw e;throw new RangeError(`Invalid ordinal format: "${n}"`,{cause:e})}}(n);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(n);return s(t,p),1n===(r=t)?"una":2n===r?"ikalawa":"ika-"+E(r);var r}};n.currency={filPH:function(n){const{isNegative:e,dollars:i,cents:r}=function(n){const e=typeof n;if("bigint"==typeof n)return n<0n?{isNegative:!0,dollars:-n,cents:0n}:{isNegative:!1,dollars:n,cents:0n};if("number"==typeof n){if(!Number.isFinite(n))throw new RangeError("Currency must be a finite number");return Number.isSafeInteger(n)?n<0?{isNegative:!0,dollars:BigInt(-n),cents:0n}:{isNegative:!1,dollars:BigInt(n),cents:0n}:o(t(n))}if("string"==typeof n)return o(n);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${e}`)}(n);s(i,b);let a="";if(e&&(a=g+" "),i>0n||0n===r){const n=E(i);if(0n===i)a+=n+" "+w;else{const e=n.lastIndexOf(" ");if(-1===e)a+=y(n)+" "+w;else{const t=n.slice(0,e+1),i=n.slice(e+1);i.endsWith("ng")?a+=n+" "+w:a+=t+y(i)+" "+w}}}if(r>0n){i>0n&&(a+=" at ");const n=E(r),e=n.lastIndexOf(" ");if(-1===e)a+=y(n)+" "+h;else{const t=n.slice(0,e+1),i=n.slice(e+1);i.endsWith("ng")?a+=n+" "+h:a+=t+y(i)+" "+h}}return a}},n.filPH=function(n){const{isNegative:o,integerPart:a,decimalPart:l}=function(n){const o=typeof n;if("bigint"==typeof n)return n<0n?{isNegative:!0,integerPart:-n}:{isNegative:!1,integerPart:n};if("number"==typeof n){if(!Number.isFinite(n))throw new RangeError("Number must be finite (NaN and Infinity are not supported)");return Number.isSafeInteger(n)?n<0?{isNegative:!0,integerPart:BigInt(-n)}:{isNegative:!1,integerPart:BigInt(n)}:r(t(n))}if("string"==typeof n)return r(function(n){const t=n.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid number format: "${n}"`);return i(t)?e(t):t}(n));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(n);s(a,m);let c="";return o&&(c=g+" "),c+=E(a),l&&(c+=" punto "+function(n){const e=[];for(const t of n){const n=parseInt(t,10);e.push(0===n?f:u[n])}return e.join(" ")}(l)),c},n.ordinal=R},"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((n="undefined"!=typeof globalThis?globalThis:n||self).n2words=n.n2words||{});