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

random-text

v1.0.0

Published

Generate random text using a given context-free grammar

Downloads

6

Readme

Random-text

使用给定的上下文无关文法生成随机文本。

用法

基本用法

var rand = randtxt.loader().load({
    'start': '<sentence>。',
    'sentence': '<subject>在<place><verb>',
    'subject': [
        '张三',
        '李四',
        '@3 王五'
    ],
    'place': [
        '学校',
        '图书馆',
        '火车站'
    ],
    'verb': [
        '看书',
        '喝水',
        '做作业'
    ]
}).done();

console.log(rand());

randtxt.loader()用于创建一个加载器对象。load()方法用于加载文法对象,可分成多个对象分开加载。done()方法完成加载过程,并返回一个函数,调用该函数即返回随机生成的文本。loader()函数包含一个类型为js对象可选的参数,用于传递选项的值。该选项对象包含的字段有:

|字段名|作用|默认值| |:-----|:---|:-----| |escapeChar|指定文法规则中的转义字符|"%"| |random|随机函数,生成一个0到指定范围内的随机数|range => Math.random() * range| |startKey|起始的非终结符|"start"|

文法对象

文法对象是一个以非终结符为键的js对象,值则是该非终结符能推导出的规则,一个规则用一个字符串表示,多个规则时用数组。文法必须包含一个起始的非终结符,默认是start。在每条规则中,尖括号<>括起来的表示非终结符,其余则是终结符。转义字符默认是%,即%<%>%%%@分别表示字符<>%%

每条规则的权重可以在规则的开头用@加数字的方式指定,默认值是1。如果只写了@而不加数字那么该规则的权重就是它包含的各个非终结符对应的规则的权重之和的积。

许可

MIT.