react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / utils / flattenChildren.js
81152 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @providesModule flattenChildren9*/1011"use strict";1213var ReactTextComponent = require('ReactTextComponent');1415var traverseAllChildren = require('traverseAllChildren');16var warning = require('warning');1718/**19* @param {function} traverseContext Context passed through traversal.20* @param {?ReactComponent} child React child component.21* @param {!string} name String name of key path to child.22*/23function flattenSingleChildIntoContext(traverseContext, child, name) {24// We found a component instance.25var result = traverseContext;26var keyUnique = !result.hasOwnProperty(name);27warning(28keyUnique,29'flattenChildren(...): Encountered two children with the same key, ' +30'`%s`. Child keys must be unique; when two children share a key, only ' +31'the first child will be used.',32name33);34if (keyUnique && child != null) {35var type = typeof child;36var normalizedValue;3738if (type === 'string') {39normalizedValue = ReactTextComponent(child);40} else if (type === 'number') {41normalizedValue = ReactTextComponent('' + child);42} else {43normalizedValue = child;44}4546result[name] = normalizedValue;47}48}4950/**51* Flattens children that are typically specified as `props.children`. Any null52* children will not be included in the resulting object.53* @return {!object} flattened children keyed by name.54*/55function flattenChildren(children) {56if (children == null) {57return children;58}59var result = {};60traverseAllChildren(children, flattenSingleChildIntoContext, result);61return result;62}6364module.exports = flattenChildren;656667