|
| 1 | +/** |
| 2 | + * Between method for Series |
| 3 | + * Returns a new Series with values between lower and upper bounds (inclusive) |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * Creates a between method for Series |
| 8 | + * @returns {Function} - Function to be attached to Series prototype |
| 9 | + */ |
| 10 | +export function between() { |
| 11 | + /** |
| 12 | + * Returns a new Series with values between lower and upper bounds (inclusive) |
| 13 | + * @param {number} lower - Lower bound |
| 14 | + * @param {number} upper - Upper bound |
| 15 | + * @param {Object} [options] - Options object |
| 16 | + * @param {boolean} [options.inclusive=true] - Whether bounds are inclusive |
| 17 | + * @returns {Series} - New Series with filtered values |
| 18 | + */ |
| 19 | + return function(lower, upper, options = {}) { |
| 20 | + const { inclusive = true } = options; |
| 21 | + |
| 22 | + if (lower === undefined || upper === undefined) { |
| 23 | + throw new Error('Both lower and upper bounds must be provided'); |
| 24 | + } |
| 25 | + |
| 26 | + if (lower > upper) { |
| 27 | + throw new Error('Lower bound must be less than or equal to upper bound'); |
| 28 | + } |
| 29 | + |
| 30 | + if (inclusive) { |
| 31 | + return this.filter((x) => { |
| 32 | + const numX = Number(x); |
| 33 | + return !isNaN(numX) && numX >= lower && numX <= upper; |
| 34 | + }); |
| 35 | + } else { |
| 36 | + return this.filter((x) => { |
| 37 | + const numX = Number(x); |
| 38 | + return !isNaN(numX) && numX > lower && numX < upper; |
| 39 | + }); |
| 40 | + } |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Registers the between method on Series prototype |
| 46 | + * @param {Class} Series - Series class to extend |
| 47 | + */ |
| 48 | +export function register(Series) { |
| 49 | + if (!Series.prototype.between) { |
| 50 | + Series.prototype.between = between(); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +export default between; |
0 commit comments