I am writing more content on lodash this month for now, and while I was at it I have noticed that I did not get around to _.uniqueId yet. As the name of the method suggests the method will return a unique value each time the method is called, so then it can be used as a way to set some kind of unique id values for an object of one kind or another. The method addresses something that comes up once in while now and then when developing projects, so it deserves a post on the subject.
Using the _.uniqueId method is fairly straight forward to use, I just need to call it, passing an optional prefix as the first argument, and a unique id will be returned by the method. That is all there is to it when it comes to just using this specific method by itself.
The value might be unique in a relative way, but it is not at all the best solution for many other use case examples. The method is just a lazy way to get a value that is unique each time it is called, and one way to do that is to have it so it just returns a count each time.
Although using a method like unique id in lodash might be an okay starting point, often I will want to make my own kind of method for this sort of thing. A useful method for expanding lodash would be to use a method like that of the lodash mixin method as this is the standard way of going about doing thus sort of thing with lodash. When doing so there is not just creating a new id, but creating an id that is a kind of hash code of come kind, and also having a way to go about extracting info about that id with another method that will do so.
So then this example opens up a whole new world of possibles when it comes to make some more professional kind of method for creating unique id values for something. There is a lot more that comes to mind when it comes to this sort of thing, such as having better control over setting what the starting count value should be, and also passing info about the object to which this id is being generated for also. That in turn raises the subject of what a checksum is, and using such values as part of the hex value, but that of course also complicated the process of extracting values from the hex value also, unless this is added as a separate part with a delimiter of some kind, in which case not so much.
I just write a Immediately Invoked Function Expression, or IIFE for short often, and then inside the body of that function expression I have a local num variable. I then return a function from within the body of this IIFE, that when called will step the num variable. The current value of this internal closed over num variable with then be used as part of the string that is returned that will be unique each time.
So it goes without saying that the value that is returned by the lodash uniqueid method is not all that unique. Depending on how unique you need the value to be though it is good enough, however it comes projects you might need to have a method that returns a value that has a lower probability of being duplicated.
A method such as this results in a value where there is still a non zero change of the value being duplicated, but it is still unique enough that the probability of that happening is pretty negligible.
If you enjoyed this post and would like to read up more on some other related topics to this post there is checking gout my main post on lodash in general. I have wrote many other posts on lodash many of which are on other specific methods such as the unique id method, and various other topics surrounding lodash.