generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
220 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,31 @@ | ||
import { logger } from '@storybook/client-logger'; | ||
|
||
// extract a story id | ||
export function extractId({ id, name }: { id?: string; name?: string }): string { | ||
export function extractId( | ||
{ | ||
id, | ||
name, | ||
}: { | ||
id?: string; | ||
name?: string; | ||
}, | ||
allocatedIds: string[] = [] | ||
): string { | ||
if (id) { | ||
return id; | ||
} | ||
|
||
return name.replace(/\W+(.)/g, (_, chr) => chr.toUpperCase()); | ||
let generated = name.replace(/\W+(.)/g, (_, chr) => chr.toUpperCase()); | ||
if (allocatedIds.indexOf(generated) >= 0) { | ||
logger.warn(`Story name conflict with exports - Please add an explicit id for story ${name}`); | ||
generated += hashCode(name); | ||
} | ||
return generated; | ||
} | ||
|
||
function hashCode(str: string): string { | ||
return str | ||
.split('') | ||
.reduce((prevHash, currVal) => ((prevHash << 5) - prevHash + currVal.charCodeAt(0)) | 0, 0) // eslint-disable-line | ||
.toString(16); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.