You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.1 KiB

7 years ago
  1. /**
  2. * HOP ref.
  3. */
  4. var has = Object.prototype.hasOwnProperty;
  5. /**
  6. * Return own keys in `obj`.
  7. *
  8. * @param {Object} obj
  9. * @return {Array}
  10. * @api public
  11. */
  12. exports.keys = Object.keys || function(obj){
  13. var keys = [];
  14. for (var key in obj) {
  15. if (has.call(obj, key)) {
  16. keys.push(key);
  17. }
  18. }
  19. return keys;
  20. };
  21. /**
  22. * Return own values in `obj`.
  23. *
  24. * @param {Object} obj
  25. * @return {Array}
  26. * @api public
  27. */
  28. exports.values = function(obj){
  29. var vals = [];
  30. for (var key in obj) {
  31. if (has.call(obj, key)) {
  32. vals.push(obj[key]);
  33. }
  34. }
  35. return vals;
  36. };
  37. /**
  38. * Merge `b` into `a`.
  39. *
  40. * @param {Object} a
  41. * @param {Object} b
  42. * @return {Object} a
  43. * @api public
  44. */
  45. exports.merge = function(a, b){
  46. for (var key in b) {
  47. if (has.call(b, key)) {
  48. a[key] = b[key];
  49. }
  50. }
  51. return a;
  52. };
  53. /**
  54. * Return length of `obj`.
  55. *
  56. * @param {Object} obj
  57. * @return {Number}
  58. * @api public
  59. */
  60. exports.length = function(obj){
  61. return exports.keys(obj).length;
  62. };
  63. /**
  64. * Check if `obj` is empty.
  65. *
  66. * @param {Object} obj
  67. * @return {Boolean}
  68. * @api public
  69. */
  70. exports.isEmpty = function(obj){
  71. return 0 == exports.length(obj);
  72. };