diff options
| author | Victor S <victorplentz@gmail.com> | 2023-10-08 09:35:35 -0300 |
|---|---|---|
| committer | Victor S <victorplentz@gmail.com> | 2023-10-08 09:35:35 -0300 |
| commit | 0ecef37608eaea3e795b55b38b2403e7e8b5fdfb (patch) | |
| tree | a2eaa97abb100acf94912d25cb97037feca6e7ad /autoload/prettier/utils/version.vim | |
| parent | 6dd9a0813b78158254987e5452455000aabd6b02 (diff) | |
| download | vim-prettier-0ecef37608eaea3e795b55b38b2403e7e8b5fdfb.tar.xz | |
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.
Diffstat (limited to 'autoload/prettier/utils/version.vim')
| -rw-r--r-- | autoload/prettier/utils/version.vim | 25 |
1 files changed, 25 insertions, 0 deletions
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 |
