src/utils/helpers/version.js   A
last analyzed

Complexity

Total Complexity 9
Complexity/F 1.8

Size

Lines of Code 27
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
eloc 17
mnd 4
bc 4
fnc 5
dl 0
loc 27
ccs 13
cts 13
cp 1
rs 10
bpm 0.8
cpm 1.8
noi 0
c 0
b 0
f 0
1
import { compare } from 'compare-versions';
2
import { identity, memoizeWith } from 'ramda';
3
import { hasValue } from '../utils';
4
5 5
export const versionMatch = (versionToMatch, { maxVersion, minVersion }) => {
6 41
  if (!hasValue(versionToMatch)) {
7 24
    return false;
8
  }
9
10 17
  const matchesMinVersion = !minVersion || compare(versionToMatch, minVersion, '>=');
11 17
  const matchesMaxVersion = !maxVersion || compare(versionToMatch, maxVersion, '<=');
12
13 17
  return !!(matchesMaxVersion && matchesMinVersion);
14
};
15
16 5
const versionIsValidSemVer = memoizeWith(identity, (version) => {
17 9
  try {
18 9
    return compare(version, version, '=');
19
  } catch (e) {
20 5
    return false;
21
  }
22
});
23
24 9
export const versionToPrintable = (version) => !versionIsValidSemVer(version) ? version : `v${version}`;
25
26 5
export const versionToSemVer = (defaultValue = 'latest') =>
27
  (version) => versionIsValidSemVer(version) ? version : defaultValue;
28