mirror of
https://github.com/fluxscape/fluxscape.git
synced 2026-01-13 07:42:54 +01:00
Initial commit
Co-Authored-By: Eric Tuvesson <eric.tuvesson@gmail.com> Co-Authored-By: mikaeltellhed <2311083+mikaeltellhed@users.noreply.github.com> Co-Authored-By: kotte <14197736+mrtamagotchi@users.noreply.github.com> Co-Authored-By: Anders Larsson <64838990+anders-topp@users.noreply.github.com> Co-Authored-By: Johan <4934465+joolsus@users.noreply.github.com> Co-Authored-By: Tore Knudsen <18231882+torekndsn@users.noreply.github.com> Co-Authored-By: victoratndl <99176179+victoratndl@users.noreply.github.com>
This commit is contained in:
108
packages/noodl-core-ui/src/utils/collaborator.ts
Normal file
108
packages/noodl-core-ui/src/utils/collaborator.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import { BADGE_COLORS } from '@noodl-core-ui/styles/badges';
|
||||
import { getColorFromMap } from '@noodl-core-ui/utils/color';
|
||||
|
||||
export interface CollaboratorBadge {
|
||||
label: string;
|
||||
color: string;
|
||||
}
|
||||
|
||||
export function getBadgeAndNameForUser(user: { name: string; email: string; id: string }): {
|
||||
name: string;
|
||||
badge: CollaboratorBadge;
|
||||
} {
|
||||
function hashCode(str) {
|
||||
var hash = 0,
|
||||
i,
|
||||
chr;
|
||||
if (!str?.length) return hash;
|
||||
for (i = 0; i < str.length; i++) {
|
||||
chr = str.charCodeAt(i);
|
||||
hash = (hash << 5) - hash + chr;
|
||||
hash |= 0; // Convert to 32bit integer
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
let badge = 'UK';
|
||||
function _makeBadge(first, second) {
|
||||
if (first === undefined) return 'UK';
|
||||
else if (second === undefined) return (first + first).toUpperCase();
|
||||
else return (first + second).toUpperCase();
|
||||
}
|
||||
|
||||
let name: string = undefined;
|
||||
|
||||
try {
|
||||
name = user.name;
|
||||
if (!name && user.email && user.email.length >= 2) {
|
||||
name = user.email.split('@')[0];
|
||||
}
|
||||
|
||||
let p;
|
||||
if (user.name && user.name.length >= 2) {
|
||||
p = user.name.split(' ');
|
||||
} else if (user.email && user.email.length >= 2) {
|
||||
p = user.email.split('.');
|
||||
}
|
||||
|
||||
if (p.length === 1) {
|
||||
badge = _makeBadge(p[0][0], p[0][1]);
|
||||
} else if (p.length >= 2) {
|
||||
badge = _makeBadge(p[0][0], p[1][0]);
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
const color = getColorFromMap(user.id, BADGE_COLORS);
|
||||
|
||||
return {
|
||||
badge: { label: badge, color: color },
|
||||
name
|
||||
};
|
||||
}
|
||||
|
||||
export function getBadgeForUser(c: { id: string; email: string; name: string }) {
|
||||
function hashCode(str) {
|
||||
var hash = 0,
|
||||
i,
|
||||
chr;
|
||||
if (!str?.length) return hash;
|
||||
for (i = 0; i < str.length; i++) {
|
||||
chr = str.charCodeAt(i);
|
||||
hash = (hash << 5) - hash + chr;
|
||||
hash |= 0; // Convert to 32bit integer
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
var badge = 'UK';
|
||||
|
||||
function _makeBadge(first, second) {
|
||||
if (first === undefined) return 'UK';
|
||||
else if (second === undefined) return (first + first).toUpperCase();
|
||||
else return (first + second).toUpperCase();
|
||||
}
|
||||
try {
|
||||
var name = c.name;
|
||||
if (!name && c.email && c.email.length >= 2) {
|
||||
name = c.email.split('@')[0];
|
||||
}
|
||||
|
||||
if (c.name && c.name.length >= 2) {
|
||||
var p = c.name.split(' ');
|
||||
} else if (c.email && c.email.length >= 2) {
|
||||
var p = c.email.split('.');
|
||||
}
|
||||
|
||||
if (p.length === 1) {
|
||||
badge = _makeBadge(p[0][0], p[0][1]);
|
||||
} else if (p.length >= 2) {
|
||||
badge = _makeBadge(p[0][0], p[1][0]);
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
var color = getColorFromMap(c.id, BADGE_COLORS);
|
||||
return {
|
||||
label: badge,
|
||||
color: color
|
||||
};
|
||||
}
|
||||
18
packages/noodl-core-ui/src/utils/color.ts
Normal file
18
packages/noodl-core-ui/src/utils/color.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { BADGE_COLORS } from '@noodl-core-ui/styles/badges';
|
||||
|
||||
export function getColorFromMap(baseString: string, colorMap: typeof BADGE_COLORS) {
|
||||
function hashCode(str) {
|
||||
var hash = 0,
|
||||
i,
|
||||
chr;
|
||||
if (!str?.length) return hash;
|
||||
for (i = 0; i < str.length; i++) {
|
||||
chr = str.charCodeAt(i);
|
||||
hash = (hash << 5) - hash + chr;
|
||||
hash |= 0; // Convert to 32bit integer
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
return colorMap[Math.abs(hashCode(baseString)) % colorMap.length];
|
||||
}
|
||||
3
packages/noodl-core-ui/src/utils/extractLetters.ts
Normal file
3
packages/noodl-core-ui/src/utils/extractLetters.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export function extractLetters(string: string) {
|
||||
return string.replace(/[0-9]/g, '');
|
||||
}
|
||||
3
packages/noodl-core-ui/src/utils/extractNumber.ts
Normal file
3
packages/noodl-core-ui/src/utils/extractNumber.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export function extractNumber(string: string) {
|
||||
return parseFloat(string.replace(/[^\d.-]/g, ''));
|
||||
}
|
||||
12
packages/noodl-core-ui/src/utils/linearMap.ts
Normal file
12
packages/noodl-core-ui/src/utils/linearMap.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export function linearMap(
|
||||
currentValue: number,
|
||||
currentMin: number,
|
||||
currentMax: number,
|
||||
newMin: number,
|
||||
newMax: number
|
||||
) {
|
||||
const newSize = newMax - newMin;
|
||||
const oldSize = currentMax - currentMin;
|
||||
const oldScale = currentValue - currentMin;
|
||||
return (newSize * oldScale) / oldSize + newMin;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export function normalizeAlphanumericString(value: string | number) {
|
||||
return value.toString().toLowerCase();
|
||||
}
|
||||
7
packages/noodl-core-ui/src/utils/stripCssUnit.ts
Normal file
7
packages/noodl-core-ui/src/utils/stripCssUnit.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export function stripCssUnit(string) {
|
||||
const cssUnitRegex = /^-?\d*\.?\d+(px|em|rem|vh|vw|%|ex|ch|cm|mm|in|pt|pc)$/i;
|
||||
if (cssUnitRegex.test(string)) {
|
||||
return string.slice(0, string.match(cssUnitRegex)[1].length * -1); // Remove the last characters representing the CSS unit
|
||||
}
|
||||
return string;
|
||||
}
|
||||
Reference in New Issue
Block a user