Last active
August 29, 2015 14:19
-
-
Save sftblw/edd8941656648094fa16 to your computer and use it in GitHub Desktop.
Carmel-backed word shortner (extracted from here https://gist.github.com/softblow/77644b5e20b2759f80d8 )
This file contains hidden or 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
| var camelShortener = { | |
| origShort : new Map(), | |
| shortOrig : new Map(), | |
| shorten : function (str, len) { | |
| if (this.origShort.get(str) !== undefined) | |
| return this.origShort.get(str); | |
| //console.log("before : " + str); | |
| var camels = str.replace(/([a-z])([A-Z])/,'$1,$2' ).split(","); | |
| // for condition purpose | |
| camels.getStrLenSum = function () { | |
| var sum = 0; | |
| this.forEach(function (elem) { | |
| sum += elem.length; | |
| }); | |
| return sum; | |
| } | |
| // phase 1 : remove vowel | |
| var index = camels.length-1; | |
| while((camels.getStrLenSum() > len ) && (index >= 0) ) { | |
| camels[index] = camels[index].replace(/[aeiou]/g,''); | |
| index--; | |
| } | |
| //phase 2 : remove letters | |
| index = camels.length-1; | |
| while((camels.getStrLenSum() > len ) || (this.shortOrig.get(camels.join('')) !== undefined) ) { | |
| if (camels[index].length > 1) | |
| camels[index] = camels[index].slice(0,-1); | |
| index--; | |
| if (index < 0) { | |
| index = camels.length-1; | |
| } | |
| } | |
| var ret = camels.join(''); | |
| this.shortOrig.set(ret, str); | |
| this.origShort.set(str, ret); | |
| //console.log("after : " + ret + " : " + ret.length); | |
| return ret; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment