Case Sensitive Accent Folding in Javascript - javascript

With this example:
accentsTidy = function(s) {
var r = s.toLowerCase();
r = r.replace(new RegExp("\\s", 'g'),"");
r = r.replace(new RegExp("[àáâãäå]", 'g'),"a");
r = r.replace(new RegExp("æ", 'g'),"ae");
r = r.replace(new RegExp("ç", 'g'),"c");
r = r.replace(new RegExp("[èéêë]", 'g'),"e");
r = r.replace(new RegExp("[ìíîï]", 'g'),"i");
r = r.replace(new RegExp("ñ", 'g'),"n");
r = r.replace(new RegExp("[òóôõö]", 'g'),"o");
r = r.replace(new RegExp("œ", 'g'),"oe");
r = r.replace(new RegExp("[ùúûü]", 'g'),"u");
r = r.replace(new RegExp("[ýÿ]", 'g'),"y");
r = r.replace(new RegExp("\\W", 'g'),"");
return r;
};
I would like to make this kind of transformation: Á É to A E.

You could do this:
accentsTidy = function(s) {
var map = [
["\\s", ""],
["[àáâãäå]", "a"],
["æ", "ae"],
["ç", "c"],
["[èéêë]", "e"],
["[ìíîï]", "i"],
["ñ", "n"],
["[òóôõö]", "o"],
["œ", "oe"],
["[ùúûü]", "u"],
["[ýÿ]", "y"],
["\\W", ""]
];
for (var i=0; i<map.length; ++i) {
s = s.replace(new RegExp(map[i][0], "gi"), function(match) {
if (match.toUpperCase() === match) {
return map[i][1].toUpperCase();
} else {
return map[i][1];
}
});
}
return s;
}
The significant changes:
regular expression patterns are case insensitive (see i modifier)
return upper-case variant of character if matched character is upper-case (match.toUpperCase() === match)

A more complete version with case sensitive support, ligatures and whatnot.
Original source at: http://lehelk.com/2011/05/06/script-to-remove-diacritics/
var defaultDiacriticsRemovalMap = [
{'base':'A', 'letters':/[\u0041\u24B6\uFF21\u00C0\u00C1\u00C2\u1EA6\u1EA4\u1EAA\u1EA8\u00C3\u0100\u0102\u1EB0\u1EAE\u1EB4\u1EB2\u0226\u01E0\u00C4\u01DE\u1EA2\u00C5\u01FA\u01CD\u0200\u0202\u1EA0\u1EAC\u1EB6\u1E00\u0104\u023A\u2C6F]/g},
{'base':'AA','letters':/[\uA732]/g},
{'base':'AE','letters':/[\u00C6\u01FC\u01E2]/g},
{'base':'AO','letters':/[\uA734]/g},
{'base':'AU','letters':/[\uA736]/g},
{'base':'AV','letters':/[\uA738\uA73A]/g},
{'base':'AY','letters':/[\uA73C]/g},
{'base':'B', 'letters':/[\u0042\u24B7\uFF22\u1E02\u1E04\u1E06\u0243\u0182\u0181]/g},
{'base':'C', 'letters':/[\u0043\u24B8\uFF23\u0106\u0108\u010A\u010C\u00C7\u1E08\u0187\u023B\uA73E]/g},
{'base':'D', 'letters':/[\u0044\u24B9\uFF24\u1E0A\u010E\u1E0C\u1E10\u1E12\u1E0E\u0110\u018B\u018A\u0189\uA779]/g},
{'base':'DZ','letters':/[\u01F1\u01C4]/g},
{'base':'Dz','letters':/[\u01F2\u01C5]/g},
{'base':'E', 'letters':/[\u0045\u24BA\uFF25\u00C8\u00C9\u00CA\u1EC0\u1EBE\u1EC4\u1EC2\u1EBC\u0112\u1E14\u1E16\u0114\u0116\u00CB\u1EBA\u011A\u0204\u0206\u1EB8\u1EC6\u0228\u1E1C\u0118\u1E18\u1E1A\u0190\u018E]/g},
{'base':'F', 'letters':/[\u0046\u24BB\uFF26\u1E1E\u0191\uA77B]/g},
{'base':'G', 'letters':/[\u0047\u24BC\uFF27\u01F4\u011C\u1E20\u011E\u0120\u01E6\u0122\u01E4\u0193\uA7A0\uA77D\uA77E]/g},
{'base':'H', 'letters':/[\u0048\u24BD\uFF28\u0124\u1E22\u1E26\u021E\u1E24\u1E28\u1E2A\u0126\u2C67\u2C75\uA78D]/g},
{'base':'I', 'letters':/[\u0049\u24BE\uFF29\u00CC\u00CD\u00CE\u0128\u012A\u012C\u0130\u00CF\u1E2E\u1EC8\u01CF\u0208\u020A\u1ECA\u012E\u1E2C\u0197]/g},
{'base':'J', 'letters':/[\u004A\u24BF\uFF2A\u0134\u0248]/g},
{'base':'K', 'letters':/[\u004B\u24C0\uFF2B\u1E30\u01E8\u1E32\u0136\u1E34\u0198\u2C69\uA740\uA742\uA744\uA7A2]/g},
{'base':'L', 'letters':/[\u004C\u24C1\uFF2C\u013F\u0139\u013D\u1E36\u1E38\u013B\u1E3C\u1E3A\u0141\u023D\u2C62\u2C60\uA748\uA746\uA780]/g},
{'base':'LJ','letters':/[\u01C7]/g},
{'base':'Lj','letters':/[\u01C8]/g},
{'base':'M', 'letters':/[\u004D\u24C2\uFF2D\u1E3E\u1E40\u1E42\u2C6E\u019C]/g},
{'base':'N', 'letters':/[\u004E\u24C3\uFF2E\u01F8\u0143\u00D1\u1E44\u0147\u1E46\u0145\u1E4A\u1E48\u0220\u019D\uA790\uA7A4]/g},
{'base':'NJ','letters':/[\u01CA]/g},
{'base':'Nj','letters':/[\u01CB]/g},
{'base':'O', 'letters':/[\u004F\u24C4\uFF2F\u00D2\u00D3\u00D4\u1ED2\u1ED0\u1ED6\u1ED4\u00D5\u1E4C\u022C\u1E4E\u014C\u1E50\u1E52\u014E\u022E\u0230\u00D6\u022A\u1ECE\u0150\u01D1\u020C\u020E\u01A0\u1EDC\u1EDA\u1EE0\u1EDE\u1EE2\u1ECC\u1ED8\u01EA\u01EC\u00D8\u01FE\u0186\u019F\uA74A\uA74C]/g},
{'base':'OI','letters':/[\u01A2]/g},
{'base':'OO','letters':/[\uA74E]/g},
{'base':'OU','letters':/[\u0222]/g},
{'base':'P', 'letters':/[\u0050\u24C5\uFF30\u1E54\u1E56\u01A4\u2C63\uA750\uA752\uA754]/g},
{'base':'Q', 'letters':/[\u0051\u24C6\uFF31\uA756\uA758\u024A]/g},
{'base':'R', 'letters':/[\u0052\u24C7\uFF32\u0154\u1E58\u0158\u0210\u0212\u1E5A\u1E5C\u0156\u1E5E\u024C\u2C64\uA75A\uA7A6\uA782]/g},
{'base':'S', 'letters':/[\u0053\u24C8\uFF33\u1E9E\u015A\u1E64\u015C\u1E60\u0160\u1E66\u1E62\u1E68\u0218\u015E\u2C7E\uA7A8\uA784]/g},
{'base':'T', 'letters':/[\u0054\u24C9\uFF34\u1E6A\u0164\u1E6C\u021A\u0162\u1E70\u1E6E\u0166\u01AC\u01AE\u023E\uA786]/g},
{'base':'TZ','letters':/[\uA728]/g},
{'base':'U', 'letters':/[\u0055\u24CA\uFF35\u00D9\u00DA\u00DB\u0168\u1E78\u016A\u1E7A\u016C\u00DC\u01DB\u01D7\u01D5\u01D9\u1EE6\u016E\u0170\u01D3\u0214\u0216\u01AF\u1EEA\u1EE8\u1EEE\u1EEC\u1EF0\u1EE4\u1E72\u0172\u1E76\u1E74\u0244]/g},
{'base':'V', 'letters':/[\u0056\u24CB\uFF36\u1E7C\u1E7E\u01B2\uA75E\u0245]/g},
{'base':'VY','letters':/[\uA760]/g},
{'base':'W', 'letters':/[\u0057\u24CC\uFF37\u1E80\u1E82\u0174\u1E86\u1E84\u1E88\u2C72]/g},
{'base':'X', 'letters':/[\u0058\u24CD\uFF38\u1E8A\u1E8C]/g},
{'base':'Y', 'letters':/[\u0059\u24CE\uFF39\u1EF2\u00DD\u0176\u1EF8\u0232\u1E8E\u0178\u1EF6\u1EF4\u01B3\u024E\u1EFE]/g},
{'base':'Z', 'letters':/[\u005A\u24CF\uFF3A\u0179\u1E90\u017B\u017D\u1E92\u1E94\u01B5\u0224\u2C7F\u2C6B\uA762]/g},
{'base':'a', 'letters':/[\u0061\u24D0\uFF41\u1E9A\u00E0\u00E1\u00E2\u1EA7\u1EA5\u1EAB\u1EA9\u00E3\u0101\u0103\u1EB1\u1EAF\u1EB5\u1EB3\u0227\u01E1\u00E4\u01DF\u1EA3\u00E5\u01FB\u01CE\u0201\u0203\u1EA1\u1EAD\u1EB7\u1E01\u0105\u2C65\u0250]/g},
{'base':'aa','letters':/[\uA733]/g},
{'base':'ae','letters':/[\u00E6\u01FD\u01E3]/g},
{'base':'ao','letters':/[\uA735]/g},
{'base':'au','letters':/[\uA737]/g},
{'base':'av','letters':/[\uA739\uA73B]/g},
{'base':'ay','letters':/[\uA73D]/g},
{'base':'b', 'letters':/[\u0062\u24D1\uFF42\u1E03\u1E05\u1E07\u0180\u0183\u0253]/g},
{'base':'c', 'letters':/[\u0063\u24D2\uFF43\u0107\u0109\u010B\u010D\u00E7\u1E09\u0188\u023C\uA73F\u2184]/g},
{'base':'d', 'letters':/[\u0064\u24D3\uFF44\u1E0B\u010F\u1E0D\u1E11\u1E13\u1E0F\u0111\u018C\u0256\u0257\uA77A]/g},
{'base':'dz','letters':/[\u01F3\u01C6]/g},
{'base':'e', 'letters':/[\u0065\u24D4\uFF45\u00E8\u00E9\u00EA\u1EC1\u1EBF\u1EC5\u1EC3\u1EBD\u0113\u1E15\u1E17\u0115\u0117\u00EB\u1EBB\u011B\u0205\u0207\u1EB9\u1EC7\u0229\u1E1D\u0119\u1E19\u1E1B\u0247\u025B\u01DD]/g},
{'base':'f', 'letters':/[\u0066\u24D5\uFF46\u1E1F\u0192\uA77C]/g},
{'base':'g', 'letters':/[\u0067\u24D6\uFF47\u01F5\u011D\u1E21\u011F\u0121\u01E7\u0123\u01E5\u0260\uA7A1\u1D79\uA77F]/g},
{'base':'h', 'letters':/[\u0068\u24D7\uFF48\u0125\u1E23\u1E27\u021F\u1E25\u1E29\u1E2B\u1E96\u0127\u2C68\u2C76\u0265]/g},
{'base':'hv','letters':/[\u0195]/g},
{'base':'i', 'letters':/[\u0069\u24D8\uFF49\u00EC\u00ED\u00EE\u0129\u012B\u012D\u00EF\u1E2F\u1EC9\u01D0\u0209\u020B\u1ECB\u012F\u1E2D\u0268\u0131]/g},
{'base':'j', 'letters':/[\u006A\u24D9\uFF4A\u0135\u01F0\u0249]/g},
{'base':'k', 'letters':/[\u006B\u24DA\uFF4B\u1E31\u01E9\u1E33\u0137\u1E35\u0199\u2C6A\uA741\uA743\uA745\uA7A3]/g},
{'base':'l', 'letters':/[\u006C\u24DB\uFF4C\u0140\u013A\u013E\u1E37\u1E39\u013C\u1E3D\u1E3B\u017F\u0142\u019A\u026B\u2C61\uA749\uA781\uA747]/g},
{'base':'lj','letters':/[\u01C9]/g},
{'base':'m', 'letters':/[\u006D\u24DC\uFF4D\u1E3F\u1E41\u1E43\u0271\u026F]/g},
{'base':'n', 'letters':/[\u006E\u24DD\uFF4E\u01F9\u0144\u00F1\u1E45\u0148\u1E47\u0146\u1E4B\u1E49\u019E\u0272\u0149\uA791\uA7A5]/g},
{'base':'nj','letters':/[\u01CC]/g},
{'base':'o', 'letters':/[\u006F\u24DE\uFF4F\u00F2\u00F3\u00F4\u1ED3\u1ED1\u1ED7\u1ED5\u00F5\u1E4D\u022D\u1E4F\u014D\u1E51\u1E53\u014F\u022F\u0231\u00F6\u022B\u1ECF\u0151\u01D2\u020D\u020F\u01A1\u1EDD\u1EDB\u1EE1\u1EDF\u1EE3\u1ECD\u1ED9\u01EB\u01ED\u00F8\u01FF\u0254\uA74B\uA74D\u0275]/g},
{'base':'oi','letters':/[\u01A3]/g},
{'base':'ou','letters':/[\u0223]/g},
{'base':'oo','letters':/[\uA74F]/g},
{'base':'p','letters':/[\u0070\u24DF\uFF50\u1E55\u1E57\u01A5\u1D7D\uA751\uA753\uA755]/g},
{'base':'q','letters':/[\u0071\u24E0\uFF51\u024B\uA757\uA759]/g},
{'base':'r','letters':/[\u0072\u24E1\uFF52\u0155\u1E59\u0159\u0211\u0213\u1E5B\u1E5D\u0157\u1E5F\u024D\u027D\uA75B\uA7A7\uA783]/g},
{'base':'s','letters':/[\u0073\u24E2\uFF53\u00DF\u015B\u1E65\u015D\u1E61\u0161\u1E67\u1E63\u1E69\u0219\u015F\u023F\uA7A9\uA785\u1E9B]/g},
{'base':'t','letters':/[\u0074\u24E3\uFF54\u1E6B\u1E97\u0165\u1E6D\u021B\u0163\u1E71\u1E6F\u0167\u01AD\u0288\u2C66\uA787]/g},
{'base':'tz','letters':/[\uA729]/g},
{'base':'u','letters':/[\u0075\u24E4\uFF55\u00F9\u00FA\u00FB\u0169\u1E79\u016B\u1E7B\u016D\u00FC\u01DC\u01D8\u01D6\u01DA\u1EE7\u016F\u0171\u01D4\u0215\u0217\u01B0\u1EEB\u1EE9\u1EEF\u1EED\u1EF1\u1EE5\u1E73\u0173\u1E77\u1E75\u0289]/g},
{'base':'v','letters':/[\u0076\u24E5\uFF56\u1E7D\u1E7F\u028B\uA75F\u028C]/g},
{'base':'vy','letters':/[\uA761]/g},
{'base':'w','letters':/[\u0077\u24E6\uFF57\u1E81\u1E83\u0175\u1E87\u1E85\u1E98\u1E89\u2C73]/g},
{'base':'x','letters':/[\u0078\u24E7\uFF58\u1E8B\u1E8D]/g},
{'base':'y','letters':/[\u0079\u24E8\uFF59\u1EF3\u00FD\u0177\u1EF9\u0233\u1E8F\u00FF\u1EF7\u1E99\u1EF5\u01B4\u024F\u1EFF]/g},
{'base':'z','letters':/[\u007A\u24E9\uFF5A\u017A\u1E91\u017C\u017E\u1E93\u1E95\u01B6\u0225\u0240\u2C6C\uA763]/g}
];
var changes;
function removeDiacritics (str) {
if(!changes) {
changes = defaultDiacriticsRemovalMap;
}
for(var i=0; i<changes.length; i++) {
str = str.replace(changes[i].letters, changes[i].base);
}
return str;
}

This should work:
function purifyString(value) {
var r = [];
for (var i = 0, length = value.length; i < length; i++) {
r.push(replaceCharacter(value.charAt(i)));
}
return r.join("");
}
function replaceCharacter(character) {
switch (character) {
case '\r': return "\r";
case '\n': return "\n";
case '\t': return "\t";
case '\f': return "\r\n";
case '\v': return "\r\n";
case '`': return "'";
case '€': return "_";
case '‚': return ",";
case 'ƒ': return "f";
case '„': return "\"";
case '…': return "...";
case '†': return "_";
case '‡': return "_";
case 'ˆ': return "^";
case '‰': return "%";
case 'Š': return "S";
case '‹': return "<";
case 'Œ': return "CE";
case 'Ž': return "Z";
case '‘': return "'";
case '’': return "'";
case '“': return "\"";
case '”': return "\"";
case '•': return "-";
case '–': return "-";
case '—': return "-";
case '˜': return "~";
case '™': return "(tm)";
case 'š': return "s";
case '›': return ">";
case 'œ': return "ce";
case 'ž': return "z";
case 'Ÿ': return "Y";
case '¡': return "i";
case '¥': return "Y";
case '¦': return "|";
case 'ª': return "a";
case '¬': return "-";
case '¯': return "-";
case '²': return "2";
case '³': return "3";
case '´': return "'";
case '¸': return ",";
case '¹': return "1";
case 'º': return "0";
case '¼': return "1/4";
case '½': return "1/2";
case '¾': return "3/4";
case '¿': return "?";
case 'À': return "A";
case 'Á': return "A";
case 'Â': return "A";
case 'Ã': return "A";
case 'Ä': return "A";
case 'Å': return "A";
case 'Æ': return "AE";
case 'Ç': return "C";
case 'È': return "E";
case 'É': return "E";
case 'Ê': return "E";
case 'Ë': return "E";
case 'Ì': return "I";
case 'Í': return "I";
case 'Î': return "I";
case 'Ï': return "I";
case 'Ð': return "D";
case 'Ñ': return "N";
case 'Ò': return "O";
case 'Ó': return "O";
case 'Ô': return "O";
case 'Õ': return "O";
case 'Ö': return "O";
case '×': return "x";
case 'Ø': return "O";
case 'Ù': return "U";
case 'Ú': return "U";
case 'Û': return "U";
case 'Ü': return "U";
case 'Ý': return "Y";
case 'ß': return "B";
case 'à': return "a";
case 'á': return "a";
case 'â': return "a";
case 'ã': return "a";
case 'ä': return "a";
case 'å': return "a";
case 'æ': return "ae";
case 'ç': return "c";
case 'è': return "e";
case 'é': return "e";
case 'ê': return "e";
case 'ë': return "e";
case 'ì': return "i";
case 'í': return "i";
case 'î': return "i";
case 'ï': return "i";
case 'ñ': return "n";
case 'ò': return "o";
case 'ó': return "o";
case 'ô': return "o";
case 'õ': return "o";
case 'ö': return "o";
case '÷': return "/";
case 'ø': return "o";
case 'ù': return "u";
case 'ú': return "u";
case 'û': return "u";
case 'ü': return "u";
case 'ý': return "y";
case 'ÿ': return "y";
case '©': return "(c)";
case '®': return "(r)";
default: return "";
}
}

