Skip to content

Instantly share code, notes, and snippets.

@shantanuo
Forked from aquilax/index.html
Last active May 17, 2021 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shantanuo/1fdaad6a29b0ffb08f72827daca13fbc to your computer and use it in GitHub Desktop.
Save shantanuo/1fdaad6a29b0ffb08f72827daca13fbc to your computer and use it in GitHub Desktop.
Sort textarea unique
<a href="javascript:(function(){Array.from(document.querySelectorAll('textarea')).map(function(b){var a=document.createElement('div');var d=document.createElement('button');d.textContent='↑';d.addEventListener('click',function(f){f.preventDefault();b.value=Array.from(new Set(b.value.split('\n'))).sort().join('\n')});var c=document.createElement('button');c.textContent='↓';c.addEventListener('click',function(f){f.preventDefault();b.value=Array.from(new Set(b.value.split('\n'))).sort().reverse().join('\n')});a.appendChild(d);a.appendChild(c);b.parentNode.insertBefore(a,b)})})();">Sort textarea unique</a>
(function() {
Array.from(document.querySelectorAll('textarea')).map(function(ta) {
var container = document.createElement('div');
var asc = document.createElement('button');
asc.textContent = '↑';
asc.addEventListener("click", function(e) {
e.preventDefault();
ta.value = Array.from(new Set(ta.value
.split(/\n(?==.+=$)/m)
.map(section => {
const [heading, body] = section.split(/(?<==)\n/);
return `${heading}\n${body.split('\n').sort().join('\n')}`;
})
.join('\n');});
var dsc = document.createElement('button');
dsc.textContent = '↓';
dsc.addEventListener("click", function(e) {
e.preventDefault();
ta.value = Array.from(new Set(ta.value
.split(/\n(?==.+=$)/m)
.map(section => {
const [heading, body] = section.split(/(?<==)\n/);
return `${heading}\n${body.split('\n').sort().reverse().join('\n')}`;
})
.join('\n');});
container.appendChild(asc);
container.appendChild(dsc);
ta.parentNode.insertBefore(container, ta);
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment