sentence-builder

1.0.0 • Public • Published

Sentence Builder

Build a sentence with variability

let sentence = [
  ['Hello', 'Hi'],
  ['World!', 'Earth!']
];
 
sentenceBuilder.build(sentence);
// Hi World!
 
sentenceBuilder.combos(sentence);
// ['Hello World!', 'Hi World!', 'Hello Earth!', 'Hi Earth!']

Installation

  1. Install the package:
npm install --save sentence-builder
  1. Require it in your project:
const sentenceBuilder = require('sentence-builder');

Usage

build()

Build a sentence:

let sentence = [
  ['Hello', 'Hi'],
  ['World!', 'Earth!']
];
 
sentenceBuilder.build(sentence);
// Hi World!

All methods take a second parameter, exclude, which can be given as a single string or an array of strings.

sentenceBuilder.build(sentence, 'Hi World!');
// Hi Earth!

Pass true as a third parameter to get output as an array.

sentenceBuilder.build(sentence, null, true);
// ['Hi', 'World!']

combos()

Effectively returns the Cartesian product.

sentenceBuilder.combos(sentence);
// ['Hello World!', 'Hi World!', 'Hello Earth!', 'Hi Earth!']

Also takes the exclude parameter.

sentenceBuilder.combos(sentence, ['Hi World!', 'Hello Earth!']);
// ['Hello World!', 'Hi Earth!']

Takes a third parameter, limit, which limits the results.

sentenceBuilder.combos(sentence, null, 3);
// ['Hello World!', 'Hi World!', 'Hello Earth!']

numCombos()

Returns the number of possible combinations.

sentenceBuilder.numCombos(sentence);
// 4
 
sentenceBuilder.numCombos(sentence, 'Hi World!');
// 3

Package Sidebar

Install

npm i sentence-builder

Weekly Downloads

1

Version

1.0.0

License

ISC

Last publish

Collaborators

  • johnwcallahan