Skip to content

Commit

Permalink
feat(Identity): add Ord fantasy-land spec
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n committed Jul 5, 2017
1 parent 5c53df6 commit 76d94d7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/fantasy-land/Identity.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { aliases } from './util';
import fl from './mapping';
import { applyTrait, functorTrait, setoidTrait, semigroupTrait, chainTrait } from './traits';
import { applyTrait, functorTrait, setoidTrait, semigroupTrait, chainTrait, ordTrait } from './traits';


// we do this here for jsdocs generate properly
const { of, ap, map, equals, concat, chain } = fl;
const { of, ap, map, equals, concat, chain, lte } = fl;


/**
Expand All @@ -24,7 +24,8 @@ const { of, ap, map, equals, concat, chain } = fl;
* {@link https://github.com/fantasyland/fantasy-land#setoid|Setoid},
* {@link https://github.com/fantasyland/fantasy-land#semigroup|Semigroup},
* {@link https://github.com/fantasyland/fantasy-land#chain|Chain},
* {@link https://github.com/fantasyland/fantasy-land#monad|Monad}
* {@link https://github.com/fantasyland/fantasy-land#monad|Monad},
* {@link https://github.com/fantasyland/fantasy-land#ord|Ord}
* @since {@link https://char0n.github.io/ramda-adjunct/1.8.0|v1.8.0}
*/
class Identity {
Expand Down Expand Up @@ -164,6 +165,26 @@ class Identity {
[chain](fn) {
return chainTrait[chain].call(this, fn);
}

/**
* Fantasy land {@link https://github.com/fantasyland/fantasy-land#ord|Ord} specification.
*
* @sig lte :: Ord a => a ~> a -> Boolean *
* @param {RA.Identity} ord
* @return {boolean}
* @example
*
* const a = Identity.of(1);
* const b = Identity.of(1);
* const c = Identity.of(2);
*
* a.lte(b); //=> true
* a.lte(c); //=> true
* c.lte(a); //=> false
*/
[lte](ord) {
return ordTrait[lte].call(this, ord);
}
}

aliases(Identity).forEach(([alias, fn]) => {
Expand Down
6 changes: 6 additions & 0 deletions src/fantasy-land/traits.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ export const chainTrait = {
return isSameType(this, newChain) ? newChain : this;
},
};

export const ordTrait = {
[fl.lte](ord) {
return isSameType(this, ord) && (this.value < ord.value || this[fl.equals](ord));
},
};
45 changes: 45 additions & 0 deletions test/fantasy-land/Identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import applicative from 'fantasy-land/laws/applicative';
import functor from 'fantasy-land/laws/functor';
import chain from 'fantasy-land/laws/chain';
import monad from 'fantasy-land/laws/monad';
import ord from 'fantasy-land/laws/ord';

import { isFunction, Identity } from '../../src/index';
import eq from '../shared/eq';
Expand Down Expand Up @@ -315,4 +316,48 @@ describe('Identity', function() {
monad.rightIdentity(Identity)(eq)(1);
});
});

describe('Ord', function() {
it('tests for Setoid spec', function() {
const a = Identity.of(1);

eq(isFunction(a[fl.equals]), true);
});

it('tests for totality', function() {
ord.totality(eq)(Identity.of(1))(Identity.of(2));
});

it('tests for antisymetry', function() {
ord.antisymmetry(eq)(Identity.of(1))(Identity.of(1));
});

it('tests for transitivity', function() {
ord.transitivity(eq)(Identity.of(1))(Identity.of(2))(Identity.of(3));
});

it('tests for value of the same Ord', function() {
const a = Identity.of(1);
const b = Identity.of(2);

eq(a.lte(b), true);
});

it('tests for value of different Ord', function() {
const a = Identity.of(1);
const b = Identity.of(2);

b['@@type'] = 'unknown-type';

eq(a.lte(b), false);
});

it('tests for returning a boolean', function() {
const a = Identity.of(1);
const b = Identity.of(2);

eq(a.lte(b), true);
eq(b.lte(a), false);
});
});
});

0 comments on commit 76d94d7

Please sign in to comment.