-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add maybeToList and maybeToArray transformations
- Loading branch information
Showing
9 changed files
with
339 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** @license ISC License (c) copyright 2019 original and current authors */ | ||
/** @author Ian Hofmann-Hicks (evil) */ | ||
|
||
const List = require('.') | ||
const Maybe = require('../core/types').proxy('Maybe') | ||
|
||
const curry = require('../core/curry') | ||
const isFunction = require('../core/isFunction') | ||
const isSameType = require('../core/isSameType') | ||
|
||
const applyTransform = maybe => | ||
maybe.either( | ||
List.empty, | ||
List.of | ||
) | ||
|
||
const err = | ||
'maybeToList: Argument must be a Maybe instanstace or a Maybe returning function' | ||
|
||
// maybeToList : Maybe a -> List a | ||
// maybeToList : (a -> Maybe b) -> a -> List b | ||
function maybeToList(maybe) { | ||
if(isFunction(maybe)) { | ||
return function(x) { | ||
const m = maybe(x) | ||
|
||
if(!isSameType(Maybe, m)) { | ||
throw new TypeError(err) | ||
} | ||
|
||
return applyTransform(m) | ||
} | ||
} | ||
|
||
if(isSameType(Maybe, maybe)) { | ||
return applyTransform(maybe) | ||
} | ||
|
||
throw new TypeError(err) | ||
} | ||
|
||
module.exports = curry(maybeToList) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const test = require('tape') | ||
const helpers = require('../test/helpers') | ||
|
||
const bindFunc = helpers.bindFunc | ||
|
||
const List = require('.') | ||
const Maybe = require('../core/Maybe') | ||
|
||
const isFunction = require('../core/isFunction') | ||
const isSameType = require('../core/isSameType') | ||
|
||
const identity = x => x | ||
|
||
const maybeToList = require('./maybeToList') | ||
|
||
test('maybeToList transform', t => { | ||
const f = bindFunc(maybeToList) | ||
|
||
t.ok(isFunction(maybeToList), 'is a function') | ||
|
||
const err = /maybeToList: Argument must be a Maybe instanstace or a Maybe returning function/ | ||
t.throws(f(undefined), err, 'throws if first arg is undefined') | ||
t.throws(f(null), err, 'throws if first arg is null') | ||
t.throws(f(0), err, 'throws if first arg is a falsey number') | ||
t.throws(f(1), err, 'throws if first arg is a truthy number') | ||
t.throws(f(''), err, 'throws if first arg is a falsey string') | ||
t.throws(f('string'), err, 'throws if first arg is a truthy string') | ||
t.throws(f(false), err, 'throws if first arg is false') | ||
t.throws(f(true), err, 'throws if first arg is true') | ||
t.throws(f([]), err, 'throws if first arg is an array') | ||
t.throws(f({}), err, 'throws if first arg is an object') | ||
|
||
t.end() | ||
}) | ||
|
||
test('maybeToList with Maybe', t => { | ||
const some = 'something' | ||
|
||
const good = maybeToList(Maybe.Just(some)) | ||
const bad = maybeToList(Maybe.Nothing()) | ||
|
||
t.ok(isSameType(List, good), 'returns a List with a Just') | ||
t.ok(isSameType(List, bad), 'returns a List with a Nothing') | ||
|
||
t.ok(good.equals(List.of(some)), 'Just maps to single item List containing the value') | ||
t.ok(bad.equals(List.empty()), 'Nothing maps to an empty List') | ||
|
||
t.end() | ||
}) | ||
|
||
test('maybeToList with Maybe returning function', t => { | ||
const some = 'something' | ||
|
||
t.ok(isFunction(maybeToList(Maybe.of)), 'returns a function') | ||
|
||
const f = bindFunc(maybeToList(identity)) | ||
|
||
const err = /maybeToList: Argument must be a Maybe instanstace or a Maybe returning function/ | ||
t.throws(f(undefined), err, 'throws if function returns undefined') | ||
t.throws(f(null), err, 'throws if function returns null') | ||
t.throws(f(0), err, 'throws if function returns a falsey number') | ||
t.throws(f(1), err, 'throws if function returns a truthy number') | ||
t.throws(f(''), err, 'throws if function returns a falsey string') | ||
t.throws(f('string'), err, 'throws if function returns a truthy string') | ||
t.throws(f(false), err, 'throws if function returns false') | ||
t.throws(f(true), err, 'throws if function returns true') | ||
t.throws(f([]), err, 'throws if function returns an array') | ||
t.throws(f({}), err, 'throws if function returns an object') | ||
|
||
const lift = | ||
x => x !== undefined ? Maybe.Just(x) : Maybe.Nothing() | ||
|
||
const good = maybeToList(lift, some) | ||
const bad = maybeToList(lift, undefined) | ||
|
||
t.ok(isSameType(List, good), 'returns a List with a Just') | ||
t.ok(isSameType(List, bad), 'returns a List with a Nothing') | ||
|
||
t.ok(good.equals(List.of(some)), 'Just maps to single item List containing the value') | ||
t.ok(bad.equals(List.empty()), 'Nothing maps to an empty List') | ||
|
||
t.end() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** @license ISC License (c) copyright 2019 original and current authors */ | ||
/** @author Ian Hofmann-Hicks (evil) */ | ||
|
||
const Maybe = require('.') | ||
|
||
const curry = require('../core/curry') | ||
const isFunction = require('../core/isFunction') | ||
const isSameType = require('../core/isSameType') | ||
|
||
const applyTransform = maybe => | ||
maybe.either(() => [], x => [ x ]) | ||
|
||
const err = | ||
'maybeToArray: Argument must be a Maybe instanstace or a Maybe returning function' | ||
|
||
function maybeToArray(maybe) { | ||
if(isFunction(maybe)) { | ||
return function(x) { | ||
const m = maybe(x) | ||
|
||
if(!isSameType(Maybe, m)) { | ||
throw new TypeError(err) | ||
} | ||
return applyTransform(m) | ||
} | ||
} | ||
|
||
if(isSameType(Maybe, maybe)) { | ||
return applyTransform(maybe) | ||
} | ||
|
||
throw new TypeError(err) | ||
} | ||
|
||
module.exports = curry(maybeToArray) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
const test = require('tape') | ||
const helpers = require('../test/helpers') | ||
|
||
const bindFunc = helpers.bindFunc | ||
|
||
const Maybe = require('../core/Maybe') | ||
|
||
const isArray = require('../core/isArray') | ||
const isFunction = require('../core/isFunction') | ||
|
||
const identity = x => x | ||
|
||
const maybeToArray = require('./maybeToArray') | ||
|
||
test('maybeToArray transform', t => { | ||
const f = bindFunc(maybeToArray) | ||
|
||
t.ok(isFunction(maybeToArray), 'is a function') | ||
|
||
const err = /maybeToArray: Argument must be a Maybe instanstace or a Maybe returning function/ | ||
t.throws(f(undefined), err, 'throws if second arg is undefined') | ||
t.throws(f(null), err, 'throws if second arg is null') | ||
t.throws(f(0), err, 'throws if second arg is a falsey number') | ||
t.throws(f(1), err, 'throws if second arg is a truthy number') | ||
t.throws(f(''), err, 'throws if second arg is a falsey string') | ||
t.throws(f('string'), err, 'throws if second arg is a truthy string') | ||
t.throws(f(false), err, 'throws if second arg is false') | ||
t.throws(f(true), err, 'throws if second arg is true') | ||
t.throws(f([]), err, 'throws if second arg is an array') | ||
t.throws(f({}), err, 'throws if second arg is an object') | ||
|
||
t.end() | ||
}) | ||
|
||
test('maybeToArray with Maybe', t => { | ||
const some = 'something' | ||
|
||
const good = maybeToArray(Maybe.Just(some)) | ||
const bad = maybeToArray(Maybe.Nothing()) | ||
|
||
t.ok(isArray(good), 'returns an Array with a Just') | ||
t.ok(isArray(bad), 'returns an Array with a Nothing') | ||
|
||
t.same(good, [ some ], 'Just maps to single item Array containing the value') | ||
t.same(bad, [], 'Nothing maps to an empty Array') | ||
|
||
t.end() | ||
}) | ||
|
||
test('maybeToArray with Maybe returning function', t => { | ||
const some = 'something' | ||
|
||
t.ok(isFunction(maybeToArray(Maybe.of)), 'returns a function') | ||
|
||
const f = bindFunc(maybeToArray(identity)) | ||
|
||
const err = /maybeToArray: Argument must be a Maybe instanstace or a Maybe returning function/ | ||
t.throws(f(undefined), err, 'throws if function returns undefined') | ||
t.throws(f(null), err, 'throws if function returns null') | ||
t.throws(f(0), err, 'throws if function returns a falsey number') | ||
t.throws(f(1), err, 'throws if function returns a truthy number') | ||
t.throws(f(''), err, 'throws if function returns a falsey string') | ||
t.throws(f('string'), err, 'throws if function returns a truthy string') | ||
t.throws(f(false), err, 'throws if function returns false') | ||
t.throws(f(true), err, 'throws if function returns true') | ||
t.throws(f([]), err, 'throws if function returns an array') | ||
t.throws(f({}), err, 'throws if function returns an object') | ||
|
||
const lift = | ||
x => x !== undefined ? Maybe.Just(x) : Maybe.Nothing() | ||
|
||
const good = maybeToArray(lift, some) | ||
const bad = maybeToArray(lift, undefined) | ||
|
||
t.ok(isArray(good), 'returns an Array with a Just') | ||
t.ok(isArray(bad), 'returns an Array with a Nothing') | ||
|
||
t.same(good, [ some ], 'Just maps to single item Array containing the value') | ||
t.same(bad, [], 'Nothing maps to an empty Array') | ||
|
||
t.end() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.