项目作者: ndaidong

项目描述 :
Util for generating random sentences, paragraphs and articles in English
高级语言: JavaScript
项目地址: git://github.com/ndaidong/txtgen.git
创建时间: 2016-05-27T10:29:33Z
项目社区:https://github.com/ndaidong/txtgen

开源协议:MIT License

下载


txtgen

Lightweight util for generating random sentences, paragraphs and articles in
English. Inspired by Sentencer and
metaphorpsum.com.

CodeQL
CI test
Coverage Status
@ndaidong/txtgen">NPM
@ndaidong/txtgen"">JSR

Demo

Setup & Usage

Deno

@ndaidong/txtgen"">https://jsr.io/@ndaidong/txtgen

  1. deno add @ndaidong/txtgen
  2. # npm (use any of npx, yarn dlx, pnpm dlx, or bunx)
  3. npx jsr add @ndaidong/txtgen
  1. import { sentence } from "@ndaidong/txtgen";
  2. for (let i = 0; i < 5; i++) {
  3. console.log(sentence());
  4. }

You can use JSR packages without an install step using jsr: specifiers:

  1. import { sentence } from "jsr:@ndaidong/txtgen";
  2. for (let i = 0; i < 5; i++) {
  3. console.log(sentence());
  4. }

You can also use npm: specifiers as before:

  1. import { sentence } from "npm:@ndaidong/txtgen";
  2. for (let i = 0; i < 5; i++) {
  3. console.log(sentence());
  4. }

Or import from esm.sh

  1. import { sentence } from "https://esm.sh/@ndaidong/txtgen";
  2. for (let i = 0; i < 5; i++) {
  3. console.log(sentence());
  4. }

Node.js & Bun

@ndaidong/txtgen"">https://www.npmjs.com/package/@ndaidong/txtgen

  1. npm i @ndaidong/txtgen
  2. # pnpm
  3. pnpm i @ndaidong/txtgen
  4. # yarn
  5. yarn add @ndaidong/txtgen
  6. # bun
  7. bun add @ndaidong/txtgen
  1. import { sentence } from "@ndaidong/txtgen";
  2. for (let i = 0; i < 5; i++) {
  3. console.log(sentence());
  4. }

You can also use CJS style:

  1. const { sentence } = require("@ndaidong/txtgen");
  2. for (let i = 0; i < 5; i++) {
  3. console.log(sentence());
  4. }

Browsers:

  1. <script type="module">
  2. import { sentence } from "https://esm.sh/@ndaidong/txtgen";
  3. // import { sentence } from 'https://unpkg.com/@ndaidong/txtgen/esm/mod.js';
  4. for (let i = 0; i < 5; i++) {
  5. console.log(sentence());
  6. }
  7. </script>

APIs

  • .sentence()
  • .paragraph([Number totalSentences])
  • .article([Number totalParagraphs])
  • .addNouns(Array nouns)
  • .addAdjectives(Array adjectives)
  • .addTemplates(Array sentenceTemplates)
  • .setNouns(Array nouns)
  • .setAdjectives(Array adjectives)
  • .setTemplates(Array sentenceTemplates)
  • .getNouns()
  • .getAdjectives()
  • .getTemplates()
  • .lorem([Number min [, Number max]])

As their name suggests, we have 4 groups of methods:

  • sentence(), paragraph(), article(): generate text by given grammatical
    unit
  • addNouns(), addAdjectives(), addTemplates(): add more samples to current
    sample set
  • setNouns(), setAdjectives(), setTemplates(): replace current sample set
    with new ones
  • getNouns(), getAdjectives(), getTemplates(): get current sample set

The set* and get* methods were added in v2.2.3 to help you customize your
sample data.

In addition, we’ve added lorem() method since v3.0.5 to generate lorem ipsum
text.

Template

If you want to add more kinds of sentences, just use the .addTemplates()
method; it expects a list of sentence templates. Each sentence template is an
English sentence, containing placeholders that can be replaced with any
alternative word.

For example:

  1. import { addTemplates } from "@ndaidong/txtgen";
  2. const templates = [
  3. "{{a_noun}} is {{a_noun}} from the right perspective",
  4. "the {{noun}} of {{a_noun}} becomes {{an_adjective}} {{noun}}",
  5. ];
  6. addTemplates(templates);

Here are the available placeholders:

  • noun
  • nouns
  • a_noun
  • adjective
  • an_adjective

Lorem ipsum

Syntax:

  1. lorem() // generate a random phrase with length from 2 to 24 words of lorem ipsum
  2. lorem(Number min) // set the minimum number of words
  3. lorem(Number min, Number max)// set the minimum/maximum number of words

Example:

  1. import { lorem } from "@ndaidong/txtgen";
  2. const phrase = lorem();
  3. console.log(phrase); // => nisi blandit feugiat tempus imperdiet etiam eu mus augue

Development

Since v4.x.x, we switched to Deno
platform, and use DNT to build Node.js
packages.

  1. git clone https://github.com/ndaidong/txtgen.git
  2. cd txtgen
  3. # test
  4. deno test
  5. # build npm packages
  6. deno task build
  7. cd npm
  8. node test_runner.js

License

The MIT License (MIT)