UNPKG

vis-utils

Version:

Utility functions for data visualization

39 lines (33 loc) 1.25 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = filterInRect; var _rectContains = require('./rectContains'); var _rectContains2 = _interopRequireDefault(_rectContains); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Filters the elements in the passed in array to those that are contained within * the specified rectangle. * * @param {Array} array The input array to filter * @param {Number[][]} rect The rectangle, a pair of two points [[x, y], [x, y]] * @param {Function} x Function that maps a point in the array to its x value * (defaults to d => d[0]) * @param {Function} y Function that maps a point in the array to its y value * (defaults to d => d[1]) * * @return {Array} The subset of the input array that is contained within the * rectangle */ function filterInRect(array, rect) { var x = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function (d) { return d[0]; }; var y = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : function (d) { return d[1]; }; return array.filter(function (d) { return (0, _rectContains2.default)(rect, [x(d), y(d)]); }); }