log-tags
Version:
Color tags for devtools console.log
31 lines (30 loc) • 1.24 kB
JavaScript
export const randomHex = () => ((Math.random() * 16) >> 0).toString(16);
export const randomHexColor = () => `#${randomHex()}${randomHex()}${randomHex()}`;
export const objectToStyleCssString = (css) => Object.entries(css)
.map(_ => _.join(":"))
.join(";");
export const strColorHash = (s) => {
const arr = [...s];
const sum1 = arr.map(_ => _.charCodeAt(0)).reduce((sum, n) => sum + n, 0);
const hashes = [0, 0, 0].map((_, i) => ((sum1 * 2) ** (i + 5) % 0xffffff) / 0xffffff);
const rgb = hashes.map(hash => {
let h = (hash * 16) >> 0;
if (h % 2 == 0)
h = Math.max(0, h - 1);
return h;
});
const hex = "#" + rgb.map(_ => _.toString(16).repeat(2)).join("");
return hex;
};
export function combineLogTags(...tags) {
const tagList = (isLogTag(tags[0]) ? tags : tags[0]);
const tagStr = tagList.map(([text]) => text).join("");
const tagStyles = tagList.map(([, style]) => style);
return [tagStr, ...tagStyles];
}
export function isStyledText(value) {
return typeof value === "string" && value.startsWith("%c");
}
export function isLogTag(target) {
return target && Array.isArray(target) && isStyledText(target[0]) && typeof target[1] === "string";
}