Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Assert that uuids and ids are valid HTML ids
- Loading branch information
Matthew Holloway
committed
Jun 3, 2020
1 parent
558510c
commit b51cb4a
Showing
10 changed files
with
81 additions
and
8 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
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
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,14 +1,29 @@ | ||
import { UUID } from '../components/ItemContext'; | ||
|
||
const DEFAULT = 0; | ||
|
||
let counter = DEFAULT; | ||
|
||
export function nextUuid(): number { | ||
export function nextUuid(): UUID { | ||
const current = counter; | ||
counter = counter + 1; | ||
|
||
return current; | ||
return `raa-${current}`; | ||
} | ||
|
||
export function resetNextUuid(): void { | ||
counter = DEFAULT; | ||
} | ||
|
||
// https://stackoverflow.com/a/14664879 | ||
// but modified to allow additional first characters per HTML5 | ||
// https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id | ||
const idRegex = /^[_\-.a-zA-Z][\w:.-]*$/; | ||
|
||
export function assertValidHtmlId(htmlId: string): void { | ||
if (!htmlId.toString().match(idRegex)) { | ||
throw new Error( | ||
`uuid must be a valid HTML Id but was given "${htmlId}"`, | ||
); | ||
} | ||
} |