npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zl-node-cin

v1.0.1

Published

对node的输入流进行封装,从而在控制台进行友好进行字符输入并获取输入结果

Downloads

1

Readme

zl-node-cin

模块说明: 对node的输入流进行封装,从而在控制台友好的进行字符输入与获取

说明

  1. node中的标准输入流与输出流是通过process.stdin 与 process. stdout 进行实现

  2. 对于输出,console对象的log/info等各方法已经非常对process.stdout做了格式化的封装,调用起来非常方便

  3. 但是,对于输入流,我们确还得使用类似于如下方式,相对比较麻烦。

        process.stdin.resume();//在旧模式下使用stdin必须调用process.stdin.resume()。注意如果调用了process.stdin.resume() stdin将转为旧模式。
        process.stdin.setEncoding('utf-8'); //设置输入流的编码方式为utf-8
        // 监听数据输入
        process.stdin.on('data', function (data) {
            process.stdin.emit('end'); //结束输入流
            // 获取输入结果:data
        });
        process.stdin.on('end', function () {
            process.stdin.pause(); //当输入流结束后就关闭输入流,不然后续代码会一直等待输入
        });

所以,这里我就对这个输入流做了一个简单的封装,有同步和异步两种方式

安装


  npm i zl-node-cin -S

使用示例---以同步方式写代码进行调用

let cin=require('zl-node-cin');
async function main() {
    // 以同步方式写代码进行调用(所在的外层main函数需要声明为异步async)
    process.stdout.write('请输入:'); //标准输出
    let num = await cin(); //将标准输入进行封装
    //更建议直接调用console.log(),他底层是封装的格式化的process.stdout.write
    process.stdout.write("输入结果为:" + num);
}
main(); //执行async入口函数:main

使用示例---以异步方式写代码进行调用


    let cin=require('zl-node-cin');
    // 以异步回调方式进行调用
    process.stdout.write('请输入2:'); //标准输出
    //将标准输入进行封装
    cin((data) => {
        //更建议直接调用console.log(),他底层是封装的格式化的process.stdout.write
        process.stdout.write("输入结果为:" + data);
    });