update
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
'use strict'
|
||||
|
||||
const ANY = Symbol('SemVer ANY')
|
||||
// hoisted class for cyclic dependency
|
||||
class Comparator {
|
||||
static get ANY () {
|
||||
return ANY
|
||||
}
|
||||
|
||||
constructor (comp, options) {
|
||||
options = parseOptions(options)
|
||||
|
||||
if (comp instanceof Comparator) {
|
||||
if (comp.loose === !!options.loose) {
|
||||
return comp
|
||||
} else {
|
||||
comp = comp.value
|
||||
}
|
||||
}
|
||||
|
||||
comp = comp.trim().split(/\s+/).join(' ')
|
||||
debug('comparator', comp, options)
|
||||
this.options = options
|
||||
this.loose = !!options.loose
|
||||
this.parse(comp)
|
||||
|
||||
if (this.semver === ANY) {
|
||||
this.value = ''
|
||||
} else {
|
||||
this.value = this.operator + this.semver.version
|
||||
}
|
||||
|
||||
debug('comp', this)
|
||||
}
|
||||
|
||||
parse (comp) {
|
||||
const r = this.options.loose ? re[t.COMPARATORLOOSE] : re[t.COMPARATOR]
|
||||
const m = comp.match(r)
|
||||
|
||||
if (!m) {
|
||||
throw new TypeError(`Invalid comparator: ${comp}`)
|
||||
}
|
||||
|
||||
this.operator = m[1] !== undefined ? m[1] : ''
|
||||
if (this.operator === '=') {
|
||||
this.operator = ''
|
||||
}
|
||||
|
||||
// if it literally is just '>' or '' then allow anything.
|
||||
if (!m[2]) {
|
||||
this.semver = ANY
|
||||
} else {
|
||||
this.semver = new SemVer(m[2], this.options.loose)
|
||||
}
|
||||
}
|
||||
|
||||
toString () {
|
||||
return this.value
|
||||
}
|
||||
|
||||
test (version) {
|
||||
debug('Comparator.test', version, this.options.loose)
|
||||
|
||||
if (this.semver === ANY || version === ANY) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (typeof version === 'string') {
|
||||
try {
|
||||
version = new SemVer(version, this.options)
|
||||
} catch (er) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return cmp(version, this.operator, this.semver, this.options)
|
||||
}
|
||||
|
||||
intersects (comp, options) {
|
||||
if (!(comp instanceof Comparator)) {
|
||||
throw new TypeError('a Comparator is required')
|
||||
}
|
||||
|
||||
if (this.operator === '') {
|
||||
if (this.value === '') {
|
||||
return true
|
||||
}
|
||||
return new Range(comp.value, options).test(this.value)
|
||||
} else if (comp.operator === '') {
|
||||
if (comp.value === '') {
|
||||
return true
|
||||
}
|
||||
return new Range(this.value, options).test(comp.semver)
|
||||
}
|
||||
|
||||
options = parseOptions(options)
|
||||
|
||||
// Special cases where nothing can possibly be lower
|
||||
if (options.includePrerelease &&
|
||||
(this.value === '<0.0.0-0' || comp.value === '<0.0.0-0')) {
|
||||
return false
|
||||
}
|
||||
if (!options.includePrerelease &&
|
||||
(this.value.startsWith('<0.0.0') || comp.value.startsWith('<0.0.0'))) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Same direction increasing (> or >=)
|
||||
if (this.operator.startsWith('>') && comp.operator.startsWith('>')) {
|
||||
return true
|
||||
}
|
||||
// Same direction decreasing (< or <=)
|
||||
if (this.operator.startsWith('<') && comp.operator.startsWith('<')) {
|
||||
return true
|
||||
}
|
||||
// same SemVer and both sides are inclusive (<= or >=)
|
||||
if (
|
||||
(this.semver.version === comp.semver.version) &&
|
||||
this.operator.includes('=') && comp.operator.includes('=')) {
|
||||
return true
|
||||
}
|
||||
// opposite directions less than
|
||||
if (cmp(this.semver, '<', comp.semver, options) &&
|
||||
this.operator.startsWith('>') && comp.operator.startsWith('<')) {
|
||||
return true
|
||||
}
|
||||
// opposite directions greater than
|
||||
if (cmp(this.semver, '>', comp.semver, options) &&
|
||||
this.operator.startsWith('<') && comp.operator.startsWith('>')) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Comparator
|
||||
|
||||
const parseOptions = require('../internal/parse-options')
|
||||
const { safeRe: re, t } = require('../internal/re')
|
||||
const cmp = require('../functions/cmp')
|
||||
const debug = require('../internal/debug')
|
||||
const SemVer = require('./semver')
|
||||
const Range = require('./range')
|
||||
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"name": "range-parser",
|
||||
"author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)",
|
||||
"description": "Range header field string parser",
|
||||
"version": "1.2.1",
|
||||
"contributors": [
|
||||
"Douglas Christopher Wilson <doug@somethingdoug.com>",
|
||||
"James Wyatt Cready <wyatt.cready@lanetix.com>",
|
||||
"Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)"
|
||||
],
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"range",
|
||||
"parser",
|
||||
"http"
|
||||
],
|
||||
"repository": "jshttp/range-parser",
|
||||
"devDependencies": {
|
||||
"deep-equal": "1.0.1",
|
||||
"eslint": "5.16.0",
|
||||
"eslint-config-standard": "12.0.0",
|
||||
"eslint-plugin-markdown": "1.0.0",
|
||||
"eslint-plugin-import": "2.17.2",
|
||||
"eslint-plugin-node": "8.0.1",
|
||||
"eslint-plugin-promise": "4.1.1",
|
||||
"eslint-plugin-standard": "4.0.0",
|
||||
"mocha": "6.1.4",
|
||||
"nyc": "14.1.1"
|
||||
},
|
||||
"files": [
|
||||
"HISTORY.md",
|
||||
"LICENSE",
|
||||
"index.js"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">= 0.6"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "eslint --plugin markdown --ext js,md .",
|
||||
"test": "mocha --reporter spec",
|
||||
"test-cov": "nyc --reporter=html --reporter=text npm test",
|
||||
"test-travis": "nyc --reporter=text npm test"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
/*!
|
||||
* range-parser
|
||||
* Copyright(c) 2012-2014 TJ Holowaychuk
|
||||
* Copyright(c) 2015-2016 Douglas Christopher Wilson
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
/**
|
||||
* Module exports.
|
||||
* @public
|
||||
*/
|
||||
|
||||
module.exports = rangeParser
|
||||
|
||||
/**
|
||||
* Parse "Range" header `str` relative to the given file `size`.
|
||||
*
|
||||
* @param {Number} size
|
||||
* @param {String} str
|
||||
* @param {Object} [options]
|
||||
* @return {Array}
|
||||
* @public
|
||||
*/
|
||||
|
||||
function rangeParser (size, str, options) {
|
||||
if (typeof str !== 'string') {
|
||||
throw new TypeError('argument str must be a string')
|
||||
}
|
||||
|
||||
var index = str.indexOf('=')
|
||||
|
||||
if (index === -1) {
|
||||
return -2
|
||||
}
|
||||
|
||||
// split the range string
|
||||
var arr = str.slice(index + 1).split(',')
|
||||
var ranges = []
|
||||
|
||||
// add ranges type
|
||||
ranges.type = str.slice(0, index)
|
||||
|
||||
// parse all ranges
|
||||
for (var i = 0; i < arr.length; i++) {
|
||||
var range = arr[i].split('-')
|
||||
var start = parseInt(range[0], 10)
|
||||
var end = parseInt(range[1], 10)
|
||||
|
||||
// -nnn
|
||||
if (isNaN(start)) {
|
||||
start = size - end
|
||||
end = size - 1
|
||||
// nnn-
|
||||
} else if (isNaN(end)) {
|
||||
end = size - 1
|
||||
}
|
||||
|
||||
// limit last-byte-pos to current length
|
||||
if (end > size - 1) {
|
||||
end = size - 1
|
||||
}
|
||||
|
||||
// invalid or unsatisifiable
|
||||
if (isNaN(start) || isNaN(end) || start > end || start < 0) {
|
||||
continue
|
||||
}
|
||||
|
||||
// add range
|
||||
ranges.push({
|
||||
start: start,
|
||||
end: end
|
||||
})
|
||||
}
|
||||
|
||||
if (ranges.length < 1) {
|
||||
// unsatisifiable
|
||||
return -1
|
||||
}
|
||||
|
||||
return options && options.combine
|
||||
? combineRanges(ranges)
|
||||
: ranges
|
||||
}
|
||||
|
||||
/**
|
||||
* Combine overlapping & adjacent ranges.
|
||||
* @private
|
||||
*/
|
||||
|
||||
function combineRanges (ranges) {
|
||||
var ordered = ranges.map(mapWithIndex).sort(sortByRangeStart)
|
||||
|
||||
for (var j = 0, i = 1; i < ordered.length; i++) {
|
||||
var range = ordered[i]
|
||||
var current = ordered[j]
|
||||
|
||||
if (range.start > current.end + 1) {
|
||||
// next range
|
||||
ordered[++j] = range
|
||||
} else if (range.end > current.end) {
|
||||
// extend range
|
||||
current.end = range.end
|
||||
current.index = Math.min(current.index, range.index)
|
||||
}
|
||||
}
|
||||
|
||||
// trim ordered array
|
||||
ordered.length = j + 1
|
||||
|
||||
// generate combined range
|
||||
var combined = ordered.sort(sortByRangeIndex).map(mapWithoutIndex)
|
||||
|
||||
// copy ranges type
|
||||
combined.type = ranges.type
|
||||
|
||||
return combined
|
||||
}
|
||||
|
||||
/**
|
||||
* Map function to add index value to ranges.
|
||||
* @private
|
||||
*/
|
||||
|
||||
function mapWithIndex (range, index) {
|
||||
return {
|
||||
start: range.start,
|
||||
end: range.end,
|
||||
index: index
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Map function to remove index value from ranges.
|
||||
* @private
|
||||
*/
|
||||
|
||||
function mapWithoutIndex (range) {
|
||||
return {
|
||||
start: range.start,
|
||||
end: range.end
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort function to sort ranges by index.
|
||||
* @private
|
||||
*/
|
||||
|
||||
function sortByRangeIndex (a, b) {
|
||||
return a.index - b.index
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort function to sort ranges by start position.
|
||||
* @private
|
||||
*/
|
||||
|
||||
function sortByRangeStart (a, b) {
|
||||
return a.start - b.start
|
||||
}
|
||||
Reference in New Issue
Block a user