lang.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * Check is a string starts with $ or _
  3. *
  4. * @param {String} str
  5. * @return {Boolean}
  6. */
  7. exports.isReserved = function (str) {
  8. var c = (str + '').charCodeAt(0)
  9. return c === 0x24 || c === 0x5F
  10. }
  11. /**
  12. * Guard text output, make sure undefined outputs
  13. * empty string
  14. *
  15. * @param {*} value
  16. * @return {String}
  17. */
  18. exports.toString = function (value) {
  19. return value == null
  20. ? ''
  21. : value.toString()
  22. }
  23. /**
  24. * Check and convert possible numeric numbers before
  25. * setting back to data
  26. *
  27. * @param {*} value
  28. * @return {*|Number}
  29. */
  30. exports.toNumber = function (value) {
  31. return (
  32. isNaN(value) ||
  33. value === null ||
  34. typeof value === 'boolean'
  35. ) ? value
  36. : Number(value)
  37. }
  38. /**
  39. * Strip quotes from a string
  40. *
  41. * @param {String} str
  42. * @return {String | false}
  43. */
  44. exports.stripQuotes = function (str) {
  45. var a = str.charCodeAt(0)
  46. var b = str.charCodeAt(str.length - 1)
  47. return a === b && (a === 0x22 || a === 0x27)
  48. ? str.slice(1, -1)
  49. : false
  50. }
  51. /**
  52. * Replace helper
  53. *
  54. * @param {String} _ - matched delimiter
  55. * @param {String} c - matched char
  56. * @return {String}
  57. */
  58. function toUpper (_, c) {
  59. return c ? c.toUpperCase () : ''
  60. }
  61. /**
  62. * Camelize a hyphen-delmited string.
  63. *
  64. * @param {String} str
  65. * @return {String}
  66. */
  67. var camelRE = /-(\w)/g
  68. exports.camelize = function (str) {
  69. return str.replace(camelRE, toUpper)
  70. }
  71. /**
  72. * Converts hyphen/underscore/slash delimitered names into
  73. * camelized classNames.
  74. *
  75. * e.g. my-component => MyComponent
  76. * some_else => SomeElse
  77. * some/comp => SomeComp
  78. *
  79. * @param {String} str
  80. * @return {String}
  81. */
  82. var classifyRE = /(?:^|[-_\/])(\w)/g
  83. exports.classify = function (str) {
  84. return str.replace(classifyRE, toUpper)
  85. }
  86. /**
  87. * Simple bind, faster than native
  88. *
  89. * @param {Function} fn
  90. * @param {Object} ctx
  91. * @return {Function}
  92. */
  93. exports.bind = function (fn, ctx) {
  94. return function (a) {
  95. var l = arguments.length
  96. return l
  97. ? l > 1
  98. ? fn.apply(ctx, arguments)
  99. : fn.call(ctx, a)
  100. : fn.call(ctx)
  101. }
  102. }
  103. /**
  104. * Convert an Array-like object to a real Array.
  105. *
  106. * @param {Array-like} list
  107. * @param {Number} [start] - start index
  108. * @return {Array}
  109. */
  110. exports.toArray = function (list, start) {
  111. start = start || 0
  112. var i = list.length - start
  113. var ret = new Array(i)
  114. while (i--) {
  115. ret[i] = list[i + start]
  116. }
  117. return ret
  118. }
  119. /**
  120. * Mix properties into target object.
  121. *
  122. * @param {Object} to
  123. * @param {Object} from
  124. */
  125. exports.extend = function (to, from) {
  126. for (var key in from) {
  127. to[key] = from[key]
  128. }
  129. return to
  130. }
  131. /**
  132. * Quick object check - this is primarily used to tell
  133. * Objects from primitive values when we know the value
  134. * is a JSON-compliant type.
  135. *
  136. * @param {*} obj
  137. * @return {Boolean}
  138. */
  139. exports.isObject = function (obj) {
  140. return obj && typeof obj === 'object'
  141. }
  142. /**
  143. * Strict object type check. Only returns true
  144. * for plain JavaScript objects.
  145. *
  146. * @param {*} obj
  147. * @return {Boolean}
  148. */
  149. var toString = Object.prototype.toString
  150. exports.isPlainObject = function (obj) {
  151. return toString.call(obj) === '[object Object]'
  152. }
  153. /**
  154. * Array type check.
  155. *
  156. * @param {*} obj
  157. * @return {Boolean}
  158. */
  159. exports.isArray = function (obj) {
  160. return Array.isArray(obj)
  161. }
  162. /**
  163. * Define a non-enumerable property
  164. *
  165. * @param {Object} obj
  166. * @param {String} key
  167. * @param {*} val
  168. * @param {Boolean} [enumerable]
  169. */
  170. exports.define = function (obj, key, val, enumerable) {
  171. Object.defineProperty(obj, key, {
  172. value : val,
  173. enumerable : !!enumerable,
  174. writable : true,
  175. configurable : true
  176. })
  177. }
  178. /**
  179. * Debounce a function so it only gets called after the
  180. * input stops arriving after the given wait period.
  181. *
  182. * @param {Function} func
  183. * @param {Number} wait
  184. * @return {Function} - the debounced function
  185. */
  186. exports.debounce = function(func, wait) {
  187. var timeout, args, context, timestamp, result
  188. var later = function() {
  189. var last = Date.now() - timestamp
  190. if (last < wait && last >= 0) {
  191. timeout = setTimeout(later, wait - last)
  192. } else {
  193. timeout = null
  194. result = func.apply(context, args)
  195. if (!timeout) context = args = null
  196. }
  197. }
  198. return function() {
  199. context = this
  200. args = arguments
  201. timestamp = Date.now()
  202. if (!timeout) {
  203. timeout = setTimeout(later, wait)
  204. }
  205. return result
  206. }
  207. }
  208. /**
  209. * Manual indexOf because it's slightly faster than
  210. * native.
  211. *
  212. * @param {Array} arr
  213. * @param {*} obj
  214. */
  215. exports.indexOf = function (arr, obj) {
  216. for (var i = 0, l = arr.length; i < l; i++) {
  217. if (arr[i] === obj) return i
  218. }
  219. return -1
  220. }