Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes type checks for pure functions and removes lodash #17

Merged
merged 1 commit into from Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .node-version
@@ -1 +1 @@
10.12.0
12.16.2
3 changes: 1 addition & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "@harvest-profit/doc-flux",
"version": "1.1.1",
"version": "1.1.2",
"description": "Flux/React framework for creating any document, just define a few DOM components to transform into the document.",
"main": "dist/index.js",
"repository": "https://github.com/HarvestProfit/DocFlux",
Expand Down Expand Up @@ -63,7 +63,6 @@
"jsdoc": "^3.5.5"
},
"dependencies": {
"lodash": "^4.17.11",
"prop-types": "^15.7.2"
}
}
13 changes: 10 additions & 3 deletions src/DocFlux.js
@@ -1,4 +1,3 @@
import _ from 'lodash';
import PropTypes from 'prop-types';
import Component from './Component';
import DOMComponent from './DOMComponent';
Expand All @@ -19,6 +18,13 @@ class Div extends Component {
}
}

export function flatten(arr) {
return arr.reduce(
(flat, toFlatten) => flat.concat(
Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten,
), []);
}

// Returns a value or default value
const setValue = (value, defaultValue) => {
if (value === undefined) {
Expand Down Expand Up @@ -67,7 +73,7 @@ export default class DocFlux {
static createElement(JSXComponent, props, ...children) {
const propsWithChildren = { ...props };
if (children) {
propsWithChildren.children = _.flattenDeep(children);
propsWithChildren.children = flatten(children);
}

return {
Expand Down Expand Up @@ -113,6 +119,7 @@ export default class DocFlux {
if (!component.node) return false;

if (typeof component.node.prototype === 'function') return true;
if (typeof component.node === 'function') return true;
if (typeof component.node.prototype === 'object') return true;
return false;
}
Expand All @@ -123,7 +130,7 @@ export default class DocFlux {
DocFlux.validateProps(props, ComponentClass.propTypes, ComponentClass.name);

let value;
if (ComponentClass.prototype.render) {
if (ComponentClass.prototype && ComponentClass.prototype.render) {
const initializedComponent = new ComponentClass(props);
value = renderFunc(initializedComponent.render(), parser);
if (ComponentClass.transform) {
Expand Down
27 changes: 27 additions & 0 deletions test/DocFlux.test.js
@@ -1,4 +1,5 @@
import { DocFlux } from '../src';
import { flatten } from '../src/DocFlux';
/** @jsx DocFlux.createElement */

import Parser from './TestFixtures/TestParser';
Expand All @@ -14,6 +15,32 @@ console.error = jest.fn((error) => {
});

describe('DocFlux', () => {
describe('flatten', () => {
it('should flatten an array of components', () => {
const a = [
<SimpleComponent />,
[<SimpleComponent />, <SimpleComponent />, [<SimplePureFunctionComponent />]]];
expect(flatten(a).length).toBe(4);
});
});
describe('isComponent', () => {
it('should be a component for class based components', () => {
expect(DocFlux.isComponent(<SimpleComponent />)).toBe(true);
});

it('should be a component for pure function based components', () => {
expect(DocFlux.isComponent(<SimplePureFunctionComponent />)).toBe(true);
});

it('should not be a component for array', () => {
expect(DocFlux.isComponent([<SimplePureFunctionComponent />])).toBe(false);
});

it('should not be a component for object', () => {
expect(DocFlux.isComponent({ node: 'cool' })).toBe(false);
});
});

describe('render', () => {
it('should render a DOM node', () => {
const component = DocFlux.render(<h1>HEY</h1>, Parser);
Expand Down