salix/front/core/lib/string.js

70 lines
1.8 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 = [
'FFA07A', // Salmon
'FFDAB9', // Peach
'a17077', // Pink
'e2553d', // Coral
'bf0e99', // Pink light
'52a500', // Green chartreuse
'00aeae', // Cian
'b754cf', // Purple middle
'8a69cd', // Blue lavender
'1fa8a1', // Green ocean
'DC143C', // Red crimson
'5681cf', // Blue steel
'FF1493', // Ping intense
'02ba02', // Green lime
'1E90FF', // Blue sky
'8B008B', // Purple dark
'cc7000', // Orange bright
'00b5b8', // Turquoise
'8B0000', // Red dark
'008080', // Green bluish
'2F4F4F', // Gray board
'7e7e7e', // Gray
'5d5d5d', // Gray dark
];