/* Toolbox — all tool logic. Runs entirely in the browser. No data leaves the page except the currency tool, which calls a public exchange-rate API. */ (function () { const $ = id => document.getElementById(id); const on = (id, ev, fn) => { const e = $(id); if (e) e.addEventListener(ev, fn); }; const num = v => { const n = parseFloat(v); return isNaN(n) ? 0 : n; }; const int = v => { const n = parseInt(v, 10); return isNaN(n) ? 0 : n; }; const inr = n => "₹" + Number(n).toLocaleString("en-IN", { maximumFractionDigits: 2 }); const n2 = n => Number(n).toLocaleString("en-IN", { maximumFractionDigits: 2 }); const set = (id, html) => { const e = $(id); if (e) e.innerHTML = html; }; /* ---------- 1. Age ---------- */ function initAge() { const go = () => { const v = $("age-dob").value; if (!v) return; const dob = new Date(v + "T00:00:00"), now = new Date(); if (dob > now) { set("age-out", 'Date of birth is in the future.'); return; } let y = now.getFullYear() - dob.getFullYear(); let m = now.getMonth() - dob.getMonth(); let d = now.getDate() - dob.getDate(); if (d < 0) { m--; d += new Date(now.getFullYear(), now.getMonth(), 0).getDate(); } if (m < 0) { y--; m += 12; } const days = Math.floor((now - dob) / 86400000); const nb = new Date(now.getFullYear(), dob.getMonth(), dob.getDate()); if (nb < now) nb.setFullYear(now.getFullYear() + 1); const toB = Math.ceil((nb - now) / 86400000); set("age-out", `${y} years, ${m} months, ${d} days
Total: ${days.toLocaleString()} days · ${Math.floor(days/7).toLocaleString()} weeks · next birthday in ${toB} days
`); }; on("age-go", "click", go); on("age-dob", "change", go); } /* ---------- 2. Percentage ---------- */ function initPercent() { const go = () => { const a = num($("p-a").value), b = num($("p-b").value), mode = $("p-mode").value; let out = ""; if (mode === "of") out = `${n2(a)}% of ${n2(b)} = ${n2(a*b/100)}`; else if (mode === "is") out = `${n2(a)} is ${b ? n2(a/b*100) : 0}% of ${n2(b)}`; else out = `Change from ${n2(a)} to ${n2(b)} = ${a ? n2((b-a)/Math.abs(a)*100) : 0}% ${b>=a?"increase":"decrease"}`; set("p-out", out); }; on("p-go", "click", go); ["p-a","p-b"].forEach(id => on(id, "input", go)); } /* ---------- 3. EMI ---------- */ function initEmi() { const go = () => { const P = num($("emi-p").value), R = num($("emi-r").value), N = int($("emi-n").value), unit = $("emi-unit").value; const months = unit === "years" ? N * 12 : N; if (P <= 0 || months <= 0) { set("emi-out", "Enter loan amount and tenure."); return; } const r = R / 12 / 100; const emi = r === 0 ? P / months : P * r * Math.pow(1+r, months) / (Math.pow(1+r, months) - 1); const total = emi * months, interest = total - P; set("emi-out", `EMI: ${inr(emi)} / month
Principal: ${inr(P)} · Total interest: ${inr(interest)} · Total payment: ${inr(total)} over ${months} months
`); }; on("emi-go", "click", go); ["emi-p","emi-r","emi-n"].forEach(id => on(id, "input", go)); } /* ---------- 4. GST ---------- */ function initGst() { const go = () => { const amt = num($("gst-amt").value), rate = num($("gst-rate").value), mode = $("gst-mode").value; let net, gst, gross; if (mode === "add") { net = amt; gst = amt * rate / 100; gross = net + gst; } else { gross = amt; net = amt / (1 + rate/100); gst = gross - net; } set("gst-out", `GST: ${inr(gst)}
Base amount: ${inr(net)} · Total (incl. GST): ${inr(gross)} @ ${n2(rate)}%
`); }; on("gst-go", "click", go); ["gst-amt","gst-rate"].forEach(id => on(id, "input", go)); } /* ---------- 5. SIP ---------- */ function initSip() { const go = () => { const P = num($("sip-p").value), R = num($("sip-r").value), Y = num($("sip-y").value); const i = R / 12 / 100, n = Y * 12; const fv = i === 0 ? P * n : P * ((Math.pow(1+i, n) - 1) / i) * (1 + i); const invested = P * n; set("sip-out", `Maturity: ${inr(fv)}
Invested: ${inr(invested)} · Estimated returns: ${inr(fv - invested)} over ${n} months
`); }; on("sip-go", "click", go); ["sip-p","sip-r","sip-y"].forEach(id => on(id, "input", go)); } /* ---------- 6. FD ---------- */ function initFd() { const go = () => { const P = num($("fd-p").value), R = num($("fd-r").value), Y = num($("fd-y").value), f = int($("fd-freq").value); const amt = P * Math.pow(1 + (R/100)/f, f*Y); set("fd-out", `Maturity: ${inr(amt)}
Principal: ${inr(P)} · Interest earned: ${inr(amt - P)} over ${n2(Y)} years (compounded ${f}x/yr)
`); }; on("fd-go", "click", go); ["fd-p","fd-r","fd-y"].forEach(id => on(id, "input", go)); } /* ---------- 7. Compound interest ---------- */ function initCompound() { const go = () => { const P = num($("ci-p").value), R = num($("ci-r").value), Y = num($("ci-y").value); const amt = P * Math.pow(1 + R/100, Y); set("ci-out", `Amount: ${inr(amt)}
Principal: ${inr(P)} · Compound interest: ${inr(amt - P)} over ${n2(Y)} years
`); }; on("ci-go", "click", go); ["ci-p","ci-r","ci-y"].forEach(id => on(id, "input", go)); } /* ---------- 8. Unit converter ---------- */ const UNITS = { length: { base: "meter", units: { Meter:1, Kilometer:1000, Centimeter:0.01, Millimeter:0.001, Mile:1609.344, Yard:0.9144, Foot:0.3048, Inch:0.0254, "Nautical mile":1852 } }, weight: { base: "gram", units: { Gram:1, Kilogram:1000, Milligram:0.001, "Metric ton":1e6, Pound:453.59237, Ounce:28.349523125, Stone:6350.29318 } }, area: { base: "sq meter", units: { "Square meter":1, "Square kilometer":1e6, "Square foot":0.09290304, "Square yard":0.83612736, Acre:4046.8564224, Hectare:10000, "Square mile":2589988.110336 } }, volume: { base: "liter", units: { Liter:1, Milliliter:0.001, "Cubic meter":1000, Gallon:3.785411784, Quart:0.946352946, Pint:0.473176473, Cup:0.2365882365, "Fluid ounce":0.0295735295625 } }, speed: { base: "m/s", units: { "Meter/second":1, "Kilometer/hour":0.2777777778, "Mile/hour":0.44704, Knot:0.5144444444, "Foot/second":0.3048 } }, time: { base: "second", units: { Second:1, Minute:60, Hour:3600, Day:86400, Week:604800, Month:2629800, Year:31557600 } }, data: { base: "byte", units: { Byte:1, Kilobyte:1024, Megabyte:1048576, Gigabyte:1073741824, Terabyte:1099511627776, Bit:0.125 } }, }; function initUnit() { const fill = () => { const cat = $("u-cat").value; let opts; if (cat === "temperature") opts = ["Celsius","Fahrenheit","Kelvin"].map(u => ``).join(""); else opts = Object.keys(UNITS[cat].units).map(u => ``).join(""); $("u-from").innerHTML = opts; $("u-to").innerHTML = opts; $("u-from").selectedIndex = 0; $("u-to").selectedIndex = 1; go(); }; const go = () => { const cat = $("u-cat").value, v = num($("u-val").value); const f = $("u-from").value, t = $("u-to").value; let out; if (cat === "temperature") { let c = f === "Celsius" ? v : f === "Fahrenheit" ? (v-32)*5/9 : v-273.15; out = t === "Celsius" ? c : t === "Fahrenheit" ? c*9/5+32 : c+273.15; } else { const U = UNITS[cat].units; out = v * U[f] / U[t]; } set("u-out", `${n2(v)} ${f} = ${Number(out.toPrecision(8))} ${t}`); }; on("u-cat", "change", fill); on("u-val", "input", go); on("u-from", "change", go); on("u-to", "change", go); fill(); } /* ---------- 9. Word counter ---------- */ function initWord() { const go = () => { const t = $("wc-text").value; const words = t.trim() ? t.trim().split(/\s+/).length : 0; const chars = t.length, charsNS = t.replace(/\s/g, "").length; const sentences = (t.match(/[.!?]+(\s|$)/g) || []).length; const paras = t.trim() ? t.trim().split(/\n\s*\n/).length : 0; const read = Math.max(1, Math.round(words / 200)); set("wc-out", `${words.toLocaleString()} words · ${chars.toLocaleString()} characters · ${charsNS.toLocaleString()} without spaces · ${sentences} sentences · ${paras} paragraphs · ~${read} min read`); }; on("wc-text", "input", go); go(); } /* ---------- 10. Case converter ---------- */ function initCase() { const t = () => $("cc-text").value; const title = s => s.replace(/\w\S*/g, w => w[0].toUpperCase() + w.slice(1).toLowerCase()); const sentence = s => s.toLowerCase().replace(/(^\s*\w|[.!?]\s+\w)/g, c => c.toUpperCase()); const handlers = { upper: s => s.toUpperCase(), lower: s => s.toLowerCase(), title, sentence, camel: s => s.trim().toLowerCase().replace(/[^a-z0-9]+(.)/g, (m,c) => c.toUpperCase()), snake: s => s.trim().toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_|_$/g, ""), kebab: s => s.trim().toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, ""), }; document.querySelectorAll("[data-case]").forEach(b => b.addEventListener("click", () => { $("cc-text").value = handlers[b.dataset.case](t()); })); } /* ---------- 11. Password generator ---------- */ function initPassword() { const go = () => { const len = int($("pw-len").value); let pool = ""; if ($("pw-upper").checked) pool += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if ($("pw-lower").checked) pool += "abcdefghijklmnopqrstuvwxyz"; if ($("pw-digits").checked) pool += "0123456789"; if ($("pw-symbols").checked) pool += "!@#$%^&*()-_=+[]{};:,.<>?"; if (!pool) { set("pw-out", 'Select at least one character set.'); return; } const arr = new Uint32Array(len); crypto.getRandomValues(arr); let pw = ""; for (let i = 0; i < len; i++) pw += pool[arr[i] % pool.length]; $("pw-out").textContent = pw; }; on("pw-go", "click", go); on("pw-len", "input", () => { $("pw-lenv").textContent = $("pw-len").value; go(); }); on("pw-copy", "click", () => { navigator.clipboard && navigator.clipboard.writeText($("pw-out").textContent); }); go(); } /* ---------- 12. Random number ---------- */ function initRandom() { const go = () => { const min = int($("rn-min").value), max = int($("rn-max").value), count = Math.min(100, Math.max(1, int($("rn-count").value))); if (max < min) { set("rn-out", 'Max must be ≥ min.'); return; } const out = []; const range = max - min + 1; const arr = new Uint32Array(count); crypto.getRandomValues(arr); for (let i = 0; i < count; i++) out.push(min + (arr[i] % range)); set("rn-out", `${out.join(", ")}`); }; on("rn-go", "click", go); go(); } /* ---------- 13. Base64 ---------- */ function initBase64() { const enc = () => { try { set("b64-out", btoa(unescape(encodeURIComponent($("b64-in").value)))); } catch(e){ set("b64-out",'Cannot encode.'); } }; const dec = () => { try { set("b64-out", decodeURIComponent(escape(atob($("b64-in").value.trim())))); } catch(e){ set("b64-out",'Invalid Base64.'); } }; on("b64-enc", "click", enc); on("b64-dec", "click", dec); } /* ---------- 14. URL encode ---------- */ function initUrl() { on("url-enc", "click", () => set("url-out", encodeURIComponent($("url-in").value))); on("url-dec", "click", () => { try { set("url-out", decodeURIComponent($("url-in").value)); } catch(e){ set("url-out",'Invalid input.'); } }); } /* ---------- 15. QR code ---------- */ function initQr() { const go = () => { const text = $("qr-text").value || " "; const canvas = $("qr-canvas"); try { const q = qrcode(0, "M"); q.addData(text); q.make(); const n = q.getModuleCount(), scale = 6, pad = 4 * scale, size = n * scale + pad * 2; canvas.width = size; canvas.height = size; const ctx = canvas.getContext("2d"); ctx.fillStyle = "#fff"; ctx.fillRect(0, 0, size, size); ctx.fillStyle = "#000"; for (let r = 0; r < n; r++) for (let c = 0; c < n; c++) if (q.isDark(r, c)) ctx.fillRect(pad + c*scale, pad + r*scale, scale, scale); } catch (e) { set("qr-out", 'Could not generate QR.'); } }; on("qr-go", "click", go); on("qr-text", "input", go); on("qr-dl", "click", () => { const a = document.createElement("a"); a.download = "qrcode.png"; a.href = $("qr-canvas").toDataURL("image/png"); a.click(); }); go(); } /* ---------- 16. Date difference ---------- */ function initDate() { const go = () => { const a = $("dd-a").value, b = $("dd-b").value; if (!a || !b) return; const d1 = new Date(a + "T00:00:00"), d2 = new Date(b + "T00:00:00"); const days = Math.round((d2 - d1) / 86400000); const wd = Math.floor(Math.abs(days)/7), rem = Math.abs(days)%7; set("dd-out", `${Math.abs(days).toLocaleString()} days
${wd} weeks ${rem} days · ${(Math.abs(days)/30.44).toFixed(1)} months · ${(Math.abs(days)/365.25).toFixed(2)} years
`); }; on("dd-go", "click", go); ["dd-a","dd-b"].forEach(id => on(id, "change", go)); } /* ---------- 17. BMI ---------- */ function initBmi() { const go = () => { const h = num($("bmi-h").value), w = num($("bmi-w").value); if (h <= 0 || w <= 0) { set("bmi-out", "Enter height and weight."); return; } const bmi = w / Math.pow(h/100, 2); let cat = "Normal"; if (bmi < 18.5) cat = "Underweight"; else if (bmi < 25) cat = "Normal"; else if (bmi < 30) cat = "Overweight"; else cat = "Obese"; set("bmi-out", `BMI: ${bmi.toFixed(1)} — ${cat}
Healthy range is 18.5–24.9
`); }; on("bmi-go", "click", go); ["bmi-h","bmi-w"].forEach(id => on(id, "input", go)); } /* ---------- 18. Currency ---------- */ const CURRENCIES = { AUD:"Australian Dollar", BGN:"Bulgarian Lev", BRL:"Brazilian Real", CAD:"Canadian Dollar", CHF:"Swiss Franc", CNY:"Chinese Yuan", CZK:"Czech Koruna", DKK:"Danish Krone", EUR:"Euro", GBP:"British Pound", HKD:"Hong Kong Dollar", HUF:"Hungarian Forint", IDR:"Indonesian Rupiah", ILS:"Israeli Shekel", INR:"Indian Rupee", ISK:"Icelandic Krona", JPY:"Japanese Yen", KRW:"South Korean Won", MXN:"Mexican Peso", MYR:"Malaysian Ringgit", NOK:"Norwegian Krone", NZD:"New Zealand Dollar", PHP:"Philippine Peso", PLN:"Polish Zloty", RON:"Romanian Leu", SEK:"Swedish Krona", SGD:"Singapore Dollar", THB:"Thai Baht", TRY:"Turkish Lira", USD:"US Dollar", ZAR:"South African Rand" }; function initCurrency() { const opts = Object.keys(CURRENCIES).sort().map(c => ``).join(""); $("fx-from").innerHTML = opts; $("fx-to").innerHTML = opts; $("fx-from").value = "USD"; $("fx-to").value = "INR"; async function rate(f, t) { try { const r = await fetch(`https://api.frankfurter.app/latest?from=${f}&to=${t}`); if (r.ok) { const d = await r.json(); if (d.rates && d.rates[t] != null) return d.rates[t]; } } catch(e){} try { const r = await fetch(`https://open.er-api.com/v6/latest/${f}`); if (r.ok) { const d = await r.json(); if (d.rates && d.rates[t] != null) return d.rates[t]; } } catch(e){} throw new Error("no rate"); } const go = async () => { const amt = num($("fx-amt").value), f = $("fx-from").value, t = $("fx-to").value; set("fx-out", "…"); try { if (f === t) { set("fx-out", `${n2(amt)} ${f} = ${n2(amt)} ${t}`); return; } const r = await rate(f, t); set("fx-out", `${n2(amt)} ${f} = ${n2(amt*r)} ${t}
1 ${f} = ${r} ${t}
`); } catch (e) { set("fx-out", 'Could not fetch rates right now.'); } }; on("fx-go", "click", go); on("fx-amt", "input", go); on("fx-swap", "click", () => { const a = $("fx-from").value; $("fx-from").value = $("fx-to").value; $("fx-to").value = a; go(); }); go(); } document.addEventListener("DOMContentLoaded", () => { [initAge, initPercent, initEmi, initGst, initSip, initFd, initCompound, initUnit, initWord, initCase, initPassword, initRandom, initBase64, initUrl, initQr, initDate, initBmi, initCurrency] .forEach(fn => { try { fn(); } catch (e) { console.error(fn.name, e); } }); }); })();