util.js 700 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*!
  2. * socket.io-node
  3. * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. /**
  10. * Converts an enumerable to an array.
  11. *
  12. * @api public
  13. */
  14. exports.toArray = function (enu) {
  15. var arr = [];
  16. for (var i = 0, l = enu.length; i < l; i++)
  17. arr.push(enu[i]);
  18. return arr;
  19. };
  20. /**
  21. * Unpacks a buffer to a number.
  22. *
  23. * @api public
  24. */
  25. exports.unpack = function (buffer) {
  26. var n = 0;
  27. for (var i = 0; i < buffer.length; ++i) {
  28. n = (i == 0) ? buffer[i] : (n * 256) + buffer[i];
  29. }
  30. return n;
  31. }
  32. /**
  33. * Left pads a string.
  34. *
  35. * @api public
  36. */
  37. exports.padl = function (s,n,c) {
  38. return new Array(1 + n - s.length).join(c) + s;
  39. }