Skip to content

Commit

Permalink
Add option to shorten a display name (#33)
Browse files Browse the repository at this point in the history
* Add option to shorten a display name

* Redesign short display name approach

* Fix comment

* typo

* PR feedback
  • Loading branch information
garethj2 authored Mar 1, 2024
1 parent 688bb15 commit d4a4ddc
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 11 deletions.
12 changes: 12 additions & 0 deletions spec/core/HDict.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { HGrid } from '../../src/core/HGrid'
import { HVal } from '../../src/core/HVal'
import { HSymbol } from '../../src/core/HSymbol'
import { HNamespace } from '../../src/core/HNamespace'
import { HRef } from '../../src/core/HRef'
import { HaysonDict } from '../../src/core/hayson'
import { HFilter } from '../../src/filter/HFilter'
import '../matchers'
Expand Down Expand Up @@ -814,6 +815,17 @@ describe('HDict', function (): void {
)
expect(i18n).toHaveBeenCalledWith('pod', 'key')
})

it('returns a shortened display name from a macro', function (): void {
const dict = new HDict({
navName: HStr.make('a nav name'),
equipRef: HRef.make('equipRef'),
siteRef: HRef.make('siteRef'),
disMacro: HStr.make(' $siteRef $equipRef $navName '),
})

expect(dict.toDis({ short: true })).toBe('a nav name')
})
}) // #toDis()

describe('#diff()', function (): void {
Expand Down
24 changes: 24 additions & 0 deletions spec/core/Util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,23 @@ describe('util', function (): void {
'dis'
)
})

it('returns navName', function (): void {
expect(dictToDis(new HDict({ navName: 'navName' }))).toBe('navName')
})

it('returns shortened display name with starting refs skipped', function (): void {
const dict = new HDict({
navName: HStr.make('a nav name'),
equipRef: HRef.make('equipRef'),
siteRef: HRef.make('siteRef'),
disMacro: HStr.make(' $siteRef $equipRef $navName '),
})

expect(
dictToDis(dict, undefined, undefined, /*shorten*/ true)
).toBe('a nav name')
})
}) // dictToDis()

describe('macro()', function (): void {
Expand Down Expand Up @@ -717,6 +734,13 @@ describe('util', function (): void {
'IdId Id something'
)
})

it('trims any whitespace', function (): void {
const scope = new HDict({ ref: HRef.make('id', 'Id') })
expect(
macro(' $ref$ref $ref something ', makeGetValue(scope))
).toBe('IdId Id something')
})
}) // macro()

describe('disKey()', function (): void {
Expand Down
7 changes: 5 additions & 2 deletions src/core/HDict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -840,20 +840,23 @@ export class HDict implements HVal, Iterable<HValRow> {
* @param options.name Optional tag name.
* @param options.def Optional default value.
* @param options.i18n Optional function to get localized strings.
* @returns The display string
* @param options.short Optional flag to automatically shorten the display name.
* @returns The display string.
*/
public toDis({
name,
def,
i18n,
short,
}: {
name?: string
def?: string
i18n?: LocalizedCallback
short?: boolean
} = {}): string {
return name
? this.get(name)?.toString() ?? def ?? ''
: dictToDis(this, def, i18n)
: dictToDis(this, def, i18n, short)
}

/**
Expand Down
45 changes: 36 additions & 9 deletions src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,31 +375,44 @@ export interface LocalizedCallback {
* 3. 'disKey' maps to qname locale key.
* 4. 'name' tag.
* 5. 'tag' tag.
* 6. 'id' tag.
* 7. default
* 6. 'navName' tag.
* 7. 'id' tag.
* 8. default
*
* If a short name is specified, resolving `disMacro` is given a
* lower precedence.
*
* @see {@link HDict.toDis}
* @see {@link macro}
* @see {@link disKey}
*
* @param dict The dict to use.
* @param def The default fallback value.
* @param i18n The localization callback.
* @param def Optional default fallback value.
* @param i18n Optional localization callback.
* @param short Optional flag to shorten the display name.
* @return The display string value.
*/
export function dictToDis(
dict: HDict,
def?: string,
i18n?: LocalizedCallback
i18n?: LocalizedCallback,
short?: boolean
): string {
let val = dict.get('dis')
if (isHVal(val)) {
return val.toString()
}

val = dict.get('disMacro')
if (isHVal(val)) {
return macro(val.toString(), (key: string) => dict.get(key), i18n)
const runMacro = (val: HVal) =>
macro(val.toString(), (val) => dict.get(val), i18n)

// If we're wanting a short display name then try other
// display names first.
if (!short) {
val = dict.get('disMacro')
if (isHVal(val)) {
return runMacro(val)
}
}

val = dict.get('disKey')
Expand All @@ -424,6 +437,20 @@ export function dictToDis(
return val.toString()
}

val = dict.get('navName')
if (isHVal(val)) {
return val.toString()
}

// If by this point we don't have a short display name
// then fallback to running the macro as a last resort.
if (short) {
val = dict.get('disMacro')
if (isHVal(val)) {
return runMacro(val)
}
}

val = dict.get('id')
if (valueIsKind<HRef>(val, Kind.Ref)) {
return val.dis
Expand Down Expand Up @@ -480,7 +507,7 @@ export function macro(
)
}

return result
return result.trim()
}

/**
Expand Down

0 comments on commit d4a4ddc

Please sign in to comment.