Hash Value
Get a consistent value from an array based on a string value.
Table of Contents
Installation
npm install hash-value --save
Usage
import hashValue from 'hash-value';
const colors = [
'#1abc9c',
'#2ecc71',
'#3498db',
'#9b59b6',
'#34495e',
'#f1c40f',
'#e67e22',
'#e74c3c',
];
hashValue('Luke Skywalker', colors);
Example
Material UI Avatar
import Avatar from '@material-ui/core/Avatar';
import {
blue,
green,
orange,
purple,
red,
teal,
} from '@material-ui/core/colors';
import hashValue from 'hash-value';
const colors = [
blue[500],
green[500],
orange[500],
purple[500],
red[500],
teal[500],
];
function ColorAvatar(props) {
const { name } = props;
const bg = hashValue(name, colors);
return <Avatar style={{ backgroundColor: bg }}>{name.charAt(0)}</Avatar>;
}
Development
npm install
npm run build