hyperformula-dc
Version:
HyperFormula is a JavaScript engine for efficient processing of spreadsheet-like data and formulas
120 lines (99 loc) • 3.29 kB
JavaScript
import "core-js/modules/es.symbol.js";
import "core-js/modules/es.symbol.description.js";
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/es.symbol.iterator.js";
import "core-js/modules/es.array.iterator.js";
import "core-js/modules/es.string.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
/**
* @license
* Copyright (c) 2021 Handsoncode. All rights reserved.
*/
import { CellError, simpleCellAddress } from '../Cell';
import { EmptyValue, getRawValue } from './InterpreterValue';
/*
* If key exists returns first index of key element in range of sorted values
* Otherwise returns first index of greatest element smaller than key
* assuming sorted values in range
* */
export function rangeLowerBound(range, key, dependencyGraph, coordinate) {
//IMPORTANT: this function does not normalize input strings
var end;
if (coordinate === 'col') {
end = range.effectiveEndColumn(dependencyGraph);
} else {
end = range.effectiveEndRow(dependencyGraph);
}
var start = range.start[coordinate];
var centerValueFn;
if (coordinate === 'row') {
centerValueFn = function centerValueFn(center) {
return getRawValue(dependencyGraph.getCellValue(simpleCellAddress(range.sheet, range.start.col, center)));
};
} else {
centerValueFn = function centerValueFn(center) {
return getRawValue(dependencyGraph.getCellValue(simpleCellAddress(range.sheet, center, range.start.row)));
};
}
var pos = lowerBound(centerValueFn, key, start, end);
if (_typeof(centerValueFn(pos)) !== _typeof(key)) {
return -1;
} else {
return pos - start;
}
}
/*
* If key exists returns first index of key element
* Otherwise returns first index of greatest element smaller than key
* assuming sorted values
* */
export function lowerBound(value, key, start, end) {
while (start <= end) {
var center = Math.floor((start + end) / 2);
var cmp = compare(key, value(center));
if (cmp > 0) {
start = center + 1;
} else if (cmp < 0) {
end = center - 1;
} else if (start != center) {
end = center;
} else {
return center;
}
}
return end;
}
/*
* numbers < strings < false < true
* */
export function compare(left, right) {
if (_typeof(left) === _typeof(right)) {
if (left === EmptyValue) {
return 0;
}
return left < right ? -1 : left > right ? 1 : 0;
}
if (left === EmptyValue) {
return -1;
}
if (right === EmptyValue) {
return 1;
}
if (right instanceof CellError) {
return -1;
}
if (typeof left === 'number' && typeof right === 'string') {
return -1;
}
if (typeof left === 'number' && typeof right === 'boolean') {
return -1;
}
if (typeof left === 'string' && typeof right === 'number') {
return 1;
}
if (typeof left === 'string' && typeof right === 'boolean') {
return -1;
}
return 1;
}