Skip to content

Instantly share code, notes, and snippets.

@EGreg
Created August 13, 2014 00:11
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 EGreg/236a49e5eda67bb4fe3f to your computer and use it in GitHub Desktop.
Save EGreg/236a49e5eda67bb4fe3f to your computer and use it in GitHub Desktop.
Here is Q.normalize
/**
* Normalizes text by converting it to lower case, and
* replacing all non-accepted characters with underscores.
*
* @static
* @method normalize
* @param {String} text
* The text to normalize
* @param {String} replacement
* Defaults to '_'. A string to replace one or more unacceptable characters.
* You can also change this default using the config Db/normalize/replacement
* @param {String} characters
* Defaults to '/[^A-Za-z0-9]+/'. A regexp characters that are not acceptable.
* You can also change this default using the config Db/normalize/characters
* @param {Number} numChars
* The maximum length of a normalized string. Default is 200.
* @return {String} the normalized string
*/
Q.normalize = function _Q_normalize(text, replacement, characters, numChars) {
if (!numChars) numChars = 200;
if (replacement === undefined) replacement = '_';
characters = characters || new RegExp("[^A-Za-z0-9]+", "g");
if (text === undefined) {
debugger; // pause here if debugging
}
var result = text.toLowerCase().replace(characters, replacement);
if (text.length > numChars) {
result = text.substr(0, numChars-11) + '_'
+ Math.abs(text.substr(numChars-11).hashCode());
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment