util.js 700 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. }