util.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 parseArgs (...args: Array<mixed>): Object {
  59. let locale: ?string = null
  60. let params: mixed = null
  61. if (args.length === 1) {
  62. if (isObject(args[0]) || Array.isArray(args[0])) {
  63. params = args[0]
  64. } else if (typeof args[0] === 'string') {
  65. locale = args[0]
  66. }
  67. } else if (args.length === 2) {
  68. if (typeof args[0] === 'string') {
  69. locale = args[0]
  70. }
  71. /* istanbul ignore if */
  72. if (isObject(args[1]) || Array.isArray(args[1])) {
  73. params = args[1]
  74. }
  75. }
  76. return { locale, params }
  77. }
  78. export function looseClone (obj: Object): Object {
  79. return JSON.parse(JSON.stringify(obj))
  80. }
  81. export function remove (arr: Array<any>, item: any): Array<any> | void {
  82. if (arr.length) {
  83. const index = arr.indexOf(item)
  84. if (index > -1) {
  85. return arr.splice(index, 1)
  86. }
  87. }
  88. }
  89. export function includes (arr: Array<any>, item: any): boolean {
  90. return !!~arr.indexOf(item)
  91. }
  92. const hasOwnProperty = Object.prototype.hasOwnProperty
  93. export function hasOwn (obj: Object | Array<*>, key: string): boolean {
  94. return hasOwnProperty.call(obj, key)
  95. }
  96. export function merge (target: Object): Object {
  97. const output = Object(target)
  98. for (let i = 1; i < arguments.length; i++) {
  99. const source = arguments[i]
  100. if (source !== undefined && source !== null) {
  101. let key
  102. for (key in source) {
  103. if (hasOwn(source, key)) {
  104. if (isObject(source[key])) {
  105. output[key] = merge(output[key], source[key])
  106. } else {
  107. output[key] = source[key]
  108. }
  109. }
  110. }
  111. }
  112. }
  113. return output
  114. }
  115. export function looseEqual (a: any, b: any): boolean {
  116. if (a === b) { return true }
  117. const isObjectA: boolean = isObject(a)
  118. const isObjectB: boolean = isObject(b)
  119. if (isObjectA && isObjectB) {
  120. try {
  121. const isArrayA: boolean = Array.isArray(a)
  122. const isArrayB: boolean = Array.isArray(b)
  123. if (isArrayA && isArrayB) {
  124. return a.length === b.length && a.every((e: any, i: number): boolean => {
  125. return looseEqual(e, b[i])
  126. })
  127. } else if (!isArrayA && !isArrayB) {
  128. const keysA: Array<string> = Object.keys(a)
  129. const keysB: Array<string> = Object.keys(b)
  130. return keysA.length === keysB.length && keysA.every((key: string): boolean => {
  131. return looseEqual(a[key], b[key])
  132. })
  133. } else {
  134. /* istanbul ignore next */
  135. return false
  136. }
  137. } catch (e) {
  138. /* istanbul ignore next */
  139. return false
  140. }
  141. } else if (!isObjectA && !isObjectB) {
  142. return String(a) === String(b)
  143. } else {
  144. return false
  145. }
  146. }