Skip to content

Instantly share code, notes, and snippets.

@MikeMKH
Last active February 24, 2017 12:48
Show Gist options
  • Save MikeMKH/239a52a55545458d5d93640ca7bf4776 to your computer and use it in GitHub Desktop.
Save MikeMKH/239a52a55545458d5d93640ca7bf4776 to your computer and use it in GitHub Desktop.
Basic Maybe example using folktale with mocha and expect.js
const expect = require('expect.js');
const Maybe = require('data.maybe');
const Nothing = Maybe.Nothing;
const Just = Maybe.Just
describe('truth', () => {
it('will be true', () => {
expect(true).to.equal(true);
})
})
describe('Maybe', () => {
describe('example use', () => {
describe('find', () => {
const find = (pred, xs) =>
xs.reduce(
(m, x) => m.orElse(() => pred(x) ? Just(x) : Nothing()),
Nothing());
it('given collection with valid predicate it must return just', () => {
const actual = find(x => x === 2, [1, 2, 3]);
expect(actual).to.eql(Just(2));
expect(actual.isEqual(Just(2))).ok();
expect(actual.isJust).ok();
}),
it('given collection with invalid predicate it must return nothing', () => {
const actual = find(x => x < 1, [1]);
expect(actual).to.eql(Nothing());
expect(actual.isEqual(Just(1))).not.ok();
expect(actual.isEqual(Nothing())).ok();
expect(actual.isNothing).ok();
}),
it('given found value it is chainable', () => {
const value = find(x => x === "found", ["not this one", "found"]);
expect(value).to.eql(Just("found"));
const actual = value.map(s => s.toUpperCase()).map(s => s[0]);
expect(actual).to.eql(Just("F"));
}),
it('given not found value it is still chainable', () => {
const value = find(x => x === "needle", ["your needle is not here"]);
expect(value.isNothing).ok();
const actual = value.map(s => s.toLowerCase()).map(s => s.trim());
expect(actual).to.eql(Nothing());
}),
it('given not found value it must be able to default it', () => {
const value = find(x => x > 42, [1, 2, 8]);
expect(value.isNothing).ok();
const actual = value.getOrElse(43);
expect(actual).to.eql(43);
}),
it('given found value it must be able to not default it', () => {
const value = find(x => x < 42, [1, 2, 8]);
expect(value.isJust).ok();
const actual = value.getOrElse(43);
expect(actual).to.eql(1);
});
}),
describe('built in', () => {
it('must be able to bring value into context', () => {
const value = 8;
const actual = Maybe.of(value);
expect(actual.isJust).ok();
expect(actual.map(x => x + 1).get()).to.equal(value + 1);
}),
it('must be able to bring nullable types into context', () => {
expect(Maybe.fromNullable(null)).to.eql(Nothing());
expect(Maybe.fromNullable(undefined)).to.eql(Nothing());
expect(Maybe.fromNullable((function(){})())).to.eql(Nothing());
}),
it('must throw TypeError if get is called on nothing', () => {
// expect(Nothing().get()).to.throwException((e) => expect(e).to.be.a(TypeError)); // expect does not seem to catch it
});
})
})
})
@MikeMKH
Copy link
Author

MikeMKH commented Feb 23, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment