-
Notifications
You must be signed in to change notification settings - Fork 8
Add relative haystack filter generation #39
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
Merged
Changes from all commits
Commits
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
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,91 @@ | ||
| /* | ||
| * Copyright (c) 2025, J2 Innovations. All Rights Reserved | ||
| */ | ||
|
|
||
| import { HDict } from '../../src/core/dict/HDict' | ||
| import { HRef } from '../../src/core/HRef' | ||
| import { makeRelativeHaystackFilter } from '../../src/filter/util' | ||
| import { HMarker } from '../../src/core/HMarker' | ||
| import { HStr } from '../../src/core/HStr' | ||
|
|
||
| describe('haystack', () => { | ||
| describe('makeRelativeHaystackFilter()', () => { | ||
| it('returns a haystack filter with dis', () => { | ||
| expect( | ||
| makeRelativeHaystackFilter( | ||
| new HDict({ | ||
| id: HRef.make('id'), | ||
| dis: 'an equip', | ||
| equip: HMarker.make(), | ||
| }) | ||
| ) | ||
| ).toEqual('equip and dis == "an equip"') | ||
| }) | ||
|
|
||
| it('returns a haystack filter without a display name with the option disabled', () => { | ||
| expect( | ||
| makeRelativeHaystackFilter( | ||
| new HDict({ | ||
| id: HRef.make('id'), | ||
| dis: 'an equip', | ||
| equip: HMarker.make(), | ||
| }), | ||
| { | ||
| useDisplayName: false, | ||
| } | ||
| ) | ||
| ).toEqual('equip') | ||
| }) | ||
|
|
||
| it('returns a haystack filter with a nav name', () => { | ||
| expect( | ||
| makeRelativeHaystackFilter( | ||
| new HDict({ | ||
| id: HRef.make('id'), | ||
| navName: 'an equip', | ||
| equip: HMarker.make(), | ||
| }) | ||
| ) | ||
| ).toEqual('equip and navName == "an equip"') | ||
| }) | ||
|
|
||
| it('returns a haystack filter with a point kind', () => { | ||
| expect( | ||
| makeRelativeHaystackFilter( | ||
| new HDict({ | ||
| id: HRef.make('id'), | ||
| navName: 'a point', | ||
| point: HMarker.make(), | ||
| kind: HStr.make('Number'), | ||
| }) | ||
| ) | ||
| ).toEqual('point and navName == "a point" and kind == "Number"') | ||
| }) | ||
|
|
||
| it('returns a haystack filter without the point kind and the option disabled', () => { | ||
| expect( | ||
| makeRelativeHaystackFilter( | ||
| new HDict({ | ||
| id: HRef.make('id'), | ||
| navName: 'a point', | ||
| point: HMarker.make(), | ||
| kind: HStr.make('Number'), | ||
| }), | ||
| { | ||
| useKind: false, | ||
| } | ||
| ) | ||
| ).toEqual('point and navName == "a point"') | ||
| }) | ||
|
|
||
| it('returns a haystack filter with an absolute id as a fallback', () => { | ||
| expect( | ||
| makeRelativeHaystackFilter( | ||
| new HDict({ | ||
| id: HRef.make('id'), | ||
| }) | ||
| ) | ||
| ).toEqual('id == @id') | ||
| }) | ||
| }) // makeRelativeHaystackFilter() | ||
| }) |
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,102 @@ | ||
| /* | ||
| * Copyright (c) 2025, J2 Innovations. All Rights Reserved | ||
| */ | ||
|
|
||
| import { HDict } from '../core/dict/HDict' | ||
| import { HMarker } from '../core/HMarker' | ||
| import { HRef } from '../core/HRef' | ||
| import { HStr } from '../core/HStr' | ||
| import { valueIsKind } from '../core/HVal' | ||
| import { Kind } from '../core/Kind' | ||
| import { HFilterBuilder } from '../filter/HFilterBuilder' | ||
|
|
||
| /** | ||
| * Relativization options. | ||
| */ | ||
| export interface RelativizeOptions { | ||
| /** | ||
| * True (or undefined) if the display name should be used in the relativization. | ||
| */ | ||
| useDisplayName?: boolean | ||
|
|
||
| /** | ||
| * True (or undefined) if a point's kind should be used in the relativization. | ||
| */ | ||
| useKind?: boolean | ||
| } | ||
|
|
||
| /** | ||
| * Makes a relative haystack filter from a record. | ||
| * | ||
| * @param record The record. | ||
| * @returns A haystack filter. | ||
| */ | ||
| export function makeRelativeHaystackFilter( | ||
| record: HDict, | ||
| options?: RelativizeOptions | ||
| ): string { | ||
| const useDisplayName = options?.useDisplayName ?? true | ||
| const useKind = options?.useKind ?? true | ||
|
|
||
| const builder = new HFilterBuilder() | ||
|
|
||
| // Build the marker tags. | ||
| for (const { name, value } of record) { | ||
| if (valueIsKind<HMarker>(value, Kind.Marker)) { | ||
| if (!builder.isEmpty()) { | ||
| builder.and() | ||
| } | ||
|
|
||
| builder.has(name) | ||
| } | ||
| } | ||
|
|
||
| if (useDisplayName) { | ||
| // Build the display name match if one is available. | ||
| if (!addDisplayNameToFilter(record, builder, 'dis')) { | ||
| if (!addDisplayNameToFilter(record, builder, 'name')) { | ||
| if (!addDisplayNameToFilter(record, builder, 'tag')) { | ||
| addDisplayNameToFilter(record, builder, 'navName') | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (useKind && record.has('point') && record.has('kind')) { | ||
| addPointKindToFilter(record, builder) | ||
| } | ||
|
|
||
| if (builder.isEmpty() && record.has('id')) { | ||
| builder.equals('id', record.get('id') as HRef) | ||
| } | ||
|
|
||
| return builder.build() | ||
| } | ||
|
|
||
| function addPointKindToFilter(record: HDict, builder: HFilterBuilder): void { | ||
| const kind = record.get<HStr>('kind')?.value | ||
| if (kind) { | ||
| if (!builder.isEmpty()) { | ||
| builder.and() | ||
| } | ||
| builder.equals('kind', kind) | ||
| } | ||
| } | ||
|
|
||
| function addDisplayNameToFilter( | ||
| record: HDict, | ||
| builder: HFilterBuilder, | ||
| tagName: string | ||
| ): boolean { | ||
| // Build the display name match. | ||
| const name = record.get<HStr>(tagName)?.value | ||
| if (name) { | ||
| if (!builder.isEmpty()) { | ||
| builder.and() | ||
| } | ||
|
|
||
| builder.equals(tagName, name) | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
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
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.