util.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * Convert a value to a string that is actually rendered.
  3. *
  4. * @param {*} val
  5. * @return {String}
  6. */
  7. export function renderString (val) {
  8. return val == null
  9. ? ''
  10. : typeof val === 'object'
  11. ? JSON.stringify(val, null, 2)
  12. : String(val)
  13. }
  14. /**
  15. * Make a map and return a function for checking if a key
  16. * is in that map.
  17. *
  18. * @param {String} str
  19. * @param {Boolean} expectsLowerCase
  20. * @return {Function}
  21. */
  22. export function makeMap (str, expectsLowerCase) {
  23. const map = Object.create(null)
  24. const list = str.split(',')
  25. for (let i = 0; i < list.length; i++) {
  26. map[list[i]] = true
  27. }
  28. return expectsLowerCase
  29. ? val => map[val.toLowerCase()]
  30. : val => map[val]
  31. }
  32. /**
  33. * Check if a tag is a built-in tag.
  34. */
  35. export const isBuiltInTag = makeMap('slot,component,render,transition', true)
  36. /**
  37. * Remove an item from an array
  38. *
  39. * @param {Array} arr
  40. * @param {*} item
  41. */
  42. export function remove (arr, item) {
  43. if (arr.length) {
  44. const index = arr.indexOf(item)
  45. if (index > -1) {
  46. return arr.splice(index, 1)
  47. }
  48. }
  49. }
  50. /**
  51. * Check whether the object has the property.
  52. *
  53. * @param {Object} obj
  54. * @param {String} key
  55. * @return {Boolean}
  56. */
  57. const hasOwnProperty = Object.prototype.hasOwnProperty
  58. export function hasOwn (obj, key) {
  59. return hasOwnProperty.call(obj, key)
  60. }
  61. /**
  62. * Check if value is primitive
  63. *
  64. * @param {*} value
  65. * @return {Boolean}
  66. */
  67. export function isPrimitive (value) {
  68. return typeof value === 'string' || typeof value === 'number'
  69. }
  70. /**
  71. * Create a cached version of a pure function.
  72. *
  73. * @param {Function} fn
  74. * @return {Function}
  75. */
  76. export function cached (fn) {
  77. const cache = Object.create(null)
  78. return function cachedFn (str) {
  79. const hit = cache[str]
  80. return hit || (cache[str] = fn(str))
  81. }
  82. }
  83. /**
  84. * Camelize a hyphen-delmited string.
  85. *
  86. * @param {String} str
  87. * @return {String}
  88. */
  89. const camelizeRE = /-(\w)/g
  90. export const camelize = cached(str => {
  91. return str.replace(camelizeRE, toUpper)
  92. })
  93. function toUpper (_, c) {
  94. return c ? c.toUpperCase() : ''
  95. }
  96. /**
  97. * Hyphenate a camelCase string.
  98. *
  99. * @param {String} str
  100. * @return {String}
  101. */
  102. const hyphenateRE = /([a-z\d])([A-Z])/g
  103. export const hyphenate = cached(str => {
  104. return str
  105. .replace(hyphenateRE, '$1-$2')
  106. .toLowerCase()
  107. })
  108. /**
  109. * Simple bind, faster than native
  110. *
  111. * @param {Function} fn
  112. * @param {Object} ctx
  113. * @return {Function}
  114. */
  115. export function bind (fn, ctx) {
  116. return function (a) {
  117. const l = arguments.length
  118. return l
  119. ? l > 1
  120. ? fn.apply(ctx, arguments)
  121. : fn.call(ctx, a)
  122. : fn.call(ctx)
  123. }
  124. }
  125. /**
  126. * Convert an Array-like object to a real Array.
  127. *
  128. * @param {Array-like} list
  129. * @param {Number} [start] - start index
  130. * @return {Array}
  131. */
  132. export function toArray (list, start) {
  133. start = start || 0
  134. let i = list.length - start
  135. const ret = new Array(i)
  136. while (i--) {
  137. ret[i] = list[i + start]
  138. }
  139. return ret
  140. }
  141. /**
  142. * Mix properties into target object.
  143. *
  144. * @param {Object} to
  145. * @param {Object} from
  146. */
  147. export function extend (to, _from) {
  148. for (const key in _from) {
  149. to[key] = _from[key]
  150. }
  151. return to
  152. }
  153. /**
  154. * Quick object check - this is primarily used to tell
  155. * Objects from primitive values when we know the value
  156. * is a JSON-compliant type.
  157. *
  158. * @param {*} obj
  159. * @return {Boolean}
  160. */
  161. export function isObject (obj) {
  162. return obj !== null && typeof obj === 'object'
  163. }
  164. /**
  165. * Strict object type check. Only returns true
  166. * for plain JavaScript objects.
  167. *
  168. * @param {*} obj
  169. * @return {Boolean}
  170. */
  171. const toString = Object.prototype.toString
  172. const OBJECT_STRING = '[object Object]'
  173. export function isPlainObject (obj) {
  174. return toString.call(obj) === OBJECT_STRING
  175. }
  176. /**
  177. * Array type check.
  178. *
  179. * @param {*} obj
  180. * @return {Boolean}
  181. */
  182. export const isArray = Array.isArray