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) 6.25 kB
/*! n2words/nb-NO v5.1.2 | MIT License | github.com/forzagreen/n2words */ function e(e){let[n,t]=e.toLowerCase().split("e");const r=parseInt(t,10);let i="";"-"===n[0]&&(i="-",n=n.slice(1));const o=n.indexOf("."),s=-1===o?n:n.slice(0,o)+n.slice(o+1),u=(-1===o?n.length:o)+r;return u>=s.length?i+s+"0".repeat(u-s.length):u<=0?i+"0."+"0".repeat(-u)+s:i+s.slice(0,u)+"."+s.slice(u)}function n(n){const r=n.toString();return t(r)?e(r):r}function t(e){return e.includes("e")||e.includes("E")}function r(e){const n="-"===e[0];n&&(e=e.slice(1));const t=e.indexOf(".");if(-1===t)return{isNegative:n,integerPart:BigInt(e)};const r=e.slice(0,t)||"0",i=e.slice(t+1);return{isNegative:n,integerPart:BigInt(r),decimalPart:i}}function i(n){let r=n.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid currency format: "${n}"`);t(r)&&(r=e(r));const i="-"===r[0];i&&(r=r.slice(1));const o=r.indexOf(".");if(-1===o)return{isNegative:i,dollars:BigInt(r),cents:0n};const s=r.slice(0,o)||"0",u=(r.slice(o+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(s),cents:BigInt(u)}}function o(e,n,t){if(null===n)return;if(e<n&&!(t&&BigInt(t)>=n))return;const r=n.toString().length-1,i=n===10n**BigInt(r)?`10^${r} - 1`:""+(n-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${i}`)}function s(e){return 10n**BigInt(3*(e+1))}const u=["","en","to","tre","fire","fem","seks","syv","åtte","ni"],l=["ti","elleve","tolv","tretten","fjorten","femten","seksten","sytten","atten","nitten"],a=["","","tjue","tretti","førti","femti","seksti","sytti","åtti","nitti"],g="tusen",c="null",f="minus",d=["million","milliard","billion","billiard","kvintillion","sekstillion","septillion","oktillion"],h=s(d.length+1),m=s(d.length+1),w=s(d.length+1),b={1:"første",2:"andre",3:"tredje",4:"fjerde",5:"femte",6:"sjette",7:"sjuende",8:"åttende",9:"niende",10:"tiende",11:"ellevte",12:"tolvte"};function p(e){if(0===e)return{word:"",hasHundred:!1};const n=e%10,t=Math.trunc(e/10)%10,r=Math.trunc(e/100),i=[];let o=!1;r>0&&(o=!0,i.push(u[r]+" hundre"));const s=e%100;return 0===s||i.push(s<10?u[n]:s<20?l[n]:0===n?a[t]:a[t]+"-"+u[n]),2===i.length?{word:i[0]+" og "+i[1],hasHundred:!0}:{word:i[0]||"",hasHundred:o}}function v(e){if(0n===e)return c;if(e<1000n)return p(Number(e)).word;if(e<1000000n){const n=Number(e/1000n),t=Number(e%1000n);let r=p(n).word+" "+g;if(t>0){const e=p(t);r+=e.hasHundred?", "+e.word:" og "+e.word}return r}return function(e){const n=e.toString(),t=n.length,r=[],i=t%3;let o=0;for(i>0&&(r.push(Number(n.slice(0,i))),o=i);o<t;)r.push(Number(n.slice(o,o+3))),o+=3;const s=[];let u=r.length-1;for(let e=0;e<r.length;e++){const n=r[e];if(0!==n){const e=p(n);s.push(0===u?{word:e.word,hasHundred:e.hasHundred,type:"units"}:1===u?{word:e.word+" "+g,hasHundred:!1,type:"thousand"}:{word:e.word+" "+d[u-2],hasHundred:!1,type:"million"})}u--}return function(e){if(0===e.length)return c;if(1===e.length)return e[0].word;const n=[];for(let t=0;t<e.length;t++){const r=e[t],i=e[t+1];n.push(r.word),i&&n.push("thousand"===r.type?i.hasHundred?", ":" og ":"million"===r.type?"units"!==i.type||i.hasHundred?" ":" og ":" ")}return n.join("")}(s)}(e)}function N(i){const{isNegative:s,integerPart:u,decimalPart:l}=function(i){const o=typeof i;if("bigint"==typeof i)return i<0n?{isNegative:!0,integerPart:-i}:{isNegative:!1,integerPart:i};if("number"==typeof i){if(!Number.isFinite(i))throw new RangeError("Number must be finite (NaN and Infinity are not supported)");return Number.isSafeInteger(i)?i<0?{isNegative:!0,integerPart:BigInt(-i)}:{isNegative:!1,integerPart:BigInt(i)}:r(n(i))}if("string"==typeof i)return r(function(n){const r=n.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid number format: "${n}"`);return t(r)?e(r):r}(i));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(i);o(u,h,l);let a="";return s&&(a=f+" "),a+=v(u),l&&(a+=" komma "+function(e){let n="",t=0;for(;t<e.length&&"0"===e[t];)n&&(n+=" "),n+=c,t++;const r=e.slice(t);return r&&(n&&(n+=" "),n+=v(BigInt(r))),n}(l)),a}function y(n){const r=function(n){const r=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 r=n.trim();if(0===r.length)throw new RangeError("Ordinals cannot be empty strings");if("-"===r[0])throw new RangeError("Ordinals cannot be negative");if(r.includes("."))throw new RangeError("Ordinals must be whole numbers");if(t(r)){const n=e(r);if(n.includes("."))throw new RangeError("Ordinals must be whole numbers");const t=BigInt(n);if(t<=0n)throw new RangeError("Ordinals must be positive integers");return t}try{const e=BigInt(r);if(e<=0n)throw new RangeError("Ordinals must be positive integers");return e}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 ${r}`)}(n);return o(r,m),function(e){if(e>=1n&&e<=12n)return b[Number(e)];const n=Number(e),t=v(e);return n>=13&&n<=19||t.endsWith("en")?t.slice(0,-2)+"ende":t.endsWith("ni")?t+"ede":t+"de"}(r)}function I(e){const{isNegative:t,dollars:r,cents:s}=function(e){const t=typeof e;if("bigint"==typeof e)return e<0n?{isNegative:!0,dollars:-e,cents:0n}:{isNegative:!1,dollars:e,cents:0n};if("number"==typeof e){if(!Number.isFinite(e))throw new RangeError("Currency must be a finite number");return Number.isSafeInteger(e)?e<0?{isNegative:!0,dollars:BigInt(-e),cents:0n}:{isNegative:!1,dollars:BigInt(e),cents:0n}:i(n(e))}if("string"==typeof e)return i(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(e);o(r,w);let u="";return t&&(u=f+" "),(r>0n||0n===s)&&(u+=1n===r?"en krone":v(r)+" kroner"),s>0n&&(r>0n&&(u+=" og "),u+=v(s)+" øre"),u}export{h as cardinalMax,w as currencyMax,m as ordinalMax,N as toCardinal,I as toCurrency,y as toOrdinal};