You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.8 KiB

  1. function indent_decode() {
  2. var custom_indent: string = (<HTMLInputElement>document.getElementById("cust_indent")).value;
  3. var result: string = descriptiveCounter(custom_indent);
  4. document.getElementById("indent_s").innerHTML = result;
  5. }
  6. export function descriptiveCounter(input: string): string {
  7. input = input.replace(/\\t/g, " ");
  8. var tokens: Array<string> = input.split("");
  9. var result = "";
  10. var repeatedCharCount = 0;
  11. for (var i = 0; i < tokens.length; i++) {
  12. var char = input.substr(i, 1);
  13. if (char == input.substr(i + 1, 1)) {
  14. repeatedCharCount++;
  15. } else {
  16. switch (char) {
  17. case " ":
  18. char = "blankspace";
  19. break;
  20. case "\t":
  21. char = "tab";
  22. break;
  23. default:
  24. char = "'" + char + "'";
  25. }
  26. repeatedCharCount = repeatedCharCount > 8 ? 8 : repeatedCharCount;
  27. if (repeatedCharCount > 0) {
  28. char += "s";
  29. }
  30. result += getCountText(repeatedCharCount, char);
  31. repeatedCharCount = 0;
  32. }
  33. }
  34. if (result.length < 0) {
  35. switch (char) {
  36. case " ":
  37. char = "blankspace";
  38. break;
  39. case "\t":
  40. char = "tab";
  41. }
  42. repeatedCharCount = repeatedCharCount > 8 ? 8 : repeatedCharCount;
  43. result = getCountText(repeatedCharCount, char);
  44. }
  45. result = result.replace(/^ & /, "")
  46. return result;
  47. }
  48. function getCountText(count: number, char: string): string {
  49. const dict = ["one", "two", "three", "four", "five", "six", "seven", "eight", "many"];
  50. const ampersand = " & ";
  51. return ampersand + dict[count] + " " + char;
  52. }