feat(jsdoc-tag): add includesInlineTag method

This commit is contained in:
Jeff Williams 2023-11-21 20:44:25 -08:00
parent 1ea356e427
commit 75d1cbdf91
No known key found for this signature in database
2 changed files with 56 additions and 1 deletions

View File

@ -77,6 +77,20 @@ export function isInlineTag(string, tagName) {
return regExpFactory(tagName, '^', '$').test(string); return regExpFactory(tagName, '^', '$').test(string);
} }
/**
* Check whether a string includes one or more inline tags. You can check for a specific inline tag
* or for any valid inline tag.
*
* @param {string} string - The string to check.
* @param {?string} tagName - The inline tag to match. May contain regexp characters. If this
* parameter is omitted, this method returns `true` for any valid inline tag.
* @returns {boolean} Set to `true` if the string includes at least one valid inline tag or `false`
* in all other cases.
*/
export function includesInlineTag(string, tagName) {
return regExpFactory(tagName).test(string);
}
/** /**
* Replace all instances of multiple inline tags with other text. * Replace all instances of multiple inline tags with other text.
* *

View File

@ -20,6 +20,10 @@ describe('@jsdoc/tag/lib/inline', () => {
expect(inline).toBeObject(); expect(inline).toBeObject();
}); });
it('exports an `includesInlineTag` function', () => {
expect(inline.includesInlineTag).toBeFunction();
});
it('exports an isInlineTag function', () => { it('exports an isInlineTag function', () => {
expect(inline.isInlineTag).toBeFunction(); expect(inline.isInlineTag).toBeFunction();
}); });
@ -32,8 +36,45 @@ describe('@jsdoc/tag/lib/inline', () => {
expect(inline.extractInlineTag).toBeFunction(); expect(inline.extractInlineTag).toBeFunction();
}); });
describe('includesInlineTag', () => {
const { includesInlineTag } = inline;
it('identifies a string that is an inline tag', () => {
expect(includesInlineTag('{@mytag hooray}', 'mytag')).toBeTrue();
});
it('identifies when a string does not include an inline tag', () => {
expect(includesInlineTag('mytag hooray', 'mytag')).toBeFalse();
});
it('identifies when a string contains an inline tag, plus extra characters', () => {
expect(includesInlineTag('this is {@mytag hooray}', 'mytag')).toBeTrue();
});
it('allows any inline tag by default', () => {
expect(includesInlineTag('Hello, {@anyoldtag will do}')).toBeTrue();
});
it('identifies things that are not inline tags when a tag name is not provided', () => {
expect(includesInlineTag('mytag hooray')).toBeFalse();
});
it('allows regexp characters in the tag name', () => {
expect(includesInlineTag('{@mytags hooray}', 'mytag\\S')).toBeTrue();
});
it('returns false (rather than throwing) with invalid input', () => {
function badInput() {
return includesInlineTag();
}
expect(badInput).not.toThrow();
expect(badInput()).toBeFalse();
});
});
describe('isInlineTag', () => { describe('isInlineTag', () => {
const isInlineTag = inline.isInlineTag; const { isInlineTag } = inline;
it('identifies an inline tag', () => { it('identifies an inline tag', () => {
expect(isInlineTag('{@mytag hooray}', 'mytag')).toBeTrue(); expect(isInlineTag('{@mytag hooray}', 'mytag')).toBeTrue();