|
1 | 1 | // Import here Polyfills if needed. Recommended core-js (npm i -D core-js)
|
2 | 2 | // import "core-js/fn/array.find"
|
3 | 3 | // ...
|
4 |
| -export default function isBetween(value: number, min: number, max: number): Boolean { |
5 |
| - const valueNumber = parseFloat(value.toString()) |
6 |
| - const minNumber = parseFloat(min.toString()) |
7 |
| - const maxNumber = parseFloat(max.toString()) |
8 | 4 |
|
9 |
| - const equal = valueNumber === minNumber || minNumber === maxNumber |
| 5 | +export class Between { |
| 6 | + valueNumber: number = 0 |
| 7 | + minNumber: number = 0 |
| 8 | + maxNumber: number = 0 |
10 | 9 |
|
11 |
| - if (isNaN(valueNumber) || isNaN(minNumber) || isNaN(maxNumber)) { |
12 |
| - throw new Error('isBetween() expects numerical input.') |
| 10 | + private toFloat(input: number) { |
| 11 | + return parseFloat(input.toString()) |
13 | 12 | }
|
14 | 13 |
|
15 |
| - if (minNumber > maxNumber) { |
16 |
| - throw new Error('Not a valid range: ' + maxNumber + ' < ' + minNumber + '.') |
| 14 | + value(val: number): Between { |
| 15 | + this.valueNumber = this.toFloat(val) |
| 16 | + return this |
| 17 | + } |
| 18 | + min(val: number): Between { |
| 19 | + this.minNumber = this.toFloat(val) |
| 20 | + return this |
| 21 | + } |
| 22 | + max(val: number): Between { |
| 23 | + this.maxNumber = this.toFloat(val) |
| 24 | + return this |
17 | 25 | }
|
18 | 26 |
|
19 |
| - const inRange = valueNumber > minNumber && valueNumber < maxNumber |
20 |
| - return equal || inRange |
21 |
| -} |
| 27 | + private isEqual() { |
| 28 | + return this.valueNumber === this.minNumber || this.minNumber === this.maxNumber |
| 29 | + } |
| 30 | + |
| 31 | + private isRange(): boolean { |
| 32 | + return this.valueNumber > this.minNumber && this.valueNumber < this.maxNumber |
| 33 | + } |
22 | 34 |
|
23 |
| -function isFalsy(input: any): Boolean { |
24 |
| - return input === null || input === undefined || input.length === 0 |
| 35 | + public calculate(): boolean { |
| 36 | + if (isNaN(this.valueNumber) || isNaN(this.minNumber) || isNaN(this.maxNumber)) { |
| 37 | + throw new Error('isBetween() expects numerical input.') |
| 38 | + } |
| 39 | + |
| 40 | + if (this.minNumber > this.maxNumber) { |
| 41 | + throw new Error('Not a valid range: ' + this.maxNumber + ' < ' + this.minNumber + '.') |
| 42 | + } |
| 43 | + |
| 44 | + return this.isEqual() || this.isRange() |
| 45 | + } |
| 46 | + public calc(): boolean { |
| 47 | + return this.calculate() |
| 48 | + } |
25 | 49 | }
|
26 |
| -function isTruthy(input: any): Boolean { |
27 |
| - return input !== null || input !== undefined || input.length !== 0 |
| 50 | + |
| 51 | +export function between(value: number) { |
| 52 | + const func = new Between() |
| 53 | + return func.value(value) |
28 | 54 | }
|
0 commit comments