-
Notifications
You must be signed in to change notification settings - Fork 667
[lookup-by-path] Add getFirstDifferenceInCommonNodes, tree
#5223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dmichon-msft
merged 2 commits into
microsoft:main
from
dmichon-msft:git-first-difference
May 7, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
common/changes/@rushstack/lookup-by-path/git-first-difference_2025-05-07-22-00.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "packageName": "@rushstack/lookup-by-path", | ||
| "comment": "Add `getFirstDifferenceInCommonNodes` API.", | ||
| "type": "minor" | ||
| } | ||
| ], | ||
| "packageName": "@rushstack/lookup-by-path" | ||
| } |
10 changes: 10 additions & 0 deletions
10
common/changes/@rushstack/lookup-by-path/git-first-difference_2025-05-07-22-01.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "packageName": "@rushstack/lookup-by-path", | ||
| "comment": "Expose `tree` accessor on `IReadonlyLookupByPath` for a readonly view of the raw tree.", | ||
| "type": "minor" | ||
| } | ||
| ], | ||
| "packageName": "@rushstack/lookup-by-path" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
libraries/lookup-by-path/src/getFirstDifferenceInCommonNodes.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| import type { IReadonlyPathTrieNode } from './LookupByPath'; | ||
|
|
||
| /** | ||
| * Options for the getFirstDifferenceInCommonNodes function. | ||
| * @beta | ||
| */ | ||
| export interface IGetFirstDifferenceInCommonNodesOptions<TItem extends {}> { | ||
| /** | ||
| * The first node to compare. | ||
| */ | ||
| first: IReadonlyPathTrieNode<TItem>; | ||
| /** | ||
| * The second node to compare. | ||
| */ | ||
| second: IReadonlyPathTrieNode<TItem>; | ||
| /** | ||
| * The path prefix to the current node. | ||
| * @defaultValue '' | ||
| */ | ||
| prefix?: string; | ||
| /** | ||
| * The delimiter used to join path segments. | ||
| * @defaultValue '/' | ||
| */ | ||
| delimiter?: string; | ||
| /** | ||
| * A function to compare the values of the nodes. | ||
| * If not provided, strict equality (===) is used. | ||
| */ | ||
| equals?: (a: TItem, b: TItem) => boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Recursively compares two path tries to find the first shared node with a different value. | ||
| * | ||
| * @param options - The options for the comparison | ||
| * @returns The path to the first differing node, or undefined if they are identical | ||
| * | ||
| * @remarks | ||
| * Ignores any nodes that are not shared between the two tries. | ||
| * | ||
| * @beta | ||
| */ | ||
| export function getFirstDifferenceInCommonNodes<TItem extends {}>( | ||
| options: IGetFirstDifferenceInCommonNodesOptions<TItem> | ||
| ): string | undefined { | ||
| const { first, second, prefix = '', delimiter = '/', equals = defaultEquals } = options; | ||
|
|
||
| return getFirstDifferenceInCommonNodesInternal({ | ||
| first, | ||
| second, | ||
| prefix, | ||
| delimiter, | ||
| equals | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Recursively compares two path tries to find the first shared node with a different value. | ||
| * | ||
| * @param options - The options for the comparison | ||
| * @returns The path to the first differing node, or undefined if they are identical | ||
| * | ||
| * @remarks | ||
| * Ignores any nodes that are not shared between the two tries. | ||
| * Separated out to avoid redundant parameter defaulting in the recursive calls. | ||
| */ | ||
| function getFirstDifferenceInCommonNodesInternal<TItem extends {}>( | ||
| options: Required<IGetFirstDifferenceInCommonNodesOptions<TItem>> | ||
| ): string | undefined { | ||
| const { first, second, prefix, delimiter, equals } = options; | ||
|
|
||
| const firstItem: TItem | undefined = first.value; | ||
| const secondItem: TItem | undefined = second.value; | ||
|
|
||
| if (firstItem !== undefined && secondItem !== undefined && !equals(firstItem, secondItem)) { | ||
| // If this value was present in both tries with different values, return the prefix for this node. | ||
| return prefix; | ||
| } | ||
|
|
||
| const { children: firstChildren } = first; | ||
| const { children: secondChildren } = second; | ||
|
|
||
| if (firstChildren && secondChildren) { | ||
| for (const [key, firstChild] of firstChildren) { | ||
| const secondChild: IReadonlyPathTrieNode<TItem> | undefined = secondChildren.get(key); | ||
| if (!secondChild) { | ||
| continue; | ||
| } | ||
| const result: string | undefined = getFirstDifferenceInCommonNodesInternal({ | ||
| first: firstChild, | ||
| second: secondChild, | ||
| prefix: key, | ||
| delimiter, | ||
| equals | ||
| }); | ||
|
|
||
| if (result !== undefined) { | ||
| return prefix ? `${prefix}${delimiter}${result}` : result; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| /** | ||
| * Default equality function for comparing two items, using strict equality. | ||
| * @param a - The first item to compare | ||
| * @param b - The second item to compare | ||
| * @returns True if the items are reference equal, false otherwise | ||
| */ | ||
| function defaultEquals<TItem>(a: TItem, b: TItem): boolean { | ||
| return a === b; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.