salix/front/core/lib/string.js

69 lines
1.7 KiB
JavaScript

/**
* Transforms a kebab-case string to camelCase. A kebab-case string
* is a string with hyphen character as word separator.
*
* @param {String} str The hyphenized string
* @return {String} The camelized string
*/
export function kebabToCamel(str) {
return str.replace(/-([a-z])/g, g => g[1].toUpperCase());
}
/**
* Transforms a camelCase to kebab-case.
*
* @param {String} str The camelized string
* @return {String} The hyphenized string
*/
export function camelToKebab(str) {
let kebabCased = str.substr(1)
.replace(/[A-Z]/g, g => `-${g[0]}`);
return `${str.charAt(0)}${kebabCased}`.toLowerCase();
}
/**
* Transforms the first letter of a string to uppercase.
*
* @param {String} str The input string
* @return {String} The transformed string
*/
export function firstUpper(str) {
return str.charAt(0).toUpperCase() + str.substr(1);
}
export function djb2a(string) {
let hash = 5381;
for (let i = 0; i < string.length; i++)
hash = ((hash << 5) + hash) ^ string.charCodeAt(i);
return hash >>> 0;
}
export function hashToColor(value) {
return '#' + colors[djb2a(value || '') % colors.length];
}
const colors = [
'b5b941', // Yellow
'ae9681', // Peach
'd78767', // Salmon
'cc7000', // Orange bright
'e2553d', // Coral
'8B0000', // Red dark
'de4362', // Red crimson
'FF1493', // Ping intense
'be39a2', // Pink light
'b754cf', // Purple middle
'a87ba8', // Pink
'8a69cd', // Blue lavender
'ab20ab', // Purple dark
'00b5b8', // Turquoise
'1fa8a1', // Green ocean
'5681cf', // Blue steel
'3399fe', // Blue sky
'6d9c3e', // Green chartreuse
'51bb51', // Green lime
'518b8b', // Gray board
'7e7e7e', // Gray
'5d5d5d', // Gray dark
];