Skip to content

Instantly share code, notes, and snippets.

@juliantcook
Created October 1, 2021 11:05
Show Gist options
  • Select an option

  • Save juliantcook/d49c61a9ee4e8423822b98b3a3ee0dbe to your computer and use it in GitHub Desktop.

Select an option

Save juliantcook/d49c61a9ee4e8423822b98b3a3ee0dbe to your computer and use it in GitHub Desktop.
Remove consecutive spaces. Usage to oneline formatted json: `pbpaste | oneline.js | remove-space.js | pbcopy`
#!/usr/bin/env node
/**
* remove more than one consecutive space
*/
const readable = process.stdin;
readable.on('readable', () => {
let chunk;
let prevCharSpace = false;
while (null !== (chunk = readable.read())) {
for (const char of `${chunk}`) {
if (!(char === ' ' && prevCharSpace)) {
process.stdout.write(char);
}
prevCharSpace = char === ' ';
}
}
});
readable.on('end', () => {
process.stdout.write('\n');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment