Skip to content

Commit

Permalink
⚡ improvement: Optimize path.js and format.js (#456) by @exoego
Browse files Browse the repository at this point in the history
* Optimize: Remove redundant assignment.

* Optimize: Remove redundant Array.isArray and inline emptiness check.

* Optimize: Remove unnecessary capture group.
  • Loading branch information
TATSUNO Yasuhiro authored and kazupon committed Nov 6, 2018
1 parent 5024c90 commit 639453c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type Token = {
value: string
}

const RE_TOKEN_LIST_VALUE: RegExp = /^(\d)+/
const RE_TOKEN_NAMED_VALUE: RegExp = /^(\w)+/
const RE_TOKEN_LIST_VALUE: RegExp = /^(?:\d)+/
const RE_TOKEN_NAMED_VALUE: RegExp = /^(?:\w)+/

export function parse (format: string): Array<Token> {
const tokens: Array<Token> = []
Expand Down
20 changes: 4 additions & 16 deletions src/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pathStateMachine[IN_DOUBLE_QUOTE] = {
* Check if an expression is a literal value.
*/

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

function empty (target: any): boolean {
/* istanbul ignore else */
if (Array.isArray(target)) {
return target.length === 0
} else {
return false
}
}

export default class I18nPath {
_cache: Object

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

const paths: Array<string> = this.parsePath(path)
if (empty(paths)) {
if (paths.length === 0) {
return null
} else {
const length: number = paths.length
let ret: any = null
let last: any = obj
let i: number = 0
while (i < length) {
const value: any = last[paths[i]]
if (value === undefined) {
last = null
break
return null
}
last = value
i++
}

ret = last
return ret
return last
}
}
}

0 comments on commit 639453c

Please sign in to comment.