Jump to content

Input loop: Difference between revisions

Added NodeJS and Typescript
(Added NodeJS and Typescript)
Line 2,288:
<lang nim>for line in "input.text".lines:
discard # process line</lang>
 
=={{header|NodeJS}}==
<lang nodejs>
#!/usr/bin/env node
 
const EventEmitter = require('events');
 
function stdinLineByLine() {
const stdin = new EventEmitter();
let buff = '';
 
process.stdin
.on('data', data => {
buff += data;
lines = buff.split(/\r\n|\n/);
buff = lines.pop();
lines.forEach(line => stdin.emit('line', line));
})
.on('end', () => {
if (buff.length > 0) stdin.emit('line', buff);
});
 
return stdin;
}
 
const stdin = stdinLineByLine();
stdin.on('line', console.log);
</lang>
 
=={{header|Oberon-2}}==
Line 2,864 ⟶ 2,892:
ENDLOOP
ENDACCESS source
</lang>
 
=={{header|TypeScript}}==
<lang typescript>
#!/usr/bin/env node
 
import EventEmitter from 'events';
 
function stdinLineByLine() {
const stdin = new EventEmitter();
let buff = '';
let lines;
 
process.stdin
.on('data', (data) => {
buff += data;
lines = buff.split(/\r\n|\n/);
buff = lines.pop();
lines.forEach((line) => stdin.emit('line', line));
})
.on('end', () => {
if (buff.length > 0) stdin.emit('line', buff);
});
 
return stdin;
}
 
const stdin = stdinLineByLine();
stdin.on('line', console.log);
 
</lang>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.