I don't know how to type those chars on my keyboard, but the following should do the job.
accentsTidyUpperCase = function(s){
var r=s.toLowerCase();
r = r.replace(new RegExp("[àáâãäå]".toUpperCase(), 'g'),"A");
r = r.replace(new RegExp("æ".toUpperCase(), 'g'),"AE");
r = r.replace(new RegExp("ç".toUpperCase(), 'g'),"C");
r = r.replace(new RegExp("[èéêë]".toUpperCase(), 'g'),"E");
r = r.replace(new RegExp("[ìíîï]".toUpperCase(), 'g'),"I");
r = r.replace(new RegExp("ñ".toUpperCase(), 'g'),"N");
r = r.replace(new RegExp("[òóôõö]".toUpperCase(), 'g'),"O");
r = r.replace(new RegExp("œ".toUpperCase(), 'g'),"OE");
r = r.replace(new RegExp("[ùúûü]".toUpperCase(), 'g'),"U");
r = r.replace(new RegExp("[ýÿ]".toUpperCase(), 'g'),"Y");
return r;
};
When you call both functions (accentsTidy and accentsTidyUpperCase) you will get lower case chars replaced by lowercase chars and upper case ones by upper case...

Related

How to convert Unicode to Preeti (ASCII) using javascript or php?

