Skip to content

Commit

Permalink
Improve algo
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixMalfait committed Nov 8, 2024
1 parent 9be6cb6 commit 15201aa
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import snakeCase from 'lodash.snakecase';

import { computeOptionValueFromLabelOrThrow } from '~/pages/settings/data-model/utils/compute-option-value-from-label.utils';

export const getOptionValueFromLabel = (label: string) => {
let transliteratedLabel = label;
try {
transliteratedLabel = computeOptionValueFromLabelOrThrow(label);
} catch (error) {
return "";
console.error(error);
return '';
}

return snakeCase(transliteratedLabel).toUpperCase();
return transliteratedLabel;
};
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const OPTION_VALUE_VALID_PATTERN = /^[a-zA-Z0-9]+$/;
export const OPTION_VALUE_VALID_PATTERN = /^[A-Z_][A-Z0-9_]*$/;
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@ import { OPTION_VALUE_VALID_PATTERN } from '~/pages/settings/data-model/constant
import { transliterateAndFormatOrThrow } from '~/pages/settings/data-model/utils/transliterate-and-format.utils';

export const computeOptionValueFromLabelOrThrow = (label: string): string => {
return transliterateAndFormatOrThrow(label, OPTION_VALUE_VALID_PATTERN);
const prefixedLabel = /^\d/.test(label) ? `OPT${label}` : label;

return transliterateAndFormatOrThrow(
prefixedLabel,
OPTION_VALUE_VALID_PATTERN,
);
};
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import toCamelCase from 'lodash.camelcase';
import { slugify, transliterate } from 'transliteration';
import { slugify } from 'transliteration';

import { isDefined } from '~/utils/isDefined';

export const transliterateAndFormatOrThrow = (
string: string,
validStringPattern: RegExp,
): string => {
let formattedString = string;

if (isDefined(formattedString.match(validStringPattern))) {
return toCamelCase(formattedString);
if (isDefined(string.match(validStringPattern))) {
return string;
}

formattedString = toCamelCase(
slugify(transliterate(formattedString, { trim: true })),
);
const formattedString = slugify(string, {
trim: true,
separator: '_',
}).toUpperCase();

if (!formattedString.match(validStringPattern)) {
throw new Error(`"${string}" is not a valid name`);
Expand Down
5 changes: 2 additions & 3 deletions packages/twenty-ui/src/display/tag/components/Tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ const StyledTag = styled.h3<{
border: ${({ variant, theme }) =>
variant === 'outline' || variant === 'border'
? `1px ${variant === 'border' ? 'solid' : 'dashed'} ${theme.border.color.strong}`
: ''};
: 'none'};
gap: ${spacing1};
min-width: ${({ preventShrink }) =>
preventShrink ? 'fit-content' : 'none;'};
min-width: ${({ preventShrink }) => (preventShrink ? 'fit-content' : 'none')};
`;

const StyledContent = styled.span`
Expand Down

0 comments on commit 15201aa

Please sign in to comment.