Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize path.js and format.js #456

Merged
merged 3 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}