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

fd-param-checker

v1.0.2

Published

用于参数检查的工具(a tool which is used for checking params)

Downloads

5

Readme

简介 (Introduce)

fd-param-checker是一个用于参数检测的工具包,致力于快速完成参数检测条件的约束

安装 (Install)

$ npm install fd-param-checker

使用 (Usage)

引入模块

const checker = require('fd-param-checker');

链式操作

使用new checker()构造一个参数检查对象,之后调用对应检查方法。

注意

链式操作末尾必须使用done()方法进行收尾操作。

only约束

only*限制为某一类型。

function func(argv) {
    let param = new checker(argv).onlyNumber().done();      // 约束参数argv仅为Number类型,返回值保存到param对象中
}

only*包含的约束

| 方法名 | 参数 | 说明 | | --------------- | ---- | ---- | | onlyNumber() | - | - | | onlyString() | - | - | | onlyUndefined() | - | - | | onlyNull() | - | - | | onlyBoolean() | - | - | | onlyArray() | - | - |

is约束

is*声明可以为某一类型。

function func(argv) {
    let param = new checker(argv).isNumber().done()      // 约束参数argv可以为Number类型(不为Number类型则会抛出异常)
}

约束变量时可以使用多个is*方法进行约束,表示该变量可以是多个约束条件中的一个。

function func(argv) {
    let param = new checker(argv).isNumber().isString().dome()      // 约束参数argv可以为Number或者String类型
}

is*包含的约束

| 方法名 | 参数 | 说明 | | ------------- | ---- | ----------------------------------------- | | isNumber() | - | - | | isString() | - | - | | isUndefined() | - | - | | isNull() | - | - | | isBoolean() | - | - | | isArray() | - | - | | isEmpty() | - | empty包括:```, undefined, null, [], {}`` |

注意 1.isEmpty()允许变量可以是“空”变量,包括:'', undefined, null, [], {}

not约束

not*声明排除某一类型。

function func(argv) {
    let param = new checker(argv).notBoolean().notUndefined().notNull().done();         // 约束参数argv不能为Boolean,Undefined,Null类型
}

not*包含的约束

| 方法名 | 参数 | 说明 | | -------------- | ---- | ----------------------------------------- | | notNumber() | - | - | | notString() | - | - | | notUndefined() | - | - | | notNull() | - | - | | notBoolean() | - | - | | notArray() | - | - | | notEmpty() | - | empty包括:```, undefined, null, [], {}`` |

limitLen约束

limitLen()限制参数长度。注意:limitLen()方法仅对Number(隐式转换为String,检查Number的字符长度)、String、Array类型有效。

    function func(argv) {
        let param = new checker(argv).isNumber().isString().isArray().limitLen(9).done();       // 约束参数可以是Number,String,Array类型,且长度为[0,9]
    }

limitLen参数列表

function limitLen(max, min=0) {
    /* 内部实现代码 */
}

| 参数名 | 必须 | 默认值 | | ------ | ---- | ------ | | max | 是 | - | | min | 否 | 0 |

limitNum约束

limitNum()约束Number类型的数值范围。

注意

1.因为涉及到安全数的概念,所以如果数值超过安全数范围(Number.MIN_SAFE_INTEGER~Number.MAX_SAFE_INTEGER)的话,则会失去比较意义。

2.该取值范围为闭区间,如果目标值等于参数也是属于允许范围的。

function func(argv) {
    let param = new checker(argv).limitNum(9, 5).done();
}

limitNum参数列表

function limitNum(max, min) {
    /* 内部实现代码 */
}

|参数名|必须|默认值| |-|-|-| |max|是|-| |min|是|-|

错误处理

如果参数不满足约束条件,则会在链式操作中抛出异常。目前的错误处理只能在链式操作之外使用try...catch

function func(argv) {
    let param;
    try {
        param = new checker(argv).isNumber().isString().limitLen(5).done();
    }
    catch(e) {
        console.error(e);
    }
}