toArray.js 947 B

12345678910111213141516171819202122232425262728293031
  1. var kindOf = require('./kindOf');
  2. var _win = this;
  3. /**
  4. * Convert array-like object into array
  5. */
  6. function toArray(val){
  7. var ret = [],
  8. kind = kindOf(val),
  9. n;
  10. if (val != null) {
  11. if ( val.length == null || kind === 'String' || kind === 'Function' || kind === 'RegExp' || val === _win ) {
  12. //string, regexp, function have .length but user probably just want
  13. //to wrap value into an array..
  14. ret[ret.length] = val;
  15. } else {
  16. //window returns true on isObject in IE7 and may have length
  17. //property. `typeof NodeList` returns `function` on Safari so
  18. //we can't use it (#58)
  19. n = val.length;
  20. while (n--) {
  21. ret[n] = val[n];
  22. }
  23. }
  24. }
  25. return ret;
  26. }
  27. module.exports = toArray;