Skip to content

Commit 3c78cfb

Browse files
committed
Implement formatShallow for all primitive representations
1 parent 4c3ef4e commit 3c78cfb

File tree

7 files changed

+45
-4
lines changed

7 files changed

+45
-4
lines changed

src/accessors/element.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
ValueRepresentation,
1717
} from '../value.js'
1818
import { UndefinedRepresentation } from '../values/primitives/undefined.ts'
19+
import type { Formatter } from '../formatter.ts'
1920

2021
export class SparseValueRepresentation implements CommonRepresentation, ShallowFunctionality {
2122
readonly #sparse: undefined
@@ -27,6 +28,10 @@ export class SparseValueRepresentation implements CommonRepresentation, ShallowF
2728
return unequal
2829
}
2930

31+
formatShallow(formatter: Formatter): void {
32+
formatter.append(formatter.theme.array.sparse)
33+
}
34+
3035
serializeShallow(encoder: Encoder): ShallowSerializationResult {
3136
encoder.staticType(staticTypeTable.undefined)
3237
return finished

src/accessors/test/element.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { Encoder } from '../../encoder.ts'
66
import { strictlyEqual, unequal, deeplyEqual } from '../../comparison.ts'
77
import { partial, finished } from '../../serialization-result.ts'
88
import { staticTypeTable } from '../../serialization-types.ts'
9+
import { deriveTheme } from '../../theme.ts'
10+
import { Formatter } from '../../formatter.ts'
911

1012
// SparseValueRepresentation Tests
1113
test('SparseValueRepresentation - compare returns strictlyEqual for other sparse values', (t) => {
@@ -29,6 +31,15 @@ test('SparseValueRepresentation - compare returns unequal for other values', (t)
2931
t.is(sparse.compare(str), unequal)
3032
})
3133

34+
test('SparseValueRepresentation - formatShallow formats as sparse', (t) => {
35+
const sparse = new SparseValueRepresentation()
36+
const theme = deriveTheme()
37+
const formatter = new Formatter(theme)
38+
sparse.formatShallow(formatter)
39+
const rendered = formatter.close().render()
40+
t.is(rendered, theme.array.sparse)
41+
})
42+
3243
test('SparseValueRepresentation - serializeShallow encodes as undefined', (t) => {
3344
const sparse = new SparseValueRepresentation()
3445

src/accessors/test/property.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ test('NamedPropertyAccessor - serialize encodes key as string and delegates to v
7474
// Create a mock value with serializeShallow
7575
const mockValue = {
7676
compare: (): Comparison => strictlyEqual,
77+
formatShallow: () => {},
7778
serializeShallow: (encoder: Encoder): typeof finished => {
7879
encoder.uint8Array(new Uint8Array([42]))
7980
return finished

src/theme.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export function normalizeTheme<T extends ThemeRecord>(root: T): NormalizedTheme<
9696
const defaultTheme = normalizeTheme({
9797
array: {
9898
bracket: { open: '[', close: ']' },
99+
sparse: '«empty item»',
99100
},
100101
aspect: { separator: '---' },
101102
bigInt: { open: '', close: '' },
@@ -119,6 +120,7 @@ const defaultTheme = normalizeTheme({
119120
constructor: { open: '(', close: ')' },
120121
name: { open: '', close: '' },
121122
},
123+
external: '«external»',
122124
function: {
123125
name: { open: '', close: '' },
124126
stringTag: { open: '', close: '' },
@@ -133,10 +135,10 @@ const defaultTheme = normalizeTheme({
133135
number: { open: '', close: '' },
134136
object: {
135137
bracket: { open: '{', close: '}' },
136-
constructorName: { open: '', close: '', empty: '\u00ABempty constructor name\u00BB' },
137-
nullPrototype: '\u00ABnull prototype\u00BB',
138-
stringTag: { open: '@', close: '', empty: '\u00ABempty string tag\u00BB' },
139-
secondaryStringTag: { open: '@', close: '', empty: '\u00ABempty string tag\u00BB' },
138+
constructorName: { open: '', close: '', empty: '«empty constructor name»' },
139+
nullPrototype: '«null prototype»',
140+
stringTag: { open: '@', close: '', empty: '«empty string tag»' },
141+
secondaryStringTag: { open: '@', close: '', empty: '«empty string tag»' },
140142
},
141143
property: {
142144
after: ',',

src/value.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { SymbolPropertyAccessor } from './accessors/property.ts'
22
import type { Comparison } from './comparison.ts'
33
import type { Encoder } from './encoder.ts'
4+
import type { Formatter } from './formatter.ts'
45
import type { ShallowSerializationResult, SerializationResult } from './serialization-result.ts'
56

67
export type CommonRepresentation = {
@@ -11,6 +12,7 @@ export type CommonRepresentation = {
1112
}
1213

1314
export type ShallowFunctionality = {
15+
formatShallow(formatter: Formatter): void
1416
serializeShallow(encoder: Encoder): ShallowSerializationResult
1517
}
1618

src/values/nodejs/external.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { staticTypeTable } from '../../serialization-types.ts'
77
import { type ShallowSerializationResult, finished } from '../../serialization-result.ts'
88
import type { Decoder } from '../../decoder.ts'
99
import type { DeserializationContext } from '../../deserialization-context.ts'
10+
import type { Formatter } from '../../formatter.ts'
1011

1112
export class ExternalRepresentation implements CommonRepresentation, ShallowFunctionality {
1213
static deserialize(context: DeserializationContext, decoder: Decoder): ExternalRepresentation {
@@ -36,6 +37,10 @@ export class ExternalRepresentation implements CommonRepresentation, ShallowFunc
3637
return this.#value === other.#value ? strictlyEqual : unequal
3738
}
3839

40+
formatShallow(formatter: Formatter): void {
41+
formatter.append(formatter.theme.external)
42+
}
43+
3944
serializeShallow(encoder: Encoder): ShallowSerializationResult {
4045
encoder.staticType(staticTypeTable.external).annotations({
4146
p: this.pointer,

src/values/nodejs/test/external.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { possiblyEqual, strictlyEqual, unequal } from '../../../comparison.ts'
88
import { staticTypeTable } from '../../../serialization-types.ts'
99
import { snapshotEncoded } from '../../test/helpers/snapshot-encoded.ts'
1010
import { finished } from '../../../serialization-result.ts'
11+
import { deriveTheme } from '../../../theme.ts'
12+
import { Formatter } from '../../../formatter.ts'
1113

1214
// @ts-expect-error ts2307: Suppress error about missing import
1315
const refNapi = await (import('ref-napi') as Promise<{ default: { instance: object } }>)
@@ -95,6 +97,19 @@ test('compare returns possiblyEqual when either context is deserialized', (t) =>
9597
)
9698
})
9799

100+
// Formatting tests
101+
test('formatShallow applies the correct theme for external values', (t) => {
102+
const context = new DescriptionContext()
103+
const externalRep = context.represent(externalValue) as ExternalRepresentation
104+
105+
const formatter = new Formatter(deriveTheme())
106+
externalRep.formatShallow(formatter)
107+
const rendered = formatter.close().render()
108+
109+
// Check if the theme was applied correctly
110+
t.is(rendered, formatter.theme.external)
111+
})
112+
98113
// Serialization tests
99114
test('serializeShallow uses the external static type', (t) => {
100115
const context = new DescriptionContext()

0 commit comments

Comments
 (0)