Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html><head><meta charset="utf-8">
- <title>LIST WRANGLER: sort -=- merge and de-duplicate -=- find unique lines</title>
- <style>*{font-size:11px}textarea{background-color:#bbb;height:98%;width:98%}</style>
- </head><body><div><!-- usage instructions? naw! --><hr>
- <div style="float:left;width:32%;height:64%">
- <input type="button" id="but1" value="sort list A" onclick="sorta()"/>
- will trim() then cull any duplicate or empty lines
- <br/><textarea id="mybox1"></textarea>
- </div><div style="float:left;width:32%;height:64%">
- <input type="button" id="but2c" value="A vs B: find common lines" onclick="comm2()"/>
- <input type="button" id="but2d"value="A vs B: find different (unique) lines" onclick="diff2()"/>
- <br/><textarea id="mybox2"></textarea>
- </div><div style="float:left;width:32%;height:64%"><span id="tell3"></span><br/>
- <textarea id="mybox3" disabled>output displays here</textarea></div>
- <div id="sidez" style="width:100%;height:100%;visibility:hidden"><br/>
- <div style="float:left;width:48%;height:32%;margin-top:8px">
- ------ lines unique to list A ------<br><textarea id="tha"></textarea></div>
- <div style="float:left;width:48%;height:32%;margin-top:8px">
- ------ lines unique to list B ------<br><textarea id="thb"></textarea></div></div>
- <script>box1 = document.getElementById("mybox1");
- box2 = document.getElementById("mybox2");
- box3 = document.getElementById("mybox3");
- bt1 = document.getElementById("but1");
- bt2c = document.getElementById("but2c");
- bt2d = document.getElementById("but2d");
- btxt3 = document.getElementById("tell3");
- var wantsep = false;
- dside = document.getElementById("sidez");
- dog1 = document.getElementById("tha");
- dog2 = document.getElementById("thb");
- function nixDupes(bob) {
- var i, hoo=bob.length, blip, uneeks=[];
- for (i = 0;i<hoo;i++){
- blip = bob[i].trim();
- if (uneeks.indexOf(blip) == -1 && blip.length >=1)uneeks.push(blip);
- }
- return uneeks;
- }
- function sorta(){
- // design decision ~~ don't disturb sidebyside divs visibility here.
- // ( I often want to paste earlier output and edit/sort it again )
- sift1 = box1.value;
- if (sift1.trim() == ""){alert('box for List A is empty !');return true;}
- sift1 = sift1.split("\n");
- if (sift1.length>49999) alert('click OK... and be patient!');
- // ------ tested: list with 102k lines (4.3Mb) ~= 4 seconds
- sift1 = nixDupes(sift1);
- sift1 = sift1.sort();
- btxt3.innerHTML = "displaying sorted List A contents";
- box3.disabled = false;
- box3.value = sift1.join('\n');
- }
- function comm2(){
- var i, j, sect = [];
- dside.style.visibility ="hidden";
- sift1 = box1.value;
- if (sift1.trim() == ""){alert('box1 (List A) is empty !');return true;}
- sift2 = box2.value;
- if (sift2.trim() == ""){alert('box2 (List B) is empty !');return true;}
- box3.disabled = false;
- sift1 = sift1.split("\n");
- sift2 = sift2.split("\n");
- if ((sift1.length + sift2.length) > 19999) alert('click OK... and be patient!');
- // ------ tested: 2 lists (102k lines, 4.3Mb each) ~= 14 seconds
- // yes, an Array.prototype iterator function would be more efficient...
- // developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
- sift1 = nixDupes(sift1);
- sift2 = nixDupes(sift2);
- for (i in sift1) {
- for (j in sift2)
- if (sift1[i] == sift2[j]) sect.push(sift1[i]);
- }
- if (!sect.length){box3.value="---none---";btxt3.innerHTML='';return true;}
- sect = sect.sort();
- btxt3.innerHTML = "AvsB ~~ merged list: lines common to A and B";
- box3.value = sect.join('\n');
- }
- function diff2(){
- var i, k, m, tmp1 = [], tmp2 = [], neeks = [];
- sift1 = box1.value;
- if (sift1.trim() == ""){alert('box1 (List A) is empty !');return true;}
- sift2 = box2.value;
- if (sift2.trim() == ""){alert('box2 (List B) is empty !');return true;}
- box3.disabled = false;
- wantsep = confirm('Display separate sets of unique A/B lines, side-by-side?');
- sift1 = sift1.split("\n");
- sift2 = sift2.split("\n");
- if ((sift1.length + sift2.length) > 29999) alert('click OK... and be patient!');
- // ------ tested: 2 lists (102k lines, 4.3Mb each) ~= 9 seconds
- sift1 = nixDupes(sift1);
- sift2 = nixDupes(sift2);
- k = sift1.length;
- m = sift2.length;
- for (i=0;i< k; i++)
- if (sift2.indexOf(sift1[i]) == -1){tmp1.push(sift1[i]);}
- for (i=0;i< m; i++)
- if (sift1.indexOf(sift2[i]) == -1){tmp2.push(sift2[i]);}
- if(tmp1.length==0 && tmp2.length==0){box3.value="---none---";btxt3.value='';return true;}
- if(wantsep === true){
- dside.style.visibility ="visible";
- dog1.value = tmp1.join('\n');
- dog2.value = tmp2.join('\n');
- box3.value = '----- output diverted to breakout boxes, below ---\n\n'
- + 'To view the merged result here, instead:\n '
- + ' click \"find different\" button again and choose \"Cancel\"';
- } else {
- tmp1.concat(tmp2); tmp2 = [];
- dside.style.visibility ="hidden";
- btxt3.innerHTML = "AvsB ~~ merged list of (trimmed,non-empty) UNIQUE lines";
- box3.value = tmp1.join('\n');
- }
- }
- onload = function(){box1.focus();
- box2.value = "(optional) paste list B here\n\n ...or use as a scratchpad while tweaking List A"}
- box2.onclick = function(){if(box2.value.indexOf('(optional) paste list B')>-1)box2.value=''}
- </script>
- </div></body></html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement