Navigateable
Updated on August 22, 2024Source codeTests
Navigateable is a class that enriches an array, allowing it to:
- Store the index-based location of an item that has been navigated to
- Retrieve the item that has been navigated to
- Store a status (
ready,navigated, ornavigated to <relative item position>, where<relative item position>isfirst,last,next,previous, orrandom) - Navigate forward or backward to a different item
- Navigate to a specific item or a random item
Construct a Navigateable instance
The Navigateable constructor accepts two parameters:
arrayoptionsNavigateable instance. See the Navigateable constructor options section for more guidance.Navigateable constructor options
initialLocation0Navigateable instance's initial index-based locationState and methods
arrayA shallow copy (Array) of the array passed to the Navigateable constructor.
If you assign a value directly to array, a setter will pass the new value to setArray.
locationThe index-based location (Number) stored by the Navigateable instance. Defaults to the initialLocation option passed to the constructor.
If you assign a value directly to location, a setter will pass the new value to setLocation.
statusNavigateable instance. status is ready after the instance is constructed, and changes to navigated after the first navigation of any kind.itemlocation in arraysetArray(array)Navigateable instance's arrayarray (Array)Navigateable instancesetLocation(location)navigate method, but it doesn't support the optional options argument.location (Number)Navigateable instancenavigate(location[, options])Navigates to a specific item.
See the How Navigateable navigates section for more information on how options affect navigation.
options argument. Navigateable instancenext(options)Steps forward through the array, increasing location by options.distance.
An options object with two properties: distance (Number) and loops (Boolean).
See the How Navigateable navigates section for more information on how options affect navigation.
Navigateable instanceprevious(options)location by options.distance.An options object with two properties: distance (Number) and loops (Boolean).
See the How Navigateable navigates section for more information on how options affect navigation.
Navigateable instancerandom()Navigateable instancefirst()Navigateable instancelast()Navigateable instance How Navigateable navigates
In general, whenever the setLocation, navigate, next, previous, random, first, or last methods are called, the Navigateable instance computes the new location and stores that location in its location property.
The only other thing you need to know about how your Navigateable instance navigates is what options are available for the navigate, next, and previous methods, and how those options affect the way Navigateable computes the final location.
navigate accepts an optional options object as its second argument. Here's a breakdown of the options object:
allowpossibleWhen allow is set to any, navigate will set location to the exact index you pass as the first argument of navigate, even if that index is impossible, e.g. a negative number, a decimal, or an index beyond the length of array.
When allow is set to possible, navigation is a bit smarter:
If the new location is less than 0, navigate will set location to 0.
If the new location is greater than the index of the last item in the array, navigate will set location to the index of the last item in the array.
Both next and previous accept an optional options object as their only argument. Here's a breakdown of the options object:
distance1next) or backward (previous) through the array.loopstrueIndicates whether or not Navigateable should loop around to the beginning or end of the array when navigating past those points.
See the table below for more info on exactly how looping works.
loops is...falsearray.length or greaterarray.length - 1false-1 or less0truearray.length - 1true0When loops is true and the computed location is greater than array.length - 1 (the largest index-based location), your Navigateable instance calculates how much greater the computed location is, then takes that number of steps forward through the array, looping back to the beginning any time it passes array.length - 1.
See the code below for specific examples:
// The loops option is true by default
const instance = new Navigateable(['Baleada', 'Logic', 'Navigateable'])
instance.navigate(2) // The array is now located at its last item
instance.next() // The array navigated past the end of the array, so it will return to the beginning
instance.location // -> 0
instance.next({ distance: 5 }) // The array's location is 0, and its largest index is 2.
/*
* The array starts at 0, where it left off.
* Your instance navigates 2 steps forward and reaches the last location (3 steps remain).
* It steps forward once more, looping around to 0 (2 steps remain).
* It then makes its final 2 steps, landing on 2 (0 steps remain).
*/
instance.location // -> 2
When loops is true and the computed location is less than 0, your Navigateable instance does the same thing, but in reverse: it calculates how much less the computed location is, then takes that number of steps backward through the array, looping around to the end any time it passes 0.
See the code below for specific examples:
// The loops option is true by default
const instance = new Navigateable(['Baleada', 'Logic', 'Navigateable'])
instance.navigate(0) // The array is now located at its first item
instance.previous() // The array navigated past the beginning of the array, so it will loops to the end
instance.location // -> 2
instance.previous({ distance: 5 }) // The array's location is 2, and its lowest index is 0.
/*
* The array starts at 2, where it left off.
* Your instance navigates 2 steps backward and reaches 0 (3 steps remain).
* It steps backward once more, looping around to 2 (2 steps remain).
* It then makes its final two steps, landing on 0 (0 steps remain).
*/
instance.location // -> 0
Using with TypeScript
The Navigateable constructor accepts one generic type that you can use to enforce a type for the items in array. By default, TypeScript will infer the item type from the initial array you pass to the constructor, but you can specify the type manually if needed.
const withInferredTypes = new Navigateable([1, 2, 3])
withInferredTypes.array = ['a', 'b', 'c'] // Type error
const withManualTypes = new Navigateable<string | number>([1, 2, 3])
withManualTypes.array = ['a', 'b', 'c'] // No type error
API design compliance
options object.arraysetArrayset<Property> methodslocation, setLocationstatus, itemnavigate, next, previous, randomstop methodnext(options), previous(options)able