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.63 kB
/*! n2words/sw-KE 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&&!(t&&BigInt(t)>=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=["sifuri","moja","mbili","tatu","nne","tano","sita","saba","nane","tisa"],l={10:"kumi",20:"ishirini",30:"thelathini",40:"arobaini",50:"hamsini",60:"sitini",70:"sabini",80:"themanini",90:"tisini"},c=["","elfu","milioni","bilioni","trilioni","kwadrilioni","kwintilioni"],f=a(c.length-1),g=a(c.length-1),w=a(c.length-1),m="sifuri",d="minus",h={1:"wa kwanza",2:"wa pili",3:"wa tatu",4:"wa nne",5:"wa tano",6:"wa sita",7:"wa saba",8:"wa nane",9:"wa tisa"};function b(n){if(n<10)return u[n];if(10===n)return l[10];if(n<20)return l[10]+" na "+u[n-10];const e=10*Math.trunc(n/10),t=n%10;return 0===t?l[e]:l[e]+" na "+u[t]}function p(n){if(n<100)return b(n);if(100===n)return"mia moja";const e=Math.trunc(n/100),t=n%100,i=[];return i.push("mia "+u[e]),t>0&&i.push(t<10?"na "+u[t]:b(t)),i.join(" ")}function v(n){if(0n===n)return m;const e=function(n){const e=[];let t=n;for(;t>0n;)e.push(Number(t%1000n)),t/=1000n;return e}(n),t=[];for(let n=e.length-1;n>=0;n--){const i=e[n];if(0!==i)if(0===n)t.push(i<10&&t.length>0?"na "+u[i]:100===i&&t.length>0?"mia":p(i));else{const e=1===i?"moja":p(i);t.push(c[n]+" "+e)}}return t.join(" ").trim()}const N={swKE: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,g),(r=t)>=1n&&r<=9n?h[Number(r)]:"wa "+v(r);var r}};n.currency={swKE: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,w);let a="";return e&&(a=d+" "),(i>0n||0n===r)&&(a+="shilingi "+v(i)),r>0n&&(i>0n&&(a+=" na "),a+="senti "+v(r)),a}},n.ordinal=N,n.swKE=function(n){const{isNegative:o,integerPart:a,decimalPart:u}=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,f,u);let l="";return o&&(l=d+" "),l+=v(a),u&&(l+=" nukta "+function(n){let e="",t=0;for(;t<n.length&&"0"===n[t];)e&&(e+=" "),e+=m,t++;const i=n.slice(t);return i&&(e&&(e+=" "),e+=v(BigInt(i))),e}(u)),l}},"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||{});