Skip to content

Instantly share code, notes, and snippets.

@beaukinstler
Last active August 23, 2018 01:12
Show Gist options
  • Save beaukinstler/f6ffb5c7ccf371f96441a033be9d7053 to your computer and use it in GitHub Desktop.
Save beaukinstler/f6ffb5c7ccf371f96441a033be9d7053 to your computer and use it in GitHub Desktop.
/* This code was created by Beau Kinstler
You may use it with no limitation, but it's provided with no warranty or liablilty for it's use.
Purpose: add some tools for obscuring information. If used on the client side,
this will not secure the information unless the client device has protections on it.
For instance, a kiosk where the user has no access to deve tools of a browswer.
Version notes:
0.0.1: This has a very specifc purpose in my work, but perhaps could be adapted
for other work.
*/
function obscurePhone(phone){
lenOfPhone = phone.length
cleanNumber = cleanNumber(phone)
switch (cleanNumber.length){
case 7:
return "***-" + phone.slice(3);
case 10:
return "(" + cleanNumber.slice(0,3) + ")***-" + cleanNumber.slice(6);
default:
cut = phone.length-4
return obscureString(phone.slice(0,cut),0) + phone.slice(cut);
}
}
function cleanNumber(phone){
number = ''
for( x in phone){
digit = parseInt(phone[x]);
if (!(isNaN(digit))){
number += phone[x];
}
}
return number
}
function obscureEmail(email){
atSymbol = email.indexOf("@");
// make sure it's pretty much a valid email address
// there's only one @, there a dot after the @, and there is a @
if(atSymbol !== email.lastIndexOf("@") && email.lastIndexOf('.') > atSymbol && atSymbol != -1 ){
return email;
}
emailName = email.slice(0,atSymbol);
emailDomain = email.slice(atSymbol);
return obscureString(emailName,2) + emailDomain;
}
function obscureString(string, num){
if( num < string.length ){
obscuredString = ''
firstPart = string.slice(0,num);
secondPart = "";
for( i = 0; i < string.slice(num).length; i++ ){
secondPart += "*";
}
obscuredString = firstPart + secondPart;
return obscuredString;
}
else{
return string;
}
}
function obscureFirstWordInMultiWordString(string){
streetNumberIndex = string.indexOf(" ");
streetNumber = string.slice(0,streetNumberIndex);
restOfAddress = string.slice(streetNumberIndex);
streetNumber = obscureString(streetNumber,0);
obscuredAddress = streetNumber + restOfAddress;
return obscuredAddress;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment