Skip to content

Commit

Permalink
fix: handle numbers and some puncuation in enum values
Browse files Browse the repository at this point in the history
Fixes #88
  • Loading branch information
MarshallOfSound committed Jun 9, 2023
1 parent c56164b commit 2187417
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/__tests__/markdown-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,34 @@ def fn():
expect(values[1].value).toBe('b_c');
expect(values[2].value).toBe('d');
});

it('should extract an enum with fullstops in the values', () => {
const values = extractStringEnum('Values includes `a`, `b.c` and `d`')!;
expect(values).not.toBe(null);
expect(values).toHaveLength(3);
expect(values[0].value).toBe('a');
expect(values[1].value).toBe('b.c');
expect(values[2].value).toBe('d');
});

it('should extract an enum with colons in the values', () => {
const values = extractStringEnum('Values includes `a`, `https:` and `d`')!;
expect(values).not.toBe(null);
expect(values).toHaveLength(3);
expect(values[0].value).toBe('a');
expect(values[1].value).toBe('https:');
expect(values[2].value).toBe('d');
});

it('should extract an enum with numbers in the values', () => {
const values = extractStringEnum('Can be `tls1`, `tls1.1`, `tls1.2` or `tls1.3`.')!;
expect(values).not.toBe(null);
expect(values).toHaveLength(4);
expect(values[0].value).toBe('tls1');
expect(values[1].value).toBe('tls1.1');
expect(values[2].value).toBe('tls1.2');
expect(values[3].value).toBe('tls1.3');
});
});

describe('with single quotes', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/markdown-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,11 @@ export enum StripReturnTypeBehavior {
export const extractStringEnum = (description: string): PossibleStringValue[] | null => {
const possibleValues: PossibleStringValue[] = [];

const inlineValuesPattern = /(?:can be|values? includes?) ((?:(?:[`|'][a-zA-Z-_]+[`|'])(?:(, | )?))*(?:(?:or|and) [`|'][a-zA-Z-_]+[`|'])?)/i;
const inlineValuesPattern = /(?:can be|values? includes?) ((?:(?:[`|'][a-zA-Z0-9-_\.:]+[`|'])(?:(, | )?))*(?:(?:or|and) [`|'][a-zA-Z0-9-_\.:]+[`|'])?)/i;
const inlineMatch = inlineValuesPattern.exec(description);
if (inlineMatch) {
const valueString = inlineMatch[1];
const valuePattern = /[`|']([a-zA-Z-_]+)[`|']/g;
const valuePattern = /[`|']([a-zA-Z0-9-_\.:]+)[`|']/g;
let value = valuePattern.exec(valueString);

while (value) {
Expand Down

0 comments on commit 2187417

Please sign in to comment.