Skip to content

Commit

Permalink
add the transpiled version of ComponentInterpolator
Browse files Browse the repository at this point in the history
ran the `dist` script and checked in results so this commit can be used
directly by importing the git ref.
  • Loading branch information
Jason Wodicka committed Jun 27, 2017
1 parent b2adb45 commit 9bf9352
Showing 1 changed file with 68 additions and 60 deletions.
128 changes: 68 additions & 60 deletions dist/ComponentInterpolator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

var React = require('react');
var invariant = require('invariant');
var _React$PropTypes = React.PropTypes;
var string = _React$PropTypes.string;
var object = _React$PropTypes.object;

var _require = require('prop-types'),
string = _require.string,
object = _require.object;

var WRAPPER_PATTERN = /(\*+)/;
var PLACEHOLDER_PATTERN = /(%\{.*?\})/;
Expand Down Expand Up @@ -53,65 +53,73 @@ var getInjectIndex = function getInjectIndex(children, containerName) {
return index;
};

var ComponentInterpolator = React.createClass({
displayName: 'ComponentInterpolator',

propTypes: {
string: string.isRequired,
wrappers: object
},

inferChildren: function inferChildren() {
var tokens = (this.props.string || '').split(WRAPPER_PATTERN);
this.keyCounter = 0;
var inferredChildren = this.interpolateAllComponents(tokens);

var currentChildren = toArray(this.props.children);

var index = getInjectIndex(currentChildren, '<ComponentInterpolator>');
invariant(index >= 0, '<ComponentInterpolator> must have a "$1" text child"');
currentChildren.splice.apply(currentChildren, [index, 1].concat(inferredChildren));
return currentChildren;
},
interpolateAllComponents: function interpolateAllComponents(tokens, eof) {
var token, child;
var children = [];
var wrappers = this.props.wrappers || {};
while (tokens.length) {
token = tokens.shift();
if (token === eof) break;
if (token.match(WRAPPER_PATTERN)) {
invariant(child = wrappers[token], '<ComponentInterpolator> expected \'' + token + '\' wrapper, none found');

child = injectNewDescendants(child, this.interpolateAllComponents(tokens, token), { key: this.keyCounter++ }, true);
children.push(child);
} else {
children.push.apply(children, this.interpolatePlaceholders(token));
}
var Counter = function Counter() {
this.count = 0;
this.next = function () {
return this.count++;
};
};

var inferChildren = function inferChildren(props) {
var tokens = (props.string || '').split(WRAPPER_PATTERN);
var inferredChildren = interpolateAllComponents(tokens, props);

var currentChildren = toArray(props.children);

var index = getInjectIndex(currentChildren, '<ComponentInterpolator>');
invariant(index >= 0, '<ComponentInterpolator> must have a "$1" text child"');
currentChildren.splice.apply(currentChildren, [index, 1].concat(inferredChildren));
return currentChildren;
};

var interpolateAllComponents = function interpolateAllComponents(tokens, props, keyCounter, eof) {
var token, child;
var children = [];
var wrappers = props.wrappers || {};
if (!keyCounter) {
keyCounter = new Counter();
}
while (tokens.length) {
token = tokens.shift();
if (token === eof) break;
if (token.match(WRAPPER_PATTERN)) {
invariant(child = wrappers[token], '<ComponentInterpolator> expected \'' + token + '\' wrapper, none found');

child = injectNewDescendants(child, interpolateAllComponents(tokens, props, keyCounter, token), { key: keyCounter.next() }, true);
children.push(child);
} else {
children.push.apply(children, interpolatePlaceholders(token, props, keyCounter));
}
return children;
},
interpolatePlaceholders: function interpolatePlaceholders(string) {
var token, child;
var tokens = string.split(PLACEHOLDER_PATTERN);
var children = [];
while (tokens.length) {
token = tokens.shift();
if (token.match(PLACEHOLDER_PATTERN)) {
token = token.slice(2, -1);
invariant(this.props.hasOwnProperty(token), '<ComponentInterpolator> expected \'' + token + '\' placeholder value, none found');
child = this.props[token];
child = child && child.type ? React.cloneElement(child, { key: this.keyCounter++ }) : child;
children.push(child);
} else {
children.push(token);
}
}
return children;
};

var interpolatePlaceholders = function interpolatePlaceholders(string, props, keyCounter) {
var token, child;
var tokens = string.split(PLACEHOLDER_PATTERN);
var children = [];
while (tokens.length) {
token = tokens.shift();
if (token.match(PLACEHOLDER_PATTERN)) {
token = token.slice(2, -1);
invariant(props.hasOwnProperty(token), '<ComponentInterpolator> expected \'' + token + '\' placeholder value, none found');
child = props[token];
child = child && child.type ? React.cloneElement(child, { key: keyCounter.next() }) : child;
children.push(child);
} else {
children.push(token);
}
return children;
},
render: function render() {
return React.createElement('span', {}, this.inferChildren());
}
});
return children;
};

var ComponentInterpolator = function ComponentInterpolator(props) {
return React.createElement('span', {}, inferChildren(props));
};

ComponentInterpolator.propTypes = {
string: string.isRequired,
wrappers: object
};

module.exports = ComponentInterpolator;

0 comments on commit 9bf9352

Please sign in to comment.