Skip to content

Commit

Permalink
add function to see registered tags
Browse files Browse the repository at this point in the history
  • Loading branch information
irfan798 committed Dec 2, 2024
1 parent ca1a30e commit 53add3b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,29 @@ export class Tag implements ToCBOR, Decodeable {
* @param tag Tag number.
* @returns Old decoder, if there was one.
*/
public static clearDecoder(tag: number): TagDecoder | undefined {
public static clearDecoder(tag: TagNumber): TagDecoder | undefined {
const old = this.#tags.get(tag);
this.#tags.delete(tag);
return old;
}

/**
* Get the decoder for a given tag number.
*
* @param tag The tag number.
* @returns The decoder function, if there is one.
*/
public static getDecoder(tag: TagNumber): TagDecoder | undefined {
return this.#tags.get(tag);
}

/**
* Get all registered decoders as object
*/
public static getAllDecoders() {

Check failure on line 130 in src/tag.ts

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

Missing return type on function

Check failure on line 130 in src/tag.ts

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

Missing return type on function

Check failure on line 130 in src/tag.ts

View workflow job for this annotation

GitHub Actions / build (20.x, ubuntu-latest)

Missing return type on function

Check failure on line 130 in src/tag.ts

View workflow job for this annotation

GitHub Actions / build (20.x, ubuntu-latest)

Missing return type on function

Check failure on line 130 in src/tag.ts

View workflow job for this annotation

GitHub Actions / build (22.x, ubuntu-latest)

Missing return type on function

Check failure on line 130 in src/tag.ts

View workflow job for this annotation

GitHub Actions / build (22.x, ubuntu-latest)

Missing return type on function

Check failure on line 130 in src/tag.ts

View workflow job for this annotation

GitHub Actions / build (23.x, ubuntu-latest)

Missing return type on function

Check failure on line 130 in src/tag.ts

View workflow job for this annotation

GitHub Actions / build (23.x, ubuntu-latest)

Missing return type on function
return Object.fromEntries(this.#tags);
}

/**
* Iterate over just the contents, so that the tag works more like an
* array. Yields One time, the contained value.
Expand Down
29 changes: 29 additions & 0 deletions test/tag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,32 @@ test('Tag', () => {

assert.deepEqual([...t], ['2023-07-21T22:16:20-0600']);
});

test('Tag Register and verify decoders', () => {
function myDecoder(_tag) {
return 'myDecoder';
}

// Register a decoder with a tag
const t0 = Tag.registerDecoder(9999, myDecoder);

Check failure on line 37 in test/tag.test.js

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

't0' is assigned a value but never used. Allowed unused vars must match /^_[^_]/u

Check failure on line 37 in test/tag.test.js

View workflow job for this annotation

GitHub Actions / build (20.x, ubuntu-latest)

't0' is assigned a value but never used. Allowed unused vars must match /^_[^_]/u

Check failure on line 37 in test/tag.test.js

View workflow job for this annotation

GitHub Actions / build (22.x, ubuntu-latest)

't0' is assigned a value but never used. Allowed unused vars must match /^_[^_]/u

Check failure on line 37 in test/tag.test.js

View workflow job for this annotation

GitHub Actions / build (23.x, ubuntu-latest)

't0' is assigned a value but never used. Allowed unused vars must match /^_[^_]/u

// Verify the we added decoder to the registry
assert.equal(Tag.getDecoder(9999), myDecoder);

// Verify execution is the same as the decoder
assert.equal(Tag.getDecoder(9999)(), myDecoder());

// Verify decoder is in all decoder list
const allDecoders = Tag.getAllDecoders();
assert.equal(allDecoders[9999], myDecoder);

// Remove the decoder from the registry
Tag.clearDecoder(9999);

// Verify the decoder is removed
assert.equal(Tag.getDecoder(9999), undefined);

// Verify the decoder is removed from all decoder list
assert.equal(Tag.getAllDecoders()[9999], undefined);
});

0 comments on commit 53add3b

Please sign in to comment.