I have two functions, one that returns two values min and a max and another function that serves as a guard. However I would like to

  1. remove the logic of the if/else inside the invalidPriceRangeException function.
  2. be able to know that priceRange returns an a min and a max without having to know that the first number is the min.

Maybe I'm over complicating things?


// @flow
// @format

type ExceptionError = {
  InvalidPriceRangeException: string | ExceptionInformation

export const priceRange = (minimumRate:number, maximumRate:number): {number, number}  => {
  this.minimumRate = minimumRate;
  this.maximumRate = maximumRate;

  // [ASK]: I would like to have this 'guard' inside the `invalidPriceRangeException` where it belongs
  if (minimumRate <= maximumRate) {
  // [ASK]: How to have priceRange return either minimumRate || maximumRate without me having to return an obj? 
    return { minimumRate, maximumRate };
  } else {
    throw new invalidPriceRangeException(minimumRate, maximumRate)

const invalidPriceRangeException = (minimumRate:number, maximumRate:number): InvalidPriceRangeException => {
  this.minimumRate = minimumRate;
  this.maximumRate = maximumRate;

  return this.msg = `minimum (${minimumRate}) is larger than maximum (${maximumRate})`
  • \$\begingroup\$ both an implementation and requirements are strange. priceRange acts more like a validator. 1) why should it return either minimumRate || maximumRate ? 2) what is this.minimumRate and this.maximumRate for, in function context? \$\endgroup\$ – RomanPerekhrest Nov 7 '19 at 16:41
  • \$\begingroup\$ @RomanPerekhrest exactly I would like to extract that validator logic outside of the priceRange function cause it doesn't belong there. I think i have looked at it too much though. \$\endgroup\$ – intercoder Nov 7 '19 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.