-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: various ID generation utilities
- Loading branch information
Simon Milfred
committed
Feb 5, 2024
1 parent
6eb0a0c
commit dd233b6
Showing
3 changed files
with
24 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* Return a GUID that is unique to the component file */ | ||
export default () => { | ||
return useGUID(getCurrentInstance()?.vnode?.type?.__file); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* Create a GUID */ | ||
function useGUID(key = useId()) { | ||
return useState('_guid-' + key, () => { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace( | ||
/[xy]/g, | ||
function (c) { | ||
const r = (Math.random() * 16) | 0, | ||
v = c == 'x' ? r : (r & 0x3) | 0x8; | ||
return v.toString(16); | ||
} | ||
); | ||
}); | ||
} | ||
export default useGUID; |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* Create a number ID (unlike useId() which generates an id usable by the DOM) */ | ||
function useNumberId(key = useId()) { | ||
const counter = useState(() => 0); | ||
return useState('_num-id-' + key, () => counter.value++); | ||
} | ||
export default useNumberId; |