Skip to content

Commit 809c982

Browse files
authored
fix: allow normal quotes as well as oxford commas (#7)
* fix: allow normal quotes as well as oxford commas * also update valuePattern * test single quote extraction
1 parent 50a9fed commit 809c982

File tree

2 files changed

+118
-48
lines changed

2 files changed

+118
-48
lines changed

src/__tests__/markdown-helpers.spec.ts

Lines changed: 116 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -38,52 +38,122 @@ describe('markdown-helpers', () => {
3838
expect(extractStringEnum('wassup')).toBe(null);
3939
});
4040

41-
it('should extract an enum of the format "can be x"', () => {
42-
const values = extractStringEnum('Can be `x`')!;
43-
expect(values).not.toBe(null);
44-
expect(values).toHaveLength(1);
45-
expect(values[0].value).toBe('x');
46-
});
47-
48-
it('should extract an enum of the format "can be x or y"', () => {
49-
const values = extractStringEnum('Can be `x` or `y`')!;
50-
expect(values).not.toBe(null);
51-
expect(values).toHaveLength(2);
52-
expect(values[0].value).toBe('x');
53-
expect(values[1].value).toBe('y');
54-
});
55-
56-
it('should extract an enum of the format "can be x, y or z"', () => {
57-
const values = extractStringEnum('Can be `x`, `y` or `z`')!;
58-
expect(values).not.toBe(null);
59-
expect(values).toHaveLength(3);
60-
expect(values[0].value).toBe('x');
61-
expect(values[1].value).toBe('y');
62-
expect(values[2].value).toBe('z');
63-
});
64-
65-
it('should extract an enum of the format "values include a', () => {
66-
const values = extractStringEnum('Values include `a`')!;
67-
expect(values).not.toBe(null);
68-
expect(values).toHaveLength(1);
69-
expect(values[0].value).toBe('a');
70-
});
71-
72-
it('should extract an enum of the format "values include a and b', () => {
73-
const values = extractStringEnum('Values include `a` and `b`')!;
74-
expect(values).not.toBe(null);
75-
expect(values).toHaveLength(2);
76-
expect(values[0].value).toBe('a');
77-
expect(values[1].value).toBe('b');
78-
});
79-
80-
it('should extract an enum of the format "values include a, b and c', () => {
81-
const values = extractStringEnum('Values include `a`, `b` and `c`')!;
82-
expect(values).not.toBe(null);
83-
expect(values).toHaveLength(3);
84-
expect(values[0].value).toBe('a');
85-
expect(values[1].value).toBe('b');
86-
expect(values[2].value).toBe('c');
41+
describe('with single quotes', () => {
42+
it('should extract an enum of the format "can be x"', () => {
43+
const values = extractStringEnum('Can be `x`')!;
44+
expect(values).not.toBe(null);
45+
expect(values).toHaveLength(1);
46+
expect(values[0].value).toBe('x');
47+
});
48+
49+
it('should extract an enum of the format "can be x or y"', () => {
50+
const values = extractStringEnum('Can be `x` or `y`')!;
51+
expect(values).not.toBe(null);
52+
expect(values).toHaveLength(2);
53+
expect(values[0].value).toBe('x');
54+
expect(values[1].value).toBe('y');
55+
});
56+
57+
it('should extract an enum of the format "can be x, y or z"', () => {
58+
const values = extractStringEnum('Can be `x`, `y` or `z`')!;
59+
expect(values).not.toBe(null);
60+
expect(values).toHaveLength(3);
61+
expect(values[0].value).toBe('x');
62+
expect(values[1].value).toBe('y');
63+
expect(values[2].value).toBe('z');
64+
});
65+
66+
it('should extract an enum of the format "can be x, y, or z"', () => {
67+
const values = extractStringEnum('Can be `x`, `y`, or `z`')!;
68+
expect(values).not.toBe(null);
69+
expect(values).toHaveLength(3);
70+
expect(values[0].value).toBe('x');
71+
expect(values[1].value).toBe('y');
72+
expect(values[2].value).toBe('z');
73+
});
74+
75+
it('should extract an enum of the format "values include a', () => {
76+
const values = extractStringEnum('Values include `a`')!;
77+
expect(values).not.toBe(null);
78+
expect(values).toHaveLength(1);
79+
expect(values[0].value).toBe('a');
80+
});
81+
82+
it('should extract an enum of the format "values include a and b', () => {
83+
const values = extractStringEnum('Values include `a` and `b`')!;
84+
expect(values).not.toBe(null);
85+
expect(values).toHaveLength(2);
86+
expect(values[0].value).toBe('a');
87+
expect(values[1].value).toBe('b');
88+
});
89+
90+
it('should extract an enum of the format "values include a, b and c', () => {
91+
const values = extractStringEnum('Values include `a`, `b` and `c`')!;
92+
expect(values).not.toBe(null);
93+
expect(values).toHaveLength(3);
94+
expect(values[0].value).toBe('a');
95+
expect(values[1].value).toBe('b');
96+
expect(values[2].value).toBe('c');
97+
});
98+
});
99+
100+
describe('with backticks', () => {
101+
it('should extract an enum of the format "can be x"', () => {
102+
const values = extractStringEnum(`Can be 'x'`)!;
103+
expect(values).not.toBe(null);
104+
expect(values).toHaveLength(1);
105+
expect(values[0].value).toBe('x');
106+
});
107+
108+
it('should extract an enum of the format "can be x or y"', () => {
109+
const values = extractStringEnum(`Can be 'x' or 'y'`)!;
110+
expect(values).not.toBe(null);
111+
expect(values).toHaveLength(2);
112+
expect(values[0].value).toBe('x');
113+
expect(values[1].value).toBe('y');
114+
});
115+
116+
it('should extract an enum of the format "can be x, y or z"', () => {
117+
const values = extractStringEnum(`Can be 'x', 'y' or 'z'`)!;
118+
expect(values).not.toBe(null);
119+
expect(values).toHaveLength(3);
120+
expect(values[0].value).toBe('x');
121+
expect(values[1].value).toBe('y');
122+
expect(values[2].value).toBe('z');
123+
});
124+
125+
it('should extract an enum of the format "can be x, y, or z"', () => {
126+
const values = extractStringEnum(`Can be 'x', 'y', or 'z'`)!;
127+
expect(values).not.toBe(null);
128+
expect(values).toHaveLength(3);
129+
expect(values[0].value).toBe('x');
130+
expect(values[1].value).toBe('y');
131+
expect(values[2].value).toBe('z');
132+
});
133+
134+
it('should extract an enum of the format "values include a', () => {
135+
const values = extractStringEnum(`Values include 'a'`)!;
136+
expect(values).not.toBe(null);
137+
expect(values).toHaveLength(1);
138+
expect(values[0].value).toBe('a');
139+
});
140+
141+
it('should extract an enum of the format "values include a and b', () => {
142+
const values = extractStringEnum(`Values include 'a' and 'b'`)!;
143+
expect(values).not.toBe(null);
144+
expect(values).toHaveLength(2);
145+
expect(values[0].value).toBe('a');
146+
expect(values[1].value).toBe('b');
147+
});
148+
149+
it('should extract an enum of the format "values include a, b and c', () => {
150+
const values = extractStringEnum(`Values include 'a', 'b' and 'c'`)!;
151+
expect(values).not.toBe(null);
152+
expect(values).toHaveLength(3);
153+
expect(values[0].value).toBe('a');
154+
expect(values[1].value).toBe('b');
155+
expect(values[2].value).toBe('c');
156+
});
87157
});
88158
});
89159

src/markdown-helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,11 @@ export enum StripReturnTypeBehavior {
323323
export const extractStringEnum = (description: string): PossibleStringValue[] | null => {
324324
const possibleValues: PossibleStringValue[] = [];
325325

326-
const inlineValuesPattern = /(?:can be|values include) ((?:(?:`[a-zA-Z-]+`)(?:, ))*(?:`[a-zA-Z-]+`)?(?: (?:or|and) `[a-zA-Z-]+`)?)/i;
326+
const inlineValuesPattern = /(?:can be|values include) ((?:(?:[`|'][a-zA-Z-]+[`|'])(?:(, | )?))*(?:(?:or|and) [`|'][a-zA-Z-]+[`|'])?)/i;
327327
const inlineMatch = inlineValuesPattern.exec(description);
328328
if (inlineMatch) {
329329
const valueString = inlineMatch[1];
330-
const valuePattern = /`([a-zA-Z-]+)`/g;
330+
const valuePattern = /[`|']([a-zA-Z-]+)[`|']/g;
331331
let value = valuePattern.exec(valueString);
332332

333333
while (value) {

0 commit comments

Comments
 (0)