Skip to content

Instantly share code, notes, and snippets.

@samtsai
Created May 7, 2013 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samtsai/5533548 to your computer and use it in GitHub Desktop.
Save samtsai/5533548 to your computer and use it in GitHub Desktop.
quick normalize functions for grooming strings passed into an analytics library like SiteCatalyst
// this would be set in a closure
// but leave some values to be cached (ie not created every function call)
// and the ability to override
var norm_regex = /\s+/g;
var norm_replace = "_";
function normalize( val, reg_pattern, separator ) {
// convert to string
var norm_val = val+"";
norm_regex = reg_pattern || norm_regex;
norm_replace = separator || norm_replace;
// analytics is expecting spaces to be "_", case to be lower, and no empty spaces before and after the string
return ""+val.trim().replace(norm_regex, norm_replace).toLowerCase();
}
// assumptions
// Trim is available in the browser
// https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/Trim
// implement our own or borrow a library's (eg jQuery)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment