Created
August 9, 2023 23:04
-
-
Save hidao80/4e8b4d0ca0a951724554bcee86f6cfcc to your computer and use it in GitHub Desktop.
Simple phone number compression/decompression functions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Compress strings that require only numbers, such as phone numbers. | |
* | |
* @param {string} num Requiring only numbers. | |
* @param {string} code String of symbols used for compression. | |
* @return {string} Compressed string | |
*/ | |
function numCps( | |
num, | |
code = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
) { | |
let radix = code.length; | |
let mod; | |
let quotient = num.replaceAll(/\D/g, ""); // Remove non-numeric characters. | |
let result = ""; | |
// Encode | |
while (quotient > radix) { | |
mod = parseInt(quotient) % radix; | |
result = code[mod] + result; | |
quotient = Math.floor(quotient / radix); | |
console.log( | |
"quotient: " + quotient + ", mod: " + mod + ", result: " + result | |
); | |
} | |
result = code[quotient] + result; | |
return (!num[0] || "0") + result; // If there are leading zeros, write back. | |
} | |
/** | |
* Undo compressed strings with numCps(). | |
* | |
* @param {string} num Compressed by numCps() | |
* @param {string} code Using the same symbol as numCps() | |
* @return {string} Original phone number | |
*/ | |
function numElg( | |
num, | |
code = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
) { | |
let radix, place, index, result; | |
place = radix = code.length; | |
index = result = 0; | |
// Decode | |
while (num.length - index) { | |
place = radix ** index; | |
index++; | |
result += code.indexOf(num[num.length - index]) * place; | |
console.log( | |
"indexOf: " + | |
code.indexOf(num[num.length - index]) + | |
", chr: " + | |
num[num.length - index] + | |
", " + | |
place + | |
"の位: " + | |
code.indexOf(num[num.length - index]) * place + | |
", result: " + | |
result | |
); | |
} | |
return (!num[0] || "0") + result; // If there are leading zeros, write back. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function numCps(n,e="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"){let t,l=e.length,r=n.replaceAll(/\D/g,""),f="";for(;r>l;)t=parseInt(r)%l,f=e[t]+f,r=Math.floor(r/l);return f=e[r]+f,(!n[0]||"0")+f}function numElg(n,e="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"){let t,l,r,f;for(l=t=e.length,r=f=0;n.length-r;)l=t**r,r++,f+=e.indexOf(n[n.length-r])*l;return(!n[0]||"0")+f} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Operation check | |
*/ | |
console.log("=== OUTPUT STERT ==="); | |
console.log("ENCODE RESULT: " + numCps("tel:03-3000-1234")); | |
console.log("DECODE RESULT: " + numElg(numCps("tel:03-3000-1234"))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment