forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
119 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* Copyright 2014 Facebook, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* @providesModule toArray | ||
* @typechecks | ||
*/ | ||
|
||
var invariant = require('invariant'); | ||
|
||
/** | ||
* Convert array-like objects to arrays. | ||
* | ||
* This API assumes the caller knows the contents of the data type. For less | ||
* well defined inputs use createArrayFrom. | ||
* | ||
* @param {object|function} obj | ||
* @return {array} | ||
*/ | ||
function toArray(obj) { | ||
var length = obj.length; | ||
|
||
// Some browse builtin objects can report typeof 'function' (e.g. NodeList in | ||
// old versions of Safari). | ||
invariant( | ||
!Array.isArray(obj) && | ||
(typeof obj === 'object' || typeof obj === 'function'), | ||
'toArray: Array-like object expected' | ||
); | ||
|
||
invariant( | ||
typeof length === 'number', | ||
'toArray: Object needs a length property' | ||
); | ||
|
||
invariant( | ||
length === 0 || | ||
(length - 1) in obj, | ||
'toArray: Object should have keys for indices' | ||
); | ||
|
||
// Old IE doesn't give collections access to hasOwnProperty. Assume inputs | ||
// without method will throw during the slice call and skip straight to the | ||
// fallback. | ||
if (obj.hasOwnProperty) { | ||
try { | ||
return Array.prototype.slice.call(obj); | ||
} catch (e) { | ||
// IE < 9 does not support Array#slice on collections objects | ||
} | ||
} | ||
|
||
// Fall back to copying key by key. This assumes all keys have a value, | ||
// so will not preserve sparsely populated inputs. | ||
var ret = Array(length); | ||
for (var ii = 0; ii < length; ii++) { | ||
ret[ii] = obj[ii]; | ||
} | ||
return ret; | ||
} | ||
|
||
module.exports = toArray; |