我应该将什么价值传递给Sanctuary.either()?

时间:2018-06-09 08:36:38

标签: javascript functional-programming sanctuary

我正在尝试运行以下示例。它使用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();
});

第28行第48个字符出现以下错误:

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

这是由以下行中的personTwo.name引起的:

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

我可以看到personTwo.name是Left。为什么这会触发错误而不是返回一个空字符串?

1 个答案:

答案 0 :(得分:0)

看起来我部分使用了过时的api。

S.identity更改为S.I示例有效。