License Key Formatting
TypeScript
function licenseKeyFormat(s, k) {
let r = s.toUpperCase().replaceAll('-', '');
for (let i = r.length - k; i > 0; i -= k) {
r = r.substr(0, i) + '-' + r.substr(i, r.length);
}
return r;
}
function log(input, expected) {
document.write(`<pre>${input}: <b>${licenseKeyFormat(...input)}</b> <small>// ${expected}</small></pre>`);
}
log(['5F3Z-2e-9-w', 4], '5F3Z-2E9W');
log(['2-5g-3-J', 2], '2-5G-3J');