From 4b2bc0be0834a9a86b00541c7c0e6669e6c21832 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 16 Sep 2014 08:36:05 -0700 Subject: [PATCH] Adding `nvm_num_version_groups` --- nvm.sh | 14 ++++++++++++++ test/fast/Unit tests/nvm_num_version_groups | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100755 test/fast/Unit tests/nvm_num_version_groups diff --git a/nvm.sh b/nvm.sh index f6e9511..8bca98c 100644 --- a/nvm.sh +++ b/nvm.sh @@ -181,6 +181,20 @@ nvm_format_version() { echo "$1" | sed -e 's/^\([0-9]\)/v\1/g' } +nvm_num_version_groups() { + local VERSION + VERSION="$1" + if [ -z "$VERSION" ]; then + echo "0" + return + fi + local NVM_NUM_DOTS + NVM_NUM_DOTS=$(echo "$VERSION" | sed -e 's/^v//' | sed -e 's/\.$//' | sed -e 's/[^\.]//g') + local NVM_NUM_GROUPS + NVM_NUM_GROUPS=".$NVM_NUM_DOTS" + echo "${#NVM_NUM_GROUPS}" +} + nvm_strip_path() { echo "$1" | sed -e "s#$NVM_DIR/[^/]*$2[^:]*:##g" -e "s#:$NVM_DIR/[^/]*$2[^:]*##g" -e "s#$NVM_DIR/[^/]*$2[^:]*##g" } diff --git a/test/fast/Unit tests/nvm_num_version_groups b/test/fast/Unit tests/nvm_num_version_groups new file mode 100755 index 0000000..893da75 --- /dev/null +++ b/test/fast/Unit tests/nvm_num_version_groups @@ -0,0 +1,21 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +. ../../../nvm.sh + +[ "~$(nvm_num_version_groups)" = "~0" ] || die "no args should give 0" +[ "~$(nvm_num_version_groups a)" = "~1" ] || die "one letter should give 1" + +[ "~$(nvm_num_version_groups 1)" = "~1" ] || die "1 should give 1" +[ "~$(nvm_num_version_groups v1)" = "~1" ] || die "v1 should give 1" +[ "~$(nvm_num_version_groups v1.)" = "~1" ] || die "v1. should give 1" + +[ "~$(nvm_num_version_groups 1.2)" = "~2" ] || die "1.2 should give 2" +[ "~$(nvm_num_version_groups v1.2)" = "~2" ] || die "v1.2 should give 2" +[ "~$(nvm_num_version_groups v1.2.)" = "~2" ] || die "v1.2. should give 2" + +[ "~$(nvm_num_version_groups 1.2.3)" = "~3" ] || die "1.2.3 should give 3" +[ "~$(nvm_num_version_groups v1.2.3)" = "~3" ] || die "v1.2.3 should give 3" +[ "~$(nvm_num_version_groups v1.2.3.)" = "~3" ] || die "v1.2.3. should give 3" +