this repo has no description
at main 62 lines 2.2 kB view raw
1import assert from 'node:assert' 2import { describe, test } from 'node:test' 3import { TaggedStringParser } from '../../src/TaggedStringParser.ts' 4import type { EntitySchema } from '../../src/types.ts' 5 6describe('Formatter Functions', () => { 7 describe('basic formatters', () => { 8 test('should apply formatter to entity values', () => { 9 const schema: EntitySchema = { 10 operation: { 11 type: 'string', 12 format: (val) => `**${val}**`, 13 }, 14 count: { 15 type: 'number', 16 format: (val) => `[${val} items]`, 17 }, 18 } 19 const parser = new TaggedStringParser({ schema }) 20 const result = parser.parse('[operation:OP-123] has [count:5]') 21 22 assert.strictEqual(result.entities[0].formattedValue, '**OP-123**') 23 assert.strictEqual(result.entities[1].formattedValue, '[5 items]') 24 }) 25 26 test('should apply formatter with uppercase transformation', () => { 27 const schema: EntitySchema = { 28 name: { 29 type: 'string', 30 format: (val) => String(val).toUpperCase(), 31 }, 32 } 33 const parser = new TaggedStringParser({ schema }) 34 const result = parser.parse('[name:alice]') 35 36 assert.strictEqual(result.entities[0].formattedValue, 'ALICE') 37 }) 38 }) 39 40 describe('entities without formatters', () => { 41 test('should default to string conversion when no formatter provided', () => { 42 const schema: EntitySchema = { 43 count: 'number', 44 enabled: 'boolean', 45 } 46 const parser = new TaggedStringParser({ schema }) 47 const result = parser.parse('[count:42] [enabled:true]') 48 49 assert.strictEqual(result.entities[0].formattedValue, '42') 50 assert.strictEqual(result.entities[1].formattedValue, 'true') 51 }) 52 53 test('should convert unknown entities to string', () => { 54 const parser = new TaggedStringParser() 55 const result = parser.parse('[count:42] [enabled:true] [name:test]') 56 57 assert.strictEqual(result.entities[0].formattedValue, '42') 58 assert.strictEqual(result.entities[1].formattedValue, 'true') 59 assert.strictEqual(result.entities[2].formattedValue, 'test') 60 }) 61 }) 62})