react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / vendor / core / mergeHelpers.js
81158 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 mergeHelpers9*10* requiresPolyfills: Array.isArray11*/1213"use strict";1415var invariant = require('invariant');16var keyMirror = require('keyMirror');1718/**19* Maximum number of levels to traverse. Will catch circular structures.20* @const21*/22var MAX_MERGE_DEPTH = 36;2324/**25* We won't worry about edge cases like new String('x') or new Boolean(true).26* Functions are considered terminals, and arrays are not.27* @param {*} o The item/object/value to test.28* @return {boolean} true iff the argument is a terminal.29*/30var isTerminal = function(o) {31return typeof o !== 'object' || o === null;32};3334var mergeHelpers = {3536MAX_MERGE_DEPTH: MAX_MERGE_DEPTH,3738isTerminal: isTerminal,3940/**41* Converts null/undefined values into empty object.42*43* @param {?Object=} arg Argument to be normalized (nullable optional)44* @return {!Object}45*/46normalizeMergeArg: function(arg) {47return arg === undefined || arg === null ? {} : arg;48},4950/**51* If merging Arrays, a merge strategy *must* be supplied. If not, it is52* likely the caller's fault. If this function is ever called with anything53* but `one` and `two` being `Array`s, it is the fault of the merge utilities.54*55* @param {*} one Array to merge into.56* @param {*} two Array to merge from.57*/58checkMergeArrayArgs: function(one, two) {59invariant(60Array.isArray(one) && Array.isArray(two),61'Tried to merge arrays, instead got %s and %s.',62one,63two64);65},6667/**68* @param {*} one Object to merge into.69* @param {*} two Object to merge from.70*/71checkMergeObjectArgs: function(one, two) {72mergeHelpers.checkMergeObjectArg(one);73mergeHelpers.checkMergeObjectArg(two);74},7576/**77* @param {*} arg78*/79checkMergeObjectArg: function(arg) {80invariant(81!isTerminal(arg) && !Array.isArray(arg),82'Tried to merge an object, instead got %s.',83arg84);85},8687/**88* @param {*} arg89*/90checkMergeIntoObjectArg: function(arg) {91invariant(92(!isTerminal(arg) || typeof arg === 'function') && !Array.isArray(arg),93'Tried to merge into an object, instead got %s.',94arg95);96},9798/**99* Checks that a merge was not given a circular object or an object that had100* too great of depth.101*102* @param {number} Level of recursion to validate against maximum.103*/104checkMergeLevel: function(level) {105invariant(106level < MAX_MERGE_DEPTH,107'Maximum deep merge depth exceeded. You may be attempting to merge ' +108'circular structures in an unsupported way.'109);110},111112/**113* Checks that the supplied merge strategy is valid.114*115* @param {string} Array merge strategy.116*/117checkArrayStrategy: function(strategy) {118invariant(119strategy === undefined || strategy in mergeHelpers.ArrayStrategies,120'You must provide an array strategy to deep merge functions to ' +121'instruct the deep merge how to resolve merging two arrays.'122);123},124125/**126* Set of possible behaviors of merge algorithms when encountering two Arrays127* that must be merged together.128* - `clobber`: The left `Array` is ignored.129* - `indexByIndex`: The result is achieved by recursively deep merging at130* each index. (not yet supported.)131*/132ArrayStrategies: keyMirror({133Clobber: true,134IndexByIndex: true135})136137};138139module.exports = mergeHelpers;140141142