Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.js 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* @flow */
  2. export const emptyObject = Object.freeze({})
  3. // These helpers produce better VM code in JS engines due to their
  4. // explicitness and function inlining.
  5. export function isUndef (v: any): boolean %checks {
  6. return v === undefined || v === null
  7. }
  8. export function isDef (v: any): boolean %checks {
  9. return v !== undefined && v !== null
  10. }
  11. export function isTrue (v: any): boolean %checks {
  12. return v === true
  13. }
  14. export function isFalse (v: any): boolean %checks {
  15. return v === false
  16. }
  17. /**
  18. * Check if value is primitive.
  19. */
  20. export function isPrimitive (value: any): boolean %checks {
  21. return (
  22. typeof value === 'string' ||
  23. typeof value === 'number' ||
  24. // $flow-disable-line
  25. typeof value === 'symbol' ||
  26. typeof value === 'boolean'
  27. )
  28. }
  29. /**
  30. * Quick object check - this is primarily used to tell
  31. * Objects from primitive values when we know the value
  32. * is a JSON-compliant type.
  33. */
  34. export function isObject (obj: mixed): boolean %checks {
  35. return obj !== null && typeof obj === 'object'
  36. }
  37. /**
  38. * Get the raw type string of a value, e.g., [object Object].
  39. */
  40. const _toString = Object.prototype.toString
  41. export function toRawType (value: any): string {
  42. return _toString.call(value).slice(8, -1)
  43. }
  44. /**
  45. * Strict object type check. Only returns true
  46. * for plain JavaScript objects.
  47. */
  48. export function isPlainObject (obj: any): boolean {
  49. return _toString.call(obj) === '[object Object]'
  50. }
  51. export function isRegExp (v: any): boolean {
  52. return _toString.call(v) === '[object RegExp]'
  53. }
  54. /**
  55. * Check if val is a valid array index.
  56. */
  57. export function isValidArrayIndex (val: any): boolean {
  58. const n = parseFloat(String(val))
  59. return n >= 0 && Math.floor(n) === n && isFinite(val)
  60. }
  61. export function isPromise (val: any): boolean {
  62. return (
  63. isDef(val) &&
  64. typeof val.then === 'function' &&
  65. typeof val.catch === 'function'
  66. )
  67. }
  68. /**
  69. * Convert a value to a string that is actually rendered.
  70. */
  71. export function toString (val: any): string {
  72. return val == null
  73. ? ''
  74. : Array.isArray(val) || (isPlainObject(val) && val.toString === _toString)
  75. ? JSON.stringify(val, null, 2)
  76. : String(val)
  77. }
  78. /**
  79. * Convert an input value to a number for persistence.
  80. * If the conversion fails, return original string.
  81. */
  82. export function toNumber (val: string): number | string {
  83. const n = parseFloat(val)
  84. return isNaN(n) ? val : n
  85. }
  86. /**
  87. * Make a map and return a function for checking if a key
  88. * is in that map.
  89. */
  90. export function makeMap (
  91. str: string,
  92. expectsLowerCase?: boolean
  93. ): (key: string) => true | void {
  94. const map = Object.create(null)
  95. const list: Array<string> = str.split(',')
  96. for (let i = 0; i < list.length; i++) {
  97. map[list[i]] = true
  98. }
  99. return expectsLowerCase
  100. ? val => map[val.toLowerCase()]
  101. : val => map[val]
  102. }
  103. /**
  104. * Check if a tag is a built-in tag.
  105. */
  106. export const isBuiltInTag = makeMap('slot,component', true)
  107. /**
  108. * Check if an attribute is a reserved attribute.
  109. */
  110. export const isReservedAttribute = makeMap('key,ref,slot,slot-scope,is')
  111. /**
  112. * Remove an item from an array.
  113. */
  114. export function remove (arr: Array<any>, item: any): Array<any> | void {
  115. if (arr.length) {
  116. const index = arr.indexOf(item)
  117. if (index > -1) {
  118. return arr.splice(index, 1)
  119. }
  120. }
  121. }
  122. /**
  123. * Check whether an object has the property.
  124. */
  125. const hasOwnProperty = Object.prototype.hasOwnProperty
  126. export function hasOwn (obj: Object | Array<*>, key: string): boolean {
  127. return hasOwnProperty.call(obj, key)
  128. }
  129. /**
  130. * Create a cached version of a pure function.
  131. */
  132. export function cached<F: Function> (fn: F): F {
  133. const cache = Object.create(null)
  134. return (function cachedFn (str: string) {
  135. const hit = cache[str]
  136. return hit || (cache[str] = fn(str))
  137. }: any)
  138. }
  139. /**
  140. * Camelize a hyphen-delimited string.
  141. */
  142. const camelizeRE = /-(\w)/g
  143. export const camelize = cached((str: string): string => {
  144. return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
  145. })
  146. /**
  147. * Capitalize a string.
  148. */
  149. export const capitalize = cached((str: string): string => {
  150. return str.charAt(0).toUpperCase() + str.slice(1)
  151. })
  152. /**
  153. * Hyphenate a camelCase string.
  154. */
  155. const hyphenateRE = /\B([A-Z])/g
  156. export const hyphenate = cached((str: string): string => {
  157. return str.replace(hyphenateRE, '-$1').toLowerCase()
  158. })
  159. /**
  160. * Simple bind polyfill for environments that do not support it,
  161. * e.g., PhantomJS 1.x. Technically, we don't need this anymore
  162. * since native bind is now performant enough in most browsers.
  163. * But removing it would mean breaking code that was able to run in
  164. * PhantomJS 1.x, so this must be kept for backward compatibility.
  165. */
  166. /* istanbul ignore next */
  167. function polyfillBind (fn: Function, ctx: Object): Function {
  168. function boundFn (a) {
  169. const l = arguments.length
  170. return l
  171. ? l > 1
  172. ? fn.apply(ctx, arguments)
  173. : fn.call(ctx, a)
  174. : fn.call(ctx)
  175. }
  176. boundFn._length = fn.length
  177. return boundFn
  178. }
  179. function nativeBind (fn: Function, ctx: Object): Function {
  180. return fn.bind(ctx)
  181. }
  182. export const bind = Function.prototype.bind
  183. ? nativeBind
  184. : polyfillBind
  185. /**
  186. * Convert an Array-like object to a real Array.
  187. */
  188. export function toArray (list: any, start?: number): Array<any> {
  189. start = start || 0
  190. let i = list.length - start
  191. const ret: Array<any> = new Array(i)
  192. while (i--) {
  193. ret[i] = list[i + start]
  194. }
  195. return ret
  196. }
  197. /**
  198. * Mix properties into target object.
  199. */
  200. export function extend (to: Object, _from: ?Object): Object {
  201. for (const key in _from) {
  202. to[key] = _from[key]
  203. }
  204. return to
  205. }
  206. /**
  207. * Merge an Array of Objects into a single Object.
  208. */
  209. export function toObject (arr: Array<any>): Object {
  210. const res = {}
  211. for (let i = 0; i < arr.length; i++) {
  212. if (arr[i]) {
  213. extend(res, arr[i])
  214. }
  215. }
  216. return res
  217. }
  218. /* eslint-disable no-unused-vars */
  219. /**
  220. * Perform no operation.
  221. * Stubbing args to make Flow happy without leaving useless transpiled code
  222. * with ...rest (https://flow.org/blog/2017/05/07/Strict-Function-Call-Arity/).
  223. */
  224. export function noop (a?: any, b?: any, c?: any) {}
  225. /**
  226. * Always return false.
  227. */
  228. export const no = (a?: any, b?: any, c?: any) => false
  229. /* eslint-enable no-unused-vars */
  230. /**
  231. * Return the same value.
  232. */
  233. export const identity = (_: any) => _
  234. /**
  235. * Generate a string containing static keys from compiler modules.
  236. */
  237. export function genStaticKeys (modules: Array<ModuleOptions>): string {
  238. return modules.reduce((keys, m) => {
  239. return keys.concat(m.staticKeys || [])
  240. }, []).join(',')
  241. }
  242. /**
  243. * Check if two values are loosely equal - that is,
  244. * if they are plain objects, do they have the same shape?
  245. */
  246. export function looseEqual (a: any, b: any): boolean {
  247. if (a === b) return true
  248. const isObjectA = isObject(a)
  249. const isObjectB = isObject(b)
  250. if (isObjectA && isObjectB) {
  251. try {
  252. const isArrayA = Array.isArray(a)
  253. const isArrayB = Array.isArray(b)
  254. if (isArrayA && isArrayB) {
  255. return a.length === b.length && a.every((e, i) => {
  256. return looseEqual(e, b[i])
  257. })
  258. } else if (a instanceof Date && b instanceof Date) {
  259. return a.getTime() === b.getTime()
  260. } else if (!isArrayA && !isArrayB) {
  261. const keysA = Object.keys(a)
  262. const keysB = Object.keys(b)
  263. return keysA.length === keysB.length && keysA.every(key => {
  264. return looseEqual(a[key], b[key])
  265. })
  266. } else {
  267. /* istanbul ignore next */
  268. return false
  269. }
  270. } catch (e) {
  271. /* istanbul ignore next */
  272. return false
  273. }
  274. } else if (!isObjectA && !isObjectB) {
  275. return String(a) === String(b)
  276. } else {
  277. return false
  278. }
  279. }
  280. /**
  281. * Return the first index at which a loosely equal value can be
  282. * found in the array (if value is a plain object, the array must
  283. * contain an object of the same shape), or -1 if it is not present.
  284. */
  285. export function looseIndexOf (arr: Array<mixed>, val: mixed): number {
  286. for (let i = 0; i < arr.length; i++) {
  287. if (looseEqual(arr[i], val)) return i
  288. }
  289. return -1
  290. }
  291. /**
  292. * Ensure a function is called only once.
  293. */
  294. export function once (fn: Function): Function {
  295. let called = false
  296. return function () {
  297. if (!called) {
  298. called = true
  299. fn.apply(this, arguments)
  300. }
  301. }
  302. }