Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Util for generating random sentences, paragraphs and articles in English

License

NotificationsYou must be signed in to change notification settings

ndaidong/txtgen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lightweight util for generating random sentences, paragraphs and articles inEnglish. Inspired bySentencer andmetaphorpsum.com.

CodeQLCI testCoverage StatusNPMJSR

Demo

Setup & Usage

Deno

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

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

You can use JSR packages without an install step usingjsr: specifiers:

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

You can also usenpm: specifiers as before:

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

Or import from esm.sh

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

Node.js & Bun

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

npm i @ndaidong/txtgen# pnpmpnpm i @ndaidong/txtgen# yarnyarn add @ndaidong/txtgen# bunbun add @ndaidong/txtgen
import{sentence}from"@ndaidong/txtgen";for(leti=0;i<5;i++){console.log(sentence());}

You can also use CJS style:

const{ sentence}=require("@ndaidong/txtgen");for(leti=0;i<5;i++){console.log(sentence());}

Browsers:

<scripttype="module">import{sentence}from"https://esm.sh/@ndaidong/txtgen";// import { sentence } from 'https://unpkg.com/@ndaidong/txtgen/esm/mod.js';for(leti=0;i<5;i++){console.log(sentence());}</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 grammaticalunit
  • addNouns(),addAdjectives(),addTemplates(): add more samples to currentsample set
  • setNouns(),setAdjectives(),setTemplates(): replace current sample setwith new ones
  • getNouns(),getAdjectives(),getTemplates(): get current sample set

Theset* andget* methods were added in v2.2.3 to help you customize yoursample data.

In addition, we've addedlorem() method since v3.0.5 to generate lorem ipsumtext.

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 anEnglish sentence, containing placeholders that can be replaced with anyalternative word.

For example:

import{addTemplates}from"@ndaidong/txtgen";consttemplates=["{{a_noun}} is {{a_noun}} from the right perspective","the {{noun}} of {{a_noun}} becomes {{an_adjective}} {{noun}}",];addTemplates(templates);

Here are the available placeholders:

  • noun
  • nouns
  • a_noun
  • adjective
  • an_adjective

Lorem ipsum

Syntax:

lorem()// generate a random phrase with length from 2 to 24 words of lorem ipsumlorem(Numbermin)// set the minimum number of wordslorem(Numbermin,Numbermax)// set the minimum/maximum number of words

Example:

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

Development

Since v4.x.x, we switched toDenoplatform, and useDNT to build Node.jspackages.

git clone https://github.com/ndaidong/txtgen.gitcd txtgen# testdenotest# build npm packagesdeno task buildcd npmnode test_runner.js

License

The MIT License (MIT)



[8]ページ先頭

©2009-2025 Movatter.jp