Skip to content

Commit

Permalink
Merge pull request #53 from ngraveio/getAllTags
Browse files Browse the repository at this point in the history
add functionality to see registered tags
  • Loading branch information
hildjj authored Dec 3, 2024
2 parents ca1a30e + 57f131d commit e397db9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
"name": "Joe Hildebrand",
"email": "[email protected]"
},
"contributors": [
{
"name": "İrfan Bilaloğlu",
"email": "[email protected]",
"url": "https://github.com/irfan798"
}
],
"license": "MIT",
"devDependencies": {
"@cto.af/eslint-config": "5.0.5",
Expand Down
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 clone of the map.
*/
public static getAllDecoders(): ReadonlyMap<TagNumber, TagDecoder> {
return new Map(this.#tags);
}

/**
* Iterate over just the contents, so that the tag works more like an
* array. Yields One time, the contained value.
Expand Down
30 changes: 30 additions & 0 deletions test/tag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,33 @@ 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
Tag.registerDecoder(9999, myDecoder);

// 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.get(9999), myDecoder);
assert.equal(allDecoders.has(9999), true);

// 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 e397db9

Please sign in to comment.