Created
August 13, 2014 00:11
-
-
Save EGreg/236a49e5eda67bb4fe3f to your computer and use it in GitHub Desktop.
Here is Q.normalize
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
/** | |
* 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