Created
June 18, 2014 12:41
-
-
Save deekayen/7a1dc6b550c5b512a6fd to your computer and use it in GitHub Desktop.
Norman keyboard layout analyzer by Nahuel Caponi from http://jsfiddle.net/Deban/qJLLm/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var keyboards = []; | |
var text = ""; | |
//To create a keyboard: | |
//call the CreateKeyboard function, then pass the name, add a comma, | |
//lastly spam your first 10 keys for each row | |
//don't use the numbers row and don't use more than 10 keys per row | |
CreateKeyboard("qwerty", "qwertyuiopasdfghjkl;zxcvbnm,./"); | |
CreateKeyboard("colemak", "qwfpgjluy;arstdhneiozxcvbkm,./"); | |
CreateKeyboard("workman", "qdrwbjfup;ashtgyneoizxmcvkl,./"); | |
CreateKeyboard("norman", "qwdfkjurl;asetgyniohzxcvbpm,./"); | |
//Functions for inner work | |
function CreateKeyboard(name, keys) { | |
keyboards.push({ | |
name: name, | |
keys: MapKeys(keys) | |
}); | |
} | |
function MapKeys(keyboard) { | |
keyboard = keyboard.split(""); | |
var keys = {}; | |
var keyValues = [4, 2, 2, 3, 4, 5, 3, 2, 2, 4, 1.5, 1, 1, 1, 3, 3, 1, 1, 1, 1.5, 4, 4, 3, 2, 5, 3, 2, 3, 4, 4]; | |
for (var i = 0; i < keyboard.length; i++) { | |
keys[keyboard[i]] = { | |
value: keyValues[i], | |
x: (i + 1) - Math.floor((i) / 10) * 10, | |
y: Math.floor((i + 1) / 10 + 1), | |
finger: GetFinger((i + 1) - Math.floor((i) / 10) * 10) | |
}; | |
} | |
return keys; | |
} | |
function CalculateScore(keys) { | |
var score = 0; | |
var scoreDoubleFinger = 0; | |
var character; | |
var lastChar = "."; | |
for (var i = 0; i < text.length; i++) { | |
character = text.charAt(i); | |
if (keys[character]) { | |
score += keys[character].value; | |
if (keys[lastChar] && (keys[lastChar].finger === keys[character].finger)) { | |
scoreDoubleFinger += 0.5 + (Math.abs(keys[lastChar].y - keys[character].y) + Math.abs(keys[lastChar].x - keys[character].x)) * (keys[character].value / 2); | |
} | |
} | |
lastChar = character; | |
} | |
return { | |
score: score, | |
scoreDoubleFingers: scoreDoubleFinger, | |
total: score + scoreDoubleFinger | |
}; | |
} | |
function GetFinger(position) { | |
var finger = position; | |
if (position >= 7) { | |
finger = position - 2; | |
} else if (position > 4) { | |
finger = position - 1; | |
} | |
return finger; | |
} | |
function Show(text) { | |
document.getElementById("result").innerHTML += text + "\n"; | |
} | |
document.getElementById("button").onclick = function () { | |
text = document.getElementById("theText").value; | |
document.getElementById("result").innerHTML = ""; | |
var score; | |
Show("Less score is better."); | |
for(var i = 0; i< keyboards.length; i++) { | |
score = CalculateScore(keyboards[i].keys); | |
Show("------------------------------------------------------------------"); | |
Show("The " + keyboards[i].name + " score was: " + score.total); | |
Show("Composed by " + score.score + " in keycaps and " + score.scoreDoubleFingers + " for double fingers."); | |
} | |
Show("------------------------------------------------------------------"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment