Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.