util.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* @flow */
  2. /**
  3. * constants
  4. */
  5. export const numberFormatKeys = [
  6. 'style',
  7. 'currency',
  8. 'currencyDisplay',
  9. 'useGrouping',
  10. 'minimumIntegerDigits',
  11. 'minimumFractionDigits',
  12. 'maximumFractionDigits',
  13. 'minimumSignificantDigits',
  14. 'maximumSignificantDigits',
  15. 'localeMatcher',
  16. 'formatMatcher',
  17. 'unit'
  18. ]
  19. /**
  20. * utilities
  21. */
  22. export function warn (msg: string, err: ?Error): void {
  23. if (typeof console !== 'undefined') {
  24. console.warn('[vue-i18n] ' + msg)
  25. /* istanbul ignore if */
  26. if (err) {
  27. console.warn(err.stack)
  28. }
  29. }
  30. }
  31. export function error (msg: string, err: ?Error): void {
  32. if (typeof console !== 'undefined') {
  33. console.error('[vue-i18n] ' + msg)
  34. /* istanbul ignore if */
  35. if (err) {
  36. console.error(err.stack)
  37. }
  38. }
  39. }
  40. export const isArray = Array.isArray
  41. export function isObject (obj: mixed): boolean %checks {
  42. return obj !== null && typeof obj === 'object'
  43. }
  44. export function isBoolean (val: mixed): boolean %checks {
  45. return typeof val === 'boolean'
  46. }
  47. export function isString (val: mixed): boolean %checks {
  48. return typeof val === 'string'
  49. }
  50. const toString: Function = Object.prototype.toString
  51. const OBJECT_STRING: string = '[object Object]'
  52. export function isPlainObject (obj: any): boolean {
  53. return toString.call(obj) === OBJECT_STRING
  54. }
  55. export function isNull (val: mixed): boolean {
  56. return val === null || val === undefined
  57. }
  58. export function isFunction (val: mixed): boolean %checks {
  59. return typeof val === 'function'
  60. }
  61. export function parseArgs (...args: Array<mixed>): Object {
  62. let locale: ?string = null
  63. let params: mixed = null
  64. if (args.length === 1) {
  65. if (isObject(args[0]) || isArray(args[0])) {
  66. params = args[0]
  67. } else if (typeof args[0] === 'string') {
  68. locale = args[0]
  69. }
  70. } else if (args.length === 2) {
  71. if (typeof args[0] === 'string') {
  72. locale = args[0]
  73. }
  74. /* istanbul ignore if */
  75. if (isObject(args[1]) || isArray(args[1])) {
  76. params = args[1]
  77. }
  78. }
  79. return { locale, params }
  80. }
  81. export function looseClone (obj: Object): Object {
  82. return JSON.parse(JSON.stringify(obj))
  83. }
  84. export function remove (arr: Array<any>, item: any): Array<any> | void {
  85. if (arr.length) {
  86. const index = arr.indexOf(item)
  87. if (index > -1) {
  88. return arr.splice(index, 1)
  89. }
  90. }
  91. }
  92. export function includes (arr: Array<any>, item: any): boolean {
  93. return !!~arr.indexOf(item)
  94. }
  95. const hasOwnProperty = Object.prototype.hasOwnProperty
  96. export function hasOwn (obj: Object | Array<*>, key: string): boolean {
  97. return hasOwnProperty.call(obj, key)
  98. }
  99. export function merge (target: Object): Object {
  100. const output = Object(target)
  101. for (let i = 1; i < arguments.length; i++) {
  102. const source = arguments[i]
  103. if (source !== undefined && source !== null) {
  104. let key
  105. for (key in source) {
  106. if (hasOwn(source, key)) {
  107. if (isObject(source[key])) {
  108. output[key] = merge(output[key], source[key])
  109. } else {
  110. output[key] = source[key]
  111. }
  112. }
  113. }
  114. }
  115. }
  116. return output
  117. }
  118. export function looseEqual (a: any, b: any): boolean {
  119. if (a === b) { return true }
  120. const isObjectA: boolean = isObject(a)
  121. const isObjectB: boolean = isObject(b)
  122. if (isObjectA && isObjectB) {
  123. try {
  124. const isArrayA: boolean = isArray(a)
  125. const isArrayB: boolean = isArray(b)
  126. if (isArrayA && isArrayB) {
  127. return a.length === b.length && a.every((e: any, i: number): boolean => {
  128. return looseEqual(e, b[i])
  129. })
  130. } else if (!isArrayA && !isArrayB) {
  131. const keysA: Array<string> = Object.keys(a)
  132. const keysB: Array<string> = Object.keys(b)
  133. return keysA.length === keysB.length && keysA.every((key: string): boolean => {
  134. return looseEqual(a[key], b[key])
  135. })
  136. } else {
  137. /* istanbul ignore next */
  138. return false
  139. }
  140. } catch (e) {
  141. /* istanbul ignore next */
  142. return false
  143. }
  144. } else if (!isObjectA && !isObjectB) {
  145. return String(a) === String(b)
  146. } else {
  147. return false
  148. }
  149. }