Neapolitan Pizza Margherita
Miss any of our Open RFC calls?Watch the recordings here! »

txtgen

2.2.7 • Public • Published

txtgen

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

NPM Build Status Coverage Status

Demo

See how it works?

Setup

Usage

const txtgen = require('txtgen');
 
const sentence = txtgen.sentence();
console.log(sentence);
 
const paragraph = txtgen.paragraph();
console.log(paragraph);
 
const article = txtgen.article();
console.log(article);

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()

As their name means, 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 since v2.2.3 to help you customize your sample data.

Template

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

For example:

import {
  addTemplates
} from 'txtgen';
 
let templates = [
  '{{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

Test

git clone https://github.com/ndaidong/txtgen.git
cd txtgen
npm i
npm test

License

The MIT License (MIT)

Install

npm i txtgen

DownloadsWeekly Downloads

443

Version

2.2.7

License

MIT

Unpacked Size

339 kB

Total Files

29

Last publish

Collaborators

  • avatar