Advertisement
Faguss

Untitled

Apr 9th, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta http-equiv="x-ua-compatible" content="ie=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1" />
  7. <title>Storage sorting</title>
  8. </head>
  9.  
  10. <body>
  11. <textarea id="input"></textarea>
  12. <h2>1</h2><div id="1"></div>
  13. <h2>2</h2><div id="2"></div>
  14. <h2>3</h2><div id="3"></div>
  15. <div id="nonmatching"></div>
  16.  
  17. <script>
  18. let input = document.getElementById("input");
  19. input.addEventListener("input", function(e){
  20. sortNumbersToColumns(e.target.value);
  21. });
  22.  
  23. document.addEventListener("DOMContentLoaded", function(event) {
  24. sortNumbersToColumns(input.value);
  25. });
  26.  
  27. function sortNumbersToColumns(input_string) {
  28. let storage_pattern = [
  29. //prefix, suffix, start, end, column, shelf
  30. ];
  31.  
  32. let input = [];
  33. let output = {
  34. "1":{},
  35. "2":{},
  36. "3":{}
  37. };
  38. let non_matching = [];
  39.  
  40. function is_numeric(str) {
  41. if (typeof str != "string") return false
  42. return !isNaN(str) && !isNaN(parseFloat(str))
  43. }
  44.  
  45. input_string.split(/\r?\n/).forEach(function(item, index, array) {
  46. item = item.replace(/\s/g, '');
  47. if (item.length > 0) {
  48. let new_item = ["", "", ""];
  49. let cut = item.length;
  50.  
  51. for(let i=0; i<=3 && i<item.length; i++) {
  52. //console.log(item[i] + ": " + (is_numeric(item[i])));
  53. cut = i;
  54.  
  55. if (is_numeric(item[i]))
  56. break;
  57. }
  58.  
  59. new_item[0] = item.slice(0,cut);
  60. new_item[1] = item.slice(cut);
  61. cut = new_item[1].length;
  62.  
  63. for(let i=new_item[1].length-1; i>=0; i--) {
  64. //console.log(new_item[1][i] + ": " + (is_numeric(new_item[1][i])));
  65. if (!is_numeric(new_item[1][i]))
  66. cut = i;
  67. }
  68.  
  69. if (cut != new_item[1].length) {
  70. new_item[2] = new_item[1].slice(cut);
  71. new_item[1] = new_item[1].slice(0,cut);
  72. }
  73.  
  74. input.push(new_item);
  75. //console.log(new_item);
  76. }
  77. });
  78.  
  79. input.forEach((registration_number) => {
  80. let matched = false;
  81. for(let i=0; i<storage_pattern.length && !matched; i++) {
  82. if (
  83. (!storage_pattern[i].hasOwnProperty("prefix") || registration_number[0]==storage_pattern[i].prefix)
  84. &&
  85. (!storage_pattern[i].hasOwnProperty("suffix") || registration_number[2]==storage_pattern[i].suffix)
  86. &&
  87. (!storage_pattern[i].hasOwnProperty("start") || is_numeric(registration_number[1]) && parseInt(registration_number[1]) >= storage_pattern[i].start)
  88. &&
  89. (!storage_pattern[i].hasOwnProperty("end") || is_numeric(registration_number[1]) && parseInt(registration_number[1]) <= storage_pattern[i].end)
  90. ) {
  91. if (!output[storage_pattern[i].column].hasOwnProperty(storage_pattern[i].shelf)) {
  92. output[storage_pattern[i].column][storage_pattern[i].shelf] = [];
  93. }
  94.  
  95. output[storage_pattern[i].column][storage_pattern[i].shelf].push(registration_number.join(""));
  96. matched = true;
  97. }
  98. }
  99.  
  100. if (!matched) {
  101. non_matching.push(registration_number.join(""));
  102. }
  103. });
  104.  
  105. Object.keys(output).forEach(column => {
  106. document.getElementById(column).innerHTML = "";
  107.  
  108. Object.keys(output[column]).forEach(shelf => {
  109. output[column][shelf].sort();
  110. document.getElementById(column).innerHTML += "<b>"+shelf+"</b>: " + output[column][shelf].join(", ") + "<br>";
  111. });
  112. });
  113.  
  114. if (non_matching.length > 0) {
  115. document.getElementById("nonmatching").innerHTML = "<h2>?</h2>" + non_matching.join("<br>");
  116. } else
  117. if (document.getElementById("nonmatching").children.length > 0) {
  118. document.getElementById("nonmatching").firstChild.remove();
  119. document.getElementById("nonmatching").innerHTML = "";
  120. }
  121.  
  122. //console.log(output);
  123. }
  124. </script>
  125. </body>
  126. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement