4.0.1 • Published 2 years ago

hashpick v4.0.1

Weekly downloads
2
License
ISC
Repository
github
Last release
2 years ago

hashpick

Pick an item from an array based on the hash of a value. Useful if you want to consistently map data that's not known beforehand to a discrete set of target values.

In short, this module enables you to do something like this:

const chalk = require('chalk');
const pickColor = require('hashpick').default([
    chalk.red,
    chalk.green,
    chalk.yellow,
    chalk.blue,
    chalk.magenta,
    chalk.cyan,
    chalk.gray,
]);

const colorize = (str) => {
    const color = pickColor(str);
    return chalk.bold(color(str));
};

console.log(colorize('config'), 'Loaded cache');
console.log(colorize('db'), 'Connected to database');
console.log(colorize('server'), 'Server listening on port 1234');
console.log(colorize('db'), 'Database connection lost');
console.log(colorize('db'), 'Reconnecting to database');
console.log(colorize('server'), 'GET / 200');

Output

Installation

npm install --save hashpick
# or
yarn add hashpick

Usage

Use the exported function to create a new picker-function based on an array of choices:

const hashpick = require('hashpick').default;
const pickNumber = hashpick(['one', 'two', 'three']);

pickNumber('any value'); // -> 'three'

API

hashpick(choices: T[]): (string) => T

Creates a new picker that will consistently pick the same value from choices based on its input.

4.0.1

2 years ago

4.0.0

2 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago