Skip to content

Instantly share code, notes, and snippets.

@amcdnl
Created April 15, 2014 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amcdnl/10768441 to your computer and use it in GitHub Desktop.
Save amcdnl/10768441 to your computer and use it in GitHub Desktop.
define(['jquery', 'angular'], function ($, angular) {
var module = angular.module('utils.acronym', []);
module.factory('Acronym', function () {
var factory = {
desiredKeyLength: 2,
maxKeyLength: 5,
ignoredWords: ["THE", "A", "AN", "AS", "AND", "OF", "OR"],
characterMap: {
199: "C",
231: "c",
252: "u",
251: "u",
250: "u",
249: "u",
233: "e",
234: "e",
235: "e",
232: "e",
226: "a",
228: "a",
224: "a",
229: "a",
225: "a",
239: "i",
238: "i",
236: "i",
237: "i",
196: "A",
197: "A",
201: "E",
230: "ae",
198: "Ae",
244: "o",
246: "o",
242: "o",
243: "o",
220: "U",
255: "Y",
214: "O",
241: "n",
209: "N"
},
getTotalLength: function(words) {
return words.join("").length
},
removeIgnoredWords: function(word) {
return $.grep(word, function (b) {
return $.inArray(b, factory.ignoredWords) === -1
})
},
createAcronym: function(b) {
var a = "";
b.forEach(function (c) {
a += c.charAt(0)
});
return a
},
getFirstSyllable: function(c) {
var b = false;
var a;
for (a = 0; a < c.length; a++) {
if (factory.isVowelOrY(c[a])) {
b = true
} else {
if (b) {
return c.substring(0, a + 1)
}
}
}
return c
},
isVowelOrY: function(a) {
return a && a.length === 1 && a.search("[AEIOUY]") !== -1
},
generate: function(b) {
b = $.trim(b);
if (!b) {
return ""
}
var a = [];
for (var d = 0, f = b.length; d < f; d++) {
var e = factory.characterMap[b.charCodeAt(d)];
a.push(e ? e : b[d]);
}
b = a.join("");
var h = [];
$.each(b.split(/\s+/), function (j, k) {
if (k) {
k = k.replace(/[^a-zA-Z]/g, "");
k = k.toUpperCase();
k.length && h.push(k);
}
});
if (factory.desiredKeyLength &&
factory.getTotalLength(h) > factory.desiredKeyLength) {
h = factory.removeIgnoredWords(h);
}
var c;
if (!h.length) {
c = ""
} else {
if (h.length === 1) {
var g = h[0];
if (factory.desiredKeyLength && g.length > factory.desiredKeyLength) {
c = factory.getFirstSyllable(g)
} else {
c = g
}
} else {
c = factory.createAcronym(h)
}
}
if (factory.maxKeyLength && c.length > factory.maxKeyLength) {
c = c.substr(0, factory.maxKeyLength)
}
return c
}
};
return factory;
});
return module;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment