1

I'm writing an ES6 module but ho no idea how to omit an argument from my main index.js but still check it in my exposed method.

// index.js
import {getStatus, openingHours} from './lib/methods.js';

export default ID => {
  if(!ID) throw new Error(`An ID needs to be passed`);

  return {
    getStatus: getStatus(ID),
    openingHours: openingHours(ID),
  };
};


// lib/methods.js
import find from 'lodash.find';
import composedFetch from '../composedFetch.js';

export const getStatus = id => composedFetch(id)
  .then(data => find(data.processOverview.processSteps, { status: `active` }))
  .catch(e => console.log(e));

export const openingHours = (id, day) => {
  if (!day) throw new Error(`Please specify a day`);

  return composedFetch(id)
    .then(data => {
      const obj = find(
        data.deliveryPoint.openingSchedules,
        { dayOfTheWeek: day.toUpperCase() }
      );

      return obj.openingHours[0];
    })
    .catch(e => console.error(e));
};

As you can see my method needs a parameter day. The way that the module should work is so that you first instantiate it with an ID and then use the methods:

import bpost from 'bpost';

const pkg = bpost('someIDhere');
const status = pkg.getStatus();
const openingHours = pkg.openingHours('monday');

I tried doing this with rest operator and default parameters but no luck yet. My test give still threw the day error with this test code (which should work once this is solved):

// methods.test.js
import bpost from '../src/index.js';

describe(`Method: global.bpost`, () => {
  it(`should show the available methods for the module`, () => {
    expect(() => bpost(`someIDhere`)).not.toThrow();
  });
});

Thanks in advance!

thibmaek
  • 1,171
  • 3
  • 13
  • 21
  • My initial though would be to change if(!day) condition to do something different based on it rather that throwing error - because in your scenario you still want to use this function even if day is not specified. Possibly some sort of facade would give clear outcome, have a look at this simple idea: http://www.dofactory.com/javascript/facade-design-pattern – eloleon Dec 04 '16 at 23:23
  • @eloleon hmm might change it with a console.error instead but I don't want the function to execute if the day is not passed. – thibmaek Dec 04 '16 at 23:35
  • I think you're looking for this http://stackoverflow.com/questions/36314/what-is-currying – duivvv Dec 05 '16 at 09:27
  • I think you are looking for this. http://stackoverflow.com/questions/36314/what-is-currying – duivvv Dec 05 '16 at 09:31
  • @duivvv already fixed it with the answer below which feels more natural to me. Thanks for the link on currying though, was a bit in the dark about the concept but understand it now ;) – thibmaek Dec 05 '16 at 14:13

1 Answers1

1

You need to do

export default ID => {
  if(!ID) throw new Error(`An ID needs to be passed`);

  return {
    getStatus: () => getStatus(ID),
    openingHours: day => openingHours(ID, day),
  };
};

to make the returned object properties actually be methods.

Alternatively,

  return {
    getStatus: getStatus.bind(null, ID),
    openingHours: openingHours.bind(null, ID),
  };

will do the same but is easier to generalise (like if you want to decorate all imported methods automatically).

Bergi
  • 630,263
  • 148
  • 957
  • 1,375