From 0ecef37608eaea3e795b55b38b2403e7e8b5fdfb Mon Sep 17 00:00:00 2001 From: Victor S Date: Sun, 8 Oct 2023 09:35:35 -0300 Subject: refactor: add version comparison functions Added 2 functions that compare versions strings according to semantic version. This is part of a plan to compose the flags of the CLI command from an object with flag details. --- autoload/prettier/utils/version.vim | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 autoload/prettier/utils/version.vim (limited to 'autoload/prettier/utils/version.vim') diff --git a/autoload/prettier/utils/version.vim b/autoload/prettier/utils/version.vim new file mode 100644 index 0000000..3cbbb73 --- /dev/null +++ b/autoload/prettier/utils/version.vim @@ -0,0 +1,25 @@ +" Returns 1 if the first argument is the greater version, returns 0 if the +" arguments are the same version, or returns -1 if the first argument is the +" lesser version. +function! prettier#utils#version#Compare_versions(a, b) abort + let [l:a_major, l:a_minor, l:a_patch; l:rest] = a:a->split('\D') + \ ->map('str2nr(v:val)') + let [l:b_major, l:b_minor, l:b_patch; l:rest] = a:b->split('\D') + \ ->map('str2nr(v:val)') + let l:is_a_greater = + \ l:a_major > l:b_major + \ || (l:a_major == l:b_major && l:a_minor > l:b_minor) + \ || (l:a_major == l:b_major && l:a_minor == l:b_minor + \ && l:a_patch > l:b_patch) + let l:is_a_equal_b = + \ l:a_major == l:b_major + \ && l:a_minor == l:b_minor + \ && l:a_patch == l:b_patch + return l:is_a_greater ? 1 : (l:is_a_equal_b ? 0 : -1) +endfunction + +" Returns 1 if the first argument is greater than or equal to the second, +" otherwise returns 0. +function! prettier#utils#version#Is_greater_or_equal_version(a, b) abort + return prettier#utils#version#Compare_versions(a:a, a:b) != -1 +endfunction -- cgit v1.3