lang.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * Simple bind, faster than native
  3. *
  4. * @param {Function} fn
  5. * @param {Object} ctx
  6. */
  7. exports.bind = function (fn, ctx) {
  8. return function () {
  9. return fn.apply(ctx, arguments)
  10. }
  11. }
  12. /**
  13. * Convert an Array-like object to a real Array.
  14. *
  15. * @param {Array-like} list
  16. * @param {Number} [i] - start index
  17. */
  18. var slice = [].slice
  19. exports.toArray = function (list, i) {
  20. return slice.call(list, i || 0)
  21. }
  22. /**
  23. * Mix properties into target object.
  24. *
  25. * @param {Object} to
  26. * @param {Object} from
  27. */
  28. exports.extend = function (to, from) {
  29. for (var key in from) {
  30. to[key] = from[key]
  31. }
  32. }
  33. /**
  34. * Mixin including non-enumerables, and copy property descriptors.
  35. *
  36. * @param {Object} to
  37. * @param {Object} from
  38. */
  39. exports.deepMixin = function (to, from) {
  40. Object.getOwnPropertyNames(from).forEach(function (key) {
  41. var descriptor = Object.getOwnPropertyDescriptor(from, key)
  42. Object.defineProperty(to, key, descriptor)
  43. })
  44. }
  45. /**
  46. * Proxy a property on one object to another.
  47. *
  48. * @param {Object} to
  49. * @param {Object} from
  50. * @param {String} key
  51. */
  52. exports.proxy = function (to, from, key) {
  53. if (to.hasOwnProperty(key)) return
  54. Object.defineProperty(to, key, {
  55. enumerable: true,
  56. configurable: true,
  57. get: function () {
  58. return from[key]
  59. },
  60. set: function (val) {
  61. from[key] = val
  62. }
  63. })
  64. }
  65. /**
  66. * Object type check. Only returns true
  67. * for plain JavaScript objects.
  68. *
  69. * @param {*} obj
  70. * @return {Boolean}
  71. */
  72. exports.isObject = function (obj) {
  73. return Object.prototype.toString.call(obj) === '[object Object]'
  74. }
  75. /**
  76. * Array type check.
  77. *
  78. * @param {*} obj
  79. * @return {Boolean}
  80. */
  81. exports.isArray = function (obj) {
  82. return Array.isArray(obj)
  83. }
  84. /**
  85. * Define a non-enumerable property
  86. *
  87. * @param {Object} obj
  88. * @param {String} key
  89. * @param {*} val
  90. * @param {Boolean} [enumerable]
  91. */
  92. exports.define = function (obj, key, val, enumerable) {
  93. Object.defineProperty(obj, key, {
  94. value : val,
  95. enumerable : !!enumerable,
  96. writable : true,
  97. configurable : true
  98. })
  99. }
  100. /**
  101. * Augment an target Object or Array by either
  102. * intercepting the prototype chain using __proto__,
  103. * or copy over property descriptors
  104. *
  105. * @param {Object|Array} target
  106. * @param {Object} proto
  107. */
  108. if ('__proto__' in {}) {
  109. exports.augment = function (target, proto) {
  110. target.__proto__ = proto
  111. }
  112. } else {
  113. exports.augment = exports.deepMixin
  114. }