salix/front/core/lib/string.js

69 lines
1.7 KiB
JavaScript
Raw Normal View History

/**
* 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) {
2019-10-26 23:30:01 +00:00
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);
}
2023-05-19 11:51:20 +00:00
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;
}
2023-05-19 11:51:20 +00:00
export function hashToColor(value) {
return '#' + colors[djb2a(value || '') % colors.length];
2023-05-19 11:51:20 +00:00
}
const colors = [
2023-05-20 12:09:11 +00:00
'b5b941', // Yellow
2023-05-19 11:51:20 +00:00
'FFDAB9', // Peach
2023-05-20 12:09:11 +00:00
'FFA07A', // Salmon
'cc7000', // Orange bright
2023-05-19 11:51:20 +00:00
'e2553d', // Coral
2023-05-20 12:09:11 +00:00
'8B0000', // Red dark
'de4362', // Red crimson
'FF1493', // Ping intense
'be39a2', // Pink light
2023-05-19 11:51:20 +00:00
'b754cf', // Purple middle
2023-05-20 12:09:11 +00:00
'a87ba8', // Pink
2023-05-19 11:51:20 +00:00
'8a69cd', // Blue lavender
2023-05-20 12:09:11 +00:00
'ab20ab', // Purple dark
'00b5b8', // Turquoise
2023-05-19 11:51:20 +00:00
'1fa8a1', // Green ocean
'5681cf', // Blue steel
2023-05-20 12:09:11 +00:00
'3399fe', // Blue sky
'6d9c3e', // Green chartreuse
'51bb51', // Green lime
'518b8b', // Gray board
2023-05-19 11:51:20 +00:00
'7e7e7e', // Gray
'5d5d5d', // Gray dark
];