6.7. Cursor Iteration Operation
The steps to iterate a cursor with targetRealm, cursor, an optional key and primaryKey to iterate to, and an optional count are as follows.
Let source be cursor’s source.
Let direction be cursor’s direction.
Assert: if primaryKey is given, source is an index and direction is
["next"](#dom-idbcursordirection-next)
or["prev"](#dom-idbcursordirection-prev)
.Let records be the list of records in source.
records is always sorted in ascending key order. In the case of source being an index, records is secondarily sorted in ascending value order (where the value in an index is the key of the record in the referenced object store).
Let range be cursor’s range.
Let position be cursor’s position.
Let object store position be cursor’s object store position.
If count is not given, let count be 1.
While count is greater than 0:
Switch on direction:
["next"](#dom-idbcursordirection-next)
Let found record be the first record in records which satisfy all of the following requirements:
If key is defined, the record’s key is greater than or equal to key.
If primaryKey is defined, the record’s key is equal to key and the record’s value is greater than or equal to primaryKey, or the record’s key is greater than key.
If position is defined, and source is an object store, the record’s key is greater than position.
If position is defined, and source is an index, the record’s key is equal to position and the record’s value is greater than object store position or the record’s key is greater than position.
The record’s key is in range.
`["nextunique"](#dom-idbcursordirection-nextunique)`
Let found record be the first record in records which satisfy all of the following requirements:
- If key is defined, the record’s key is [greater than](#greater-than) or [equal to](#equal-to) key.
- If position is defined, the record’s key is [greater than](#greater-than) position.
- The record’s key is [in](#in) range.
`["prev"](#dom-idbcursordirection-prev)`
Let found record be the last record in records which satisfy all of the following requirements:
- If key is defined, the record’s key is [less than](#less-than) or [equal to](#equal-to) key.
- If primaryKey is defined, the record’s key is [equal to](#equal-to) key and the record’s value is [less than](#less-than) or [equal to](#equal-to) primaryKey, or the record’s key is [less than](#less-than) key.
- If position is defined, and source is an [object store](#object-store), the record’s key is [less than](#less-than) position.
- If position is defined, and source is an [index](#index-concept), the record’s key is [equal to](#equal-to) position and the record’s value is [less than](#less-than) object store position or the record’s key is [less than](#less-than) position.
- The record’s key is [in](#in) range.
`["prevunique"](#dom-idbcursordirection-prevunique)`
Let temp record be the last record in records which satisfy all of the following requirements:
- If key is defined, the record’s key is [less than](#less-than) or [equal to](#equal-to) key.
- If position is defined, the record’s key is [less than](#less-than) position.
- The record’s key is [in](#in) range.
If temp record is defined, let found record be the first record in records whose key is [equal to](#equal-to) temp record’s key.
Iterating with `["prevunique"](#dom-idbcursordirection-prevunique)` visits the same records that `["nextunique"](#dom-idbcursordirection-nextunique)` visits, but in reverse order.
2. If found record is not defined, then:
1. Set cursor’s [key](#cursor-key) to undefined.
2. If source is an [index](#index-concept), set cursor’s [object store position](#cursor-object-store-position) to undefined.
3. If cursor’s [key only flag](#cursor-key-only-flag) is unset, set cursor’s [value](#cursor-value) to undefined.
4. Return null.
3. Let position be found record’s key.
4. If source is an [index](#index-concept), let object store position be found record’s value.
5. Decrease count by 1.
Set cursor’s position to position.
If source is an index, set cursor’s object store position to object store position.
Set cursor’s key to found record’s key.
If cursor’s key only flag is unset, then:
Let serialized be found record’s referenced value.
Set cursor’s value to ! StructuredDeserialize(serialized, targetRealm)
Set cursor’s got value flag.
Return cursor.