Skip to content

Instantly share code, notes, and snippets.

@colelawrence
Created June 24, 2014 15:34
Show Gist options
  • Select an option

  • Save colelawrence/2669cc7d32f98698a2c3 to your computer and use it in GitHub Desktop.

Select an option

Save colelawrence/2669cc7d32f98698a2c3 to your computer and use it in GitHub Desktop.
Abbreviate multiple strings to two characters
(function () {
var abbrs = []
var abbr = function (full) {
full = full
.replace(/^[^\w\d]+/, "")
.replace(/\s+/g, "_")
.replace(/[^\w\d]/g, "")
// Some's class => Somes_class
var abbr = full[0]
var testMatch;
// awo_awe1P_awe => 1 [7]
var trial1 = /[a-z_]([A-Z0-9])/g
// awp_pawoe_ea => p [4]
var trial0 = /_([a-z])/g
for (var trial = -1; trial !== full.length; trial++ ) {
switch (trial) {
case -1:
testMatch = trial1.exec(full)
break;
case 0:
testMatch = trial0.exec(full)
break;
default:
testMatch = [false,full[trial]]
}
if (testMatch) {
if (testMatch[0])
trial-- // Reduce trials if global matcher
if (abbrs.indexOf(abbr + testMatch[1]) === -1) {
abbr += testMatch[1]
break
}
}
}
abbrs.push(abbr)
return abbr
}
if (typeof module === "object")
module.exports = abbr
else
this.abbr = abbr
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment