UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.

46 lines (45 loc) 2.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getColorForInitial = getColorForInitial; const primitives_1 = require("../guards/primitives"); const constants_1 = require("./constants"); const helpers_1 = require("./helpers"); const utils_1 = require("./utils"); function getColorForInitial(input = '', opacity = 100) { let initial; const hexOpacity = (0, utils_1.percentToHex)(opacity); const NUMBERS = '0123456789'; const DEFAULT = '#010514'; if (!input) return (0, helpers_1._applyOpacity)(DEFAULT, hexOpacity); if ((0, primitives_1.isString)(input)) { initial = input[0]; if (NUMBERS.includes(initial)) { return (0, helpers_1._applyOpacity)(constants_1.NUMBER_COLOR_PALETTE[parseInt(initial, 10)], hexOpacity); } const upperInitial = initial.toUpperCase(); const index = upperInitial.charCodeAt(0) - 'A'.charCodeAt(0); if (index >= 0 && index < constants_1.ALPHABET_COLOR_PALETTE?.length) { return (0, helpers_1._applyOpacity)(constants_1.ALPHABET_COLOR_PALETTE[index], hexOpacity); } return (0, helpers_1._applyOpacity)(DEFAULT, hexOpacity); } else if ((0, primitives_1.isNumber)(input)) { initial = input.toString()[0]; if (NUMBERS.includes(initial)) { return (0, helpers_1._applyOpacity)(constants_1.NUMBER_COLOR_PALETTE[parseInt(initial, 10)], hexOpacity); } return (0, helpers_1._applyOpacity)(DEFAULT, hexOpacity); } else if (Array.isArray(input)) { if (input?.length < 1) return [...constants_1.ALPHABET_COLOR_PALETTE, ...constants_1.NUMBER_COLOR_PALETTE].map((color) => (0, helpers_1._applyOpacity)(color, hexOpacity)); return input.flatMap((el) => { if (Array.isArray(el)) { return getColorForInitial(el, opacity); } return getColorForInitial(el, opacity); }); } return (0, helpers_1._applyOpacity)(DEFAULT, hexOpacity); }