* Internal function to perform boolean operations * @param other Other Series or number to compare with * @param bOps Name of operation to perform [ne, ge, le, gt, lt, eq]
(other: Series | number | Array<number> | boolean[], bOps: string)
| 1444 | * @param bOps Name of operation to perform [ne, ge, le, gt, lt, eq] |
| 1445 | */ |
| 1446 | private boolOps(other: Series | number | Array<number> | boolean[], bOps: string) { |
| 1447 | const data = []; |
| 1448 | const lSeries = this.values; |
| 1449 | let rSeries; |
| 1450 | |
| 1451 | if (typeof other == "number") { |
| 1452 | rSeries = Array(this.values.length).fill(other); //create array of repeated value for broadcasting |
| 1453 | } else if (typeof other == "string" && ["eq", "ne"].includes(bOps)) { |
| 1454 | rSeries = Array(this.values.length).fill(other); |
| 1455 | } else if (other instanceof Series) { |
| 1456 | rSeries = other.values; |
| 1457 | } else if (Array.isArray(other)) { |
| 1458 | rSeries = other; |
| 1459 | } else { |
| 1460 | throw new Error("ParamError: value for other not supported. It must be either a scalar, Array or Series"); |
| 1461 | } |
| 1462 | |
| 1463 | if (!(lSeries.length === rSeries.length)) { |
| 1464 | throw new Error("LengthError: length of other must be equal to length of Series"); |
| 1465 | } |
| 1466 | |
| 1467 | |
| 1468 | for (let i = 0; i < lSeries.length; i++) { |
| 1469 | let lVal = lSeries[i]; |
| 1470 | let rVal = rSeries[i]; |
| 1471 | let bool = null; |
| 1472 | switch (bOps) { |
| 1473 | case "lt": |
| 1474 | bool = lVal < rVal ? true : false; |
| 1475 | data.push(bool); |
| 1476 | break; |
| 1477 | case "gt": |
| 1478 | bool = lVal > rVal ? true : false; |
| 1479 | data.push(bool); |
| 1480 | break; |
| 1481 | case "le": |
| 1482 | bool = lVal <= rVal ? true : false; |
| 1483 | data.push(bool); |
| 1484 | break; |
| 1485 | case "ge": |
| 1486 | bool = lVal >= rVal ? true : false; |
| 1487 | data.push(bool); |
| 1488 | break; |
| 1489 | case "ne": |
| 1490 | bool = lVal !== rVal ? true : false; |
| 1491 | data.push(bool); |
| 1492 | break; |
| 1493 | case "eq": |
| 1494 | bool = lVal === rVal ? true : false; |
| 1495 | data.push(bool); |
| 1496 | break; |
| 1497 | } |
| 1498 | } |
| 1499 | return new Series(data, { |
| 1500 | index: this.index, |
| 1501 | config: { ...this.config } |
| 1502 | }); |
| 1503 | } |