Skip to content

Commit

Permalink
Added Turnout Item Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
dcyoung-dev committed Jan 5, 2024
1 parent 047dc9a commit 4721c29
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/parsers/genericParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { eraseParser, storeParser } from './eeproms/index.js'
import { powerParser } from './powers/index.js'
import { locoParser, throttleParser } from './throttles/index.js'
import { decoderAddressParser } from './decoders/index.js'
import { turnoutDCCParser, turnoutListParser, turnoutParser } from './turnouts/index.js'
import { turnoutDCCParser, turnoutItemParser, turnoutListParser, turnoutParser } from './turnouts/index.js'

type ParserFunction = (command: string) => ParserResult<any>
type ParseResult = (command: string) => Promise<ParserResult<any>>
Expand Down Expand Up @@ -45,6 +45,7 @@ export const genericParser: GenericParser = () => {
decoderAddressParser,
turnoutParser,
turnoutListParser,
turnoutItemParser,
turnoutDCCParser
]

Expand Down
1 change: 1 addition & 0 deletions src/parsers/turnouts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './turnoutParser.js'
export * from './turnoutDCCParser.js'
export * from './turnoutListParser.js'
export * from './turnoutItemParser.js'
47 changes: 47 additions & 0 deletions src/parsers/turnouts/turnoutItemParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Command, parseCommand, removeDoubleQuotes } from '../../utils/index.js'
import {
FunctionName,
ParserResult,
ParserStatus
} from '../../types/index.js'
import { ParserAttributeError, ParserKeyError } from '../errors/index.js'
import { TurnoutState } from '../../commands/index.js'

interface TurnoutItemParams {
turnoutId: number
thrown: TurnoutState
description: string
}

export type TurnoutItemResult = ParserResult<TurnoutItemParams>
const turnoutItemParserKey = 'jT'

const parseFromCommand: (params: Command) => TurnoutItemResult = ({ key, attributes }) => {
const [turnoutId, state, description] = attributes

if (key !== turnoutItemParserKey || attributes.length !== 3) {
throw new ParserKeyError('turnoutItemParser', key)
}

if (!description.includes('"')) {
throw new ParserAttributeError('description', description, 'description must be wrapped in double quotes')
}

const thrown = parseInt(state)

return {
key: turnoutItemParserKey,
parser: FunctionName.TURNOUT_ITEM,
status: ParserStatus.SUCCESS,
params: {
turnoutId: parseInt(turnoutId),
thrown: thrown === 0 ? TurnoutState.CLOSED : TurnoutState.THROWN,
description: removeDoubleQuotes(description)
}
}
}

export const turnoutItemParser: (command: string) => TurnoutItemResult = (command) => {
const commandParams = parseCommand(command)
return parseFromCommand(commandParams)
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export enum FunctionName {
DECODER_ADDRESS = 'decoderAddress',
TURNOUT = 'turnoutParser',
TURNOUT_LIST = 'turnoutListParser',
TURNOUT_ITEM = 'turnoutItemParser',
TURNOUT_DCC = 'turnoutDCCParser'
}

Expand Down
38 changes: 38 additions & 0 deletions tests/unit/parsers/genericParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,44 @@ describe('genericParser()', () => {
status: ParserStatus.SUCCESS
}
},
{
command: '<jT 1 22 333 4444>',
expectation: {
key: 'jT',
parser: FunctionName.TURNOUT_LIST,
params: {
turnoutIds: [1, 22, 333, 4444]
},
status: ParserStatus.SUCCESS
}
},
{
command: '<jT 70 1 "Turnout 70 Thrown">',
expectation: {
key: 'jT',
parser: FunctionName.TURNOUT_ITEM,
status: ParserStatus.SUCCESS,
params: {
turnoutId: 70,
thrown: TurnoutState.THROWN,
description: 'Turnout 70 Thrown'
}
}
},

{
command: '<jT 70 1 "10">',
expectation: {
key: 'jT',
parser: FunctionName.TURNOUT_ITEM,
status: ParserStatus.SUCCESS,
params: {
turnoutId: 70,
thrown: TurnoutState.THROWN,
description: '10'
}
}
},
{
command: '<H 1 0>',
expectation: {
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/parsers/turnouts/turnoutItemParser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
FunctionName, ParserKeyError,
ParserStatus,
turnoutItemParser, TurnoutItemResult,
TurnoutState
} from '../../../../src'

describe('turnoutItemParser()', function () {
it('parses `<jT 1 0 "Description">`', () => {
const result = turnoutItemParser('<jT 1 0 "Description">')

const expected: TurnoutItemResult = {
key: 'jT',
parser: FunctionName.TURNOUT_ITEM,
status: ParserStatus.SUCCESS,
params: {
turnoutId: 1,
thrown: TurnoutState.CLOSED,
description: 'Description'
}
}
expect(result).toEqual(expected)
})

describe('with incorrect key', function () {
it('throws a ParserKeyError', function () {
expect(() => {
turnoutItemParser('<incorrect-key>')
}).toThrowError(ParserKeyError)
})
})
})

0 comments on commit 4721c29

Please sign in to comment.