Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix (core): makeTheme should perform deep merges of objects #1403

Merged
merged 2 commits into from
May 13, 2024
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
53 changes: 53 additions & 0 deletions packages/checkbox/checkbox.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,59 @@ describe('checkbox prompt', () => {
await expect(answer).resolves.toEqual([1]);
});

describe('theme: icon', () => {
it('checked/unchecked', async () => {
const { answer, events, getScreen } = await render(checkbox, {
message: 'Select a number',
choices: numberedChoices,
theme: {
icon: {
checked: '√',
unchecked: 'x',
},
},
});
events.keypress('space');
expect(getScreen()).toMatchInlineSnapshot(`
"? Select a number
❯√ 1
x 2
x 3
x 4
x 5
x 6
x 7"
`);
events.keypress('enter');
await answer;
});

it('cursor', async () => {
const { answer, events, getScreen } = await render(checkbox, {
message: 'Select a number',
choices: numberedChoices,
theme: {
icon: {
cursor: '>',
},
},
});
events.keypress('space');
expect(getScreen()).toMatchInlineSnapshot(`
"? Select a number
>◉ 1
◯ 2
◯ 3
◯ 4
◯ 5
◯ 6
◯ 7"
`);
events.keypress('enter');
await answer;
});
});

describe('theme: style.renderSelectedChoices', () => {
it('renderSelectedChoices', async () => {
const { answer, events, getScreen } = await render(checkbox, {
Expand Down
36 changes: 33 additions & 3 deletions packages/core/src/lib/make-theme.mts
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
import type { Prettify, PartialDeep } from '@inquirer/type';
import { defaultTheme, type Theme } from './theme.mjs';

function isPlainObject(value: unknown): value is object {
if (typeof value !== 'object' || value === null) return false;

let proto = value;
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto);
}

return Object.getPrototypeOf(value) === proto;
}

function deepMerge<T extends object>(...objects: Partial<T>[]): T {
const output: { [key: string]: unknown } = {};

for (const obj of objects) {
for (const [key, value] of Object.entries(obj)) {
const prevValue = output[key];

output[key] =
isPlainObject(prevValue) && isPlainObject(value)
? deepMerge(prevValue, value)
: value;
}
}

return output as T;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those as uses aren't safe since they force a type. Ideally we could have TS type check without forcing the types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

}

export function makeTheme<SpecificTheme extends object>(
...themes: ReadonlyArray<undefined | PartialDeep<Theme<SpecificTheme>>>
): Prettify<Theme<SpecificTheme>> {
return Object.assign({}, defaultTheme, ...themes, {
style: Object.assign({}, defaultTheme.style, ...themes.map((theme) => theme?.style)),
});
const themesToMerge = [
defaultTheme,
...themes.filter((theme) => theme != null),
] as Theme<SpecificTheme>[];
return deepMerge(...themesToMerge);
}
Loading