I am new to javascript and recently joined Bachelor.In my first semester,I was given project to convert Unicode to Preeti (Nepali font) which is ASCII code.The Dictionary for conversion was listed as :
unicodeToPreetiDict = \
{
"अ": "c",
"आ": "cf",
"ा": "f",
"इ": "O",
"ई": "O{",
"र्": "{",
"उ": "p",
"ए": "P",
"े": "]",
"ै": "}",
"ो": "f]",
"ौ": "f}",
"ओ": "cf]",
"औ": "cf}",
"ं": "+",
"ँ": "F",
"ि": "l",
"ी": "L",
"ु": "'",
"ू": '"',
"क": "s",
"ख": "v",
"ग": "u",
"घ": "3",
"ङ": "ª",
"च": "r",
"छ": "5",
"ज": "h",
"झ": "´",
"ञ": "`",
"ट": "6",
"ठ": "7",
"ड": "8",
"ढ": "9",
"ण": "0f",
"त": "t",
"थ": "y",
"द": "b",
"ध": "w",
"न": "g",
"प": "k",
"फ": "km",
"ब": "a",
"भ": "e",
"म": "d",
"य": "o",
"र": "/",
"रू": "?",
"ृ": "[",
"ल": "n",
"व": "j",
"स": ";",
"श": "z",
"ष": "if",
"ज्ञ": "1",
"ह": "x",
"१": "!",
"२": "#",
"३": "#",
"४": "$",
"५": "%",
"६": "^",
"७": "&",
"८": "*",
"९": "(",
"०": ")",
"।": ".",
"्": "\\",
"ऊ": "pm",
"-": " ",
"(": "-",
")": "_"
}
Left part of character is Unicode whereas right part is Preeti font maybe.
You can check the conversion after writing code in this sample site:
http://unicode.shresthasushil.com.np/
If we give input as कलम (unicode),it must be converted to Preeti.I need source code in javascript or php to convert unicode to preeti.
One way is to hard code it with a switch
function convert(x)
{
switch(x)
{
case "अ":
return "c";
case "आ":
return "cf";
case "ा":
return "f";
case "इ":
return "O";
case "ई":
return "O{";
case "र्":
return "{";
case "उ":
return "p";
case "ए":
return "P";
case "े":
return "]";
case "ै":
return "}";
case "ो":
return "f]";
case "ौ":
return "f}";
case "ओ":
return "cf]";
case "औ":
return "cf}";
case "ं":
return "+";
case "ँ":
return "F";
case "ि":
return "l";
case "ी":
return "L";
case "ु":
return "'";
case "ू":
return '"';
case "क":
return "s";
case "ख":
return "v";
case "ग":
return "u";
case "घ":
return "3";
case "ङ":
return "ª";
case "च":
return "r";
case "छ":
return "5";
case "ज":
return "h";
case "झ":
return "´";
case "ञ":
return "`";
case "ट":
return "6";
case "ठ":
return "7";
case "ड":
return "8";
case "ढ":
return "9";
case "ण":
return "0f";
case "त":
return "t";
case "थ":
return "y";
case "द":
return "b";
case "ध":
return "w";
case "न":
return "g";
case "प":
return "k";
case "फ":
return "km";
case "ब":
return "a";
case "भ":
return "e";
case "म":
return "d";
case "य":
return "o";
case "र":
return "/";
case "रू":
return "?";
case "ृ":
return "[";
case "ल":
return "n";
case "व":
return "j";
case "स":
return ";";
case "श":
return "z";
case "ष":
return "if";
case "ज्ञ":
return "1";
case "ह":
return "x";
case "१":
return "!";
case "२":
return "#";
case "३":
return "#";
case "४":
return "$";
case "५":
return "%";
case "६":
return "^";
case "७":
return "&";
case "८":
return "*";
case "९":
return "(";
case "०":
return ")";
case "।":
return ".";
case "्":
return "\\";
case "ऊ":
return "pm";
case "-":
return " ";
case "(":
return "-";
case ")":
return "_";
}
}
console.log(convert("ऊ"));
And when you're sure it's working as intended and done testing, you can smash it into one line so you dont waste text editor space.
function convert(x){
switch(x) {case "अ": return "c"; case "आ": return "cf"; case "ा": return "f"; case "इ": return "O"; case "ई": return "O{"; case "र्": return "{"; case "उ": return "p"; case "ए": return "P"; case "े": return "]"; case "ै": return "}"; case "ो": return "f]"; case "ौ": return "f}"; case "ओ": return "cf]"; case "औ": return "cf}"; case "ं": return "+"; case "ँ": return "F"; case "ि": return "l"; case "ी": return "L"; case "ु": return "'"; case "ू": return '"'; case "क": return "s"; case "ख": return "v"; case "ग": return "u"; case "घ": return "3"; case "ङ": return "ª"; case "च": return "r"; case "छ": return "5"; case "ज": return "h"; case "झ": return "´"; case "ञ": return "`"; case "ट": return "6"; case "ठ": return "7"; case "ड": return "8"; case "ढ": return "9"; case "ण": return "0f"; case "त": return "t"; case "थ": return "y"; case "द": return "b"; case "ध": return "w"; case "न": return "g"; case "प": return "k"; case "फ": return "km"; case "ब": return "a"; case "भ": return "e"; case "म": return "d"; case "य": return "o"; case "र": return "/"; case "रू": return "?"; case "ृ": return "["; case "ल": return "n"; case "व": return "j"; case "स": return ";"; case "श": return "z"; case "ष": return "if"; case "ज्ञ": return "1"; case "ह": return "x"; case "१": return "!"; case "२": return "#"; case "३": return "#"; case "४": return "$"; case "५": return "%"; case "६": return "^"; case "७": return "&"; case "८": return "*"; case "९": return "("; case "०": return ")"; case "।": return "."; case "्": return "\\"; case "ऊ": return "pm"; case "-": return " "; case "(": return "-"; case ")": return "_"; }
}
And so if you had a string of unicode characters, your code might look like this:
function convert(x){
switch(x) {case "अ": return "c"; case "आ": return "cf"; case "ा": return "f"; case "इ": return "O"; case "ई": return "O{"; case "र्": return "{"; case "उ": return "p"; case "ए": return "P"; case "े": return "]"; case "ै": return "}"; case "ो": return "f]"; case "ौ": return "f}"; case "ओ": return "cf]"; case "औ": return "cf}"; case "ं": return "+"; case "ँ": return "F"; case "ि": return "l"; case "ी": return "L"; case "ु": return "'"; case "ू": return '"'; case "क": return "s"; case "ख": return "v"; case "ग": return "u"; case "घ": return "3"; case "ङ": return "ª"; case "च": return "r"; case "छ": return "5"; case "ज": return "h"; case "झ": return "´"; case "ञ": return "`"; case "ट": return "6"; case "ठ": return "7"; case "ड": return "8"; case "ढ": return "9"; case "ण": return "0f"; case "त": return "t"; case "थ": return "y"; case "द": return "b"; case "ध": return "w"; case "न": return "g"; case "प": return "k"; case "फ": return "km"; case "ब": return "a"; case "भ": return "e"; case "म": return "d"; case "य": return "o"; case "र": return "/"; case "रू": return "?"; case "ृ": return "["; case "ल": return "n"; case "व": return "j"; case "स": return ";"; case "श": return "z"; case "ष": return "if"; case "ज्ञ": return "1"; case "ह": return "x"; case "१": return "!"; case "२": return "#"; case "३": return "#"; case "४": return "$"; case "५": return "%"; case "६": return "^"; case "७": return "&"; case "८": return "*"; case "९": return "("; case "०": return ")"; case "।": return "."; case "्": return "\\"; case "ऊ": return "pm"; case "-": return " "; case "(": return "-"; case ")": return "_"; }
}
var str = "आअाअअ";
for(var i = 0; i < str.length; i++)
{
console.log(convert(str[i]));
}
Make sure that in your html file you have a <meta charset="utf-8"> tag.
If for some reason you can't get this method to work, there are some other methods I found out there regarding splitting using charCodeAt.
An example of this kind of method would be like so :
var str = "आअाअअ";
var arr = stringToArray(str);
console.log(arr);
function convert(x){
switch(x) {case "अ": return "c"; case "आ": return "cf"; case "ा": return "f"; case "इ": return "O"; case "ई": return "O{"; case "र्": return "{"; case "उ": return "p"; case "ए": return "P"; case "े": return "]"; case "ै": return "}"; case "ो": return "f]"; case "ौ": return "f}"; case "ओ": return "cf]"; case "औ": return "cf}"; case "ं": return "+"; case "ँ": return "F"; case "ि": return "l"; case "ी": return "L"; case "ु": return "'"; case "ू": return '"'; case "क": return "s"; case "ख": return "v"; case "ग": return "u"; case "घ": return "3"; case "ङ": return "ª"; case "च": return "r"; case "छ": return "5"; case "ज": return "h"; case "झ": return "´"; case "ञ": return "`"; case "ट": return "6"; case "ठ": return "7"; case "ड": return "8"; case "ढ": return "9"; case "ण": return "0f"; case "त": return "t"; case "थ": return "y"; case "द": return "b"; case "ध": return "w"; case "न": return "g"; case "प": return "k"; case "फ": return "km"; case "ब": return "a"; case "भ": return "e"; case "म": return "d"; case "य": return "o"; case "र": return "/"; case "रू": return "?"; case "ृ": return "["; case "ल": return "n"; case "व": return "j"; case "स": return ";"; case "श": return "z"; case "ष": return "if"; case "ज्ञ": return "1"; case "ह": return "x"; case "१": return "!"; case "२": return "#"; case "३": return "#"; case "४": return "$"; case "५": return "%"; case "६": return "^"; case "७": return "&"; case "८": return "*"; case "९": return "("; case "०": return ")"; case "।": return "."; case "्": return "\\"; case "ऊ": return "pm"; case "-": return " "; case "(": return "-"; case ")": return "_"; }
}
function stringToArray() {
var i = 0,
arr = [],
codePoint;
while (!isNaN(codePoint = knownCharCodeAt(str, i))) {
arr.push(String.fromCodePoint(codePoint));
i++;
}
return arr;
}
if (!String.fromCodePoint) {
// ES6 Unicode Shims 0.1 , © 2012 Steven Levithan , MIT License
String.fromCodePoint = function fromCodePoint () {
var chars = [], point, offset, units, i;
for (i = 0; i < arguments.length; ++i) {
point = arguments[i];
offset = point - 0x10000;
units = point > 0xFFFF ? [0xD800 + (offset >> 10), 0xDC00 + (offset & 0x3FF)] : [point];
chars.push(String.fromCharCode.apply(null, units));
}
return chars.join("");
}
}
function knownCharCodeAt(str, idx) {
str += '';
var code,
end = str.length;
var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
while ((surrogatePairs.exec(str)) != null) {
var li = surrogatePairs.lastIndex;
if (li - 2 < idx) {
idx++;
}
else {
break;
}
}
if (idx >= end || idx < 0) {
return NaN;
}
code = str.charCodeAt(idx);
var hi, low;
if (0xD800 <= code && code <= 0xDBFF) {
hi = code;
low = str.charCodeAt(idx + 1);
// Go one further, since one of the "characters"
// is part of a surrogate pair
return ((hi - 0xD800) * 0x400) +
(low - 0xDC00) + 0x10000;
}
return code;
}
I strongly recommend just using the first method I made for you though, as it is much easier to control.
You use arrays to search/replace in str_replace in PHP
$translated_msg = str_replace(["अ","आ","ा","इ","ई","र्","उ","ए","े","ै","ो","ौ","ओ","औ","ं","ँ","ि","ी","ु","ू","क","ख","ग","घ","ङ","च","छ","ज","झ","ञ","ट","ठ","ड","ढ","ण","त","थ","द","ध","न","प","फ","ब","भ","म","य","र","रू","ृ","ल","व","स","श","ष","ज्ञ","ह","१","२","३","४","५","६","७","८","९","०","।","्","ऊ","-","(",")"], ["3","ª","r","5","h","´","`","6","7","8","9","0f","t","y","b","w","g","k","km","a","e","d","o","/","?","[","n","j",";","z","if","1","x","!","#","#","$","%","^","&","*","(",")",".","\\","pm"," ","-","_"], $some_msg);

Javascript switch statement comparisons throwing me for a loop

