Skip to content

Instantly share code, notes, and snippets.

@7LayersDesign
Created August 9, 2016 17:58
Show Gist options
  • Select an option

  • Save 7LayersDesign/ed402d78419e0242ed2e77c07189ad95 to your computer and use it in GitHub Desktop.

Select an option

Save 7LayersDesign/ed402d78419e0242ed2e77c07189ad95 to your computer and use it in GitHub Desktop.
DFP Key Cleaning
var _cleanValue = function(val) {
// Convert pipes and spaces to underscores
val = val.replace(/\|/g, "_");
val = val.replace(/\ /g, "_");
val = val.replace(/\-/g, "_");
// Strip out any other bad characters.
val = val.replace(/\;/g, "");
val = val.replace(/\&/g, "");
val = val.replace(/\\/g, "");
val = val.replace(/\//g, "");
var invalidChars = "#\",*.()=+<>[]| ";
var encodeChars = "$-_.";
var valChars = val.split("");
var chars = []; // Final set of valid characters
var arrLen = valChars.length;
var curChar;
for (var i = 0; i < valChars.length; i++) {
curChar = valChars[i];
// Check if invalid
if( invalidChars.indexOf(curChar) === -1 ){
// check if needs encoding
if( encodeChars.indexOf(curChar) > -1 ){
// Needs escaping
chars.push(escape(curChar));
} else {
// Valid and safe
chars.push(valChars[i]);
}
}
}
// join chars and return
return chars.join("");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment