Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
Simplifing tag generator
Browse files Browse the repository at this point in the history
  • Loading branch information
egalli committed Dec 20, 2017
1 parent e64d4ab commit 4a5680d
Showing 1 changed file with 27 additions and 67 deletions.
94 changes: 27 additions & 67 deletions Node-DC-SSR-electrode/src/client/components/generators/TagCount.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,12 @@
import React from 'react';
import PropTypes from 'prop-types';

export default class TagCount extends React.Component {
static propTypes = {
count: PropTypes.number,
depth: PropTypes.number,
node: PropTypes.oneOfType([
PropTypes.element,
PropTypes.string
]),
leaf: PropTypes.node,
useCache: PropTypes.bool
};

static defaultProps = {
count: 3000,
depth: 7,
node: 'div',
leaf: '.',
useCache: false
};

static cache = {};

createTag(children) {
const {node, leaf} = this.props;
if(children.length > 0) {
return React.createElement(node, null, ...children);
} else {
return React.createElement(node, null, leaf);
}
}

generateTagsHelper(curdepth, children, genState) {
const {count, depth} = this.props;

if(curdepth >= depth) {
return this.createTag([]);
}

const newChildren = Math.max(0, Math.min(children, count - genState.count));
genState.count += newChildren;
const numberOfChildren = (depth, count) => {

const elems = new Array(newChildren);
for(let ii = 0; ii < newChildren; ++ii) {
elems[ii] = this.generateTagsHelper(curdepth + 1, children, genState);
if(depth == 1 || depth > count) {
return count;
}

return this.createTag(elems);
}

generateTags() {
const {depth, node, count} = this.props;

let children = 2;

for(; ; ++children) {
Expand All @@ -63,30 +17,36 @@ export default class TagCount extends React.Component {
}
}

const genState = {count: 1};

const tags = React.createElement(node, null,
this.generateTagsHelper(0, children, genState));
return children;
};

return tags;
}
export default class TagCount extends React.Component {

render() {
const {useCache, count, depth, node, leaf} = this.props;
let key;
if(useCache) {
key = [count, depth, node, leaf].join('<=>');
if(TagCount.cache[key]) {
return TagCount.cache[key];
}
let {depth, count: tagCount} = this.props;

if(depth <= 0) {
return <div>{'This is a leaf'}</div>;
}

const tags = this.generateTags();
tagCount -= 1;

const childCount = numberOfChildren(depth, tagCount);
const childTagCount = Math.ceil(tagCount / childCount);
let tagsRemaining = tagCount;

const children = [];

for(let ii = 0; ii < childCount; ++ii) {
children.push(<TagCount
key={ii}
depth={depth - 1}
count={Math.min(tagsRemaining, childTagCount)}
/>);

if(useCache) {
TagCount.cache[key] = tags;
tagsRemaining -= childTagCount;
}

return tags;
return <div>{children}</div>;
}
};
}

0 comments on commit 4a5680d

Please sign in to comment.