source: node_modules/ramda-adjunct/src/fantasy-land/traits.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.4 KB
RevLine 
[d24f17c]1import { equals, pathSatisfies } from 'ramda';
2
3import isString from '../isString';
4import isNumber from '../isNumber';
5import isFunction from '../isFunction';
6import { isSameType } from './util';
7import * as fl from './mapping';
8
9export const functorTrait = {
10 [fl.map](fn) {
11 return this.constructor[fl.of](fn(this.value));
12 },
13};
14
15export const applyTrait = {
16 [fl.ap](applyWithFn) {
17 return applyWithFn.map((fn) => fn(this.value));
18 },
19};
20
21export const setoidTrait = {
22 [fl.equals](setoid) {
23 return isSameType(this, setoid) && equals(this.value, setoid.value);
24 },
25};
26
27export const semigroupTrait = {
28 [fl.concat](semigroup) {
29 let concatenatedValue = this.value;
30
31 if (isString(this.value) || isNumber(this.value)) {
32 concatenatedValue = this.value + semigroup.value;
33 } else if (pathSatisfies(isFunction, ['value', fl.concat], this)) {
34 concatenatedValue = this.value[fl.concat](semigroup.value);
35 } else if (pathSatisfies(isFunction, ['value', 'concat'], this)) {
36 concatenatedValue = this.value.concat(semigroup.value);
37 }
38
39 return this.constructor[fl.of](concatenatedValue);
40 },
41};
42
43export const chainTrait = {
44 [fl.chain](fn) {
45 const newChain = fn(this.value);
46
47 return isSameType(this, newChain) ? newChain : this;
48 },
49};
50
51export const ordTrait = {
52 [fl.lte](ord) {
53 return (
54 isSameType(this, ord) && (this.value < ord.value || this[fl.equals](ord))
55 );
56 },
57};
Note: See TracBrowser for help on using the repository browser.