Last active
January 3, 2016 10:59
-
-
Save jsmolina/8453525 to your computer and use it in GitHub Desktop.
This js script simulates Drupal pathauto module that generates an automatic url based on a node title. It is useful for web crawling, testing, ...
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
String.stopWords = 'a,an,as,at,before,but,by,for,from,is,in,into,like,of,off,on,onto,per,since,than,the,this,that,to,up,via,with'; | |
String.escapeRegExp = function(str) { | |
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); | |
}; | |
String.prototype.replaceAll = function (find, replace) { | |
return this.replace(new RegExp(String.escapeRegExp(find), 'g'), replace); | |
}; | |
String.prototype.ltrim = function () { | |
return this.replace(/^\s+/, ""); | |
}; | |
String.isStopWord = function (word) { | |
var regex = new RegExp("\\b" + word + "\\b", "i"); | |
if (String.stopWords.search(regex) < 0) { | |
return false; | |
} else { | |
return true; | |
} | |
}; | |
String.prototype.removeStopWords = function () { | |
words = []; | |
this.replace(/\b[^ ]+[\b]?/g, | |
function ($0) { | |
if (!String.isStopWord($0)) { | |
words[words.length] = $0.trim(); | |
} | |
} | |
); | |
return words.join(" "); | |
}; | |
String.prototype.pathauto = function() { | |
var expected_path = this.toLowerCase(); | |
expected_path = expected_path.replace(new RegExp("[()&!+`\.\:;]+", 'g'), ""); | |
expected_path = expected_path.replace(new RegExp("[\|{}]+", 'g'), ""); | |
expected_path = expected_path.replace(new RegExp("[\[]+", 'g'), ""); | |
expected_path = expected_path.replace(new RegExp("\]", 'g'), ""); | |
expected_path = expected_path.replace(new RegExp(",", 'g'), ""); | |
expected_path = expected_path.replace(new RegExp("[\+=*%\^\$#@!~\?<>/]+", 'g'), ""); | |
expected_path = expected_path.replace(new RegExp("[\\\\]+", 'g'), ""); | |
expected_path = expected_path.replaceAll("—", "EMPHASIZEDHYPHEN1"); | |
expected_path = expected_path.replaceAll("–", "EMPHASIZEDHYPHEN2"); | |
expected_path = expected_path.removeStopWords(); | |
expected_path = expected_path.replace(new RegExp("[ ]+", 'g'), " "); | |
expected_path = expected_path.ltrim(); | |
expected_path = expected_path.replaceAll(" ", "-"); | |
expected_path = expected_path.replaceAll("EMPHASIZEDHYPHEN1", "%E2%80%94"); | |
expected_path = expected_path.replaceAll("EMPHASIZEDHYPHEN2", "%E2%80%93"); | |
expected_path = expected_path.substr(0, 87); | |
return expected_path; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Taking some ideas from:
https://github.com/mirzaasif/JS-StopWord/blob/master/StopWord.js