simplify algorithm

pull/16748/head
Rich Harris 5 days ago
parent 3ef5612021
commit 1c91c597f7

@ -408,27 +408,23 @@ export class TreeHeadState {
* @param {number[]} path * @param {number[]} path
*/ */
set_title(value, path) { set_title(value, path) {
// perform a depth-first (lexicographic) comparison using the path. Reject sets const current = this.#title.path;
// from earlier than or equal to the current value.
const current_path = this.#title.path; let i = 0;
let l = Math.min(path.length, current.length);
const max_len = Math.max(path.length, current_path.length);
for (let i = 0; i < max_len; i++) { // skip identical prefixes - [1, 2, 3, ...] === [1, 2, 3, ...]
const contender_segment = path[i]; while (i < l && path[i] === current[i]) i += 1;
const current_segment = current_path[i];
if (path[i] === undefined) return;
// contender shorter than current and all previous segments equal -> earlier
if (contender_segment === undefined) return; // replace title if
// current shorter than contender and all previous segments equal -> contender is later // - incoming path is longer - [7, 8, 9] > [7, 8]
if (current_segment === undefined || contender_segment > current_segment) { // - incoming path is later - [7, 8, 9] > [7, 8, 8]
if (current[i] === undefined || path[i] > current[i]) {
this.#title.path = path; this.#title.path = path;
this.#title.value = value; this.#title.value = value;
return;
}
if (contender_segment < current_segment) return;
// else equal -> continue
} }
// paths are equal -> keep current value (do nothing)
} }
/** /**

Loading…
Cancel
Save