Last active
June 19, 2025 20:06
-
-
Save libraplanet/69514b3318241d465672c060f43e45e2 to your computer and use it in GitHub Desktop.
sort and unique for lines from standard input.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function readStdinToLinsePromise() { | |
return new Promise(function(resolve, reject) { | |
const lines = []; | |
const reader = require('readline').createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
terminal: false, | |
}); | |
reader.on('line', function(line) { | |
lines.push(line); | |
}); | |
reader.on('close', function() { | |
resolve(lines); | |
}); | |
reader.on('error', function(error) { | |
reject(error); | |
}); | |
}); | |
} | |
(async function () { | |
if(process.stdin.isTTY) { | |
// is terminal. (nop) | |
} else { | |
// is not terminal. | |
let lines = await readStdinToLinsePromise(); | |
lines = [...lines].sort(); | |
lines = [...new Set(lines)]; | |
lines.forEach(function(line) { | |
console.info(line); | |
}); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment