SHIFT + D

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, or navigated to <relative item position>, where <relative item position> is first, last, next, previous, or random)
  • 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:

Parameter
Type
Required
Description
array
Array
yes
The array that will be made navigable.
options
Object
no
Options for the Navigateable instance. See the Navigateable constructor options section for more guidance.

Navigateable constructor options

Option
Type
Default
Description
initialLocation
Number
0
The Navigateable instance's initial index-based location

State and methods

Property
Type
Description
Parameters
Return value
array
Getter/Setter
See return value
N/A

A 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.

location
Getter/Setter
See return value
N/A

The 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.

status
Getter
See return value
N/A
The status (String) of the Navigateable instance. status is ready after the instance is constructed, and changes to navigated after the first navigation of any kind.
item
Getter
See return value
N/A
The item (can be any type) located at location in array
setArray(array)
Function
Sets the Navigateable instance's array
The new array (Array)
The Navigateable instance
setLocation(location)
Function
An alias for the navigate method, but it doesn't support the optional options argument.
The new location (Number)
The Navigateable instance
navigate(location[, options])
Function

Navigates to a specific item.

See the How Navigateable navigates section for more information on how options affect navigation.

The index-based location (Number) of the item that should be navigated to, and an optional options argument.
The Navigateable instance
next(options)
Function

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.

The Navigateable instance
previous(options)
Function
Steps backward through the array, decreasing 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.

The Navigateable instance
random()
Function
Navigates to a random item
none
The Navigateable instance
first()
Function
Navigates to the first item in the array
none
The Navigateable instance
last()
Function
Navigates to the last item in the array
none
The 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:

Option
Type
Default
Description
allow
String
possible

When 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:

Option
Type
Default
Description
distance
Number
1
The number of items that will be traversed when the navigateable instance is stepping forward (next) or backward (previous) through the array.
loops
Boolean
true

Indicates 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.

When loops is...
And the computed location is...
The new location is...
false
array.length or greater
array.length - 1
false
-1 or less
0
true
greater than array.length - 1
See explanation below
true
less than 0
See explanation below

When 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

Spec
Compliance status
Notes
Access functionality by constructing an instance
Constructor accepts two parameters: a piece of state, and an options object.
Constructor does not access the DOM
Takes the form of a JavaScript Object
State and methods are accessible through properties of the object
Methods always return the instance
Stores the constructor's state in a public getter named after the state's type
array
Has a public method you can use to set a new value for that public getter
setArray
Has a setter for that getter so you can assign a new value directly
Any other public getters that should be set by you in some cases also have setters and set<Property> methods
location, setLocation
Has at least one additional getter property that you can't (and shouldn't) set directly
status, item
Has one or more public methods that expose core functionality
navigate, next, previous, random
Either has no side effects or has side effects that can be cleaned up with a stop method
no side effects
Uses the sentence template to decide what state type should be accepted by a constructor
"An array can be navigated."
Constructor does not accept options that only customize the behavior of public methods, it allows those options to be passed to the method itself as a parameter.
next(options), previous(options)
Named after its core action, proper-cased and suffixed with able

What is Baleada?

Edit doc on GitHub

ON THIS PAGE

NavigateableConstruct a Navigateable instanceNavigateable constructor optionsState and methodsHow Navigateable navigatesUsing with TypeScriptAPI design compliance