Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

Commit 8c1e300

Browse files
committed
👕 refactor(flowtype): update flowtype definitions
1 parent 4db315d commit 8c1e300

File tree

6 files changed

+88
-71
lines changed

6 files changed

+88
-71
lines changed

decls/i18n.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,47 @@
1-
declare type Dictionary<T> = { [key: string]: T }
1+
declare type Locale = string;
2+
declare type Path = string;
3+
declare type Message = ?string | MessageObject | MessageArray;
4+
declare type MessageObject = { [key: Path]: Message };
5+
declare type MessageArray = Array<Message>;
6+
declare type Messages = { [key: Locale]: MessageObject };
7+
declare type TranslateResult = string | Array<string>;
28

3-
declare type Messages = Dictionary<any>
4-
5-
declare type MissingHandler = (locale: string, key: string, vm?: any) => void
9+
declare type MissingHandler = (locale: Locale, key: Path, vm?: any) => void;
610

711
declare type I18nOptions = {
8-
locale?: string,
9-
fallbackLocale?: string,
12+
locale?: Locale,
13+
fallbackLocale?: Locale,
1014
messages?: Messages,
1115
formatter?: Formatter,
1216
missing?: MissingHandler,
1317
root?: I18n,
1418
fallbackRoot?: boolean,
1519
sync?: boolean
16-
}
20+
};
1721

1822
declare interface I18n {
1923
static install: () => void,
2024
static version: string,
2125
get vm() :any,
22-
get locale (): string,
23-
set locale (locale: string): void,
24-
get fallbackLocale (): string,
25-
set fallbackLocale (locale: string): void,
26+
get locale (): Locale,
27+
set locale (locale: Locale): void,
28+
get fallbackLocale (): Locale,
29+
set fallbackLocale (locale: Locale): void,
2630
get messages (): Messages,
2731
set messages (messages: Messages): void,
2832
get missing (): ?MissingHandler,
2933
set missing (handler: MissingHandler): void,
3034
get formatter (): Formatter,
3135
set formatter (formatter: Formatter): void,
32-
t (key: string, ...args: any): string,
33-
tc (key: string, choice?: number, ...args: any): any,
34-
te (key: string, ...args: any): boolean,
36+
t (key: Path, ...args: any): TranslateResult,
37+
tc (key: Path, choice?: number, ...args: any): TranslateResult,
38+
te (key: Path, ...args: any): boolean,
3539
watchLocale (): any,
3640
unwatchLocale (): boolean
37-
}
41+
};
3842

39-
declare type FormatterOptions = Dictionary<any>
43+
declare type FormatterOptions = { [key: string]: any };
4044

4145
declare interface Formatter {
42-
format (message: string, ...args: any): any
43-
}
44-
46+
format (message: string, ...args: any): string
47+
};