I'm trying to use a switch statement to convert some shortened tokens into full words, last time I did it, it worked, this time not so much. I think it's something wrong with the types, but I have no idea how to fix it. Weirdly enough, the modifier portion works correctly, but not the source part.
function keyToSource(key)
{
let fullSource, source, modifier;
if(key.includes("-"))
{
modifier = key.substring(key.indexOf("-") + 1, key.length);
source = key.substring(0, key.indexOf("-"));
}
else source = key;
switch(source)
{
case "Bo": fullSource = "Body"; break;
case "Ca": fullSource = "Capture"; break;
case "FA": fullSource = "Forearms"; break;
case "HL": fullSource = "Hindlegs"; break;
case "HS": fullSource = "Hard Shell"; break;
case "IR": fullSource = "Investigation Reward"; break;
case "PB": fullSource = "Palico Bonus"; break;
case "Pl": fullSource = "Plunderblade"; break;
case "SD": fullSource = "Shiny Drop"; break;
case "Ta": fullSource = "Tail"; break;
case "Tr": fullSource = "Track"; break;
default: fullSource = "Error"; break;
}
if(typeof modifier !== 'undefined')
{
switch(modifier)
{
case "C": fullSource += " carve"; break;
case "G": fullSource += "(Gold)"; break;
case "S": fullSource += "(Silver)"; break;
case "W": fullSource += " wound"; break;
default: fullSource = "Error" + fullSource; break;
}
}
return fullSource;
}
console.log(keyToSource("Ta"));
console.log(keyToSource("Ta-C"));
Your code appears to work. However, you can clean up the logic quite a lot by moving your switch statements into objects:
function keyToSource(key) {
const k = key.split("-");
const source = {
"Bo": "Body",
"Ca": "Capture",
"FA": "Forearms",
"HL": "Hindlegs",
"HS": "Hard Shell",
"IR": "Investigation Reward",
"PB": "Palico Bonus",
"Pl": "Plunderblade",
"SD": "Shiny Drop",
"Ta": "Tail",
"Tr": "Track"
};
const modifier = {
"C": " carve",
"G": "(Gold)",
"S": "(Silver)",
"W": " wound"
};
return (source[k[0]] || "") + (modifier[k[1]] || "");
}
console.log(keyToSource("Ta"));
console.log(keyToSource("Ta-C"));
console.log(keyToSource("PB"));
console.log(keyToSource("Ta-G"));
console.log(keyToSource("SD-W"));
console.log(keyToSource("HS-C"));
console.log(keyToSource("as- da-sdf")); // test invalid entry
Feel free to post a clarification if this isn't cutting it for you.

new to chrome extension: not working as expected

