javascriptfunctional-programmingsanctuary

What value should I pass to Sanctuary.either()?


I am trying to run the following example. It uses sanctuary.js.

const {create, env} = require('sanctuary');
const S = create({checkTypes: false, env: env});
const test = require('tape');

class Person {
  constructor(name){
    this.name = S.maybeToEither('')(S.toMaybe(name));
  }
};

const capitalize = (string) => string[0].toUpperCase() + string.substring(1);
const tigerify = (string) => `${string}, the tiger`;
const display = (string) => string.toString();

const tigerize = S.compose(capitalize)(tigerify);

test("Displaying a person", (assert) => {
  const personOne = new Person("tony");
  const actual = S.either(S.identity)(tigerize)(personOne.name);
  const expected = 'Tony, the tiger';

  assert.equal(actual, expected);
  assert.end();
});

test("Displaying an anonymous person", (assert) => {
  const personTwo = new Person(null);
  const actual = S.either(S.identity)(tigerize)(personTwo.name);
  const expected = '';

  assert.equal(actual, expected);
  assert.end();
});

The following error occurs on line 28 character 48:

TypeError: (intermediate value)(intermediate value)(intermediate value) is not a function

This is caused by personTwo.name in the following line:

const actual = S.either(S.identity)(tigerize)(personTwo.name);

I can see that personTwo.name is a Left. Why does this trigger an error rather than returning an empty string?


Solution

  • Looks like I was partly using an out of date api.

    By changing S.identity to S.I the example worked.