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

Core: Speed up combineParameters #11736

Merged
merged 4 commits into from
Jul 30, 2020
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
4 changes: 4 additions & 0 deletions lib/client-api/src/parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ describe('client-api.parameters', () => {
},
});
});

it('ignores undefined additions', () => {
expect(combineParameters({ a: 1 }, { a: 2 }, { a: undefined })).toEqual({ a: 2 });
});
});
42 changes: 35 additions & 7 deletions lib/client-api/src/parameters.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
// Utilities for handling parameters
import mergeWith from 'lodash/mergeWith';

import { Parameters } from '@storybook/addons';
import isPlainObject from 'lodash/isPlainObject';

export const combineParameters = (...parameterSets: Parameters[]) =>
mergeWith({}, ...parameterSets, (objValue: any, srcValue: any) => {
// Treat arrays as scalars:
if (Array.isArray(srcValue)) return srcValue;
/**
* Safely combine parameters recursively. Only copy objects when needed.
* Algorithm = always overwrite the existing value UNLESS both values
* are plain objects. In this case flag the key as "special" and handle
* it with a heuristic.
*/
export const combineParameters = (...parameterSets: Parameters[]) => {
const mergeKeys: Record<string, boolean> = {};
const combined = parameterSets.reduce((acc, p) => {
Object.entries(p).forEach(([key, value]) => {
const existing = acc[key];
if (Array.isArray(value) || typeof existing === 'undefined') {
acc[key] = value;
} else if (isPlainObject(value) && isPlainObject(existing)) {
// do nothing, we'll handle this later
mergeKeys[key] = true;
} else if (typeof value !== 'undefined') {
acc[key] = value;
}
});
return acc;
}, {} as Parameters);

return undefined;
Object.keys(mergeKeys).forEach((key) => {
const mergeValues = parameterSets
.map((p) => p[key])
.filter((value) => typeof value !== 'undefined');
if (mergeValues.every((value) => isPlainObject(value))) {
combined[key] = combineParameters(...mergeValues);
} else {
combined[key] = mergeValues[mergeValues.length - 1];
}
});

return combined;
};
4 changes: 1 addition & 3 deletions lib/client-api/src/story_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,7 @@ export default class StoryStore {
return Array.from(new Set(this.raw().map((s) => s.kind)));
}

getStoriesForKind(kind: string) {
return this.raw().filter((story) => story.kind === kind);
}
getStoriesForKind = (kind: string) => this.raw().filter((story) => story.kind === kind);

getRawStory(kind: string, name: string) {
return this.getStoriesForKind(kind).find((s) => s.name === name);
Expand Down