I'm new to Google Chrome extension development. I have very basic knowledge of html and javaScript.
I have created folder name "Extension".
in which there are 4 files as below.
1.manifest.json
{
"manifest_version": 2,
"name": "lenght Unit converter",
"description": "This extension will convert the lenght from one unit to other ",
"version": "1.0",
"browser_action": {
"default_icon": "Refresh.png",
"default_popup": "popup.html"
},
"permissions": [
"activeTab"
]
}
2.popup.html
<!doctype html>
<html>
<head>
<title>Length Unit Converter</title>
<script src="popup.js"></script>
</head>
<body>
<h1>Lenght Unit Converter</h1>
<input type="text" name="inputText" id="inputValue" required onfocus="clearTextBox()">
<select id="list1" onchange="getSelectValue1()">
<option value="km">Kilometre</option>
<option value="m">Metre</option>
<option value="cm">Centimetre</option>
<option value="mm">Millimetre</option>
<option value="mc">Micrometre</option>
<option value="nm">Nanometre</option>
<option value="ml">Mile</option>
<option value="yd">Yard</option>
<option value="ft">Foot</option>
<option value="ic">Inch</option>
<option value="ntm">Nautical mile</option>
</select>
<br>
<input id="display" type="text" name="display" readonly>
<select id="list2" onchange="getSelectValue2()">
<option value="km">Kilometre</option>
<option value="m">Metre</option>
<option value="cm">Centimetre</option>
<option value="mm">Millimetre</option>
<option value="mc">Micrometre</option>
<option value="nm">Nanometre</option>
<option value="ml">Mile</option>
<option value="yd">Yard</option>
<option value="ft">Foot</option>
<option value="ic">Inch</option>
<option value="ntm">Nautical mile</option>
</select>
</br>
<br><input type="button" value="convert" onclick="setValueToTextBox()"></br>
</body>
</html>
3.popup.js
In popup.js file i have written my logic for functions which are present inside the html file.
Refresh.png
Refresh.png is 28x28 size of png format image.
Whenever i open my popup.html file with firefox it works fine. Screenshot is given below.
enter image description here
enter image description here
**Rather You Can Run my code from below here
var input1;
var input2;
var inputValue;
function clearTextBox() {
document.getElementById("display").value="";
}
function getInputValue(){
inputValue=document.getElementById("inputValue").value.toString();
}
function getSelectValue1() {
input1=document.getElementById("list1").value.toString();
}
function getSelectValue2() {
input2= document.getElementById("list2").value.toString();
document.getElementById("display").value="";
}
function setValueToTextBox()
{
document.getElementById("display").value=answer().toString();
}
function answer() {
getSelectValue1();
getSelectValue2();
getInputValue();
if (input1 == "km") {
switch (input2) {
case "km":
return parseInt(inputValue) * 1;
break;
case "m":
return parseInt(inputValue) * 1000;
break;
case "cm":
return parseInt(inputValue) * 100000;
break;
case "mm":
return parseInt(inputValue) * 1000000;
break;
case "mc":
return parseInt(inputValue) * 1000000000;
break;
case "nm":
return parseInt(inputValue) * 1000000000000;
break;
case "ml":
return parseInt(inputValue) * 0.621371;
break;
case "yd":
return parseInt(inputValue) * 1093.61;
break;
case "ft":
return parseInt(inputValue) * 3280.84;
break;
case "ic":
return parseInt(inputValue) * 39370.1;
break;
case "ntm":
return parseInt(inputValue) * 0.539957;
break;
}
}
if (input1 == "m") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.001;
break;
case "m":
return parseInt(inputValue) * 1;
break;
case "cm":
return parseInt(inputValue) * 100;
break;
case "mm":
return parseInt(inputValue) * 1000;
break;
case "mc":
return parseInt(inputValue) * 1000000;
break;
case "nm":
return parseInt(inputValue) * 1000000000;
break;
case "ml":
return parseInt(inputValue) * 0.000621371;
break;
case "yd":
return parseInt(inputValue) * 1.09361;
break;
case "ft":
return parseInt(inputValue) * 3.28084;
break;
case "ic":
return parseInt(inputValue) * 39.3701;
break;
case "ntm":
return parseInt(inputValue) * 0.000539957;
break;
}
}
if (input1 == "cm") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.00001;
break;
case "m":
return parseInt(inputValue) * 0.01;
break;
case "cm":
return parseInt(inputValue) * 1;
break;
case "mm":
return parseInt(inputValue) * 10;
break;
case "mc":
return parseInt(inputValue) * 10000;
break;
case "nm":
return parseInt(inputValue) * 10000000;
break;
case "ml":
return parseInt(inputValue) * 0.0000062137;
break;
case "yd":
return parseInt(inputValue) * 0.0109361;
break;
case "ft":
return parseInt(inputValue) * 0.0328084;
break;
case "ic":
return parseInt(inputValue) * 0.393701;
break;
case "ntm":
return parseInt(inputValue) * 0.0000053996;
break;
}
}
if (input1 == "mm") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.000001;
break;
case "m":
return parseInt(inputValue) * 0.001;
break;
case "cm":
return parseInt(inputValue) * 0.1;
break;
case "mm":
return parseInt(inputValue) * 1;
break;
case "mc":
return parseInt(inputValue) * 1000;
break;
case "nm":
return parseInt(inputValue) * 1000000;
break;
case "ml":
return parseInt(inputValue) * 0.00000062137;
break;
case "yd":
return parseInt(inputValue) * 0.00109361;
break;
case "ft":
return parseInt(inputValue) * 0.00328084;
break;
case "ic":
return parseInt(inputValue) * 0.0393701;
break;
case "ntm":
return parseInt(inputValue) * 0.00000053996;
break;
}
}
if (input1 == "mc") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.000000001;
break;
case "m":
return parseInt(inputValue) * 0.000001;
break;
case "cm":
return parseInt(inputValue) * 0.0001;
break;
case "mm":
return parseInt(inputValue) * 0.001;
break;
case "mc":
return parseInt(inputValue) * 1;
break;
case "nm":
return parseInt(inputValue) * 1000;
break;
case "ml":
return parseInt(inputValue) * 0.00000000062137;
break;
case "yd":
return parseInt(inputValue) * 0.0000010936;
break;
case "ft":
return parseInt(inputValue) * 0.0000032808;
break;
case "ic":
return parseInt(inputValue) * 0.00003937;
break;
case "ntm":
return parseInt(inputValue) * 0.00000000053996;
break;
}
}
if (input1 == "nm") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.000000000001;
break;
case "m":
return parseInt(inputValue) * 0.000000001;
break;
case "cm":
return parseInt(inputValue) * 0.0000001;
break;
case "mm":
return parseInt(inputValue) * 0.000001;
break;
case "mc":
return parseInt(inputValue) * 0.001;
break;
case "nm":
return parseInt(inputValue) * 1;
break;
case "ml":
return parseInt(inputValue) * 0.00000000000062137;
break;
case "yd":
return parseInt(inputValue) * 0.0000000010936;
break;
case "ft":
return parseInt(inputValue) * 0.0000000032808;
break;
case "ic":
return parseInt(inputValue) * 0.00000003937;
break;
case "ntm":
return parseInt(inputValue) * 0.00000000000053996;
break;
}
}
if (input1 == "ml") {
switch (input2) {
case "km":
return parseInt(inputValue) * 1.60934;
break;
case "m":
return parseInt(inputValue) * 1609.34;
break;
case "cm":
return parseInt(inputValue) * 160934;
break;
case "mm":
return parseInt(inputValue) * 1609000;
break;
case "mc":
return parseInt(inputValue) * 1609000000;
break;
case "nm":
return parseInt(inputValue) * 1609000000000;
break;
case "ml":
return parseInt(inputValue) * 1;
break;
case "yd":
return parseInt(inputValue) * 1760;
break;
case "ft":
return parseInt(inputValue) * 5280;
break;
case "ic":
return parseInt(inputValue) * 63360;
break;
case "ntm":
return parseInt(inputValue) * 0.868976;
break;
}
}
if (input1 == "yd") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.0009144;
break;
case "m":
return parseInt(inputValue) * 0.9144;
break;
case "cm":
return parseInt(inputValue) * 91.44;
break;
case "mm":
return parseInt(inputValue) * 914.4;
break;
case "mc":
return parseInt(inputValue) * 914400;
break;
case "nm":
return parseInt(inputValue) * 914400000;
break;
case "ml":
return parseInt(inputValue) * 0.000568182;
break;
case "yd":
return parseInt(inputValue) * 1;
break;
case "ft":
return parseInt(inputValue) * 3;
break;
case "ic":
return parseInt(inputValue) * 36;
break;
case "ntm":
return parseInt(inputValue) * 0.000493737;
break;
}
}
if (input1 == "ft") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.0003048;
break;
case "m":
return parseInt(inputValue) * 0.3048;
break;
case "cm":
return parseInt(inputValue) * 30.48;
break;
case "mm":
return parseInt(inputValue) * 304.8;
break;
case "mc":
return parseInt(inputValue) * 304800;
break;
case "nm":
return parseInt(inputValue) * 304800000;
break;
case "ml":
return parseInt(inputValue) * 0.000189394;
break;
case "yd":
return parseInt(inputValue) * 0.333333;
break;
case "ft":
return parseInt(inputValue) * 1;
break;
case "ic":
return parseInt(inputValue) * 12;
break;
case "ntm":
return parseInt(inputValue) * 0.000164579;
break;
}
}
if (input1 == "ic") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.0000254;
break;
case "m":
return parseInt(inputValue) * 0.0254;
break;
case "cm":
return parseInt(inputValue) * 2.54;
break;
case "mm":
return parseInt(inputValue) * 25.4;
break;
case "mc":
return parseInt(inputValue) * 25400;
break;
case "nm":
return parseInt(inputValue) * 25400000;
break;
case "ml":
return parseInt(inputValue) * 0.000015783;
break;
case "yd":
return parseInt(inputValue) * 0.0277778;
break;
case "ft":
return parseInt(inputValue) * 0.0833333;
break;
case "ic":
return parseInt(inputValue) * 1;
break;
case "ntm":
return parseInt(inputValue) * 0.000013715;
break;
}
}
if (input1 == "ic") {
switch (input2) {
case "km":
return parseInt(inputValue) * 0.0000254;
break;
case "m":
return parseInt(inputValue) * 0.0254;
break;
case "cm":
return parseInt(inputValue) * 2.54;
break;
case "mm":
return parseInt(inputValue) * 25.4;
break;
case "mc":
return parseInt(inputValue) * 25400;
break;
case "nm":
return parseInt(inputValue) * 25400000;
break;
case "ml":
return parseInt(inputValue) * 0.000015783;
break;
case "yd":
return parseInt(inputValue) * 0.0277778;
break;
case "ft":
return parseInt(inputValue) * 0.0833333;
break;
case "ic":
return parseInt(inputValue) * 1;
break;
case "ntm":
return parseInt(inputValue) * 0.000013715;
break;
}
}
if (input1 == "ntm") {
switch (input2) {
case "km":
return parseInt(inputValue) * 1.852;
break;
case "m":
return parseInt(inputValue) * 1852;
break;
case "cm":
return parseInt(inputValue) * 185200;
break;
case "mm":
return parseInt(inputValue) * 1852000;
break;
case "mc":
return parseInt(inputValue) * 1852000000;
break;
case "nm":
return parseInt(inputValue) * 1852000000000;
break;
case "ml":
return parseInt(inputValue) * 1.15078;
break;
case "yd":
return parseInt(inputValue) * 2025.37;
break;
case "ft":
return parseInt(inputValue) * 6076.12;
break;
case "ic":
return parseInt(inputValue) * 72913.4;
break;
case "ntm":
return parseInt(inputValue) * 1;
break;
}
}
}
<!doctype html>
<html>
<head>
<title>Length Unit Converter</title>
<script src="popup.js"></script>
</head>
<body>
<h1>Lenght Unit Converter</h1>
<input type="text" name="inputText" id="inputValue" required onfocus="clearTextBox()">
<select id="list1" onchange="getSelectValue1()">
<option value="km">Kilometre</option>
<option value="m">Metre</option>
<option value="cm">Centimetre</option>
<option value="mm">Millimetre</option>
<option value="mc">Micrometre</option>
<option value="nm">Nanometre</option>
<option value="ml">Mile</option>
<option value="yd">Yard</option>
<option value="ft">Foot</option>
<option value="ic">Inch</option>
<option value="ntm">Nautical mile</option>
</select>
<br>
<input id="display" type="text" name="display" readonly>
<select id="list2" onchange="getSelectValue2()">
<option value="km">Kilometre</option>
<option value="m">Metre</option>
<option value="cm">Centimetre</option>
<option value="mm">Millimetre</option>
<option value="mc">Micrometre</option>
<option value="nm">Nanometre</option>
<option value="ml">Mile</option>
<option value="yd">Yard</option>
<option value="ft">Foot</option>
<option value="ic">Inch</option>
<option value="ntm">Nautical mile</option>
</select>
</br>
<br><input type="button" value="convert" onclick="setValueToTextBox()"></br>
</body>
</html>
**
But when i load it on google chrome browser ...it looks normal as i expected. Screenshot is given below!
enter image description here
my extension looks as i expected but it's functions not work like normal html and javascript program.
Please Help me to solve this!!! thanks in advance!

