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

checkjs-tool

v0.0.3

Published

用于检查各种条件是否符合预期

Downloads

2

Readme

checkjs

用法

用于检查各种条件是否符合预期

默认可以检查的项,也可以自己扩展
isString: '非字符串',
isFunction: '非函数',
isNumber: '非数字',
isPlainObject: '非对象',
isEmptyObject: '非空对象'	,
isArray: '非数组',
isEmptyArray: '空数组',
inArray: '数组没有此元素',
inObject: '对象没有此属性或方法',
isFileType: '不是此类型的文件',
isDir: '目标路径不是文件夹',
isFile: '目标路径不是文件',
isEmptyDirectory: '目标路径为非空目录',
hasFile: '目标路径下文件不存在',
hasDir: '目标路径下文件夹不存在',
hasDirectory: '目标路径不存在'
所有参数都是可选参数
var Check = require('./check');

var check = new Check(
	[
	    {
	        key: 5,
	        name: {
	            isString: true
	        }
	    },
		{
			key: {name:'test object'},
			opt: 'name',
			name: {
				inObject: true
			}
			
		},
		{
			key: ["xml", "html", "css", "js"],
			opt: 'jade',
			name: {
				inArray: true
			}
		},
		{
			key: false,
			name: {
				isBoolean: true
			}
		},		
		{
			key: 'home.css',
			opt: 'css',
			name: {
				isFileType: true
			}
		},
		{
			key: '/Users/haixialiu/Documents/tx工作/nodejs/test',
			name: {
				isDir: true
			}
		},
		{
			key: path,
			opt: 'srcc',
			name: {
				hasDir: true
			}
		},
		{
			key: path,
			opt: 'uzconfig.js',
			opt2: {
				deep: true, //默认false:当前目录查找,true:深度遍历向下查找
				direct: true  //默认false,true:深度遍历向上查找
			},
			name: {
				hasFile: true
			}
		},		
        {
			key: '12344',
			custom : {
				is:'isCustom',
				cb: function(obj){
						return obj == '12344';
					}
			},
			name: {
				isCustom: true
			}
		},	
	], 
	{
		isCustom: 'isCustom'
	}, 
	{
		isCustom: '不等于默认值'
	}
);

//检查结果
console.log(check.result)
{ code: '1',
  msg: 
   [ { index: 0, check: 'isString', key: 5, isString: '非字符串' },
     { index: 2, check: 'inArray', key: [Object], inArray: '数组没有此元素' },
     { index: 6,
       check: 'hasDir',
       key: '/Users/haixialiu/Documents/tx工作/nodejs/test',
       hasDir: '目标路径下文件夹不存在' } 
   ] 
}

===============================================================================================

var check = new Check(
	[
	    {
	        key: 'test string',
	        name: {
	            isString: true
	        }
	    },
		{
			key: {name:'test object'},
			opt: 'name',
			name: {
				inObject: true
			}
			
		},
		{
			key: ["xml", "html", "css", "js"],
			opt: 'css',
			name: {
				inArray: true
			}
		},
		{
			key: 'home.css',
			opt: 'css',
			name: {
				isFileType: true
			}
		},
		{
			key: '/Users/haixialiu/Documents/tx工作/nodejs/test',
			name: {
				isDir: true
			}
		}	
	]
);

//检查结果
console.log(check.result)
{ code: '0' } //全部通过检查
     
需要检查的所有项作为参数放到数组中
[
	    {
	        key: str, //检查的内容可以是常规数据类型/路径/文件/文件夹/自定义内容
	        name: {
	            isString: true
	        }
	    }
]	    
如果自定义检查项需要配置参数 @custom,还需要增加map属性,errMsg错误提示,直接作为函数的第二、第三个参数;
如果增加了custom属性,但是未配置map errMsg也不会做任何检查。
[
		{
			key: '12344',
			custom : {
				is:'isCustom',
				cb: function(obj){
						return obj == '123';
					}
			},
			name: {
				isCustom: true
			}
		},
		{
			isCustom: 'isCustom'
		}, 
		{
			isCustom: '不等于默认值'
		}		
]