Add UserInput library for simplified user input #51541
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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();