CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
index.js163 linesDownload Raw Back to range-parser
1/*!2 * range-parser3 * Copyright(c) 2012-2014 TJ Holowaychuk4 * Copyright(c) 2015-2016 Douglas Christopher Wilson5 * MIT Licensed6 */7 8'use strict'9 10/**11 * Module exports.12 * @public13 */14 15module.exports = rangeParser16 17/**18 * Parse "Range" header `str` relative to the given file `size`.19 *20 * @param {Number} size21 * @param {String} str22 * @param {Object} [options]23 * @return {Array}24 * @public25 */26 27function rangeParser (size, str, options) {28  if (typeof str !== 'string') {29    throw new TypeError('argument str must be a string')30  }31 32  var index = str.indexOf('=')33 34  if (index === -1) {35    return -236  }37 38  // split the range string39  var arr = str.slice(index + 1).split(',')40  var ranges = []41 42  // add ranges type43  ranges.type = str.slice(0, index)44 45  // parse all ranges46  for (var i = 0; i < arr.length; i++) {47    var range = arr[i].split('-')48    var start = parseInt(range[0], 10)49    var end = parseInt(range[1], 10)50 51    // -nnn52    if (isNaN(start)) {53      start = size - end54      end = size - 155    // nnn-56    } else if (isNaN(end)) {57      end = size - 158    }59 60    // limit last-byte-pos to current length61    if (end > size - 1) {62      end = size - 163    }64 65    // invalid or unsatisifiable66    if (isNaN(start) || isNaN(end) || start > end || start < 0) {67      continue68    }69 70    // add range71    ranges.push({72      start: start,73      end: end74    })75  }76 77  if (ranges.length < 1) {78    // unsatisifiable79    return -180  }81 82  return options && options.combine83    ? combineRanges(ranges)84    : ranges85}86 87/**88 * Combine overlapping & adjacent ranges.89 * @private90 */91 92function combineRanges (ranges) {93  var ordered = ranges.map(mapWithIndex).sort(sortByRangeStart)94 95  for (var j = 0, i = 1; i < ordered.length; i++) {96    var range = ordered[i]97    var current = ordered[j]98 99    if (range.start > current.end + 1) {100      // next range101      ordered[++j] = range102    } else if (range.end > current.end) {103      // extend range104      current.end = range.end105      current.index = Math.min(current.index, range.index)106    }107  }108 109  // trim ordered array110  ordered.length = j + 1111 112  // generate combined range113  var combined = ordered.sort(sortByRangeIndex).map(mapWithoutIndex)114 115  // copy ranges type116  combined.type = ranges.type117 118  return combined119}120 121/**122 * Map function to add index value to ranges.123 * @private124 */125 126function mapWithIndex (range, index) {127  return {128    start: range.start,129    end: range.end,130    index: index131  }132}133 134/**135 * Map function to remove index value from ranges.136 * @private137 */138 139function mapWithoutIndex (range) {140  return {141    start: range.start,142    end: range.end143  }144}145 146/**147 * Sort function to sort ranges by index.148 * @private149 */150 151function sortByRangeIndex (a, b) {152  return a.index - b.index153}154 155/**156 * Sort function to sort ranges by start position.157 * @private158 */159 160function sortByRangeStart (a, b) {161  return a.start - b.start162}163