stdin-line
TypeScript icon, indicating that this package has built-in type declarations

0.0.4 • Public • Published

Motivation

This library was created after my unsuccessful attempts to participate in the Google Code Jam competition using TypeScript.

Solutions must read from standard input (stdin), but node.js doesn't provide a user friendly way to do it. Even the official examples use a lot of boilerplate to construct a state machine handling the input. This resulted in me wasting 90% of my time dealing with the input instead of solving the algorithmic problems. Because of this I created this library that allows you to read from stdin using an intuitive async/await syntax.

Installation

You can add it to your JavaScript/TypeScript project with:

$ npm i stdin-line
# or 
$ yarn add stdin-line

Usage

Create a StdinLineStream object and use the methods getLine() and getLineAsNumbers() to read from stdin. Both return promises and can be used with async/await. After you are done close the stream with close(). If you don't close the stream the application will continue listening for input and never finish.

If you are using it for Google Code Jam you will need to submit only one file. In this case the best solution is to use a tool like rollup to boundle this library with your solution and submit it as one regular JavaScript file. Rollup will require 2 other plugins (rollup-plugin-node-resolve and rollup-plugin-commonjs) to be able to put everything together. An example of a complete Google Code Jam qualification round solution can be found in the examples folder.

Example

Read 2 numbers from stdin and display the sum of them.

import { StdinLineStream } from "stdin-line";
 
(async function() {
  let inputStream = new StdinLineStream();
  let [a, b] = await inputStream.getLineAsNumbers();
  console.log(+ b);
  inputStream.close();
})();

Performance

Some competitive programming challanges can be bound by stdin read performance. I'm not sure how big the overhead of this sync/awaits approach is. This needs to be further investigated.

Readme

Keywords

Package Sidebar

Install

npm i stdin-line

Weekly Downloads

0

Version

0.0.4

License

BSD-2-Clause

Unpacked Size

4.95 kB

Total Files

6

Last publish

Collaborators

  • bkolobara