Cache values in local variables to avoid repeated lookups.

master
Jordan Harband 2015-02-10 10:36:12 -08:00
parent f0d81e2d33
commit 286c808739
1 changed files with 23 additions and 13 deletions

36
nvm.sh
View File

@ -473,8 +473,19 @@ nvm_ls() {
return return
fi fi
local NVM_IOJS_PREFIX
NVM_IOJS_PREFIX="$(nvm_iojs_prefix)"
local NVM_NODE_PREFIX
NVM_NODE_PREFIX="$(nvm_node_prefix)"
local NVM_VERSION_DIR_IOJS
NVM_VERSION_DIR_IOJS="$(nvm_version_dir iojs)"
local NVM_VERSION_DIR_NEW
NVM_VERSION_DIR_NEW="$(nvm_version_dir new)"
local NVM_VERSION_DIR_OLD
NVM_VERSION_DIR_OLD="$(nvm_version_dir old)"
case "$PATTERN" in case "$PATTERN" in
"$(nvm_iojs_prefix)" | "$(nvm_node_prefix)") "$NVM_IOJS_PREFIX" | "$NVM_NODE_PREFIX" )
PATTERN="$PATTERN-" PATTERN="$PATTERN-"
;; ;;
*) *)
@ -490,8 +501,7 @@ nvm_ls() {
fi fi
else else
case "$PATTERN" in case "$PATTERN" in
"$(nvm_iojs_prefix)-" | "$(nvm_node_prefix)-" | "system") "$NVM_IOJS_PREFIX-" | "$NVM_NODE_PREFIX-" | "system") ;;
;;
*) *)
local NUM_VERSION_GROUPS local NUM_VERSION_GROUPS
NUM_VERSION_GROUPS="$(nvm_num_version_groups "$PATTERN")" NUM_VERSION_GROUPS="$(nvm_num_version_groups "$PATTERN")"
@ -513,19 +523,19 @@ nvm_ls() {
local NVM_ADD_SYSTEM local NVM_ADD_SYSTEM
NVM_ADD_SYSTEM=false NVM_ADD_SYSTEM=false
if nvm_is_iojs_version "$PATTERN"; then if nvm_is_iojs_version "$PATTERN"; then
NVM_DIRS_TO_TEST_AND_SEARCH="$(nvm_version_dir iojs)" NVM_DIRS_TO_TEST_AND_SEARCH="$NVM_VERSION_DIR_IOJS"
PATTERN="$(nvm_strip_iojs_prefix "$PATTERN")" PATTERN="$(nvm_strip_iojs_prefix "$PATTERN")"
if nvm_has_system_iojs; then if nvm_has_system_iojs; then
NVM_ADD_SYSTEM=true NVM_ADD_SYSTEM=true
fi fi
elif [ "_$PATTERN" = "_$(nvm_node_prefix)-" ]; then elif [ "_$PATTERN" = "_$NVM_NODE_PREFIX-" ]; then
NVM_DIRS_TO_TEST_AND_SEARCH="$(nvm_version_dir old) $(nvm_version_dir new)" NVM_DIRS_TO_TEST_AND_SEARCH="$NVM_VERSION_DIR_OLD $NVM_VERSION_DIR_NEW"
PATTERN='' PATTERN=''
if nvm_has_system_node; then if nvm_has_system_node; then
NVM_ADD_SYSTEM=true NVM_ADD_SYSTEM=true
fi fi
else else
NVM_DIRS_TO_TEST_AND_SEARCH="$(nvm_version_dir old) $(nvm_version_dir new) $(nvm_version_dir iojs)" NVM_DIRS_TO_TEST_AND_SEARCH="$NVM_VERSION_DIR_OLD $NVM_VERSION_DIR_NEW $NVM_VERSION_DIR_IOJS"
if nvm_has_system_iojs || nvm_has_system_node; then if nvm_has_system_iojs || nvm_has_system_node; then
NVM_ADD_SYSTEM=true NVM_ADD_SYSTEM=true
fi fi
@ -540,17 +550,17 @@ nvm_ls() {
PATTERN='v' PATTERN='v'
fi fi
VERSIONS="$(command find $NVM_DIRS_TO_SEARCH -maxdepth 1 -type d -name "$PATTERN*" \ VERSIONS="$(command find $NVM_DIRS_TO_SEARCH -maxdepth 1 -type d -name "$PATTERN*" \
| command sed "s#$(nvm_version_dir iojs)/#"$(nvm_iojs_prefix)"-#" \ | command sed "s#$NVM_VERSION_DIR_IOJS/#"$NVM_IOJS_PREFIX"-#" \
| command grep -v "$(nvm_version_dir iojs)" \ | command grep -v "$NVM_VERSION_DIR_IOJS" \
| command sed "s#^$NVM_DIR/##" \ | command sed "s#^$NVM_DIR/##" \
| command grep -v -e '^versions$' \ | command grep -v -e '^versions$' \
| command sed 's#^versions/##' \ | command sed 's#^versions/##' \
| sed -e 's/^v/node-v/' \ | sed -e "s/^v/$NVM_NODE_PREFIX-v/" \
| sed -e 's#^\(iojs\)[-/]v#\1.v#' | sed -e 's#^\(node\)[-/]v#\1.v#' \ | sed -e "s#^\($NVM_IOJS_PREFIX\)[-/]v#\1.v#" | sed -e "s#^\($NVM_NODE_PREFIX\)[-/]v#\1.v#" \
| command sort -t. -u -k 2.2,2n -k 3,3n -k 4,4n \ | command sort -t. -u -k 2.2,2n -k 3,3n -k 4,4n \
| command sort -s -t- -k1.1,1.1 \ | command sort -s -t- -k1.1,1.1 \
| command sed 's/^\(iojs\)\./\1-/' \ | command sed "s/^\($NVM_IOJS_PREFIX\)\./\1-/" \
| command sed 's/^node\.//')" | command sed "s/^$NVM_NODE_PREFIX\.//")"
if [ $ZHS_HAS_SHWORDSPLIT_UNSET -eq 1 ] && nvm_has "unsetopt"; then if [ $ZHS_HAS_SHWORDSPLIT_UNSET -eq 1 ] && nvm_has "unsetopt"; then
unsetopt shwordsplit unsetopt shwordsplit