mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 18:07:49 +00:00
add uniq Map for generated ID (#3644)
This commit is contained in:
committed by
John-David Dalton
parent
54f05aa5cb
commit
7f24cab7dd
14
uniqueId.js
14
uniqueId.js
@@ -1,5 +1,5 @@
|
|||||||
/** Used to generate unique IDs. */
|
/** Used to generate unique IDs. */
|
||||||
let idCounter = 0
|
let idCounter = {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a unique ID. If `prefix` is given, the ID is appended to it.
|
* Generates a unique ID. If `prefix` is given, the ID is appended to it.
|
||||||
@@ -17,8 +17,16 @@ let idCounter = 0
|
|||||||
* uniqueId()
|
* uniqueId()
|
||||||
* // => '105'
|
* // => '105'
|
||||||
*/
|
*/
|
||||||
function uniqueId(prefix='') {
|
function uniqueId(prefix='$lodash$') {
|
||||||
const id = ++idCounter
|
if(!idCounter[prefix]){
|
||||||
|
idCounter[prefix] = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
const id =++idCounter[prefix]
|
||||||
|
if(prefix === '$lodash$') {
|
||||||
|
return `${id}`
|
||||||
|
}
|
||||||
|
|
||||||
return `${prefix + id}`
|
return `${prefix + id}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user