npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

text-variation

v1.0.3

Published

Generate variations of a given template string

Downloads

3

Readme

text-variation

GitHub Workflow Status npm npm bundle size

Generate random variations of a string template.

import getVariation from "text-variation";

getVariation("{Hey|Hello} Mike, {how are you?|nice to meet you!}"); // Will return one of:
// Hey Mike, how are you?
// Hey Mike, nice to meet you!
// Hello Mike, how are you?
// Hello Mike, nice to meet you!

Installation

Using npm

npm install text-variation

Using yarn

yarn add text-variation

Usage

The getVariation function is available as a default and named export. It accepts two parameters, template and config.

template

Required. The template with variations. By default the variation group is marked with braces and the variations inside it are separated by pipes. For example the template:

Hey {John|Mike|Mark}, my name is {Jake|Todd}, nice to {meet you.|meetcha!}

Can result in 12 different variations.

config

Optional. Must be an object. Configuration that can change how the function parses the template and chooses the variations. All properties are optional:

config.randomFn: () => number

Used to choose a variation inside each group. Useful if you'd like to use a seeded generator for consistent results or testing. Defaults to Math.random.

import getVariation from "text-variation";
import seedrandom from "seedrandom";

const seeded = seedrandom("varying-text-test");
const template = "{Hey|Hello} Mike, {how are you?|nice to meet you!}";
getVariation(template, { randomFn: seeded }); // Will always return "Hey Mike, nice to meet you!"

config.variationRegex: RegExp

Used to find the variation groups inside the template. Defaults to /\{(.*?)\}/g.

To work properly, it needs a single capturing group and the global g flag. If no capturing group is provided, the function will throw an error when trying to split the possible variations. If the global flag is not passed, only the first occurrance in the template will be replaced.

/\{(.*?)\}/g // Default, valid
/\[(.*?)\]/g // With brackets instead of braces, valid
/\{.*?\}/g   // No capturing group, will throw an error
/\{(.*?)\}/  // No global flag, will not match all occurrances in the template

config.variationChar: string

The character that separates variations inside a group. Defaults to |.

Generating all variations of a template

When creating a new template, it can be useful to know all possible variations. You can use getAllVariations to generate every possible variation of a template. Accepts the same config options as getVariation.

import { getAllVariations } from "text-variation";

const template = "{Hey|Hello} Mike, {how are you?|nice to meet you!}";
getAllVariations(template); // ["Hey Mike, how are you?", "Hey Mike, nice to meet you!", "Hello Mike, how are you?", "Hello Mike, nice to meet you!"]

Note that depending on the amount of variation groups and the variations inside them, this can result in huge lists. For example, the template:

{Hey|Hello|Howdy} {Mike|John|Luke|Todd}! Did you {see|view|look} the {file|document} I send you {yesterday|Friday|last weekend}? It's {very relevant|important|critical|classified}

Will result in 3 * 4 * 3 * 2 * 3 * 4 = 864 possible variations.