Skip to content

Instantly share code, notes, and snippets.

@sftblw
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save sftblw/edd8941656648094fa16 to your computer and use it in GitHub Desktop.

Select an option

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 )
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