[Refactor] Use `awk` to improve version comparison performance
parent
4842641e78
commit
8a138d98ed
39
nvm.sh
39
nvm.sh
|
@ -166,19 +166,32 @@ nvm_rc_version() {
|
||||||
}
|
}
|
||||||
|
|
||||||
nvm_version_greater() {
|
nvm_version_greater() {
|
||||||
local LHS
|
command awk 'BEGIN {
|
||||||
LHS="$(nvm_normalize_version "$1")"
|
if (ARGV[1] == "" || ARGV[2] == "") exit(1)
|
||||||
local RHS
|
split(ARGV[1], a, /\./);
|
||||||
RHS="$(nvm_normalize_version "$2")"
|
split(ARGV[2], b, /\./);
|
||||||
[ "$LHS" -gt "$RHS" ];
|
for (i=1; i<=3; i++) {
|
||||||
|
if (a[i] && a[i] !~ /^[0-9]+$/) exit(2);
|
||||||
|
if (b[i] && b[i] !~ /^[0-9]+$/) { exit(0); }
|
||||||
|
if (a[i] < b[i]) exit(3);
|
||||||
|
else if (a[i] > b[i]) exit(0);
|
||||||
|
}
|
||||||
|
exit(4)
|
||||||
|
}' "${1#v}" "${2#v}";
|
||||||
}
|
}
|
||||||
|
|
||||||
nvm_version_greater_than_or_equal_to() {
|
nvm_version_greater_than_or_equal_to() {
|
||||||
local LHS
|
command awk 'BEGIN {
|
||||||
LHS="$(nvm_normalize_version "$1")"
|
if (ARGV[1] == "" || ARGV[2] == "") exit(1)
|
||||||
local RHS
|
split(ARGV[1], a, /\./);
|
||||||
RHS="$(nvm_normalize_version "$2")"
|
split(ARGV[2], b, /\./);
|
||||||
[ "$LHS" -ge "$RHS" ];
|
for (i=1; i<=3; i++) {
|
||||||
|
if (a[i] && a[i] !~ /^[0-9]+$/) exit(2);
|
||||||
|
if (a[i] < b[i]) exit(3);
|
||||||
|
else if (a[i] > b[i]) exit(0);
|
||||||
|
}
|
||||||
|
exit(0)
|
||||||
|
}' "${1#v}" "${2#v}";
|
||||||
}
|
}
|
||||||
|
|
||||||
nvm_version_dir() {
|
nvm_version_dir() {
|
||||||
|
@ -331,7 +344,7 @@ nvm_is_valid_version() {
|
||||||
*)
|
*)
|
||||||
local VERSION
|
local VERSION
|
||||||
VERSION="$(nvm_strip_iojs_prefix "$1")"
|
VERSION="$(nvm_strip_iojs_prefix "$1")"
|
||||||
nvm_version_greater "$VERSION"
|
nvm_version_greater_than_or_equal_to "$VERSION" 0
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
@ -398,9 +411,7 @@ nvm_prepend_path() {
|
||||||
|
|
||||||
nvm_binary_available() {
|
nvm_binary_available() {
|
||||||
# binaries started with node 0.8.6
|
# binaries started with node 0.8.6
|
||||||
local FIRST_VERSION_WITH_BINARY
|
nvm_version_greater_than_or_equal_to "$(nvm_strip_iojs_prefix "${1-}")" v0.8.6
|
||||||
FIRST_VERSION_WITH_BINARY="0.8.6"
|
|
||||||
nvm_version_greater_than_or_equal_to "$(nvm_strip_iojs_prefix "$1")" "$FIRST_VERSION_WITH_BINARY"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nvm_print_formatted_alias() {
|
nvm_print_formatted_alias() {
|
||||||
|
|
|
@ -4,7 +4,10 @@ die () { echo $@ ; exit 1; }
|
||||||
|
|
||||||
. ../../../nvm.sh
|
. ../../../nvm.sh
|
||||||
|
|
||||||
[ "$(nvm_version_path foo)" = "$NVM_DIR/foo" ] || die '"nvm_version_path foo" did not return correct location'
|
OUTPUT="$(nvm_version_path foo)"
|
||||||
|
EXPECTED_OUTPUT="$NVM_DIR/foo"
|
||||||
|
[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "'nvm_version_path foo' did not return correct location; expected '$EXPECTED_OUTPUT', got '$OUTPUT'"
|
||||||
|
|
||||||
[ "$(nvm_version_path 2>&1)" = "version is required" ] || die '"nvm_version_path" did not error out'
|
[ "$(nvm_version_path 2>&1)" = "version is required" ] || die '"nvm_version_path" did not error out'
|
||||||
[ "$(nvm_version_path v0.11.0)" = "$NVM_DIR/v0.11.0" ] || die 'old version has the wrong path'
|
[ "$(nvm_version_path v0.11.0)" = "$NVM_DIR/v0.11.0" ] || die 'old version has the wrong path'
|
||||||
[ "$(nvm_version_path v0.12.0)" = "$NVM_DIR/versions/node/v0.12.0" ] || die 'new version has the wrong path'
|
[ "$(nvm_version_path v0.12.0)" = "$NVM_DIR/versions/node/v0.12.0" ] || die 'new version has the wrong path'
|
||||||
|
|
Loading…
Reference in New Issue