Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option: fix should.js .eql() behaviour #1297

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions lib/util/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,35 @@ class Option {
}

class Some extends Option {
#value;
// Should.js uses `for ... in` to compare objects, so while should.js is in
// use an Option's value property must be "enumerable".
//
// See: https://github.com/shouldjs/equal/blob/master/index.js
// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties
//
// TODO revert from __please_use_get to #value when should.js is removed.
alxndrsn marked this conversation as resolved.
Show resolved Hide resolved
//#value;

/* istanbul ignore next */
constructor(value) {
super();
this.#value = value;
this.__please_use_get = value;
alxndrsn marked this conversation as resolved.
Show resolved Hide resolved
}

get() { return this.#value; }
get() { return this.__please_use_get; }

map(fn) { return Option.of(fn(this.#value)); }
filter(predicate) { return predicate(this.#value) === true ? this : none; } // eslint-disable-line no-use-before-define
map(fn) { return Option.of(fn(this.__please_use_get)); }
filter(predicate) { return predicate(this.__please_use_get) === true ? this : none; } // eslint-disable-line no-use-before-define

orNull() { return this.#value; }
orElse() { return this.#value; }
orElseGet() { return this.#value; }
orThrow() { return this.#value; }
orNull() { return this.__please_use_get; }
orElse() { return this.__please_use_get; }
orElseGet() { return this.__please_use_get; }
orThrow() { return this.__please_use_get; }

isDefined() { return true; }
isEmpty() { return false; }

ifDefined(consumer) { consumer(this.#value); }
ifDefined(consumer) { consumer(this.__please_use_get); }

// Used by ramda for comparing objects.
equals(that) {
Expand Down
36 changes: 36 additions & 0 deletions test/unit/util/option.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like these tests!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added more tests - to use the same test cases against chai & node's built-in assert module. I think these will be helpful to avoid breaking anything when/if doing incremental migration away from should.js.

Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,40 @@ describe('(libs/FP) Option type', () => {
});
});
});

describe('should.js interactions', () => {

// N.B. should.equal() is different from should.eql():
//
// * .eql(): check equality using ===
alxndrsn marked this conversation as resolved.
Show resolved Hide resolved
// * .eql(): check equality using "should-equal" module
//
// See: https://www.npmjs.com/package/should-equal

describe('should.eql()', () => {
[
true,
false,
0,
1,
'',
'non-empty string',
].forEach(val => {
it(`should recognise two Options of '${val}' to be equal`, () => {
Option.of(val).should.eql(Option.of(val));
});
});

[
[ 0, 1 ],
[ 0, false ],
[ 0, '' ],
[ false, '' ],
].forEach((a, b) => {
it(`should not recognise Options of '${a}' and '${b}' as equal`, () => {
Option.of(a).should.not.eql(Option.of(b));
});
});
});
});
});