react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / vendor / core / createArrayFrom.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 createArrayFrom9* @typechecks10*/1112var toArray = require('toArray');1314/**15* Perform a heuristic test to determine if an object is "array-like".16*17* A monk asked Joshu, a Zen master, "Has a dog Buddha nature?"18* Joshu replied: "Mu."19*20* This function determines if its argument has "array nature": it returns21* true if the argument is an actual array, an `arguments' object, or an22* HTMLCollection (e.g. node.childNodes or node.getElementsByTagName()).23*24* It will return false for other array-like objects like Filelist.25*26* @param {*} obj27* @return {boolean}28*/29function hasArrayNature(obj) {30return (31// not null/false32!!obj &&33// arrays are objects, NodeLists are functions in Safari34(typeof obj == 'object' || typeof obj == 'function') &&35// quacks like an array36('length' in obj) &&37// not window38!('setInterval' in obj) &&39// no DOM node should be considered an array-like40// a 'select' element has 'length' and 'item' properties on IE841(typeof obj.nodeType != 'number') &&42(43// a real array44Array.isArray(obj) ||45// arguments46('callee' in obj) ||47// HTMLCollection/NodeList48('item' in obj)49)50);51}5253/**54* Ensure that the argument is an array by wrapping it in an array if it is not.55* Creates a copy of the argument if it is already an array.56*57* This is mostly useful idiomatically:58*59* var createArrayFrom = require('createArrayFrom');60*61* function takesOneOrMoreThings(things) {62* things = createArrayFrom(things);63* ...64* }65*66* This allows you to treat `things' as an array, but accept scalars in the API.67*68* If you need to convert an array-like object, like `arguments`, into an array69* use toArray instead.70*71* @param {*} obj72* @return {array}73*/74function createArrayFrom(obj) {75if (!hasArrayNature(obj)) {76return [obj];77} else if (Array.isArray(obj)) {78return obj.slice();79} else {80return toArray(obj);81}82}8384module.exports = createArrayFrom;858687