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

variant-generator

v1.0.1

Published

Small utility that generates multiple variants for each combination of parameters

Downloads

7

Readme

Variant Generator

Small utility that generates multiple variation for each combination of input parameters

Installation

Github Package Registry

Add to .npmrc in your project:

@flut1:registry=https://npm.pkg.github.com

Then call npm install --save @flut1/variant-generator (unfortunately at the time of writing using yarn still results in an error)

(fallback) NPM Registry

npm install --save variant-generator
yarn add variant-generator

Explanation

This utility considers a different approach to generating all possibilities of a given input value.

Consider the following example:

const sentences = [];
for (const emotion of ['happy', 'moody', 'scared']) {
  for (const animal of ['cat', 'dog']) {
    for (const location of ['bar', 'house']) {
      sentences.push(`a ${emotion} ${animal} walked into a ${location}`);
    }
  }
}
console.log(sentences.join(', '));
// a happy cat walked into a bar, a happy cat walked into a house, a happy dog walked into a bar, a happy dog walked into a house, a moody cat walked into a bar, a moody cat walked into a house, a moody dog walked into a bar, a moody dog walked into a house, a scared cat walked into a bar, a scared cat walked into a house, a scared dog walked into a bar, a scared dog walked into a house

variant-generator allows you to generate these kinds of combinations, but instead of wrapping a loop outside of the value, the inputs are yielded inline. This is done using es6 generators. The following example logs the same sentences:

const createVariants = require('variant-generator');

const sentenceGenerator = createVariants(function* () {
  return `a ${
    yield ['happy', 'moody', 'scared']
  } ${
    yield ['cat', 'dog']
  } walked into a ${
    yield ['bar', 'house']
  }`;
});
console.log(sentenceGenerator.next().value);
// a happy cat walked into a bar
console.log(sentenceGenerator.next().value);
// a happy cat walked into a house
console.log(sentenceGenerator.next().value);
// a happy dog walked into a bar

// etc...

Motivation

Admittedly using this utility may make your code more difficult to understand. This is especially the case with a trivial example like in the explanation above. The main reason I published this utility anyway is I just thought it was an interesting thought exercise. However, there are a couple of hypothetical situations where this utility may be useful:

Composability

When having multiple variant generator functions, such as the ones below:

function* generatePeople() {
  return {
    firstName: yield ['Jack', 'Dwayne'],
    lastName: yield ['Johnson', 'Black'],
  };
}

function* generateJob() {
  return {
    role: yield ['plumber', 'teacher', 'programmer'],
    location: yield ['Sydney', 'London'],
  };
}

You can generate all combinations between them using the yield* operator:

const combinedGenerator = createVariants(function*() {
  return {
    ...(yield* generatePeople()),
    ...(yield* generateJob()),
  };
});
console.log(JSON.stringify(Array.from(combinedGenerator), null, ' '));
// [
//  {
//   "firstName": "Jack",
//   "lastName": "Johnson",
//   "role": "plumber",
//   "location": "Sydney"
//  },
//  {
//   "firstName": "Jack",
//   "lastName": "Johnson",
//   "role": "plumber",
//   "location": "London"
//  },
// ...
//  {
//   "firstName": "Dwayne",
//   "lastName": "Black",
//   "role": "programmer",
//   "location": "London"
//  }
// ]

Large configuration

For large objects, it can be useful to write the possible input values inline rather than using a loop. Consider the following example:

// webpack.config.js
function getWebpackConfigs() {
  return ['last 2 chrome versions', '> .5% and not last 2 versions'].map(browsersList => ({
    entry: getWebpackEntryConfig(),
    output: getWebpackOutputConfig(),
    module: getWebpackModuleConfig(browsersList),
    // ...
  }));
}


// webpack.config.module.js
function getWebpackModuleConfig(browsersList) {
  return {
    module: {
      rules: [
        {
          test: /\.css$/,
          oneOf: [
            {
              resourceQuery: /inline/, // foo.css?inline
              use: 'url-loader'
            },
            {
              resourceQuery: /external/, // foo.css?external
              use: 'file-loader'
            }
          ]
        },
        {
          test: /\.js$/,
          use:  {
            loader: 'babel-loader',
            options: getBabelLoaderOptions(browsersList),
          }
        },
        // ...
      ]
    }
  }
}

function getBabelLoaderOptions(browsersList) {
  return {
    presets: ['@babel/preset-env', { targets: browsersList }],
    plugins: ['@babel/plugin-proposal-object-rest-spread']
  }
}

Note how:

  • We have to pass a variable down to all levels of helper functions. This may become messy with many variables.
  • In getBabelLoaderOptions(), we can't directly see the possible values of browsersList. We have to navigate to a different file to find the actual values.

In contrast, the following may be somewhat easier to navigate:

// webpack.config.js
function getWebpackConfigs() {
  return Array.from(createVariants(function* () {
    return {
      entry: yield* getWebpackEntryConfig(),
      output: yield* getWebpackOutputConfig(),
      module: yield* getWebpackModuleConfig(),
    };
  }));
}

// webpack.config.module.js
// ...
function* getBabelLoaderOptions() {
  return {
    presets: [
      '@babel/preset-env',
      { targets: yield ['last 2 chrome versions', '> .5% and not last 2 versions'] },
    ],
    plugins: ['@babel/plugin-proposal-object-rest-spread'],
  };
}

note: this above is just a hypothetical example. I do not necessarily advocate to organize your webpack configuration in this way.

Typescript usage

This module is written in TypeScript 3.6. TypeScript will infer some of the return types from the passed generator function:

const variants = Array.from(
  createVariants(function*() {
    return {
      foo: yield [1, 2],
      bar: yield [true, false],
    };
  })
);

// typeof variants:
// Array<{ foo: any, bar: any }>

Unfortunately, I cannot infer the typings from yield statements. So those parts of the returned value become type any. If you rather have strict typings, you have to explicitly annotate them:

const variants = Array.from(
  createVariants<{ foo: number, bar: boolean }>(function*() {
    return {
      foo: yield [1, 2],
      bar: yield [true, false],
    };
  })
);

iterateCombinations utility

Sometimes you may want to execute a piece of work for each combination, but you are not interested in any return value. You can force the returned iterable to go through each variation by passing it to Array.from(), but this may look somewhat counter-intuitive. For this use case this module provides an iterateCombinations() utility:

const { iterateCombinations } = require('variant-generator');

iterateCombinations(function*() {
  const result = doSomeHeavyCalculation({
    input: {
      x: yield [0.2, 1.2],
      y: yield [Math.PI, 42],
    },
  });

  storeResultInFile(result);
});

Don't give this util too much credit. The definition is literally: g => { Array.from(createVariants(g)); }