444cfe8aad587ea534c731d30db26e824c82a9d7e9b39403bb5a61daf3377e46532ca6d4145f276756f19c3b069fd60c72fc846bcc6451f2144143f3707b9e 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. ## Utilities
  3. */
  4. var Util = {}
  5. Util.extend = function extend() {
  6. var target = arguments[0] || {},
  7. i = 1,
  8. length = arguments.length,
  9. options, name, src, copy, clone
  10. if (length === 1) {
  11. target = this
  12. i = 0
  13. }
  14. for (; i < length; i++) {
  15. options = arguments[i]
  16. if (!options) continue
  17. for (name in options) {
  18. src = target[name]
  19. copy = options[name]
  20. if (target === copy) continue
  21. if (copy === undefined) continue
  22. if (Util.isArray(copy) || Util.isObject(copy)) {
  23. if (Util.isArray(copy)) clone = src && Util.isArray(src) ? src : []
  24. if (Util.isObject(copy)) clone = src && Util.isObject(src) ? src : {}
  25. target[name] = Util.extend(clone, copy)
  26. } else {
  27. target[name] = copy
  28. }
  29. }
  30. }
  31. return target
  32. }
  33. Util.each = function each(obj, iterator, context) {
  34. var i, key
  35. if (this.type(obj) === 'number') {
  36. for (i = 0; i < obj; i++) {
  37. iterator(i, i)
  38. }
  39. } else if (obj.length === +obj.length) {
  40. for (i = 0; i < obj.length; i++) {
  41. if (iterator.call(context, obj[i], i, obj) === false) break
  42. }
  43. } else {
  44. for (key in obj) {
  45. if (iterator.call(context, obj[key], key, obj) === false) break
  46. }
  47. }
  48. }
  49. Util.type = function type(obj) {
  50. return (obj === null || obj === undefined) ? String(obj) : Object.prototype.toString.call(obj).match(/\[object (\w+)\]/)[1].toLowerCase()
  51. }
  52. Util.each('String Object Array RegExp Function'.split(' '), function(value) {
  53. Util['is' + value] = function(obj) {
  54. return Util.type(obj) === value.toLowerCase()
  55. }
  56. })
  57. Util.isObjectOrArray = function(value) {
  58. return Util.isObject(value) || Util.isArray(value)
  59. }
  60. Util.isNumeric = function(value) {
  61. return !isNaN(parseFloat(value)) && isFinite(value)
  62. }
  63. Util.keys = function(obj) {
  64. var keys = [];
  65. for (var key in obj) {
  66. if (obj.hasOwnProperty(key)) keys.push(key)
  67. }
  68. return keys;
  69. }
  70. Util.values = function(obj) {
  71. var values = [];
  72. for (var key in obj) {
  73. if (obj.hasOwnProperty(key)) values.push(obj[key])
  74. }
  75. return values;
  76. }
  77. /*
  78. ### Mock.heredoc(fn)
  79. * Mock.heredoc(fn)
  80. 以直观、安全的方式书写(多行)HTML 模板。
  81. **使用示例**如下所示:
  82. var tpl = Mock.heredoc(function() {
  83. /*!
  84. {{email}}{{age}}
  85. <!-- Mock {
  86. email: '@EMAIL',
  87. age: '@INT(1,100)'
  88. } -->
  89. *\/
  90. })
  91. **相关阅读**
  92. * [Creating multiline strings in JavaScript](http://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript)、
  93. */
  94. Util.heredoc = function heredoc(fn) {
  95. // 1. 移除起始的 function(){ /*!
  96. // 2. 移除末尾的 */ }
  97. // 3. 移除起始和末尾的空格
  98. return fn.toString()
  99. .replace(/^[^\/]+\/\*!?/, '')
  100. .replace(/\*\/[^\/]+$/, '')
  101. .replace(/^[\s\xA0]+/, '').replace(/[\s\xA0]+$/, '') // .trim()
  102. }
  103. Util.noop = function() {}
  104. module.exports = Util