Skip to content

Commit

Permalink
fix: allow normal quotes as well as oxford commas (#7)
Browse files Browse the repository at this point in the history
* fix: allow normal quotes as well as oxford commas

* also update valuePattern

* test single quote extraction
  • Loading branch information
codebytere authored Jul 25, 2019
1 parent 50a9fed commit 809c982
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 48 deletions.
162 changes: 116 additions & 46 deletions src/__tests__/markdown-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,52 +38,122 @@ describe('markdown-helpers', () => {
expect(extractStringEnum('wassup')).toBe(null);
});

it('should extract an enum of the format "can be x"', () => {
const values = extractStringEnum('Can be `x`')!;
expect(values).not.toBe(null);
expect(values).toHaveLength(1);
expect(values[0].value).toBe('x');
});

it('should extract an enum of the format "can be x or y"', () => {
const values = extractStringEnum('Can be `x` or `y`')!;
expect(values).not.toBe(null);
expect(values).toHaveLength(2);
expect(values[0].value).toBe('x');
expect(values[1].value).toBe('y');
});

it('should extract an enum of the format "can be x, y or z"', () => {
const values = extractStringEnum('Can be `x`, `y` or `z`')!;
expect(values).not.toBe(null);
expect(values).toHaveLength(3);
expect(values[0].value).toBe('x');
expect(values[1].value).toBe('y');
expect(values[2].value).toBe('z');
});

it('should extract an enum of the format "values include a', () => {
const values = extractStringEnum('Values include `a`')!;
expect(values).not.toBe(null);
expect(values).toHaveLength(1);
expect(values[0].value).toBe('a');
});

it('should extract an enum of the format "values include a and b', () => {
const values = extractStringEnum('Values include `a` and `b`')!;
expect(values).not.toBe(null);
expect(values).toHaveLength(2);
expect(values[0].value).toBe('a');
expect(values[1].value).toBe('b');
});

it('should extract an enum of the format "values include a, b and c', () => {
const values = extractStringEnum('Values include `a`, `b` and `c`')!;
expect(values).not.toBe(null);
expect(values).toHaveLength(3);
expect(values[0].value).toBe('a');
expect(values[1].value).toBe('b');
expect(values[2].value).toBe('c');
describe('with single quotes', () => {
it('should extract an enum of the format "can be x"', () => {
const values = extractStringEnum('Can be `x`')!;
expect(values).not.toBe(null);
expect(values).toHaveLength(1);
expect(values[0].value).toBe('x');
});

it('should extract an enum of the format "can be x or y"', () => {
const values = extractStringEnum('Can be `x` or `y`')!;
expect(values).not.toBe(null);
expect(values).toHaveLength(2);
expect(values[0].value).toBe('x');
expect(values[1].value).toBe('y');
});

it('should extract an enum of the format "can be x, y or z"', () => {
const values = extractStringEnum('Can be `x`, `y` or `z`')!;
expect(values).not.toBe(null);
expect(values).toHaveLength(3);
expect(values[0].value).toBe('x');
expect(values[1].value).toBe('y');
expect(values[2].value).toBe('z');
});

it('should extract an enum of the format "can be x, y, or z"', () => {
const values = extractStringEnum('Can be `x`, `y`, or `z`')!;
expect(values).not.toBe(null);
expect(values).toHaveLength(3);
expect(values[0].value).toBe('x');
expect(values[1].value).toBe('y');
expect(values[2].value).toBe('z');
});

it('should extract an enum of the format "values include a', () => {
const values = extractStringEnum('Values include `a`')!;
expect(values).not.toBe(null);
expect(values).toHaveLength(1);
expect(values[0].value).toBe('a');
});

it('should extract an enum of the format "values include a and b', () => {
const values = extractStringEnum('Values include `a` and `b`')!;
expect(values).not.toBe(null);
expect(values).toHaveLength(2);
expect(values[0].value).toBe('a');
expect(values[1].value).toBe('b');
});

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

describe('with backticks', () => {
it('should extract an enum of the format "can be x"', () => {
const values = extractStringEnum(`Can be 'x'`)!;
expect(values).not.toBe(null);
expect(values).toHaveLength(1);
expect(values[0].value).toBe('x');
});

it('should extract an enum of the format "can be x or y"', () => {
const values = extractStringEnum(`Can be 'x' or 'y'`)!;
expect(values).not.toBe(null);
expect(values).toHaveLength(2);
expect(values[0].value).toBe('x');
expect(values[1].value).toBe('y');
});

it('should extract an enum of the format "can be x, y or z"', () => {
const values = extractStringEnum(`Can be 'x', 'y' or 'z'`)!;
expect(values).not.toBe(null);
expect(values).toHaveLength(3);
expect(values[0].value).toBe('x');
expect(values[1].value).toBe('y');
expect(values[2].value).toBe('z');
});

it('should extract an enum of the format "can be x, y, or z"', () => {
const values = extractStringEnum(`Can be 'x', 'y', or 'z'`)!;
expect(values).not.toBe(null);
expect(values).toHaveLength(3);
expect(values[0].value).toBe('x');
expect(values[1].value).toBe('y');
expect(values[2].value).toBe('z');
});

it('should extract an enum of the format "values include a', () => {
const values = extractStringEnum(`Values include 'a'`)!;
expect(values).not.toBe(null);
expect(values).toHaveLength(1);
expect(values[0].value).toBe('a');
});

it('should extract an enum of the format "values include a and b', () => {
const values = extractStringEnum(`Values include 'a' and 'b'`)!;
expect(values).not.toBe(null);
expect(values).toHaveLength(2);
expect(values[0].value).toBe('a');
expect(values[1].value).toBe('b');
});

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

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

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

while (value) {
Expand Down

0 comments on commit 809c982

Please sign in to comment.