Skip to content

shyok21/ezinput

Repository files navigation

ezinput

Node.js CI

Minimal and beginner-friendly CLI input utility for JavaScript.

npm version npm downloads License

Now supports structured inputs (JSON, arrays, objects) — added in v1.1.0


Installation

npm install ezinput

Usage (v1.x - CP style batch input)

In examples/sum.js:

const input = require('ezinput')(); // default: reads from stdin

const t = input.int();
for (let i = 0; i < t; i++) {
  const [a, b] = input.ints();
  console.log(`Sum of ${a} + ${b}:`, a + b);
}

Examples

You can try out working examples inside the examples/ folder.

Run with pre-supplied input:

Runs the script with redirected input from a sample file.

npm run example

Run with manual input:

Prompts user to type values directly into the terminal.

npm run example-cmd
>>> 3
>>> 5 10
>>> 15 20
>>> 25 30

and press Ctrl+D to end input.

Supported Methods

input.int()           // Single integer
input.float()         // Single float
input.ints()          // Space-separated numbers (ints or floats)
input.strings()       // Space-separated strings
input.line()          // Raw line
input.lines(n)        // Next n lines (string[])
input.numbers(n)      // n lines of number arrays (2D array)
// v1.1.0 additions
input.json()          // Parses JSON array or object (single or multiline)
input.array()         // Parses array from JSON input
input.object()        // Parses object from JSON input

Upcoming Features

v1.2.0 — Interactive Live Mode (Readline)

  • CP-style stdin is good, but for scripting we’ll support:

    const input = require('ezinput').interactive();
    await input.ask('Enter number: ')
  • Works like real-time prompt() or readline.question().

  • input.fromEditor() → Temporary input via in-editor (like vim)

v1.3.0 — Hybrid Input Factory

  • Smart factory: choose mode based on config flag:
    const input = require('ezinput')({ mode: 'interactive' }); // or 'batch'
  • No change to API. Internally switches between readline and fs.

Tests

npm test

We use jest and fs.readFileSync mocks for testing stdin input.

Dev Notes

  • Singleton pattern (like require('ezinput')()) for consistent state
  • Reset via: require('ezinput').reset(source) to reinitialize input
  • Supports both file and stdin as input source

Inspiration

Built to fix JavaScript’s awkward input handling during CP and scripts. This utility mimics the ease of:

  • cin >> in C++
  • input() in Python
  • Scanner.nextInt() in Java

License

MIT

About

Simple and readable command-line input utility for Node.js.

Resources

License

Stars

Watchers

Forks

Packages

No packages published