我正在尝试运行以下示例。它使用 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
. 为什么这会触发错误而不是返回空字符串?