util.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Should
  3. * Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
  4. * MIT Licensed
  5. */
  6. var type = require('should-type');
  7. var config = require('./config');
  8. /**
  9. * Check if given obj just a primitive type wrapper
  10. * @param {Object} obj
  11. * @returns {boolean}
  12. * @private
  13. */
  14. exports.isWrapperType = function(obj) {
  15. return obj instanceof Number || obj instanceof String || obj instanceof Boolean;
  16. };
  17. exports.merge = function(a, b) {
  18. if(a && b) {
  19. for(var key in b) {
  20. a[key] = b[key];
  21. }
  22. }
  23. return a;
  24. };
  25. var hasOwnProperty = Object.prototype.hasOwnProperty;
  26. exports.forEach = function forEach(obj, f, context) {
  27. if(exports.isGeneratorFunction(obj)) {
  28. return forEach(obj(), f, context);
  29. } else if (exports.isGeneratorObject(obj)) {
  30. var value = obj.next();
  31. while(!value.done) {
  32. if(f.call(context, value.value, 'value', obj) === false)
  33. return;
  34. value = obj.next();
  35. }
  36. } else {
  37. for(var prop in obj) {
  38. if(hasOwnProperty.call(obj, prop)) {
  39. if(f.call(context, obj[prop], prop, obj) === false)
  40. return;
  41. }
  42. }
  43. }
  44. };
  45. exports.some = function(obj, f, context) {
  46. var res = false;
  47. exports.forEach(obj, function(value, key) {
  48. if(f.call(context, value, key, obj)) {
  49. res = true;
  50. return false;
  51. }
  52. }, context);
  53. return res;
  54. };
  55. exports.isEmptyObject = function(obj) {
  56. for(var prop in obj) {
  57. if(hasOwnProperty.call(obj, prop)) {
  58. return false;
  59. }
  60. }
  61. return true;
  62. };
  63. exports.isIndexable = function(obj) {
  64. var t = type(obj);
  65. return (t.type === type.OBJECT && t.cls === type.ARRAY) ||
  66. (t.type === type.OBJECT && t.cls === type.BUFFER) ||
  67. (t.type === type.OBJECT && t.cls === type.ARGUMENTS) ||
  68. (t.type === type.OBJECT && t.cls === type.ARRAY_BUFFER) ||
  69. (t.type === type.OBJECT && t.cls === type.TYPED_ARRAY) ||
  70. (t.type === type.OBJECT && t.cls === type.DATA_VIEW) ||
  71. (t.type === type.OBJECT && t.cls === type.STRING) ||
  72. (t.type === type.STRING);
  73. };
  74. exports.length = function(obj) {
  75. var t = type(obj);
  76. switch(t.type) {
  77. case type.STRING:
  78. return obj.length;
  79. case type.OBJECT:
  80. switch(t.cls) {
  81. case type.ARRAY_BUFFER:
  82. case type.TYPED_ARRAY:
  83. case type.DATA_VIEW:
  84. return obj.byteLength;
  85. case type.ARRAY:
  86. case type.BUFFER:
  87. case type.ARGUMENTS:
  88. case type.FUNCTION:
  89. return obj.length;
  90. }
  91. }
  92. };
  93. exports.convertPropertyName = function(name) {
  94. if(typeof name == 'symbol') {
  95. return name;
  96. } else {
  97. return String(name);
  98. }
  99. };
  100. exports.isGeneratorObject = function(obj) {
  101. if(!obj) return false;
  102. return typeof obj.next == 'function' &&
  103. typeof obj[Symbol.iterator] == 'function' &&
  104. obj[Symbol.iterator]() === obj;
  105. };
  106. //TODO find better way
  107. exports.isGeneratorFunction = function(f) {
  108. if(typeof f != 'function') return false;
  109. return /^function\s*\*\s*/.test(f.toString());
  110. };
  111. exports.format = function(value, opts) {
  112. return config.getFormatter(opts).format(value);
  113. };
  114. exports.functionName = require('should-format').Formatter.functionName;
  115. exports.formatProp = function(value) {
  116. return config.getFormatter().formatPropertyName(String(value));
  117. };