-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
047dc9a
commit 4721c29
Showing
6 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './turnoutParser.js' | ||
export * from './turnoutDCCParser.js' | ||
export * from './turnoutListParser.js' | ||
export * from './turnoutItemParser.js' |
This file contains 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,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) | ||
} |
This file contains 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
This file contains 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
This file contains 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,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) | ||
}) | ||
}) | ||
}) |