- Notifications
You must be signed in to change notification settings - Fork17
Util for generating random sentences, paragraphs and articles in English
License
ndaidong/txtgen
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Lightweight util for generating random sentences, paragraphs and articles inEnglish. Inspired bySentencer andmetaphorpsum.com.
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());}
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());}
<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>
.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 grammaticalunitaddNouns()
,addAdjectives()
,addTemplates()
: add more samples to currentsample setsetNouns()
,setAdjectives()
,setTemplates()
: replace current sample setwith new onesgetNouns()
,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.
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
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
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
The MIT License (MIT)
About
Util for generating random sentences, paragraphs and articles in English