diff --git a/packages/@lazy-node/semver-ampersand/lib/Range.js b/packages/@lazy-node/semver-ampersand/lib/Range.js index 153335c26..294e23d1c 100644 --- a/packages/@lazy-node/semver-ampersand/lib/Range.js +++ b/packages/@lazy-node/semver-ampersand/lib/Range.js @@ -13,6 +13,7 @@ const toRangeString_1 = require("./range/toRangeString"); const assert_1 = require("./util/assert"); const fixComparatorSet_1 = require("./range/fixComparatorSet"); const parseOptionsOrLoose_1 = require("./internal/parseOptionsOrLoose"); +const split_1 = require("./util/split"); class SemverRange extends (0, class_without_call_parent_constructor_1.default)(range_1.default) { constructor(rawSource, optionsOrLoose) { // skip original semverRange constructor @@ -34,8 +35,7 @@ class SemverRange extends (0, class_without_call_parent_constructor_1.default)(r this.format(); } _buildComparatorsSet(range, options) { - let comparatorsSet = range - .split(/\s*\|\|\s*/) + let comparatorsSet = (0, split_1.splitDoubleVerticalBar)(range) // map the range to a 2d array of comparators .map(range => this.parseRange.call({ // avoid false value to be cache key diff --git a/packages/@lazy-node/semver-ampersand/lib/Range.ts b/packages/@lazy-node/semver-ampersand/lib/Range.ts index fe9ea809d..812aee1c0 100644 --- a/packages/@lazy-node/semver-ampersand/lib/Range.ts +++ b/packages/@lazy-node/semver-ampersand/lib/Range.ts @@ -10,6 +10,7 @@ import { toRangeString } from './range/toRangeString'; import { assertInvalidComparatorSet } from './util/assert'; import { fixComparatorSet } from './range/fixComparatorSet'; import { parseOptionsOrLoose } from './internal/parseOptionsOrLoose'; +import { splitDoubleVerticalBar } from './util/split'; export class SemverRange extends classWithoutCallParentConstructor(semverRange) { @@ -59,8 +60,7 @@ export class SemverRange extends classWithoutCall protected _buildComparatorsSet(range: string, options: IOptions): IComparatorSetInput { - let comparatorsSet = range - .split(/\s*\|\|\s*/) + let comparatorsSet = splitDoubleVerticalBar(range) // map the range to a 2d array of comparators .map(range => this.parseRange.call({ // avoid false value to be cache key diff --git a/packages/@lazy-node/semver-ampersand/lib/validRange.js b/packages/@lazy-node/semver-ampersand/lib/validRange.js index e6bb1b6c4..80fd5654e 100644 --- a/packages/@lazy-node/semver-ampersand/lib/validRange.js +++ b/packages/@lazy-node/semver-ampersand/lib/validRange.js @@ -9,19 +9,18 @@ const assert_1 = require("./util/assert"); * Return the valid range or null if it's not valid */ function validRange(range, optionsOrLoose) { - optionsOrLoose = (0, parseOptionsOrLoose_1.parseOptionsOrLoose)(optionsOrLoose); if (typeof range !== 'string') { throw new TypeError(`range should be string, but got ${range}`); } - if ((0, assert_1.hasInvalidCharacter)(range, optionsOrLoose)) { - return null; - } + optionsOrLoose = (0, parseOptionsOrLoose_1.parseOptionsOrLoose)(optionsOrLoose); try { - return new Range_1.default(range, optionsOrLoose).toRangeString(); + if (!(0, assert_1.hasInvalidCharacter)(range, optionsOrLoose)) { + return new Range_1.default(range, optionsOrLoose).toRangeString(); + } } catch (er) { - return null; } + return null; } exports.validRange = validRange; exports.default = validRange; diff --git a/packages/@lazy-node/semver-ampersand/lib/validRange.ts b/packages/@lazy-node/semver-ampersand/lib/validRange.ts index d206b2df8..96481942c 100644 --- a/packages/@lazy-node/semver-ampersand/lib/validRange.ts +++ b/packages/@lazy-node/semver-ampersand/lib/validRange.ts @@ -9,27 +9,26 @@ import { hasInvalidCharacter } from './util/assert'; */ export function validRange(range: string, optionsOrLoose?: IOptionsOrLoose) { - optionsOrLoose = parseOptionsOrLoose(optionsOrLoose) - if (typeof range !== 'string') { throw new TypeError(`range should be string, but got ${range}`) } - if (hasInvalidCharacter(range, optionsOrLoose)) - { - return null - } + optionsOrLoose = parseOptionsOrLoose(optionsOrLoose) try { - return new SemverRange(range, optionsOrLoose).toRangeString() + if (!hasInvalidCharacter(range, optionsOrLoose)) + { + return new SemverRange(range, optionsOrLoose).toRangeString() + } } catch (er) { - return null + } + return null } export default validRange diff --git a/packages/@lazy-node/semver-ampersand/package.json b/packages/@lazy-node/semver-ampersand/package.json index 48d65b8ac..5b1a46a4e 100644 --- a/packages/@lazy-node/semver-ampersand/package.json +++ b/packages/@lazy-node/semver-ampersand/package.json @@ -29,12 +29,10 @@ "main": "index.js", "scripts": { "lint": "ynpx --quiet eslint -- **/*.ts", - "test": "echo \"Error: no test specified\"", - "test:jest": "ynpx --quiet jest -- --coverage", - "test:mocha": "ynpx --quiet -p ts-node -p mocha mocha -- --require ts-node/register \"!(node_modules)/**/*.{test,spec}.{ts,tsx}\"", + "test": "yarn run test:jest", + "test:jest": "jest", "preversion": "yarn run test", "prepublishOnly": "echo prepublishOnly", - "prepublishOnly:check-bin": "ynpx --quiet @yarn-tool/check-pkg-bin", "prepublishOnly:update": "yarn run ncu && yarn run sort-package-json", "ncu": "yarn-tool ncu -u", "sort-package-json": "yarn-tool sort" @@ -47,8 +45,8 @@ }, "gitHead": "02f74c14e885366e9be773564ea43f7425f9d97a", "dependencies": { - "class-without-call-parent-constructor": "^2.0.1", - "split-smartly2": "^1.1.10", + "class-without-call-parent-constructor": "^2.0.4", + "split-smartly2": "^1.1.11", "ts-type": "^1.2.35", "tslib": "^2.3.0" } diff --git a/packages/@lazy-node/semver-ampersand/test/classes/range.spec.ts b/packages/@lazy-node/semver-ampersand/test/classes/range.spec.ts index b6292f9bc..55f3db745 100644 --- a/packages/@lazy-node/semver-ampersand/test/classes/range.spec.ts +++ b/packages/@lazy-node/semver-ampersand/test/classes/range.spec.ts @@ -1,13 +1,13 @@ import { Comparator } from 'semver'; -import { SemverRange, Range } from '../../lib/Range'; +import { Range } from '../../lib/Range'; import rangeIntersection from '../fixtures/range-intersection' import rangeInclude from '../fixtures/range-include' import rangeExclude from '../fixtures/range-exclude' import rangeParse from '../fixtures/range-parse' -import { reDoubleVerticalBar, separatorDoubleVerticalBar } from '../../lib/const'; import { inspect } from 'util'; +import { _replaceDoubleVerticalBar } from '../util'; //jest.setTimeout(5000 * 10); @@ -123,12 +123,3 @@ test('missing range parameter in range intersect', () => }).toThrowError(new TypeError('a Range is required')) }) -function _replaceDoubleVerticalBar(expected: string) -{ - if (expected != null) - { - return expected.replace(reDoubleVerticalBar, separatorDoubleVerticalBar) - } - - return expected -} diff --git a/packages/@lazy-node/semver-ampersand/test/fixtures/range-parse.ts b/packages/@lazy-node/semver-ampersand/test/fixtures/range-parse.ts index ea46e9016..e6fd10180 100644 --- a/packages/@lazy-node/semver-ampersand/test/fixtures/range-parse.ts +++ b/packages/@lazy-node/semver-ampersand/test/fixtures/range-parse.ts @@ -13,14 +13,10 @@ export default [ ['>=*', '*'], ['', '*'], ['*', '*'], - ['*', '*'], ['>=1.0.0', '>=1.0.0'], ['>1.0.0', '>1.0.0'], ['<=2.0.0', '<=2.0.0'], ['1', '>=1.0.0 <2.0.0-0'], - ['<=2.0.0', '<=2.0.0'], - ['<=2.0.0', '<=2.0.0'], - ['<2.0.0', '<2.0.0'], ['<2.0.0', '<2.0.0'], ['>= 1.0.0', '>=1.0.0'], ['>= 1.0.0', '>=1.0.0'], @@ -33,25 +29,19 @@ export default [ ['< 2.0.0', '<2.0.0'], ['<\t2.0.0', '<2.0.0'], ['>=0.1.97', '>=0.1.97'], - ['>=0.1.97', '>=0.1.97'], ['0.1.20 || 1.2.4', '0.1.20||1.2.4'], ['>=0.2.3 || <0.0.1', '>=0.2.3||<0.0.1'], - ['>=0.2.3 || <0.0.1', '>=0.2.3||<0.0.1'], - ['>=0.2.3 || <0.0.1', '>=0.2.3||<0.0.1'], ['||', '*'], ['2.x.x', '>=2.0.0 <3.0.0-0'], ['1.2.x', '>=1.2.0 <1.3.0-0'], ['1.2.x || 2.x', '>=1.2.0 <1.3.0-0||>=2.0.0 <3.0.0-0'], - ['1.2.x || 2.x', '>=1.2.0 <1.3.0-0||>=2.0.0 <3.0.0-0'], ['x', '*'], ['2.*.*', '>=2.0.0 <3.0.0-0'], ['1.2.*', '>=1.2.0 <1.3.0-0'], ['1.2.* || 2.*', '>=1.2.0 <1.3.0-0||>=2.0.0 <3.0.0-0'], - ['*', '*'], ['2', '>=2.0.0 <3.0.0-0'], ['2.3', '>=2.3.0 <2.4.0-0'], ['~2.4', '>=2.4.0 <2.5.0-0'], - ['~2.4', '>=2.4.0 <2.5.0-0'], ['~>3.2.1', '>=3.2.1 <3.3.0-0'], ['~1', '>=1.0.0 <2.0.0-0'], ['~>1', '>=1.0.0 <2.0.0-0'], @@ -74,7 +64,6 @@ export default [ ['>= 1', '>=1.0.0'], ['<1.2', '<1.2.0-0'], ['< 1.2', '<1.2.0-0'], - ['1', '>=1.0.0 <2.0.0-0'], ['>01.02.03', '>1.2.3', true], ['>01.02.03', null], ['~1.2.3beta', '>=1.2.3-beta <1.3.0-0', { loose: true }], diff --git a/packages/@lazy-node/semver-ampersand/test/options.cache.spec.ts b/packages/@lazy-node/semver-ampersand/test/options.cache.spec.ts new file mode 100644 index 000000000..9a08272d7 --- /dev/null +++ b/packages/@lazy-node/semver-ampersand/test/options.cache.spec.ts @@ -0,0 +1,13 @@ +import { Range } from '../lib/Range'; + +test(`check cache is work`, () => +{ + let range = '>01.02.03'; + + expect(new Range(range, true)).toMatchSnapshot(); + //expect(actual).toBeInstanceOf(Date); + expect(() => new Range(range)).toThrowErrorMatchingSnapshot(); + +}); + + diff --git a/packages/@lazy-node/semver-ampersand/test/ranges/valid.test.ts b/packages/@lazy-node/semver-ampersand/test/ranges/valid.test.ts new file mode 100644 index 000000000..3ac16e268 --- /dev/null +++ b/packages/@lazy-node/semver-ampersand/test/ranges/valid.test.ts @@ -0,0 +1,11 @@ +import rangeParse from '../fixtures/range-parse'; +import validRange from '../../lib/validRange'; +import { inspect } from 'util'; +import { _replaceDoubleVerticalBar } from '../util'; + +describe('valid range test', () => { + // validRange(range) -> result + // translate ranges into their canonical form + rangeParse.forEach(([pre, wanted, options]) => + test(`validRange(${pre}, ${inspect(options)}) === ${wanted = _replaceDoubleVerticalBar(wanted)}`, () => expect(validRange(pre, options)).toBe(wanted))) +}) diff --git a/packages/@lazy-node/semver-ampersand/test/util.ts b/packages/@lazy-node/semver-ampersand/test/util.ts new file mode 100644 index 000000000..dbb4a954d --- /dev/null +++ b/packages/@lazy-node/semver-ampersand/test/util.ts @@ -0,0 +1,11 @@ +import { reDoubleVerticalBar, separatorDoubleVerticalBar } from '../lib/const'; + +export function _replaceDoubleVerticalBar(expected: string) +{ + if (expected != null) + { + return expected.replace(reDoubleVerticalBar, separatorDoubleVerticalBar) + } + + return expected +}