Created
May 7, 2013 15:32
-
-
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 file contains 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
// 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