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.37 kB
/*! n2words/sv-SE v5.1.2 | MIT License | github.com/forzagreen/n2words */ var e,n;e=this,n=function(e){"use strict";function n(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 t(e){const t=e.toString();return r(t)?n(t):t}function r(e){return e.includes("e")||e.includes("E")}function i(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 o(e){let t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid currency format: "${e}"`);r(t)&&(t=n(t));const i="-"===t[0];i&&(t=t.slice(1));const o=t.indexOf(".");if(-1===o)return{isNegative:i,dollars:BigInt(t),cents:0n};const s=t.slice(0,o)||"0",u=(t.slice(o+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(s),cents:BigInt(u)}}function s(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 u(e){return 10n**BigInt(3*(e+1))}const a=["","ett","två","tre","fyra","fem","sex","sju","åtta","nio"],l=["tio","elva","tolv","tretton","fjorton","femton","sexton","sjutton","arton","nitton"],c=["","","tjugo","trettio","fyrtio","femtio","sextio","sjuttio","åttio","nittio"],f="hundra",g="noll",d="minus",h=["tusen","miljon","miljard","biljon","biljard","triljon","triljard","kvadriljon"],m=u(h.length),w=u(h.length),b=u(h.length),p={1:"första",2:"andra",3:"tredje",4:"fjärde",5:"femte",6:"sjätte",7:"sjunde",8:"åttonde",9:"nionde",10:"tionde",11:"elfte",12:"tolfte"};function v(e){if(0===e)return{word:"",hasHundred:!1,lessThan100:!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(1===r?f:a[r]+" "+f));let s="";return 1===t?s=l[n]:t>=2?s=n>0?c[t]+"-"+a[n]:c[t]:n>0&&(s=a[n]),o&&s?{word:i[0]+" och "+s,hasHundred:!0,lessThan100:!1}:o?{word:i[0],hasHundred:!0,lessThan100:!1}:{word:s,hasHundred:!1,lessThan100:!0}}function N(e){if(0n===e)return g;if(e<1000n)return v(Number(e)).word;if(e<1000000n){const n=Number(e/1000n),t=Number(e%1000n);let r=1===n?h[0]:v(n).word+" "+h[0];if(t>0){const e=v(t);r+=e.lessThan100?" och "+e.word:" "+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=v(n);if(0===u)s.push({word:e.word,hasHundred:e.hasHundred,isScale:!1});else{const t=h[u-1];let r;r=1===n?1===u?"":"en":e.word,r&&s.push({word:r,hasHundred:!1,isScale:!1}),s.push({word:t,hasHundred:!1,isScale:!0})}}u--}return function(e){if(0===e.length)return g;if(1===e.length)return e[0].word;const n=[];for(let t=0;t<e.length;t++){const r=e[t];t===e.length-1&&e.length>1&&e[t-1].isScale&&!r.hasHundred&&n.push("och"),n.push(r.word)}return n.join(" ")}(s)}(e)}const y={svSE:function(e){const t=function(e){const t=typeof e;if("bigint"==typeof e){if(e<=0n)throw new RangeError("Ordinals must be positive integers");return e}if("number"==typeof e){if(!Number.isFinite(e))throw new RangeError("Ordinals must be finite numbers");if(!Number.isInteger(e))throw new RangeError("Ordinals must be whole numbers");if(e<=0)throw new RangeError("Ordinals must be positive integers");return BigInt(e)}if("string"==typeof e)return function(e){const t=e.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(r(t)){const e=n(t);if(e.includes("."))throw new RangeError("Ordinals must be whole numbers");const r=BigInt(e);if(r<=0n)throw new RangeError("Ordinals must be positive integers");return r}try{const e=BigInt(t);if(e<=0n)throw new RangeError("Ordinals must be positive integers");return e}catch(n){if(n instanceof RangeError)throw n;throw new RangeError(`Invalid ordinal format: "${e}"`,{cause:n})}}(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(e);return s(t,w),(i=t)>=1n&&i<=12n?p[Number(i)]:N(i)+"de";var i}};e.currency={svSE:function(e){const{isNegative:n,dollars:r,cents:i}=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}:o(t(e))}if("string"==typeof e)return o(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${n}`)}(e);s(r,b);let u="";return n&&(u=d+" "),(r>0n||0n===i)&&(u+=1n===r?"en krona":N(r)+" kronor"),i>0n&&(r>0n&&(u+=" och "),u+=N(i)+" öre"),u}},e.ordinal=y,e.svSE=function(e){const{isNegative:o,integerPart:u,decimalPart:a}=function(e){const o=typeof e;if("bigint"==typeof e)return e<0n?{isNegative:!0,integerPart:-e}:{isNegative:!1,integerPart:e};if("number"==typeof e){if(!Number.isFinite(e))throw new RangeError("Number must be finite (NaN and Infinity are not supported)");return Number.isSafeInteger(e)?e<0?{isNegative:!0,integerPart:BigInt(-e)}:{isNegative:!1,integerPart:BigInt(e)}:i(t(e))}if("string"==typeof e)return i(function(e){const t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid number format: "${e}"`);return r(t)?n(t):t}(e));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(e);s(u,m,a);let l="";return o&&(l=d+" "),l+=N(u),a&&(l+=" komma "+function(e){let n="",t=0;for(;t<e.length&&"0"===e[t];)n&&(n+=" "),n+=g,t++;const r=e.slice(t);return r&&(n&&(n+=" "),n+=N(BigInt(r))),n}(a)),l}},"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).n2words=e.n2words||{});