Minimal and beginner-friendly CLI input utility for JavaScript.
Now supports structured inputs (JSON, arrays, objects) — added in v1.1.0
npm install ezinput
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);
}
You can try out working examples inside the examples/ folder.
Runs the script with redirected input from a sample file.
npm run example
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.
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
-
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()
orreadline.question()
. -
input.fromEditor()
→ Temporary input via in-editor (likevim
)
- 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.
npm test
We use jest
and fs.readFileSync
mocks for testing stdin input.
- 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
Built to fix JavaScript’s awkward input handling during CP and scripts. This utility mimics the ease of:
cin >>
in C++input()
in PythonScanner.nextInt()
in Java
MIT