src/format.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default class BaseFormatter {
1111

1212
get options (): FormatterOptions { return this._options }
1313

14-
format (message: string, ...args: any): any {
14+
format (message: string, ...args: any): string {
1515
return template(message, ...args)
1616
}
1717
}

src/index.js

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

33
import { install, Vue } from './install'
4-
import { warn, isNull, parseArgs, fetchChoice } from './util'
4+
import { warn, isNull, parseArgs, fetchChoice, isPlainObject } from './util'
55
import BaseFormatter from './format'
66
import getPathValue from './path'
77

@@ -16,13 +16,13 @@ export default class VueI18n {
1616
_root: ?I18n
1717
_sync: ?boolean
1818
_fallbackRoot: boolean
19-
_fallbackLocale: string
19+
_fallbackLocale: Locale
2020
_missing: ?MissingHandler
2121
_exist: Function
2222
_watcher: any
2323

2424
constructor (options: I18nOptions = {}) {
25-
const locale: string = options.locale || 'en-US'
25+
const locale: Locale = options.locale || 'en-US'
2626
const messages: Messages = options.messages || {}
2727
this._vm = null
2828
this._fallbackLocale = options.fallbackLocale || 'en-US'
@@ -32,15 +32,15 @@ export default class VueI18n {
3232
this._sync = options.sync || false
3333
this._fallbackRoot = options.fallbackRoot || false
3434

35-
this._exist = (message: Object, key: string): boolean => {
35+
this._exist = (message: Object, key: Path): boolean => {
3636
if (!message || !key) { return false }
3737
return !isNull(getPathValue(message, key))
3838
}
3939

4040
this._resetVM({ locale, messages })
4141
}
4242

43-
_resetVM (data: { locale: string, messages: Messages }): void {
43+
_resetVM (data: { locale: Locale, messages: Messages }): void {
4444
const silent = Vue.config.silent
4545
Vue.config.silent = true
4646
this._vm = new Vue({ data })
@@ -70,19 +70,19 @@ export default class VueI18n {
7070
get messages (): Messages { return this._vm.$data.messages }
7171
set messages (messages: Messages): void { this._vm.$set(this._vm, 'messages', messages) }
7272

73-
get locale (): string { return this._vm.$data.locale }
74-
set locale (locale: string): void { this._vm.$set(this._vm, 'locale', locale) }
73+
get locale (): Locale { return this._vm.$data.locale }
74+
set locale (locale: Locale): void { this._vm.$set(this._vm, 'locale', locale) }
7575

76-
get fallbackLocale (): string { return this._fallbackLocale }
77-
set fallbackLocale (locale: string): void { this._fallbackLocale = locale }
76+
get fallbackLocale (): Locale { return this._fallbackLocale }
77+
set fallbackLocale (locale: Locale): void { this._fallbackLocale = locale }
7878

7979
get missing (): ?MissingHandler { return this._missing }
8080
set missing (handler: MissingHandler): void { this._missing = handler }
8181

8282
get formatter (): Formatter { return this._formatter }
8383
set formatter (formatter: Formatter): void { this._formatter = formatter }
8484

85-
_warnDefault (locale: string, key: string, result: ?any, vm: ?any): ?string {
85+
_warnDefault (locale: Locale, key: Path, result: ?any, vm: ?any): ?string {
8686
if (!isNull(result)) { return result }
8787
if (this.missing) {
8888
this.missing.apply(null, [locale, key, vm])
@@ -101,43 +101,57 @@ export default class VueI18n {
101101
return !val && !isNull(this._root) && this._fallbackRoot
102102
}
103103

104-
_interpolate (message: Messages, key: string, args: any): any {
104+
_interpolate (message: MessageObject, key: Path, args: any): any {
105105
if (!message) { return null }
106106

107-
let val: PathValue = getPathValue(message, key)
108-
if (Array.isArray(val)) { return val }
109-
if (isNull(val)) { val = message[key] }
110-
if (isNull(val)) { return null }
111-
if (typeof val !== 'string') {
112-
warn(`Value of key '${key}' is not a string!`)
113-
return null
107+
const pathRet: PathValue = getPathValue(message, key)
108+
if (Array.isArray(pathRet)) { return pathRet }
109+
110+
let ret: mixed
111+
if (isNull(pathRet)) {
112+
if (isPlainObject(message)) {
113+
ret = message[key]
114+
if (typeof ret !== 'string') {
115+
warn(`Value of key '${key}' is not a string!`)
116+
return null
117+
}
118+
} else {
119+
return null
120+
}
121+
} else {
122+
if (typeof pathRet === 'string') {
123+
ret = pathRet
124+
} else {
125+
warn(`Value of key '${key}' is not a string!`)
126+
return null
127+
}
114128
}
115129

116130
// Check for the existance of links within the translated string
117-
if (val.indexOf('@:') >= 0) {
131+
if (ret.indexOf('@:') >= 0) {
118132
// Match all the links within the local
119133
// We are going to replace each of
120134
// them with its translation
121-
const matches: any = val.match(/(@:[\w|.]+)/g)
135+
const matches: any = ret.match(/(@:[\w|.]+)/g)
122136
for (const idx in matches) {
123137
const link = matches[idx]
124138
// Remove the leading @:
125139
const linkPlaceholder = link.substr(2)
126140
// Translate the link
127141
const translatedstring = this._interpolate(message, linkPlaceholder, args)
128142
// Replace the link with the translated string
129-
val = val.replace(link, translatedstring)
143+
ret = ret.replace(link, translatedstring)
130144
}
131145
}
132146

133-
return !args ? val : this._format(val, args)
147+
return !args ? ret : this._format(ret, args)
134148
}
135149

136-
_format (val: any, ...args: any): any {
137-
return this._formatter.format(val, ...args)
150+
_format (message: string, ...args: any): string {
151+
return this._formatter.format(message, ...args)
138152
}
139153

140-
_translate (messages: Messages, locale: string, fallback: string, key: string, args: any): any {
154+
_translate (messages: Messages, locale: Locale, fallback: Locale, key: Path, args: any): any {
141155
let res: any = null
142156
res = this._interpolate(messages[locale], key, args)
143157
if (!isNull(res)) { return res }
@@ -153,11 +167,11 @@ export default class VueI18n {
153167
}
154168
}
155169

156-
_t (key: string, _locale: string, messages: Messages, host: any, ...args: any): any {
170+
_t (key: Path, _locale: Locale, messages: Messages, host: any, ...args: any): any {
157171
if (!key) { return '' }
158172

159173
const parsedArgs = parseArgs(...args)
160-
const locale = parsedArgs.locale || _locale
174+
const locale: Locale = parsedArgs.locale || _locale
161175

162176
const ret: any = this._translate(messages, locale, this.fallbackLocale, key, parsedArgs.params)
163177
if (this._isFallbackRoot(ret)) {
@@ -171,11 +185,11 @@ export default class VueI18n {
171185
}
172186
}
173187

174-
t (key: string, ...args: any): string {
188+
t (key: Path, ...args: any): TranslateResult {
175189
return this._t(key, this.locale, this.messages, null, ...args)
176190
}
177191

178-
_tc (key: string, _locale: string, messages: Messages, host: any, choice?: number, ...args: any): any {
192+
_tc (key: Path, _locale: Locale, messages: Messages, host: any, choice?: number, ...args: any): any {
179193
if (!key) { return '' }
180194
if (choice !== undefined) {
181195
return fetchChoice(this._t(key, _locale, messages, host, ...args), choice)
@@ -184,16 +198,16 @@ export default class VueI18n {
184198
}
185199
}
186200

187-
tc (key: string, choice?: number, ...args: any): any {
201+
tc (key: Path, choice?: number, ...args: any): TranslateResult {
188202
return this._tc(key, this.locale, this.messages, null, choice, ...args)
189203
}
190204

191-
_te (key: string, _locale: string, messages: Messages, ...args: any): boolean {
192-
const locale = parseArgs(...args).locale || _locale
205+
_te (key: Path, _locale: Locale, messages: Messages, ...args: any): boolean {
206+
const locale: Locale = parseArgs(...args).locale || _locale
193207
return this._exist(messages[locale], key)
194208
}
195209

196-
te (key: string, ...args: any): boolean {
210+
te (key: Path, ...args: any): boolean {
197211
return this._te(key, this.locale, this.messages, ...args)
198212
}
199213
}

src/mixin.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@ import { isPlainObject, warn } from './util'
55

66
export default {
77
computed: {
8-
$t () {
8+
$t (): Function {
99
if (!this.$i18n) {
1010
throw Error(`Failed in $t due to not find VueI18n instance`)
1111
}
1212
// add dependency tracking !!
1313
const locale: string = this.$i18n.locale
1414
const messages: Messages = this.$i18n.messages
15-
return (key: string, ...args: any): string => {
15+
return (key: string, ...args: any): TranslateResult => {
1616
return this.$i18n._t(key, locale, messages, this, ...args)
1717
}
1818
},
1919

20-
$tc () {
20+
$tc (): Function {
2121
if (!this.$i18n) {
2222
throw Error(`Failed in $tc due to not find VueI18n instance`)
2323
}
2424
// add dependency tracking !!
2525
const locale: string = this.$i18n.locale
2626
const messages: Messages = this.$i18n.messages
27-
return (key: string, choice?: number, ...args: any): string => {
27+
return (key: string, choice?: number, ...args: any): TranslateResult => {
2828
return this.$i18n._tc(key, locale, messages, this, choice, ...args)
2929
}
3030
},
3131

32-
$te () {
32+
$te (): Function {
3333
if (!this.$i18n) {
3434
throw Error(`Failed in $te due to not find VueI18n instance`)
3535
}
@@ -42,7 +42,7 @@ export default {
4242
}
4343
},
4444

45-
beforeCreate () {
45+
beforeCreate (): void {
4646
const options: any = this.$options
4747
if (options.i18n) {
4848
if (options.i18n instanceof VueI18n) {
@@ -67,7 +67,7 @@ export default {
6767
}
6868
},
6969

70-
destroyed () {
70+
destroyed (): void {
7171
if (this._localeWatcher) {
7272
this.$i18n.unwatchLocale()
7373
delete this._localeWatcher

src/path.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { isObject, isPlainObject, hasOwn } from './util'
99
*/
1010

1111
// cache
12-
const pathCache = Object.create(null)
12+
const pathCache: { [key: Path]: any } = Object.create(null)
1313

1414
// actions
1515
const APPEND = 0
@@ -166,7 +166,7 @@ function formatSubPath (path: string): boolean | string {
166166
* Parse a string path into an array of segments
167167
*/
168168

169-
function parse (path): ?Array<string> {
169+
function parse (path: Path): ?Array<string> {
170170
const keys: Array<string> = []
171171
let index: number = -1
172172
let mode: number = BEFORE_PATH
@@ -265,7 +265,7 @@ function parse (path): ?Array<string> {
265265
* External parse that check for a cache hit first
266266
*/
267267

268-
function parsePath (path: string): Array<string> {
268+
function parsePath (path: Path): Array<string> {
269269
let hit: ?Array<string> = pathCache[path]
270270
if (!hit) {
271271
hit = parse(path)
@@ -276,8 +276,8 @@ function parsePath (path: string): Array<string> {
276276
return hit || []
277277
}
278278

279-
export type PathValue = | string | number | boolean | null | PathValueObject | PathValueArray
280-
export type PathValueObject = Dictionary<PathValue>
279+
export type PathValue = PathValueObject | PathValueArray | string | number | boolean | null
280+
export type PathValueObject = { [key: string]: PathValue }
281281
export type PathValueArray = Array<PathValue>
282282

283283
function empty (target: any): boolean {
@@ -298,17 +298,17 @@ function empty (target: any): boolean {
298298
/**
299299
* Get path value from path string
300300
*/
301-
export default function getPathValue (obj: Object, path: string): PathValue {
301+
export default function getPathValue (obj: mixed, path: Path): PathValue {
302302
if (!isObject(obj)) { return null }
303303

304304
const paths: Array<string> = parsePath(path)
305305
if (empty(paths)) {
306306
return null
307307
} else {
308-
const length = paths.length
308+
const length: number = paths.length
309309
let ret: any = null
310310
let last: any = obj
311-
let i = 0
311+
let i: number = 0
312312
while (i < length) {
313313
const value: any = last[paths[i]]
314314
if (value === undefined) {

src/util.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function warn (msg: string, err: ?Error): void {
1313
}
1414
}
1515

16-
const hasOwnProperty = Object.prototype.hasOwnProperty
16+
const hasOwnProperty: Function = Object.prototype.hasOwnProperty
1717
export function hasOwn (obj: Object, key: string): boolean {
1818
return hasOwnProperty.call(obj, key)
1919
}
@@ -36,8 +36,8 @@ export function isObject (obj: mixed): boolean {
3636
return obj !== null && typeof obj === 'object'
3737
}
3838

39-
const toString = Object.prototype.toString
40-
const OBJECT_STRING = '[object Object]'
39+
const toString: Function = Object.prototype.toString
40+
const OBJECT_STRING: string = '[object Object]'
4141
export function isPlainObject (obj: any): boolean {
4242
return toString.call(obj) === OBJECT_STRING
4343
}

0 commit comments

Comments
 (0)