In lodash there is a clamp number method that can be use to return a number value that is based off of a given number that is to be clamped between a lower and upper bound. However it would seem that there is not a wrap number method which is also to be found in libraries and frameworks that provide such a method. If I want a wrap number method in lodash then I will need to add one to lodash by way of a mixin using the lodash mixin method.
In this section I will be starting out with just a few basic examples of clamping and wrapping numbers in lodash. When it comes to clamping a number there is the lodash clamp method, but wrapping a number is something that seems that it must be added to lodash. In any case I will be going over some quick simple examples of both of these kinds of methods in lodash as well as other various lodash features and methods.
First off say that you are using a method like the lodash get method as a way to get an element in an array and for a default value to be returned in the event that an element is empty or I give an out of range index value.
This method works fine as long as I am okay with the default value being what I pass when calling the lodash get method. In many situations I might not want to have a value that I pass, but for the method to check if a given index value is out of rage of the array or not, and if so give the closest element that is in range. Also there is the idea of having a kind if wrap method that will wrap around to the begging or end of the array and give whatever element is a result of that when giving an out of range index value.
So then when it comes to clamping an index value I can just use the lodash clamp method. This method works by giving a number and then a min and max number to clamp that number to, the returned number is then the number that was given or a number that has been clamped to the range of the number goes out or range.
So then the clamp method is like setting some boundaries for a given value to make it so that the return value will always be in that range. However it is like setting up walls, rather than having no walls at all just looping back around when a value goes out of bounds. So then on tip of having a clamp number method there should also be a wrap number methods.
If I want a wrap number method it would seem that I will have to add one to lodash. This one that I have added into lodash by way of the lodash mixin method was based off of the source code of the phaser game framework. The phaser game framework features both a wrap and clamp method in the math object of the framework.
Anyway once I add the wrap number method to lodash it will take the same arguments as the lodash clamp method only as the name suggests it will wrap around like the odometer in a car and give an index value from the upper bounds backward or vis versa.
So then that will be it for now when it comes to the subject of clamping and wrapping numbers in lodash at least for now until the next time I get around to doing a little editing of this post. If you are looking for event more reading on lodash there is my main post on lodash that I have put a fair about of time into, and get around to editing fairly often.
Although lodash has a lot of useful methods it seems like many of them are methods that I never use, also I have to say that there are methods that I use all the time that I think should be part of it but of course they are not such as with the wrap number method. This is why I sometimes thing that it might be best to make my own utilities library from the ground up actually.