x-platform-io-js
v1.2.4
Published
Script to easily switch Node.js I/O to web
Readme
X Platform IO JS
Script to easily switch Node.js I/O to web
Installation
Install the script:
npm i x-platform-io-jsOr download the script if you want to use npm:
wget -O x-platform-IO.js https://gitlab.com/sgb004/x-platform-io-js/-/raw/main/x-platform-IO.jsUsage
If you code changes between Node.js and web, you can use this script to switch I/O to web.
import { prompt, alert, closeReadLine } from './x-platform-IO.js';Where:
promptis a function that returns a promise that resolves to the user input.alertis a function that displays a message in the console.- The first argument is the message.
- The second argument is the type of the message.
logis the default type.errorwarnorwarninginfodebug
closeReadLineis a function that closes the readline interface.
Example:
import { prompt, alert, closeReadLine } from './x-platform-IO.js';
alert('Welcome to the calculator!', 'info');
const num1 = parseInt(await prompt('Enter first number: '));
const num2 = parseInt(await prompt('Enter second number: '));
if (isNaN(num1) || isNaN(num2)) {
alert('Invalid input!', 'error');
} else {
alert(`Result: ${num1 + num2}`, 'log');
}
closeReadLine();If you want to use the script in a Node.js project, you can use it like this:
import terminal from './x-platform-IO.js';
terminal.out('Welcome to the calculator!', 'info');
const num1 = parseInt(await terminal.in('Enter first number: '));
const num2 = parseInt(await terminal.in('Enter second number: '));
if (isNaN(num1) || isNaN(num2)) {
terminal.out('Invalid input!', 'error');
} else {
terminal.out(`Result: ${num1 + num2}`, 'log');
}
terminal.closeIO();The methods terminal.in, terminal.out and terminal.closeIO work the same way as the methods prompt, alert and closeReadLine in the example above.
Aditionally, you can use the next methods:
terminal.outErrorworks the same asalert(message, 'error')terminal.outWarningorterminal.outWarnworks the same asalert(message, 'warn')terminal.outSuccessworks the same asalert(message, 'success')terminal.outInfoworks the same asalert(message, 'info')terminal.outDebugworks the same asalert(message, 'debug')
