wix-style-react
Version:
wix-style-react
25 lines (23 loc) • 934 B
JavaScript
;
exports.__esModule = true;
exports.default = void 0;
var _constants = require("./constants");
/* stringToColor returns a different color for each string.
** Each string will ALWAYS receives the same color, so the color will always be consistent per string
**/
var stringToColor = name => {
if (!name) {
return;
}
var hash = stringToNumber(name);
var index = hash % _constants.avatarColorList.length; // this sets an index between 0 and array.length.
return _constants.avatarColorList[index];
};
/* stringToNumber creates a number for each string.
** for example the string: "Hi" number will be 177.
** charCode of "H" = 72, charCode of "i" = 105. 72 + 105 = 177
** This creates a unique number for each string
**/
var stringToNumber = str => str.split('').reduce((acc, char) => acc + char.charCodeAt(), 0);
var _default = exports.default = stringToColor;
//# sourceMappingURL=string-to-color.js.map