sounisi5011/semver-range-intersect

View on GitHub
src/index.ts

Summary

Maintainability
A
25 mins
Test Coverage
import semver from 'semver';

import { MultiRange } from './multi-range';
import { isIntersectRanges } from './utils';

export function intersect(...ranges: string[]): string | null {
    const semverRangeList = (() => {
        try {
            return ranges.map(rangeStr => new semver.Range(rangeStr));
        } catch (err) {
            return null;
        }
    })();

    if (!semverRangeList || !isIntersectRanges(semverRangeList)) {
        return null;
    }

    const intersectRange = semverRangeList
        .map(range => new MultiRange(range.set))
        .reduce(
            (multiRangeA, multiRangeB) => multiRangeA.intersect(multiRangeB),
            new MultiRange(null),
        );

    return intersectRange.valid ? String(intersectRange) || '*' : null;
}