Skip to content

Commit

Permalink
fix Just and Nothing toString methods to return value.
Browse files Browse the repository at this point in the history
  • Loading branch information
joelnet committed Oct 24, 2018
1 parent 102edde commit 5fb6ef2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mojiscript",
"version": "0.8.1",
"version": "0.9.0",
"description": "MojiScript is an Async First, opinionated, and functional language designed to have 100% compatibility with JavaScript engines.",
"repository": {
"type": "git",
Expand Down
8 changes: 2 additions & 6 deletions type/Just.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ const Just = value => Object.create(
}
)

function toString() {
return `Just (${JSON.stringify(this.value)})`
}

function map(func) {
return Just(func(this.value))
}
Expand All @@ -23,8 +19,8 @@ const prototype = {
'@@type': typeJust,
map,
'fantasy-land/map': map,
toString,
inspect: toString,
toString() { return this.value.toString() },
inspect() { return `Just (${JSON.stringify(this.value)})` },
toJSON() { return this.value }
}

Expand Down
2 changes: 1 addition & 1 deletion type/Nothing.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { typeNothing } = require('./_allTypes')
const Nothing = Object.freeze(Object.create(
{
'@@type': typeNothing,
toString: () => 'Nothing',
toString: () => '',
inspect: () => 'Nothing',
map: () => Nothing,
'fantasy-land/map': () => Nothing,
Expand Down
25 changes: 23 additions & 2 deletions type/__tests__/Just.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ describe('type/Just', () => {

test('Just(888).toString', () => {
expect.assertions(1)
const expected = 'Just (888)'
const expected = '888'
const actual = Just(888).toString()
expect(actual).toBe(expected)
})

test('Just(abc).toString', () => {
expect.assertions(1)
const expected = 'Just ("abc")'
const expected = 'abc'
const actual = Just('abc').toString()
expect(actual).toBe(expected)
})
Expand All @@ -67,4 +67,25 @@ describe('type/Just', () => {
const actual = Just(888)['@@type']
expect(actual).toBe(expected)
})

test('cast to string', () => {
expect.assertions(1)
const expected = 'a'
const actual = `${Just('a')}`
expect(actual).toBe(expected)
})

test('append strings', () => {
expect.assertions(1)
const expected = 'ab'
const actual = Just('a') + Just('b')
expect(actual).toBe(expected)
})

test('Number(Just()) returns number', () => {
expect.assertions(1)
const expected = 3
const actual = Number(Just(3))
expect(actual).toBe(expected)
})
})
9 changes: 8 additions & 1 deletion type/__tests__/Nothing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('type/Nothing', () => {

test('toString', () => {
expect.assertions(1)
const expected = 'Nothing'
const expected = ''
const actual = Nothing.toString()
expect(actual).toBe(expected)
})
Expand Down Expand Up @@ -60,4 +60,11 @@ describe('type/Nothing', () => {
const actual = Object.keys(Nothing)
expect(actual).toEqual(expected)
})

test('Number(Nothing) returns 0', () => {
expect.assertions(1)
const expected = 0
const actual = Number(Nothing)
expect(actual).toBe(expected)
})
})

0 comments on commit 5fb6ef2

Please sign in to comment.