Skip to content

Commit fa29c6f

Browse files
committed
Remove maybe
1 parent 786d806 commit fa29c6f

3 files changed

Lines changed: 21 additions & 38 deletions

File tree

index.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { Maybe, maybe } = require('./src/maybe')
2-
const { Either, either, left } = require('./src/either')
1+
const { Either, either, left, fromNullable } = require('./src/either')
32

43
const id = _ => _
4+
const always = a => () => a
55

66
const DEFAULTS = {
77
types: {},
@@ -24,18 +24,19 @@ const reduce = fn => zero => xs => xs.reduce(fn, zero)
2424
const split = d => s => s.split(d)
2525

2626
const createRootObj = compose(
27-
n => maybe(Object.create(null))(Array)(Maybe.of(isNaN(n) ? undefined : n)),
27+
either(Object.create, Array),
28+
n => (isNaN(n) ? left(null) : Either.of(n)),
2829
Number
2930
)
3031

3132
const normalizeField = delimiter => m =>
32-
Maybe.of(m)
33+
fromNullable(m)
3334
.map(m => m.indexOf(delimiter) > -1)
3435
.map(b => (b ? m : m + delimiter + m))
3536
.map(split(delimiter))
3637

3738
const getMapSpec = delimiter => mapping =>
38-
Maybe.of(mapping)
39+
fromNullable(mapping)
3940
.map(Array.isArray)
4041
.map(b => (b ? Either.of(mapping) : left(mapping)))
4142
.map(
@@ -62,7 +63,11 @@ const getMappingFilter = (type, types) => {
6263
const getKey = reduce((accum, k) => (accum ? accum[k] : undefined))
6364

6465
const get = (key, delimiter = DEFAULTS.objDelimiter) => obj =>
65-
compose(maybe(obj)(getKey(obj)), map(split(delimiter)), Maybe.of)(key)
66+
compose(
67+
either(always(obj), getKey(obj)),
68+
map(split(delimiter)),
69+
fromNullable
70+
)(key)
6671

6772
const setKey = value =>
6873
reduce((accum, key, i, array) => {
@@ -73,9 +78,10 @@ const setKey = value =>
7378

7479
const assign = (key, delimiter = DEFAULTS.objDelimiter) => (obj, value) =>
7580
compose(
76-
maybe(obj)(compose(() => obj, setKey(value)(obj))),
81+
always(obj),
82+
either(id, setKey(value)(obj)),
7783
map(split(delimiter)),
78-
Maybe.of
84+
fromNullable
7985
)(key)
8086

8187
class Mapper {
@@ -86,7 +92,7 @@ class Mapper {
8692
map(mappings, curr, next = Object.create(null)) {
8793
return mappings.map(normalizeMapping).reduce(
8894
(accum, mapping) =>
89-
Maybe.of(mapping.field)
95+
fromNullable(mapping.field)
9096
.chain(getMapSpec(this.config.mapDelimiter))
9197
.chain(([sourceField, targetField]) =>
9298
Either.of(sourceField)
@@ -100,9 +106,10 @@ class Mapper {
100106
/* Begin user-land transforms */
101107
)(_, mapping, this.config, curr, accum)
102108
)
103-
.map(Maybe.of)
109+
.map(fromNullable)
104110
.chain(
105-
maybe(accum)(
111+
either(
112+
always(accum),
106113
assign(targetField, this.config.objDelimiter).bind(
107114
this,
108115
accum

src/either.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ Either.of = x => new Right(x)
4848

4949
const either = (f, g) => e => (e.isLeft ? f(e._) : g(e._))
5050
const left = x => new Left(x)
51+
const fromNullable = a => (a === undefined ? new Left(a) : new Right(a))
5152

5253
module.exports = {
5354
Either,
5455
either,
55-
left
56+
left,
57+
fromNullable
5658
}

src/maybe.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)