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 🙏

© 2025 – Pkg Stats / Ryan Hefner

yi-convert

v1.0.1

Published

提供了一些常用格式如csv,json的转换和schema的数据规范化

Readme

convert

这是一个用于各种格式转换的模块,暂时提供了把json对象写成csv文件和把csv读出成为json两个方法,同时提供了schema格式化数据的方法,以后将会提供更多转换方法。 ##安装

##CLASS

  • json2csv,把json对象转换成csv文件。

引入json2csv类

var json2csv = require('yi-convert').json2csv;

初始化json对象数组

var jsonArr = new json2csv([
        {
          uid: '123456',
          name: 'avicha',
          age: 23,
          sex: 'boy'
        }, {
          uid: '654321',
          name: 'yi',
          age: '19',
          sex: 'girl'
        }
      ]);

指定从json转换为csv文件的表头

jsonArr.fields({
        'uid': '用户Id',
        'name': '名字',
        'age': '年龄',
        'sex': '性别'
      });

使用gbk编码写成csv文件

jsonArr.toCSVFile('user.csv', {encode: 'gbk'}, function(err, csvtext) {
        if (!err && csvtext) {
          console.log(csvtext);
        }
        else{
          console.error(err);
        }
    });
  • csv2json,把csv文件转换成json对象。

引入csv2json类

var csv2json = require('yi-convert').csv2json;

初始化指定csv文件路径

var goodscsv = new csv2json('../csv/goods.csv');

指定从csv转换为json对象的字段

goodscsv.fields({
        '商品Id': 'itemId',
        '名称': 'title',
        '链接': 'link',
        '销量': 'sold',
        '价格': 'price',
        '图片地址': 'picUrl',
        '分类': 'cat'
      });

使用gbk解码csv文件,把结果返回。

goodscsv.toJSON({
        headers: true,
        decode: 'gbk'
      }, function(err, jsonArr) {
        if (!err && jsonArr) {
          console.log(jsonArr);
        }
        else {
          console.error(err);
        }
      });
  • schema,根据schema的定义,规范化json的数据,通常在数据库插入数据前进行此规范化。

引入schema类

Schema = require('yi-convert').schema;

初始化Schema结构

Goods = new Schema({
    itemId: {
      type: String,
      required: true
    },
    itemPrice: {
      type: Number,
      required: true
    },
    buyNumber: {
      type: Number,
      "default": 0
    },
    data: Object
  });

运行Schema的format方法规范化json对象

goods = Goods.format({
    itemId: 123456,
    itemPrice: '12.35',
    data: {
      score: 100
    }
  });
if (goods) {
    console.log(goods1);
  }