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);
Please tell me how I can do with two parameters and simplify this code to a normal state. Thank you! This logic is very necessary for me, I hope for your indulgence.
var treshhold_two = 0;
function Test(attack, ratio) {
switch (attack) {
case 0,01:
switch (ratio) {
case 2:
treshhold = 2798,6;
break;
case 4:
treshhold = 3678,16;
break;
case 6:
treshhold = 5757,12;
break;
}
break;
case >0,01:
switch (attack, ratio) {
case 0,03,2:
treshhold = -5,75712;
break;
case 0,03,4:
treshhold = -5,75712 * 1,1;
break; // -45%
case 0,03,6:
treshhold = -5,75712 * 0,96;
break; // -52%, and etc.
...
}
break;
}
}
Try:
var treshhold_two = 0;
function Test(attack, ratio) {
if(attack == 0,01) {
switch (ratio) {
case 2:
treshhold = 2798,6;
break;
case 4:
treshhold = 3678,16;
break;
case 6:
treshhold = 5757,12;
break;
}
}
else {
switch (attack) {
case 0,03:
if(ratio==2) treshhold = -5,75712;
if(ratio==4) treshhold = -5,75712 * 1,1;
if(ratio==6) treshhold = -5,75712 * 0,96;
break;
...
}
}
}
//option:1
var treshhold = 0;
function Test(attack, ratio) {
switch (attack) {
case 0,01:
if(ratio==2) treshhold = 2798,6;
if(ratio==4) treshhold = 3678,16;
if(ratio==6) treshhold = 5757,12;
break;
case 0,03:
if(ratio==2) treshhold = -5,75712;
if(ratio==4) treshhold = -5,75712 * 1,1;
if(ratio==6) treshhold = -5,75712 * 0,96;
break;
// ...
}
}
}
//option:2
var treshhold = 0;
function Test(attack, ratio) {
switch (attack) {
case 0,01:
switch (ratio) {
case 2: treshhold = 2798,6; break;
case 4: treshhold = 3678,16; break;
case 6: treshhold = 5757,12; break;
}
case 0,03:
switch (ratio) {
case 2: treshhold = -5,75712; break;
case 4: treshhold = -5,75712 * 1,1; break;
case 6: treshhold = -5,75712 * 0,96;; break;
}
// ...
}
}
//option:3
var treshhold = 1223456;
function Test(ratio, attack) {
switch (ratio) {
case 2:
switch (attack) {
case 0,01: ... break;
case 0,03: ... break;
case 0,1: ... break;
}
break;
case 4:
switch (attack) {
case 0,01: ... break;
case 0,03: ... break;
case 0,1: ... break;
}
break;
case 6:
switch (attack) {
case 0,01: ... break;
case 0,03: ... break;
case 0,1: ... break;
}
break;
}
// option?
var treshhold_two = 0;
function Test(attack, ratio) {
if (attack == 0.01) {
switch (ratio) {
case 2:
treshhold = 2798.6;
break;
case 4:
treshhold = 3678.16;
break;
case 6:
treshhold = 5757.12;
break;
}
}
else if (attack > 0.01) {
switch (attack, ratio) {
case 0,03,2: // what does it mean ?
treshhold = -5.75712;
break;
case 0,03,4: // what does it mean ?
treshhold = -5.75712 * 1.1;
break; // -45%
case 0,03,6: // what does it mean ?
treshhold = -5.75712 * 0.96;
break; // -52%, and etc.
...
}
}
}
I want to simplify this long jquery/javascript code, can you help me? I still learn :)
Here's my jquery code:
$('.pagination-link').click(function() {
setTimeout(function() {
currentAnchor = $('body').attr('class');
switch (currentAnchor) {
case 'active-slide-1':
$('#rond').removeClass().addClass('rond1').animate();
break;
case 'active-slide-2':
$('#rond').removeClass().addClass('rond2').animate();
break;
case 'active-slide-3':
$('#rond').removeClass().addClass('rond3').animate();
break;
case 'active-slide-4':
$('#rond').removeClass().addClass('rond4').animate();
break;
case 'active-slide-5':
$('#rond').removeClass().addClass('rond5').animate();
break;
case 'active-slide-6':
$('#rond').removeClass().addClass('rond6').animate();
break;
case 'active-slide-7':
$('#rond').removeClass().addClass('rond7').animate();
break;
case 'active-slide-8':
$('#rond').removeClass().addClass('rond8').animate();
break;
default:
$('#rond').removeClass();
}
}, 50);
});
$('.overlay-menu > ul > li > a').click(function() {
setTimeout(function() {
currentAnchor = $('body').attr('class');
switch (currentAnchor) {
case 'active-slide-1':
$('#rond').removeClass().addClass('rond1').animate();
break;
case 'active-slide-2':
$('#rond').removeClass().addClass('rond2').animate();
break;
case 'active-slide-3':
$('#rond').removeClass().addClass('rond3').animate();
break;
case 'active-slide-4':
$('#rond').removeClass().addClass('rond4').animate();
break;
case 'active-slide-5':
$('#rond').removeClass().addClass('rond5').animate();
break;
case 'active-slide-6':
$('#rond').removeClass().addClass('rond6').animate();
break;
case 'active-slide-7':
$('#rond').removeClass().addClass('rond7').animate();
break;
case 'active-slide-8':
$('#rond').removeClass().addClass('rond8').animate();
break;
default:
$('#rond').removeClass();
}
}, 50);
});
$(window).mousewheel(function() {
setTimeout(function() {
currentAnchor = $('body').attr('class');
switch (currentAnchor) {
case 'active-slide-1':
$('#rond').removeClass().addClass('rond1').animate();
break;
case 'active-slide-2':
$('#rond').removeClass().addClass('rond2').animate();
break;
case 'active-slide-3':
$('#rond').removeClass().addClass('rond3').animate();
break;
case 'active-slide-4':
$('#rond').removeClass().addClass('rond4').animate();
break;
case 'active-slide-5':
$('#rond').removeClass().addClass('rond5').animate();
break;
case 'active-slide-6':
$('#rond').removeClass().addClass('rond6').animate();
break;
case 'active-slide-7':
$('#rond').removeClass().addClass('rond7').animate();
break;
case 'active-slide-8':
$('#rond').removeClass().addClass('rond8').animate();
break;
default:
$('#rond').removeClass();
}
}, 50);
});
I don't know if I have to use php to get the end of currentAnchor and put it as a parameters. Thank you for your help !
EDIT: I found that the default case is not necessary in my code. But I learn something new if I had my defaut case was important. So, here it's the new one:
function rondClass() {
setTimeout(function() {
currentAnchor = $('body').attr('class');
var currentClass = currentAnchor.replace('active-slide-', 'rond');
$('#rond').removeClass().addClass(currentClass).animate();
}, 50);
}
$('.pagination-link').click(rondClass);
$('.overlay-menu > ul > li > a').click(rondClass);
$(window).mousewheel(rondClass);
Thank you everyone !
You could just replace active-slide- with rond.
var currentAnchor = $('body').attr('class');
var newClass = currentAnchor.replace('active-slide-', 'rond');
$('#rond').removeClass().addClass(newClass).animate();
To handle the default case, you can handle this using indexOf or match:
// indexOf version
if (currentAnchor.indexOf('active-slide-') !== 0) {
$('#rond').removeClass();
} else {
$('#rond').removeClass().addClass(newClass).animate();
}
// match version
if (!currentAnchor.match(/^active-slide-/)) {
$('#rond').removeClass();
} else {
$('#rond').removeClass().addClass(newClass).animate();
}
Replace switch/case with programmatic approach and extract function to avoid code duplication.
var onClick = function() {
setTimeout(function() {
currentAnchor = $('body').attr('class');
var slide = currentAnchor.match(/active\-slide\-(\d)/);
if (slide) {
$('#rond').removeClass().addClass('rond' + slide[1]).animate();
} else {
$('#rond').removeClass();
}
}, 50)
})
$('.overlay-menu > ul > li > a').click(onClick);
$('.pagination-link').click(onClick);