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.99 kB
/*! n2words/da-DK v5.1.2 | MIT License | github.com/forzagreen/n2words */ function e(e){let[t,n]=e.toLowerCase().split("e");const r=parseInt(n,10);let i="";"-"===t[0]&&(i="-",t=t.slice(1));const o=t.indexOf("."),s=-1===o?t:t.slice(0,o)+t.slice(o+1),u=(-1===o?t.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 t(t){const r=t.toString();return n(r)?e(r):r}function n(e){return e.includes("e")||e.includes("E")}function r(e){const t="-"===e[0];t&&(e=e.slice(1));const n=e.indexOf(".");if(-1===n)return{isNegative:t,integerPart:BigInt(e)};const r=e.slice(0,n)||"0",i=e.slice(n+1);return{isNegative:t,integerPart:BigInt(r),decimalPart:i}}function i(t){let r=t.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid currency format: "${t}"`);n(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,t,n){if(null===t)return;if(e<t&&!(n&&BigInt(n)>=t))return;const r=t.toString().length-1,i=t===10n**BigInt(r)?`10^${r} - 1`:""+(t-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=["","et","to","tre","fire","fem","seks","syv","otte","ni"],l=["","en","to","tre","fire","fem","seks","syv","otte","ni"],a=["ti","elleve","tolv","tretten","fjorten","femten","seksten","sytten","atten","nitten"],g=["","","tyve","tredive","fyrre","halvtreds","treds","halvfjerds","firs","halvfems"],f="tusind",c="nul",d="minus",m=["millioner","millarder","billioner","billarder","trillioner","trillarder","quadrillioner","quadrillarder"],h=s(m.length+1),b=s(m.length+1),p=s(m.length+1),w={1:"første",2:"anden",3:"tredje",4:"fjerde",5:"femte",6:"sjette",7:"syvende",8:"ottende",9:"niende",10:"tiende",11:"ellevte",12:"tolvte"};function v(e){if(0===e)return"";const t=e%10,n=Math.trunc(e/10)%10,r=Math.trunc(e/100),i=[];r>0&&i.push(u[r]+"hundrede");const o=e%100;return 0===o||i.push(o<10?u[t]:o<20?a[t]:0===t?g[n]:l[t]+"og"+g[n]),2===i.length?i[0]+" og "+i[1]:i[0]||""}function N(e){if(0n===e)return c;if(e<1000n)return v(Number(e));if(e<1000000n){const t=Number(e/1000n),n=Number(e%1000n);let r=v(t)+f;return n>0&&(r+="e og "+v(n)),r}return function(e){const t=e.toString(),n=t.length,r=[],i=n%3;let o=0;for(i>0&&(r.push(Number(t.slice(0,i))),o=i);o<n;)r.push(Number(t.slice(o,o+3))),o+=3;const s=[];let u=r.length-1;for(let e=0;e<r.length;e++){const t=r[e];if(0!==t){const e=v(t);if(0===u)s.push({word:e,type:"units"});else if(1===u)s.push({word:e+f,type:"thousand"});else{let n=e;1===t&&(n="en"),s.push({word:n+" "+m[u-2],type:"million"})}}u--}return function(e){if(0===e.length)return c;const t=[];for(let n=0;n<e.length;n++){const r=e[n],i=e[n+1];"thousand"===r.type&&i&&"units"===i.type?(t.push(r.word+"e og "+i.word),n++):t.push(r.word)}return t.join(" ")}(s)}(e)}function y(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(t(i))}if("string"==typeof i)return r(function(t){const r=t.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid number format: "${t}"`);return n(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=d+" "),a+=N(u),l&&(a+=" komma "+function(e){let t="",n=0;for(;n<e.length&&"0"===e[n];)t&&(t+=" "),t+=c,n++;const r=e.slice(n);return r&&(t&&(t+=" "),t+=N(BigInt(r))),t}(l)),a}function I(t){const r=function(t){const r=typeof t;if("bigint"==typeof t){if(t<=0n)throw new RangeError("Ordinals must be positive integers");return t}if("number"==typeof t){if(!Number.isFinite(t))throw new RangeError("Ordinals must be finite numbers");if(!Number.isInteger(t))throw new RangeError("Ordinals must be whole numbers");if(t<=0)throw new RangeError("Ordinals must be positive integers");return BigInt(t)}if("string"==typeof t)return function(t){const r=t.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(n(r)){const t=e(r);if(t.includes("."))throw new RangeError("Ordinals must be whole numbers");const n=BigInt(t);if(n<=0n)throw new RangeError("Ordinals must be positive integers");return n}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: "${t}"`,{cause:e})}}(t);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${r}`)}(t);return o(r,b),(i=r)>=1n&&i<=12n?w[Number(i)]:N(i)+"de";var i}function E(e){const{isNegative:n,dollars:r,cents:s}=function(e){const n=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(t(e))}if("string"==typeof e)return i(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${n}`)}(e);o(r,p);let u="";return n&&(u=d+" "),(r>0n||0n===s)&&(u+=1n===r?"en krone":N(r)+" kroner"),s>0n&&(r>0n&&(u+=" og "),u+=N(s)+" øre"),u}export{h as cardinalMax,p as currencyMax,b as ordinalMax,y as toCardinal,E as toCurrency,I as toOrdinal};