diff --git a/dist/ComponentInterpolator.js b/dist/ComponentInterpolator.js index d658d2e..58c4097 100644 --- a/dist/ComponentInterpolator.js +++ b/dist/ComponentInterpolator.js @@ -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 = /(%\{.*?\})/; @@ -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, ''); - invariant(index >= 0, ' 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], ' 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, ''); + invariant(index >= 0, ' 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], ' 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), ' 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), ' 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; \ No newline at end of file