diff --git a/README.md b/README.md index 9fed340..adf4a69 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# MojiScript ![project:experimental](https://img.shields.io/badge/project-experimental-orange.svg) [![build status](https://travis-ci.org/joelnet/MojiScript.svg?branch=master)](https://travis-ci.org/joelnet/MojiScript) [![Coverage Status](https://coveralls.io/repos/github/joelnet/MojiScript/badge.svg?branch=master&v=1)](https://coveralls.io/github/joelnet/MojiScript?branch=master) ![project:experimental](https://img.shields.io/badge/tests-374-green.svg) [![All Contributors](https://img.shields.io/badge/all_contributors-19-orange.svg?style=flat-square)](#contributors) +# MojiScript ![project:experimental](https://img.shields.io/badge/project-experimental-orange.svg) [![build status](https://travis-ci.org/joelnet/MojiScript.svg?branch=master)](https://travis-ci.org/joelnet/MojiScript) [![Coverage Status](https://coveralls.io/repos/github/joelnet/MojiScript/badge.svg?branch=master&v=1)](https://coveralls.io/github/joelnet/MojiScript?branch=master) ![project:experimental](https://img.shields.io/badge/tests-380-green.svg) [![All Contributors](https://img.shields.io/badge/all_contributors-19-orange.svg?style=flat-square)](#contributors) ![MojiScript logo](https://raw.githubusercontent.com/joelnet/MojiScript/master/media/MS_logo_64.png) diff --git a/type/Just.js b/type/Just.js index 544601f..b6cd272 100644 --- a/type/Just.js +++ b/type/Just.js @@ -41,6 +41,10 @@ function getOrElse() { return this.value } +function getValue(errCallback, dataCallback) { + return dataCallback(this.value) +} + const prototype = { '@@type': typeJust, ap, @@ -49,6 +53,7 @@ const prototype = { flatMap, leftMap, getOrElse, + getValue, 'fantasy-land/ap': ap, 'fantasy-land/map': map, toString() { return this.value.toString() }, diff --git a/type/Left.js b/type/Left.js index 819e978..27c504d 100644 --- a/type/Left.js +++ b/type/Left.js @@ -47,6 +47,10 @@ function getOrElse(defaultValue) { return defaultValue } +function getValue(errCallback) { + return errCallback(this.value) +} + const prototype = { '@@type': typeLeft, ap, @@ -56,6 +60,7 @@ const prototype = { leftMap, leftFlatMap, getOrElse, + getValue, 'fantasy-land/ap': ap, 'fantasy-land/map': map, inspect() { diff --git a/type/Nothing.js b/type/Nothing.js index b65b38d..0413c76 100644 --- a/type/Nothing.js +++ b/type/Nothing.js @@ -5,6 +5,10 @@ function getOrElse(defaultValue) { return defaultValue } +function getValue(errCallback) { + return errCallback() +} + const Nothing = Object.freeze(Object.create( { '@@type': typeNothing, @@ -16,6 +20,7 @@ const Nothing = Object.freeze(Object.create( leftMap: func => Just(func()), flatMap: () => Nothing, getOrElse, + getValue, 'fantasy-land/map': () => Nothing, toJSON: () => null }, diff --git a/type/Right.js b/type/Right.js index 5d216c6..5f45ffa 100644 --- a/type/Right.js +++ b/type/Right.js @@ -49,6 +49,10 @@ function getOrElse() { return this.value } +function getValue(errCallback, dataCallback) { + return dataCallback(this.value) +} + const prototype = { '@@type': typeRight, ap, @@ -58,6 +62,7 @@ const prototype = { leftMap, leftFlatMap, getOrElse, + getValue, 'fantasy-land/ap': ap, 'fantasy-land/map': map, inspect() { diff --git a/type/__tests__/Just.test.js b/type/__tests__/Just.test.js index e2b2cbb..93af7ff 100644 --- a/type/__tests__/Just.test.js +++ b/type/__tests__/Just.test.js @@ -74,6 +74,13 @@ describe('type/Just', () => { expect(actual).toBe(expected) }) + test('getValue', () => { + expect.assertions(1) + const expected = 'abc' + const actual = Just('abc').getValue(() => 123, x => x) + expect(actual).toBe(expected) + }) + test('fantasy-land/map', () => { expect.assertions(1) const expected = 888 diff --git a/type/__tests__/Left.test.js b/type/__tests__/Left.test.js index 645ea0b..78408fe 100644 --- a/type/__tests__/Left.test.js +++ b/type/__tests__/Left.test.js @@ -105,6 +105,13 @@ describe('type/Left', () => { expect(actual).toBe(expected) }) + test('getValue', () => { + expect.assertions(1) + const expected = 888 + const actual = Left(expected).getValue(err => err, () => 123) + expect(actual).toBe(expected) + }) + test('Left(abc).inspect', () => { expect.assertions(1) const expected = 'Left ("abc")' diff --git a/type/__tests__/Nothing.test.js b/type/__tests__/Nothing.test.js index 0f0af50..6b08c83 100644 --- a/type/__tests__/Nothing.test.js +++ b/type/__tests__/Nothing.test.js @@ -58,6 +58,12 @@ describe('type/Nothing', () => { expect(actual).toBe(expected) }) + test('getValue', () => { + expect.assertions(1) + const expected = 'abc' + const actual = Nothing.getValue(() => expected, () => 123) + expect(actual).toBe(expected) + }) test('fantasy-land/map', () => { expect.assertions(1) diff --git a/type/__tests__/Right.test.js b/type/__tests__/Right.test.js index 4812602..ef1ce2d 100644 --- a/type/__tests__/Right.test.js +++ b/type/__tests__/Right.test.js @@ -84,6 +84,13 @@ describe('type/Right', () => { expect(actual).toBe(expected) }) + test('getValue', () => { + expect.assertions(1) + const expected = 888 + const actual = Right(expected).getValue(() => 123, data => data) + expect(actual).toBe(expected) + }) + test('Right(abc).inspect', () => { expect.assertions(1) const expected = 'Right ("abc")'