Skip to content

Commit

Permalink
extract Just related duplicated code (ramda#3276)
Browse files Browse the repository at this point in the history
* Use Maybe.Just and Maybe.Nothing instead

* import Just, Nothing directly from Maybe.js
  • Loading branch information
zydmayday authored Apr 27, 2022
1 parent dec329d commit 50c6b57
Show file tree
Hide file tree
Showing 21 changed files with 26 additions and 108 deletions.
6 changes: 1 addition & 5 deletions test/difference.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var {Just} = require('./shared/Maybe.js');


describe('difference', function() {
Expand All @@ -18,11 +19,6 @@ describe('difference', function() {
});

it('has R.equals semantics', function() {
function Just(x) { this.value = x; }
Just.prototype.equals = function(x) {
return x instanceof Just && R.equals(x.value, this.value);
};

eq(R.difference([0], [-0]).length, 1);
eq(R.difference([-0], [0]).length, 1);
eq(R.difference([NaN], [NaN]).length, 0);
Expand Down
6 changes: 1 addition & 5 deletions test/dropRepeats.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var {Just} = require('./shared/Maybe.js');


describe('dropRepeats', function() {
Expand All @@ -21,11 +22,6 @@ describe('dropRepeats', function() {
});

it('has R.equals semantics', function() {
function Just(x) { this.value = x; }
Just.prototype.equals = function(x) {
return x instanceof Just && R.equals(x.value, this.value);
};

eq(R.dropRepeats([0, -0]).length, 2);
eq(R.dropRepeats([-0, 0]).length, 2);
eq(R.dropRepeats([NaN, NaN]).length, 1);
Expand Down
6 changes: 1 addition & 5 deletions test/eqBy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var {Just} = require('./shared/Maybe.js');


describe('eqBy', function() {
Expand All @@ -13,11 +14,6 @@ describe('eqBy', function() {
});

it('has R.equals semantics', function() {
function Just(x) { this.value = x; }
Just.prototype.equals = function(x) {
return x instanceof Just && R.equals(x.value, this.value);
};

eq(R.eqBy(R.identity, 0, -0), false);
eq(R.eqBy(R.identity, -0, 0), false);
eq(R.eqBy(R.identity, NaN, NaN), true);
Expand Down
6 changes: 1 addition & 5 deletions test/eqProps.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var {Just} = require('./shared/Maybe.js');


describe('eqProps', function() {
Expand All @@ -9,11 +10,6 @@ describe('eqProps', function() {
});

it('has R.equals semantics', function() {
function Just(x) { this.value = x; }
Just.prototype.equals = function(x) {
return x instanceof Just && R.equals(x.value, this.value);
};

eq(R.eqProps('value', {value: 0}, {value: -0}), false);
eq(R.eqProps('value', {value: -0}, {value: 0}), false);
eq(R.eqProps('value', {value: NaN}, {value: NaN}), true);
Expand Down
4 changes: 2 additions & 2 deletions test/filter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var Maybe = require('./shared/Maybe.js');
var {Just} = require('./shared/Maybe.js');


describe('filter', function() {
Expand Down Expand Up @@ -33,7 +33,7 @@ describe('filter', function() {
});

it('correctly uses fantasy-land implementations', function() {
var m1 = Maybe.Just(-1);
var m1 = Just(-1);
var m2 = R.filter(function(x) { return x > 0; } , m1);

eq(m2.isNothing, true);
Expand Down
6 changes: 1 addition & 5 deletions test/includes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var {Just} = require('./shared/Maybe.js');


describe('includes', function() {
Expand All @@ -16,11 +17,6 @@ describe('includes', function() {
});

it('has R.equals semantics', function() {
function Just(x) { this.value = x; }
Just.prototype.equals = function(x) {
return x instanceof Just && R.equals(x.value, this.value);
};

eq(R.includes(0, [-0]), false);
eq(R.includes(-0, [0]), false);
eq(R.includes(NaN, [NaN]), true);
Expand Down
6 changes: 1 addition & 5 deletions test/indexOf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var {Just} = require('./shared/Maybe.js');


describe('indexOf', function() {
Expand Down Expand Up @@ -46,11 +47,6 @@ describe('indexOf', function() {
});

it('has R.equals semantics', function() {
function Just(x) { this.value = x; }
Just.prototype.equals = function(x) {
return x instanceof Just && R.equals(x.value, this.value);
};

eq(R.indexOf(0, [-0]), -1);
eq(R.indexOf(-0, [0]), -1);
eq(R.indexOf(NaN, [NaN]), 0);
Expand Down
6 changes: 1 addition & 5 deletions test/intersection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var {Just} = require('./shared/Maybe.js');


describe('intersection', function() {
Expand All @@ -24,11 +25,6 @@ describe('intersection', function() {
});

it('has R.equals semantics', function() {
function Just(x) { this.value = x; }
Just.prototype.equals = function(x) {
return x instanceof Just && R.equals(x.value, this.value);
};

eq(R.intersection([0], [-0]).length, 0);
eq(R.intersection([-0], [0]).length, 0);
eq(R.intersection([NaN], [NaN]).length, 1);
Expand Down
6 changes: 1 addition & 5 deletions test/lastIndexOf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var {Just} = require('./shared/Maybe.js');


describe('lastIndexOf', function() {
Expand Down Expand Up @@ -37,11 +38,6 @@ describe('lastIndexOf', function() {
});

it('has R.equals semantics', function() {
function Just(x) { this.value = x; }
Just.prototype.equals = function(x) {
return x instanceof Just && R.equals(x.value, this.value);
};

eq(R.lastIndexOf(0, [-0]), -1);
eq(R.lastIndexOf(-0, [0]), -1);
eq(R.lastIndexOf(NaN, [NaN]), 0);
Expand Down
4 changes: 2 additions & 2 deletions test/lift.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var Maybe = require('./shared/Maybe.js');
var {Just} = require('./shared/Maybe.js');

var not = function(x) { return !x; };
var add3 = R.curry(function add3(a, b, c) {
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('lift', function() {

it('works with other functors such as "Maybe"', function() {
var addM = R.lift(R.add);
eq(addM(Maybe.Just(3), Maybe.Just(5)), Maybe.Just(8));
eq(addM(Just(3), Just(5)), Just(8));
});

});
4 changes: 2 additions & 2 deletions test/liftN.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var Maybe = require('./shared/Maybe.js');
var {Just} = require('./shared/Maybe.js');


var addN = function() {
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('liftN', function() {

it('works with other functors such as "Maybe"', function() {
var addM = R.liftN(2, R.add);
eq(addM(Maybe.Just(3), Maybe.Just(5)), Maybe.Just(8));
eq(addM(Just(3), Just(5)), Just(8));
});

it('interprets [a] as a functor', function() {
Expand Down
6 changes: 1 addition & 5 deletions test/pathEq.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var {Just} = require('./shared/Maybe.js');


describe('pathEq', function() {
Expand Down Expand Up @@ -34,11 +35,6 @@ describe('pathEq', function() {
});

it('has R.equals semantics', function() {
function Just(x) { this.value = x; }
Just.prototype.equals = function(x) {
return x instanceof Just && R.equals(x.value, this.value);
};

eq(R.pathEq(0, ['value'], {value: -0}), false);
eq(R.pathEq(-0, ['value'], {value: 0}), false);
eq(R.pathEq(NaN, ['value'], {value: NaN}), true);
Expand Down
6 changes: 1 addition & 5 deletions test/propEq.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var {Just} = require('./shared/Maybe.js');


describe('propEq', function() {
Expand All @@ -22,11 +23,6 @@ describe('propEq', function() {
});

it('has R.equals semantics', function() {
function Just(x) { this.value = x; }
Just.prototype.equals = function(x) {
return x instanceof Just && R.equals(x.value, this.value);
};

eq(R.propEq(0, 'value', {value: -0}), false);
eq(R.propEq(-0, 'value', {value: 0}), false);
eq(R.propEq(NaN, 'value', {value: NaN}), true);
Expand Down
16 changes: 3 additions & 13 deletions test/reject.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var {Just, Nothing} = require('./shared/Maybe.js');


describe('reject', function() {
Expand All @@ -26,21 +27,10 @@ describe('reject', function() {
});

it('dispatches to `filter` method', function() {
function Nothing() {}
Nothing.value = new Nothing();
Nothing.prototype.filter = function() {
return this;
};

function Just(x) { this.value = x; }
Just.prototype.filter = function(pred) {
return pred(this.value) ? this : Nothing.value;
};

var m = new Just(42);
eq(R.filter(R.T, m), m);
eq(R.filter(R.F, m), Nothing.value);
eq(R.reject(R.T, m), Nothing.value);
eq(R.filter(R.F, m), Nothing);
eq(R.reject(R.T, m), Nothing);
eq(R.reject(R.F, m), m);
});

Expand Down
6 changes: 1 addition & 5 deletions test/symmetricDifference.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var fc = require('fast-check');
var {Just} = require('./shared/Maybe.js');


describe('symmetricDifference', function() {
Expand All @@ -20,11 +21,6 @@ describe('symmetricDifference', function() {
});

it('has R.equals semantics', function() {
function Just(x) { this.value = x; }
Just.prototype.equals = function(x) {
return x instanceof Just && R.equals(x.value, this.value);
};

eq(R.symmetricDifference([0], [-0]).length, 2);
eq(R.symmetricDifference([-0], [0]).length, 2);
eq(R.symmetricDifference([NaN], [NaN]).length, 0);
Expand Down
10 changes: 1 addition & 9 deletions test/toString.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var assert = require('assert');

var R = require('../source/index.js');
var {Just} = require('./shared/Maybe.js');


describe('toString', function() {
Expand Down Expand Up @@ -148,15 +149,6 @@ describe('toString', function() {
};
assert.strictEqual(R.toString(new Point(1, 2)), 'new Point(1, 2)');

function Just(x) {
if (!(this instanceof Just)) {
return new Just(x);
}
this.value = x;
}
Just.prototype.toString = function() {
return 'Just(' + R.toString(this.value) + ')';
};
assert.strictEqual(R.toString(Just(42)), 'Just(42)');
assert.strictEqual(R.toString(Just([1, 2, 3])), 'Just([1, 2, 3])');
assert.strictEqual(R.toString(Just(Just(Just('')))), 'Just(Just(Just("")))');
Expand Down
6 changes: 1 addition & 5 deletions test/union.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var {Just} = require('./shared/Maybe.js');


describe('union', function() {
Expand All @@ -10,11 +11,6 @@ describe('union', function() {
});

it('has R.equals semantics', function() {
function Just(x) { this.value = x; }
Just.prototype.equals = function(x) {
return x instanceof Just && R.equals(x.value, this.value);
};

eq(R.union([0], [-0]).length, 2);
eq(R.union([-0], [0]).length, 2);
eq(R.union([NaN], [NaN]).length, 1);
Expand Down
6 changes: 1 addition & 5 deletions test/uniq.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var {Just} = require('./shared/Maybe.js');


describe('uniq', function() {
Expand All @@ -17,11 +18,6 @@ describe('uniq', function() {
});

it('has R.equals semantics', function() {
function Just(x) { this.value = x; }
Just.prototype.equals = function(x) {
return x instanceof Just && R.equals(x.value, this.value);
};

eq(R.uniq([-0, -0]).length, 1);
eq(R.uniq([0, -0]).length, 2);
eq(R.uniq([NaN, NaN]).length, 1);
Expand Down
7 changes: 1 addition & 6 deletions test/uniqBy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var {Just} = require('./shared/Maybe.js');


describe('uniqBy', function() {
Expand All @@ -17,12 +18,6 @@ describe('uniqBy', function() {
});

it('has R.equals semantics', function() {
function Just(x) {
this.value = x;
}
Just.prototype.equals = function(x) {
return x instanceof Just && R.equals(x.value, this.value);
};
eq(R.uniqBy(R.identity, [-0, 0]).length, 2);
eq(R.uniqBy(R.identity, [NaN, NaN]).length, 1);
eq(R.uniqBy(R.identity, [new Just([1, 2, 3]), new Just([1, 2, 3])]).length, 1);
Expand Down
5 changes: 1 addition & 4 deletions test/unnest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var assert = require('assert');

var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var Maybe = require('./shared/Maybe.js');
var {Just, Nothing} = require('./shared/Maybe.js');


describe('unnest', function() {
Expand Down Expand Up @@ -31,9 +31,6 @@ describe('unnest', function() {
});

it('is equivalent to R.chain(R.identity)', function() {
var Nothing = Maybe.Nothing;
var Just = Maybe.Just;

eq(R.unnest(Nothing), Nothing);
eq(R.unnest(Just(Nothing)), Nothing);
eq(R.unnest(Just(Just(Nothing))), Just(Nothing));
Expand Down
Loading

0 comments on commit 50c6b57

Please sign in to comment.