covutils
Version:
Utilities for creating, transforming, and handling Coverage Data objects.
173 lines (155 loc) • 5.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.normalizeIndexSubsetConstraints = normalizeIndexSubsetConstraints;
exports.subsetDomainByIndex = subsetDomainByIndex;
var _constants = require('../constants.js');
/**
* After normalization, all constraints are start,stop,step objects.
* It holds that stop > start, step > 0, start >= 0, stop >= 1.
* For each axis, a constraint exists.
*/
function normalizeIndexSubsetConstraints(domain, constraints) {
// check and normalize constraints to simplify code
var normalizedConstraints = {};
for (var axisName in constraints) {
if (!domain.axes.has(axisName)) {
// TODO clarify cov behaviour in the JS API spec
continue;
}
if (constraints[axisName] === undefined || constraints[axisName] === null) {
continue;
}
if (typeof constraints[axisName] === 'number') {
var constraint = constraints[axisName];
normalizedConstraints[axisName] = { start: constraint, stop: constraint + 1 };
} else {
normalizedConstraints[axisName] = constraints[axisName];
}
var _normalizedConstraint = normalizedConstraints[axisName];
var _normalizedConstraint2 = _normalizedConstraint.start;
var start = _normalizedConstraint2 === undefined ? 0 : _normalizedConstraint2;
var _normalizedConstraint3 = _normalizedConstraint.stop;
var stop = _normalizedConstraint3 === undefined ? domain.axes.get(axisName).values.length : _normalizedConstraint3;
var _normalizedConstraint4 = _normalizedConstraint.step;
var step = _normalizedConstraint4 === undefined ? 1 : _normalizedConstraint4;
if (step <= 0) {
throw new Error('Invalid constraint for ' + axisName + ': step=' + step + ' must be > 0');
}
if (start >= stop || start < 0) {
throw new Error('Invalid constraint for ' + axisName + ': stop=' + stop + ' must be > start=' + start + ' and both >= 0');
}
normalizedConstraints[axisName] = { start: start, stop: stop, step: step };
}
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = domain.axes.keys()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _axisName = _step.value;
if (!(_axisName in normalizedConstraints)) {
var len = domain.axes.get(_axisName).values.length;
normalizedConstraints[_axisName] = { start: 0, stop: len, step: 1 };
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return normalizedConstraints;
}
function subsetDomainByIndex(domain, constraints) {
constraints = normalizeIndexSubsetConstraints(domain, constraints);
// subset the axis arrays of the domain (immediately + cached)
var newdomain = {
type: _constants.DOMAIN,
domainType: domain.domainType,
axes: new Map(domain.axes),
referencing: domain.referencing
};
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
var _loop = function _loop() {
var axisName = _step2.value;
var axis = domain.axes.get(axisName);
var coords = axis.values;
var bounds = axis.bounds;
var constraint = constraints[axisName];
var newcoords = void 0;
var newbounds = void 0;
var start = constraint.start;
var stop = constraint.stop;
var step = constraint.step;
if (start === 0 && stop === coords.length && step === 1) {
newcoords = coords;
newbounds = bounds;
} else if (step === 1) {
// TypedArray has subarray which creates a view, while Array has slice which makes a copy
if (coords.subarray) {
newcoords = coords.subarray(start, stop);
} else {
newcoords = coords.slice(start, stop);
}
if (bounds) {
newbounds = {
get: function get(i) {
return bounds.get(start + i);
}
};
}
} else {
var q = Math.trunc((stop - start) / step);
var r = (stop - start) % step;
var len = q + r;
newcoords = new coords.constructor(len); // array or typed array
for (var i = start, j = 0; i < stop; i += step, j++) {
newcoords[j] = coords[i];
}
if (bounds) {
newbounds = {
get: function get(i) {
return bounds.get(start + i * step);
}
};
}
}
var newaxis = {
dataType: axis.dataType,
coordinates: axis.coordinates,
values: newcoords,
bounds: newbounds
};
newdomain.axes.set(axisName, newaxis);
};
for (var _iterator2 = Object.keys(constraints)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
_loop();
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
return newdomain;
}