util.js 5.4 KB

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