Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/utils/expandMentionStylesForIndicators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { HtmlStyle, MentionStyleProperties } from '../types';
import { isMentionStyleRecord } from './isMentionStyleRecord';
import { omitUndefined } from './omitUndefined';

export function expandMentionStylesForIndicators(
mention: HtmlStyle['mention'] | undefined,
Expand All @@ -10,9 +11,11 @@ export function expandMentionStylesForIndicators(
for (const indicator of indicators) {
out[indicator] = {
...htmlStyleToMergeWith.mention,
...(isMentionStyleRecord(mention)
? (mention[indicator] ?? mention.default ?? {})
: mention),
...omitUndefined(
isMentionStyleRecord(mention)
? (mention[indicator] ?? mention.default)
: mention
),
};
}
return out;
Expand Down
7 changes: 4 additions & 3 deletions src/utils/normalizeHtmlStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DEFAULT_ENRICHED_TEXT_STYLE,
} from './defaultHtmlStyle';
import { expandMentionStylesForIndicators } from './expandMentionStylesForIndicators';
import { omitUndefined } from './omitUndefined';

const MENTION_DEFAULT_KEY = '_default';

Expand Down Expand Up @@ -74,12 +75,12 @@ const convertToEnrichedTextHtmlStyleInternal = (
if (typeof value === 'object' && value !== null) {
mentionStyles[key] = {
...DEFAULT_ENRICHED_TEXT_STYLE.mention,
...(value as MentionStyleProperties),
...omitUndefined(value as MentionStyleProperties),
};
} else {
mentionStyles[MENTION_DEFAULT_KEY] = {
...DEFAULT_ENRICHED_TEXT_STYLE.mention,
...(mention as MentionStyleProperties),
...omitUndefined(mention as MentionStyleProperties),
};
}
}
Expand Down Expand Up @@ -115,7 +116,7 @@ const assignDefaultValues = <T extends Record<string, any>>(

merged[key] = {
...(base[key] ?? {}),
...(style[key as keyof typeof style] as object),
...omitUndefined(style[key as keyof typeof style] as object),
};
}

Expand Down
18 changes: 18 additions & 0 deletions src/utils/omitUndefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Returns a shallow copy of `obj` without the keys whose value is `undefined`.
*/
export function omitUndefined<T extends object>(
obj: T | null | undefined
): Partial<T> {
const result: Partial<T> = {};
if (!obj) {
return result;
}

for (const key of Object.keys(obj) as (keyof T)[]) {
if (obj[key] !== undefined) {
result[key] = obj[key];
}
}
return result;
}
Loading