Skip to content

Commit

Permalink
[util] Stop counting equal segments after first miss
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2020
1 parent ef9aa91 commit 57614d0
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/@sanity/util/src/pathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ export function isEqual(path: Path, otherPath: Path): boolean {
}

export function numEqualSegments(path: Path, otherPath: Path): number {
return path.filter((segment, i) => isSegmentEqual(segment, otherPath[i])).length
const length = Math.min(path.length, otherPath.length)
for (let i = 0; i < length; i++) {
if (!isSegmentEqual(path[i], otherPath[i])) {
return i
}
}
return length
}

export function isSegmentEqual(segmentA: PathSegment, segmentB: PathSegment): boolean {
Expand Down

0 comments on commit 57614d0

Please sign in to comment.