util.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* @flow */
  2. /**
  3. * constants
  4. */
  5. export const numberFormatKeys = [
  6. 'compactDisplay',
  7. 'currency',
  8. 'currencyDisplay',
  9. 'currencySign',
  10. 'localeMatcher',
  11. 'notation',
  12. 'numberingSystem',
  13. 'signDisplay',
  14. 'style',
  15. 'unit',
  16. 'unitDisplay',
  17. 'useGrouping',
  18. 'minimumIntegerDigits',
  19. 'minimumFractionDigits',
  20. 'maximumFractionDigits',
  21. 'minimumSignificantDigits',
  22. 'maximumSignificantDigits'
  23. ]
  24. /**
  25. * utilities
  26. */
  27. export function warn (msg: string, err: ?Error): void {
  28. if (typeof console !== 'undefined') {
  29. console.warn('[vue-i18n] ' + msg)
  30. /* istanbul ignore if */
  31. if (err) {
  32. console.warn(err.stack)
  33. }
  34. }
  35. }
  36. export function error (msg: string, err: ?Error): void {
  37. if (typeof console !== 'undefined') {
  38. console.error('[vue-i18n] ' + msg)
  39. /* istanbul ignore if */
  40. if (err) {
  41. console.error(err.stack)
  42. }
  43. }
  44. }
  45. export const isArray = Array.isArray
  46. export function isObject (obj: mixed): boolean %checks {
  47. return obj !== null && typeof obj === 'object'
  48. }
  49. export function isBoolean (val: mixed): boolean %checks {
  50. return typeof val === 'boolean'
  51. }
  52. export function isString (val: mixed): boolean %checks {
  53. return typeof val === 'string'
  54. }
  55. const toString: Function = Object.prototype.toString
  56. const OBJECT_STRING: string = '[object Object]'
  57. export function isPlainObject (obj: any): boolean {
  58. return toString.call(obj) === OBJECT_STRING
  59. }
  60. export function isNull (val: mixed): boolean {
  61. return val === null || val === undefined
  62. }
  63. export function isFunction (val: mixed): boolean %checks {
  64. return typeof val === 'function'
  65. }
  66. export function parseArgs (...args: Array<mixed>): Object {
  67. let locale: ?string = null
  68. let params: mixed = null
  69. if (args.length === 1) {
  70. if (isObject(args[0]) || isArray(args[0])) {
  71. params = args[0]
  72. } else if (typeof args[0] === 'string') {
  73. locale = args[0]
  74. }
  75. } else if (args.length === 2) {
  76. if (typeof args[0] === 'string') {
  77. locale = args[0]
  78. }
  79. /* istanbul ignore if */
  80. if (isObject(args[1]) || isArray(args[1])) {
  81. params = args[1]
  82. }
  83. }
  84. return { locale, params }
  85. }
  86. export function looseClone (obj: Object): Object {
  87. return JSON.parse(JSON.stringify(obj))
  88. }
  89. export function remove (arr: Set<any>, item: any): Set<any> | void {
  90. if (arr.delete(item)) {
  91. return arr
  92. }
  93. }
  94. export function arrayFrom (arr: Set<any>): Array<any> {
  95. const ret = []
  96. arr.forEach(a => ret.push(a))
  97. return ret
  98. }
  99. export function includes (arr: Array<any>, item: any): boolean {
  100. return !!~arr.indexOf(item)
  101. }
  102. const hasOwnProperty = Object.prototype.hasOwnProperty
  103. export function hasOwn (obj: Object | Array<*>, key: string): boolean {
  104. return hasOwnProperty.call(obj, key)
  105. }
  106. export function merge (target: Object): Object {
  107. const output = Object(target)
  108. for (let i = 1; i < arguments.length; i++) {
  109. const source = arguments[i]
  110. if (source !== undefined && source !== null) {
  111. let key
  112. for (key in source) {
  113. if (hasOwn(source, key)) {
  114. if (isObject(source[key])) {
  115. output[key] = merge(output[key], source[key])
  116. } else {
  117. output[key] = source[key]
  118. }
  119. }
  120. }
  121. }
  122. }
  123. return output
  124. }
  125. export function looseEqual (a: any, b: any): boolean {
  126. if (a === b) { return true }
  127. const isObjectA: boolean = isObject(a)
  128. const isObjectB: boolean = isObject(b)
  129. if (isObjectA && isObjectB) {
  130. try {
  131. const isArrayA: boolean = isArray(a)
  132. const isArrayB: boolean = isArray(b)
  133. if (isArrayA && isArrayB) {
  134. return a.length === b.length && a.every((e: any, i: number): boolean => {
  135. return looseEqual(e, b[i])
  136. })
  137. } else if (!isArrayA && !isArrayB) {
  138. const keysA: Array<string> = Object.keys(a)
  139. const keysB: Array<string> = Object.keys(b)
  140. return keysA.length === keysB.length && keysA.every((key: string): boolean => {
  141. return looseEqual(a[key], b[key])
  142. })
  143. } else {
  144. /* istanbul ignore next */
  145. return false
  146. }
  147. } catch (e) {
  148. /* istanbul ignore next */
  149. return false
  150. }
  151. } else if (!isObjectA && !isObjectB) {
  152. return String(a) === String(b)
  153. } else {
  154. return false
  155. }
  156. }
  157. /**
  158. * Sanitizes html special characters from input strings. For mitigating risk of XSS attacks.
  159. * @param rawText The raw input from the user that should be escaped.
  160. */
  161. function escapeHtml(rawText: string): string {
  162. return rawText
  163. .replace(/</g, '&lt;')
  164. .replace(/>/g, '&gt;')
  165. .replace(/"/g, '&quot;')
  166. .replace(/'/g, '&apos;')
  167. }
  168. /**
  169. * Escapes html tags and special symbols from all provided params which were returned from parseArgs().params.
  170. * This method performs an in-place operation on the params object.
  171. *
  172. * @param {any} params Parameters as provided from `parseArgs().params`.
  173. * May be either an array of strings or a string->any map.
  174. *
  175. * @returns The manipulated `params` object.
  176. */
  177. export function escapeParams(params: any): any {
  178. if(params != null) {
  179. Object.keys(params).forEach(key => {
  180. if(typeof(params[key]) == 'string') {
  181. params[key] = escapeHtml(params[key])
  182. }
  183. })
  184. }
  185. return params
  186. }