Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UserInput library for simplified user input #51541

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

oNavShaHo
Copy link

I'm submitting this pull request to enhance the user input handling in Node.js. Coming from a Java background, I found the process of taking user input in Node.js less intuitive, especially when compared to the simplicity of Scanner in Java. As a result, I've introduced a new UserInput class to make user input in Node.js more straightforward and familiar for developers transitioning from Java.

Changes:

UserInput Class:

Created a new UserInput class to encapsulate input-related functionality.
Implemented methods for obtaining integer, BigInt, boolean, and string inputs.
Validation and Error Handling:

Added validation checks to ensure input correctness.
Implemented error handling with clear error messages to guide users in case of invalid input.
Async-Await Support:

Updated the class to support asynchronous usage with async/await for a more synchronous-looking code structure.
Example Usage:

Included example code demonstrating how to use the new UserInput class for obtaining various types of input.
Motivation:

The motivation behind this enhancement is to make the process of taking user input in Node.js more accessible, especially for individuals with a background in Java. The improvements aim to simplify the user experience and promote a smoother transition for developers new to Node.js.

Impact:

These changes do not introduce breaking changes but provide an additional utility for users seeking a more convenient way to handle user input in Node.js.

Testing:

I've tested the new UserInput class extensively with various inputs, including edge cases, to ensure its reliability and correctness.

Feedback Requested:

I would appreciate feedback on the design, implementation, and usability of the UserInput class. Additionally, suggestions for any additional features or improvements are welcome.

As for the test code, you can create a separate file (e.g., test.js) to demonstrate the usage of your UserInput class. Here's an example test code for your user input class:(being a beginner this is what i did to test)
// test.js
const { createUserInput } = require('./path-to-your-promises-module');

// Test getInt
const testGetInt = async () => {
const nav = createUserInput();
try {
const a = await nav.getInt('Enter an integer: ');
console.log('getInt result:', a);
} catch (error) {
console.error('Error:', error.message);
} finally {
nav.close();
}
};

// Test getBigInt
const testGetBigInt = async () => {
const nav = createUserInput();
try {
const a = await nav.getBigInt('Enter a bigint: ');
console.log('getBigInt result:', a);
} catch (error) {
console.error('Error:', error.message);
} finally {
nav.close();
}
};

// Test getBool
const testGetBool = async () => {
const nav = createUserInput();
try {
const a = await nav.getBool('Enter a boolean (true/false): ');
console.log('getBool result:', a);
} catch (error) {
console.error('Error:', error.message);
} finally {
nav.close();
}
};

// Test getString
const testGetString = async () => {
const nav = createUserInput();
try {
const a = await nav.getString('Enter a string: ');
console.log('getString result:', a);
} catch (error) {
console.error('Error:', error.message);
} finally {
nav.close();
}
};

// Run the tests
testGetInt();
testGetBigInt();
testGetBool();
testGetString();

promises js - node - Visual Studio Code 22-01-2024 16_26_30

@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Jan 22, 2024
@marco-ippolito
Copy link
Member

Hi @oNavShaHo thanks for your contribution, unfortunately I think should be a npm package rather than something in the Node.js core

@oNavShaHo
Copy link
Author

Thank you for your insights, @marco-ippolito. As a newcomer in the open-source community, I'd appreciate your perspective on this matter. I've noticed that many competitive programming platforms, particularly those utilized for initial placement or recruitment rounds, endorse the use of Node.js. However, these platforms commonly impose restrictions on the usage of npm packages. Given the growing number of developers turning to Node.js for honing their skills in data structures and algorithms on such platforms, I believe there's a demand for a simple library. This library would streamline the process of receiving user input in a manner akin to std::cin in C++, Scanner in Java, and the input function in Python. Expressing user input in Node.js can be cumbersome, especially when compared to the simplicity in Java and Python. When competing with individuals using these languages, we often find ourselves writing an excessive amount of code for a straightforward input task.

@aduh95
Copy link
Contributor

aduh95 commented Jan 22, 2024

Note that you can already do the following:

import { createInterface } from 'node:readline/promises';

const rl = createInterface({
  input: process.stdin,
  output: process.stdout,
});

const name = await rl.question("What's your name? ");
const age = Number(await rl.question("What's your age? "));
const iq = BigInt(await rl.question("What's your IQ? "));
console.log(`Hello ${name}, you're ${age} year old and have an IQ of ${iq}!`);

async function getBool(prompt) {
  let response;
  do {
    response = await rl.question(prompt + '[Y/n] ');
  } while (!['Y', 'y', '', 'N', 'n'].includes(response));
  return response !== 'N' && response !== 'n';
}

console.log(await getBool('Was that accurate? ') ? 'Great!' : 'Oh no!');

rl.close()

I don't think it would be worth it to add yet another API that you'd have to learn rather than just doing it as above – although putting that in an npm package is a possibility like Marco said (in fact it may already exists).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants