Skip to content
Closed
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
13 changes: 12 additions & 1 deletion src/mergeProps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/**
* Merges multiple props objects into one. Unlike `Object.assign()` or `{ ...a, ...b }`, it skips
* properties whose value is explicitly set to `undefined`.
*
* @example
* ```ts
* const { a, b } = mergeProps(defaults, config, props);
* ```
*/
function mergeProps<A, B>(a: A, b: B): B & A;
function mergeProps<A, B, C>(a: A, b: B, c: C): C & B & A;
Expand All @@ -11,7 +16,13 @@ function mergeProps(...items: any[]) {
if (item) {
for (const key of Object.keys(item)) {
if (item[key] !== undefined) {
ret[key] = item[key];
if (key === 'className') {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

别介,这太黑了。你可以做个配置,支持合并逻辑。

ret[key] = `${ret[key] || ''} ${item[key] || ''}`.trim();
} else if (key === 'style') {
ret[key] = { ...ret[key], ...item[key] };
} else {
ret[key] = item[key];
}
}
}
}
Expand Down
102 changes: 102 additions & 0 deletions tests/mergeProps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,106 @@ describe('mergeProps', () => {
it('handles empty objects', () => {
expect(mergeProps({}, { a: 1 }, {})).toEqual({ a: 1 });
});

describe('className', () => {
it('merges strings', () => {
const a = { className: 'a' };
const b = { className: 'b' };
expect(mergeProps(a, b)).toEqual({ className: 'a b' });
});

it('keeps previous when later is undefined', () => {
expect(mergeProps({ className: 'a' }, { className: undefined })).toEqual({
className: 'a',
});
});

it('omits key when only undefined', () => {
expect(
(mergeProps as (...items: any[]) => any)({ className: undefined }),
).toEqual({});
});

it('null merges like empty string', () => {
expect(
mergeProps({ className: 'a' }, { className: null as any }),
).toEqual({ className: 'a' });
expect(
mergeProps({ className: null as any }, { className: 'b' }),
).toEqual({ className: 'b' });
expect(
(mergeProps as (...items: any[]) => any)({ className: null as any }),
).toEqual({ className: '' });
});

it('empty string className', () => {
expect(mergeProps({ className: 'a' }, { className: '' })).toEqual({
className: 'a',
});
expect(mergeProps({ className: '' }, { className: 'b' })).toEqual({
className: 'b',
});
expect(
(mergeProps as (...items: any[]) => any)({ className: '' }),
).toEqual({ className: '' });
});

it('whitespace-only className is trimmed', () => {
expect(mergeProps({ className: 'a' }, { className: ' ' })).toEqual({
className: 'a',
});
expect(mergeProps({ className: ' ' }, { className: 'b' })).toEqual({
className: 'b',
});
expect(
(mergeProps as (...items: any[]) => any)({ className: ' ' }),
).toEqual({ className: '' });
});
});

describe('style', () => {
it('merges objects', () => {
const a = { style: { color: 'red' } };
const b = { style: { backgroundColor: 'blue' } };
expect(mergeProps(a, b)).toEqual({
style: { color: 'red', backgroundColor: 'blue' },
});
});

it('keeps previous when later is undefined', () => {
expect(
mergeProps({ style: { color: 'red' } }, { style: undefined }),
).toEqual({ style: { color: 'red' } });
});

it('null source does not wipe previous style', () => {
expect(
mergeProps({ style: { color: 'red' } }, { style: null as any }),
).toEqual({ style: { color: 'red' } });
});

it('applies when earlier style is undefined or null', () => {
expect(
mergeProps({ style: undefined }, { style: { color: 'red' } }),
).toEqual({ style: { color: 'red' } });
expect(
mergeProps({ style: null as any }, { style: { color: 'red' } }),
).toEqual({ style: { color: 'red' } });
});

it('nested properties may be null or undefined', () => {
expect(
mergeProps(
{ style: { color: 'red', margin: undefined as any } },
{ style: { padding: null as any, color: 'blue' } },
),
).toEqual({
style: {
color: 'blue',
margin: undefined,
padding: null,
},
});
});
});
});
Loading