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 🙏

© 2026 – Pkg Stats / Ryan Hefner

exception-data-generator

v1.2.0

Published

generate exception data required by unit tests

Readme

异常数据生成工具

使用此工具,通过配置,来生成相应数据的各种异常状态。

举例:用户配置模板数据为:

{"a": "int|undefined,null|1"}

说明:

"a"为数据字段名,字段值分为三部分:

第一个参数"int"表明这是一个int类型的字段

第二个参数"undefined,null"表示异常值可能返回undefined,也可能是返回null(支持留空,表示永远信任此字段)

第三个参数"1",为开发自定义的合法值(int、str、bool支持多枚举值输出到正常数据)

模板参数说明:

整理了数据契约中常见的字段类型,以下是推荐配置

  • int类型:{"a": "int|undefined,null|1"} - undefined,null

  • float类型:{"b": "float|undefined,null|2.3"} - undefined,null

  • str类型:{"c": "string|undefined,null,''|'test'"} - undefined,null,""

  • bool类型:{"d": "bool|undefined,null|true"} - undefined,null

  • binary类型:{"e": "binary|undefined,null,''|合法字节流"} - undefined,null,""

  • obj类型:{"f": {...}} - 工具自动输出“undefined,null,{}”的异常,{...}为合法值

  • intArray类型:{"g": [1,2,3]] - 工具自动输出“undefined,null,[]”的异常,[1,2,3]为合法值

  • strArray类型:{"h": ['a','b','c']] - 工具自动输出“undefined,null,[]”的异常,['a','b','c']为合法值

  • objArray类型:{"i": [{...}]] - 工具自动输出“undefined,null,[]”的异常,[{...}]为合法值

模板语法更新说明:

1.0.6版本更新:

string / int / float / bool类型数据支持跳过模板语法直接配置,例如:"a": 123,效果等同于 "a":"int||123",简化配置。(但要注意,配置多个枚举值的情况还是要按照模板语法撰写)

1.1.0版本更新:

objArray类型支持多对象配置:{"i": [{...},{...},...]]

1.2.0版本更新:

getTestDataSet & getAbnormalTestCase方法支持传入options对象,初步添加trustedItemList字段,通过配置二维数组的方式,使异常数据列表不返回对应路径下对象的异常值(完全信任所配路径下的对象)

实际项目举例:

const tpl = {
  "productList": [
    {
      "productID": "int|undefined,null|123",
      "productName": "string|undefined,null,''|'testProductName'",
      "tag": ['a','b','c'],
      "starCity": "string|undefined,null,''|'Shanghai'",
      "score": "float|undefined,null|2.3",
      "salePrice": "float|undefined,null|11.1",
      "netPrice": "float|undefined,null|12.1",
      "coverImage": "string|undefined,null,''|'https://dimg04.c-ctrip.com/images/fd/activity/g4/M00/D7/C4/CggYHVaMv62AQLlYAAF2rH5eBek422_C_260_195.jpg'",
      "currency": "string|undefined,null,''|'CNY'",
      "currencyCode": "string|undefined,null,''|'CNY'",
      "brandType": "int|undefined,null|1,2", // 注意这边为多个枚举值
      "brandName": "string|undefined,null,''|'testBrandName'"
    }
  ],
  "isBst": "bool|undefined,null|true,false",
  "priceType": "int|undefined,null|1,2",
  "location": {
    "query": {
      "ssr": 0
    }
  }
}

工具暴露4个方法:

1、getTestDataSet——返回三个参数[mockSkeletonData, mockNormalDataList, mockAbnormalDataList],分别为“第一个正常值的骨架数据”、“所有正常枚举值循环出来的正常数据列表”、“所有异常枚举值循环出来的异常数据列表”。

2、getSkeletonData——第一个正常值的骨架数据

3、getNormalTestCase——所有正常枚举值循环出来的正常数据列表

4、getAbnormalTestCase——所有异常枚举值循环出来的异常数据列表

代码实例:

import {
  getTestDataSet,
  getSkeletonData,
  getNormalTestCase,
  getAbnormalTestCase,
} from 'exception-data-generator'

const [mockSkeletonData, mockNormalDataList, mockAbnormalDataList] = getTestDataSet(tpl, '数据描述', {
  trustedItemList: [
    ['location'], // location对象不返回异常
    ['location', 'query'] // location->query对象不返回异常
  ]
})
// or
const skeletonData = getSkeletonData(tpl, '数据描述')
const getNormalTestCase = getNormalTestCase(tpl, '数据描述')
const getAbnormalTestCase = getAbnormalTestCase(tpl, '数据描述', {
  trustedItemList: [
    ['location'], // location对象不返回异常
    ['location', 'query'] // location->query对象不返回异常
  ]
})