Dobre praktyki programistyczne

Wymieszane litery

23.01.2011

Znalazłem kilka stron, na których można „wymieszać litery”, ale skrypty działały po stronie serwera. Napisałem więc wersję w JavaScript, która robi to samo, ale w przeglądarce.

Skrypt publikuję na licencji ISC (podobnej do MIT, ale prostszej).

  1. /**
  2.  * Gets text from input field, mix it and then display result in output field
  3.  *
  4.  * @param {string} input ID of input field
  5.  * @param {string} output ID of output field
  6.  */
  7. function mixup(input, output) {
  8. var text = document.getElementById(input).value;
  9. text = mix(text);
  10. document.getElementById(output).value = text;
  11. }
  12.  
  13. /**
  14.  * Mixes letters within words, including Unicode
  15.  *
  16.  * @param {string} text Text to mix
  17.  * @returns {string} Mixed text
  18.  */
  19. function mix(text) {
  20. text = String(text);
  21. var begin = 0;
  22. var end = 0;
  23. var wordMode = false;
  24. var sb = new StringBuilder();
  25. for( var i = 0; i < text.length; i++) {
  26. var c = text.charAt(i);
  27. var letter = isUnicodeLetter(c);
  28. var last = i == text.length - 1;
  29. if((!letter || last) && wordMode) {// first non-letter after word
  30. wordMode = false;
  31. end = last ? i + 1 : i;
  32. var wordLenght = end - begin;
  33. if(wordLenght > 3) {// word length enough to mix
  34. sb.append(mixWord(text.substring(begin + 1, end - 1)));
  35. } else if(wordLenght > 1) {
  36. sb.append(text.substring(begin + 1, end - 1));
  37. } else if(wordLenght == 1) {
  38. end++;
  39. }
  40. } else if(letter && !wordMode) {// first letter of word
  41. wordMode = true;
  42. begin = i;
  43. sb.append(text.substring(end - 1, begin + 1));// add part between last word and current word
  44. }
  45. }
  46. sb.append(text.substring(end - 1));
  47. return sb.toString();
  48. }
  49.  
  50. /**
  51.  * Mixes all letters in a word, including borders
  52.  *
  53.  * @param {string} word Word to mix, should has only Unicode letters
  54.  * @returns {string} Mixed word
  55.  */
  56. function mixWord(word) {
  57. var w = word.split('');
  58. var m = [];
  59. for( var c in w) {
  60. var i;
  61. do {
  62. i = Math.floor(w.length * Math.random());
  63. } while(m[i] != null);
  64. m[i] = w[c];
  65. }
  66. return m.join('');
  67. }
  68.  
  69. /**
  70.  * Tests if character is Unicode letter
  71.  * @param c Character to test
  72.  * @returns {Boolean} True if c is Unicode letter, false otherwise
  73.  */
  74. function isUnicodeLetter(c) {
  75. return c.toUpperCase() != c.toLowerCase();
  76. }
  77.  
  78. /**
  79.  * Class to effective string concatenation
  80.  */
  81. function StringBuilder() {
  82. this._strings = [];
  83. this.append = function(s) {
  84. this._strings.push(s);
  85. };
  86. this.toString = function() {
  87. return this._strings.join('');
  88. };
  89. }

Pobierz pliki JS i HTML – 3KB.

One Response to “Wymieszane litery”

Leave a Response