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

yingyya-cpt

v1.0.4

Published

Simple Command system

Downloads

12

Readme

Simple command system (简易命令系统)

一个简易的命令系统,支持自定义参数类型,自定义参数规则。

这件事应该从 2023-08-30 说起,我看见了某个机器人的触发方式,类似于 Minecraft 的命令系统,但是她是使用 正则表达式(Regular expression) 实现的,有那么一点点不符合逻辑(我看不顺眼),所以就产出了这个项目。

其他语言将在”近期“陆续发布。

使用

安装

使用 npm

npm install yingyya-cpt

使用 yarn

yarn add yingyya-cpt

代码

index.ts

import Cpt from 'yingyya-cpt';
import MyCustomParam from './MyCustomParam';

// 注册自定义类型 自定义类型必须优先注册
Cpt.CommandFactory.registerParamType(MyCustomParam);
//                                                       内置类型-string                 内置类型-number             自定义类型-my
// 内置类型有 string number boolean
// 注册命令
Cpt.CommandFactory.register('/music search song [keyword:string,regex=/.+/,len=2@3] [num:number,range=1@3] [t:my,append=abdc]', (ctx) => {
	console.log(`/music search song触发`);
	// 获取参数的内容
	// input.${参数键}
	console.log(`keyword: ${ctx.get('input.keyword')}`);
	console.log(`num: ${ctx.get('input.num')}`);
	console.log(`t: ${ctx.get('input.t')}`);
	console.log('===============================');
	console.log((ctx.get('my-ctx-prop') as Date)?.toLocaleString());
});
// 触发命令
Cpt.CommandFactory.commit('/music search song "aaa" 1 a');
console.log('----------------------------------------');
// 自定义命令上下文
const context = new Cpt.CommandContext();
context.set('my-ctx-prop', new Date());
Cpt.CommandFactory.commit('/music search song "aaa" 1 a', context);

MyCustomParam.ts

import Cpt from 'yingyya-cpt';

class MyParamType extends Cpt.CommandParamType {
	constructor() {
		super();
		// 填写参数类型时的内容
		// [KEY:my]
		this.type = 'my';
	}

	// 额外规则验证
	// [KEY:my,append=aa]
	propValidate(key: string, value: string): boolean {
		if (key === 'append') {
			if (value.length === 0) {
				throw new ParamPropException(this.type, key, `需要正确的追加文字。`);
			}
		}
		return true;
	}

	// 支持的额外规则列表
	// 参数声明中的额外规则必须在此处声明
	props(): string[] {
		return [
			'append'
		];
	}

	// 转换成内部类型
    // 如字符串"true"转成true
	toValue(value: any): any {
		return value;
	}

	// 参数验证
    // value为参数值
    // params为当前参数对象
	validate(value: any, params?: CommandParam): boolean {
		return (value as string).length !== 0;
	}
}