Can I use this.something without defining it?

https://jsfiddle.net/b3nyzp9n/
When probabilityAPI.card() is called, it returns ' of '
It seems like this.apicardrankvar doesn't exist at all.
So when I defined apicardrankvar and apicardsuitvar, it returned "undefined of undefined".
I am trying to get it to say a card name, such as "3 of Diamonds"
var probabilityAPI = {
coin: function() {
if(Math.random() >= 0.5) {
return "Heads";
} else if(Math.random() > 0.5){
return "Tails";
}},
dice: function() {
return Math.floor(Math.random() * (Math.floor(7) - Math.ceil(1))) + Math.ceil(1);
},
spinner: function() {
return Math.floor(Math.random() * (Math.floor(9) - Math.ceil(0))) + Math.ceil(1);
},
apimarblevar: Math.floor(Math.random() * (Math.floor(6) - Math.ceil(1))) + Math.ceil(1),
marble: function(apimarblevar) {
switch(this.apimarblevar)
{
case 1:
return "A"
break;
case 2:
return "B"
break;
case 3:
return "C"
break;
case 4:
return "D"
break;
case 5:
return "E"
break;
}
},
apidreidelvar: Math.floor(Math.random() * (Math.floor(5) - Math.ceil(1))) + Math.ceil(1),
dreidel: function() {
switch(this.apidreidelvar) {
case 1:
return "Nun"
break;
case 2:
return "Gimel"
break;
case 3:
return "He"
break;
case 4:
return "Shin"
break;
}
},
apicardsuitvar: "",
apicardrankvar: "",
apicardrankrandom: Math.floor(Math.random() * (Math.floor(14) - Math.ceil(1))) + Math.ceil(1),
apicardsuit: function(){
switch(this.apidreidelvar) {
case 1:
this.apicardsuitvar = "Clubs"
break;
case 2:
this.apicardsuitvar = "Diamonds"
break;
case 3:
this.apicardsuitvar = "Hearts"
break;
case 4:
this.apicardsuitvar = "Spades"
break;
}
},
apicardrank: function() {
switch(this.apicardrankrandom) {
case 1:
this.apicardrankvar = "Ace"
break;
case 2:
this.apicardrankvar = "2"
break;
case 3:
this.apicardrankvar = "3"
break;
case 4:
this.apicardrankvar = "4"
break;
case 5:
this.apicardrankvar = "5"
break;
case 6:
this.apicardrankvar = "6"
break;
case 7:
this.apicardrankvar = "7"
break;
case 8:
this.apicardrankvar = "8"
break;
case 9:
this.apicardrankvar = "9"
break;
case 10:
this.apicardrankvar = "10"
break;
case 11:
this.apicardrankvar = "Jack"
break;
case 12:
this.apicardrankvar = "Queen"
break;
case 13:
this.apicardrankvar = "King"
break;
}
},
card: function() {
return this.apicardrankvar + " of " + this.apicardsuitvar
}
};
probabilityAPI.card();

Switching the background image in angularjs

I am trying to add a background image to this tile layout I have going on in angularjs.
javascript
angular.module('MyApp', ['ngMaterial', 'ngMessages'])
.controller('gridListDemoCtrl', function($scope) {
this.tiles = buildGridModel({
icon: "avatar:svg-",
title: "Svg-",
background: ""
});
function buildGridModel(tileTmpl) {
var it, results = [];
for (var j = 11; j > 0; j--) {
it = angular.extend({}, tileTmpl);
it.icon = it.icon + (j - 1);
it.title = it.title + (j - 1);
it.span = { row: 1, col: 1 };
switch (j - 1) {
case 0:
it.title = "My First Video";
break;
case 1:
it.background = "red";
it.title = "Yo";
break;
case 2:
it.background = "green";
break;
case 3:
it.background = "darkBlue";
break;
case 4:
it.background = "blue";
break;
case 5:
it.background = "yellow";
break;
case 6:
it.background = "pink";
break;
case 7:
it.background = "darkBlue";
break;
case 8:
it.background = "purple";
break;
case 9:
it.background = "deepBlue";
break;
case 10:
it.background = "lightPurple";
break;
case 11:
it.background = "yellow";
break;
}
results.push(it);
}
return results;
}
})
.config(function($mdIconProvider) {
$mdIconProvider.iconSet("avatar", 'icons/avatar-icons.svg', 128);
});
What should I do? Should I add/change a directive? I can post the html/css if needed. Thank you for your help.

Categories