From 639453c2b34966e8807a376b890f8babe0ff90d1 Mon Sep 17 00:00:00 2001 From: TATSUNO Yasuhiro Date: Tue, 6 Nov 2018 15:18:29 +0900 Subject: [PATCH] :zap: 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. --- src/format.js | 4 ++-- src/path.js | 20 ++++---------------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/src/format.js b/src/format.js index 64335360c..5b26d11f0 100644 --- a/src/format.js +++ b/src/format.js @@ -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 { const tokens: Array = [] diff --git a/src/path.js b/src/path.js index 9337ae432..5976e82fc 100644 --- a/src/path.js +++ b/src/path.js @@ -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) } @@ -253,15 +253,6 @@ export type PathValue = PathValueObject | PathValueArray | string | number | boo export type PathValueObject = { [key: string]: PathValue } export type PathValueArray = Array -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 @@ -290,25 +281,22 @@ export default class I18nPath { if (!isObject(obj)) { return null } const paths: Array = 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 } } }