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