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

Commit 639453c

Browse files
TATSUNO Yasuhirokazupon
authored andcommitted
⚡ improvement: Optimize path.js and format.js (#456) by @exoego
* Optimize: Remove redundant assignment. * Optimize: Remove redundant Array.isArray and inline emptiness check. * Optimize: Remove unnecessary capture group.
1 parent 5024c90 commit 639453c

File tree

2 files changed

+6
-18
lines changed

2 files changed

+6
-18
lines changed

src/format.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ type Token = {
2727
value: string
2828
}
2929

30-
const RE_TOKEN_LIST_VALUE: RegExp = /^(\d)+/
31-
const RE_TOKEN_NAMED_VALUE: RegExp = /^(\w)+/
30+
const RE_TOKEN_LIST_VALUE: RegExp = /^(?:\d)+/
31+
const RE_TOKEN_NAMED_VALUE: RegExp = /^(?:\w)+/
3232

3333
export function parse (format: string): Array<Token> {
3434
const tokens: Array<Token> = []

src/path.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pathStateMachine[IN_DOUBLE_QUOTE] = {
8383
* Check if an expression is a literal value.
8484
*/
8585

86-
const literalValueRE: RegExp = /^\s?(true|false|-?[\d.]+|'[^']*'|"[^"]*")\s?$/
86+
const literalValueRE: RegExp = /^\s?(?:true|false|-?[\d.]+|'[^']*'|"[^"]*")\s?$/
8787
function isLiteral (exp: string): boolean {
8888
return literalValueRE.test(exp)
8989
}
@@ -253,15 +253,6 @@ export type PathValue = PathValueObject | PathValueArray | string | number | boo
253253
export type PathValueObject = { [key: string]: PathValue }
254254
export type PathValueArray = Array<PathValue>
255255

256-
function empty (target: any): boolean {
257-
/* istanbul ignore else */
258-
if (Array.isArray(target)) {
259-
return target.length === 0
260-
} else {
261-
return false
262-
}
263-
}
264-
265256
export default class I18nPath {
266257
_cache: Object
267258

@@ -290,25 +281,22 @@ export default class I18nPath {
290281
if (!isObject(obj)) { return null }
291282

292283
const paths: Array<string> = this.parsePath(path)
293-
if (empty(paths)) {
284+
if (paths.length === 0) {
294285
return null
295286
} else {
296287
const length: number = paths.length
297-
let ret: any = null
298288
let last: any = obj
299289
let i: number = 0
300290
while (i < length) {
301291
const value: any = last[paths[i]]
302292
if (value === undefined) {
303-
last = null
304-
break
293+
return null
305294
}
306295
last = value
307296
i++
308297
}
309298

310-
ret = last
311-
return ret
299+
return last
312300
}
313301
}
314302
}

0 commit comments

Comments
 (0)