diff --git a/CHANGELOG.md b/CHANGELOG.md index bb3bb43..02e45dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,7 +72,7 @@ A decimal key naming a tag the table knows, `{ 1063: … }`, is refused in favour of the name, and so are `alert_on_msg_delivery` and `failed_broadcast_area_identifier` in favour of `alert_on_message_delivery` and `broadcast_area_identifier`, the names they read back under. The - two alternate names are gone from `tlvs` too, which is now typed by `TlvName`: index it with one. + two alternate names are gone from `tlvs` too, which is now typed by `TlvName`: narrow a `string` with `isTlvName()` before indexing it. - `cmds.broadcast_sm_resp.tlvMap` is removed; nothing read it. ## 0.5.0 diff --git a/README.md b/README.md index 864aa30..ba17dc9 100644 --- a/README.md +++ b/README.md @@ -655,7 +655,7 @@ if (isCommand(pduObj, 'submit_sm')) { | Messages | `encodeMessage`, `decodeMessage`, `splitMessage`, `bitCount`, `messageOctets`, `concatOf`, `concatInfo`, `detect`, `unencodable`, `messageClassOf`, `dataCodingByEncoding`, `encodingByDataCoding` | | Receipts | `dlrFromPdu`, `parseReceipt`, `receiptCodes` | | Time and ids | `smppDate`, `smppTime`, `uuidv7` | -| Spec tables | `cmds`, `consts`, `encodings`, `errors`, `tlvs`, `types`, the `cmdsById`, `constsById`, `errorsById` and `tlvsById` maps, and all of them grouped as `defs`. `isCommandName`, `isErrorName`, `isEncodingName`, `commandNameById` and `errorNameById` narrow a value into them. | +| Spec tables | `cmds`, `consts`, `encodings`, `errors`, `tlvs`, `types`, the `cmdsById`, `constsById`, `errorsById` and `tlvsById` maps, and all of them grouped as `defs`. `isCommandName`, `isErrorName`, `isEncodingName`, `isTlvName`, `commandNameById` and `errorNameById` narrow a value into them. | | Types | Every option, result, event payload and table entry has a named type: `ClientOptions`, `ServerOptions`, `SendSmsOptions`, `SendSmsResult`, `Sms`, `Dlr`, `MessageDlr`, `Receipt`, `PduObject`, `PduHeader`, `SmppLog`, `Result` and the rest in `dist/index.d.ts`. | ## What changed per release diff --git a/src/defs/tlvs.ts b/src/defs/tlvs.ts index c48ef40..fc4d6b0 100644 --- a/src/defs/tlvs.ts +++ b/src/defs/tlvs.ts @@ -253,7 +253,7 @@ function readTlv(pdu: Buffer, offset: number): Result<{ octets: number; occurren return { occurrence: { definition, tagId, value }, octets: 4 + tagLength }; } -function isTlvName(name: string): name is TlvName { +export function isTlvName(name: string): name is TlvName { return Object.hasOwn(specs, name); } diff --git a/src/index.ts b/src/index.ts index 837f3f0..509250a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ export { cmds, cmdsById, commandNameById, isCommandName } from './defs/commands. export { consts, constsById } from './defs/constants.ts'; export { dataCodingByEncoding, detect, encodingByDataCoding, encodings, isEncodingName, messageClassOf, unencodable } from './defs/encodings.ts'; export { errorNameById, errors, errorsById, isErrorName } from './defs/errors.ts'; -export { tlvs, tlvsById } from './defs/tlvs.ts'; +export { isTlvName, tlvs, tlvsById } from './defs/tlvs.ts'; export { types } from './defs/types.ts'; export { diff --git a/test/pdu.test.ts b/test/pdu.test.ts index 06d60b8..c773865 100644 --- a/test/pdu.test.ts +++ b/test/pdu.test.ts @@ -3,7 +3,7 @@ import test, { describe } from 'node:test'; import { PduRefusedError, refusalAnswer } from '../src/pdu-refusal.ts'; import { isCommand, isResp, objToPdu, pduReturn, pduToObj } from '../src/pdu.ts'; import { paramText } from '../src/defs/types.ts'; -import { tlvsById } from '../src/defs/tlvs.ts'; +import { isTlvName, tlvsById } from '../src/defs/tlvs.ts'; function encode(...args: Parameters): Buffer { const { buffer, err } = objToPdu(...args); @@ -502,6 +502,10 @@ describe('TLVs', () => { const { err } = objToPdu({ cmdName: 'broadcast_sm_resp', params, tlvs: { failed_broadcast_area_identifier: { tagValue: areas } } }); assert.match(err?.message ?? '', /key it broadcast_area_identifier/); + assert.deepEqual( + ['broadcast_area_identifier', 'failed_broadcast_area_identifier', 'constructor'].map(isTlvName), + [true, false, false], + ); }); test('refuses a repeatable TLV given one value, and a lone TLV given several', () => {