Skip to content

Commit

Permalink
Give a proper warning if improper arguments are passed
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Jacobs committed Mar 21, 2016
1 parent 8764bb1 commit 4adcd45
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
23 changes: 23 additions & 0 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@ describe('markdown-to-jsx', () => {

afterEach(() => ReactDOM.unmountComponentAtNode(mountNode));

it('should throw if not passed a string (first arg)', () => {
expect(() => converter('')).not.toThrow();

expect(() => converter()).toThrow();
expect(() => converter(1)).toThrow();
expect(() => converter(function(){})).toThrow();
expect(() => converter({})).toThrow();
expect(() => converter([])).toThrow();
expect(() => converter(null)).toThrow();
expect(() => converter(true)).toThrow();
});

it('should throw if not passed an object or undefined (second arg)', () => {
expect(() => converter('')).not.toThrow();
expect(() => converter('', {})).not.toThrow();

expect(() => converter('', 1)).toThrow();
expect(() => converter('', function(){})).toThrow();
expect(() => converter('', [])).toThrow();
expect(() => converter('', null)).toThrow();
expect(() => converter('', true)).toThrow();
});

it('should handle a basic string', () => {
const element = render(converter('Hello.'));
const elementNode = ReactDOM.findDOMNode(element);
Expand Down
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,17 @@ function extractDefinitionsFromASTTree(ast) {
export default function markdownToJSX(markdown, remarkOptions = {}) {
let ast;

if (typeof markdown !== 'string') {
throw new Error(`markdown-to-jsx: the first argument must be
a string`);
}

if (Object.prototype.toString.call(remarkOptions) !== '[object Object]') {
throw new Error(`markdown-to-jsx: the second argument must be
undefined or an object literal ({}) containing
valid remark options`);
}

remarkOptions.position = remarkOptions.position || false;
remarkOptions.footnotes = remarkOptions.footnotes || true;

Expand Down

0 comments on commit 4adcd45

Please sign in to comment.