UNPKG

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