Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardiwagner committed Nov 15, 2017
1 parent 4a8af53 commit 0e17d4b
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 2,490 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"name": "z",
"version": "1.0.1",
"main": "src/z.js",
"description": "pattern matching for javascript",
"description": "native pattern matching for javascript",
"scripts": {
"test": "NODE_PATH=. mocha **/*.spec.js",
"test:watch": "npm test -- --watch",
Expand Down
6 changes: 3 additions & 3 deletions src/getMatchDetails.js
@@ -1,10 +1,10 @@
const functionReflector = require('js-function-reflector')

module.exports = func => {
const reflectedFunction = functionReflector(func)
module.exports = (matchFunction) => {
const reflectedFunction = functionReflector(matchFunction)

return {
args: reflectedFunction.args,
func
func: matchFunction
}
}
4 changes: 0 additions & 4 deletions src/match.js
Expand Up @@ -13,8 +13,6 @@ module.exports = (match, subjectToMatch) => {
if (matchValue === Boolean && typeof subjectToMatch === 'boolean') {
return option.Some(subjectToMatch)
}
// TODO check these!
// if(matchValue === Null && typeof subjectToMatch === 'string') return option.Some(subjectToMatch)
if (matchValue === undefined && typeof subjectToMatch === 'undefined') {
return option.Some(subjectToMatch)
}
Expand All @@ -24,8 +22,6 @@ module.exports = (match, subjectToMatch) => {
if (matchValue === String && typeof subjectToMatch === 'string') {
return option.Some(subjectToMatch)
}
// TODO check it!
// if(matchValue === Symbol && typeof subjectToMatch === 'string') return option.Some(subjectToMatch)
if (matchValue === Object && typeof subjectToMatch === 'object') {
return option.Some(subjectToMatch)
}
Expand Down
2 changes: 1 addition & 1 deletion src/option.js
@@ -1,4 +1,4 @@
module.exports = {
Some: value => Object({ value, hasValue: true }),
Some: (value) => Object({ value, hasValue: true }),
None: Object()
}
2 changes: 1 addition & 1 deletion src/z.js
Expand Up @@ -40,7 +40,7 @@ const resolveMatchFunctions = (subjectToMatch, functions) => {
}
}

module.exports = subjectToMatch =>
module.exports = (subjectToMatch) =>
function () {
const functions = Object.keys(arguments).map(key => arguments[key])

Expand Down
37 changes: 28 additions & 9 deletions tests/arrays.spec.js
Expand Up @@ -22,57 +22,76 @@ describe('arrays', () => {
})

it('should match single item array with comparsion', () => {
const result = z([1])((x = [2]) => false, (x = [1]) => x)
const result = z([1])(
(x = [2]) => false,
(x = [1]) => x
)

result.should.deep.equal([1])
})

it('should match tail array', () => {
const result = z([1, 2, 3, 4])((x, y, xs) => [x].concat(xs))
const result = z([1, 2, 3, 4])(
(x, y, xs) => [x].concat(xs)
)

result.should.deep.equal([1, 3, 4])
})

it('should match if even with match has more arguments than subject', () => {
const result = z([1])(
(x, y, xs) => false, x => x
(x, y, xs) => false,
x => x
)

result.should.deep.equal([1])
})

it('should extract array from head when has tail argument', () => {
const result = z([1])((x, y, xs) => false, (x, xs = []) => x)
const result = z([1])(
(x, y, xs) => false,
(x, xs = []) => x
)

result.should.equal(1)
})

it('should extract head value of array', () => {
const result = z([1, 2])((x = 1, xs) => x)
const result = z([1, 2])(
(x = 1, xs) => x
)

result.should.equal(1)
})

it('should match array of array on head', () => {
const result = z([[1], [2]])((x = Array, xs) => xs)
const result = z([[1], [2]])(
(x = Array, xs) => xs
)

result.should.deep.equal([[2]])
})

it('should match array of array on tail', () => {
const result = z([[1], [2]])((x, xs = Array) => xs)
const result = z([[1], [2]])(
(x, xs = Array) => xs
)

result.should.deep.equal([[2]])
})

it('should match empty array of on head', () => {
const result = z([[], [2]])((x = [], xs) => xs)
const result = z([[], [2]])(
(x = [], xs) => xs
)

result.should.deep.equal([[2]])
})

it('should match empty array of on tail', () => {
const result = z([1, []])((x, xs = [[]]) => xs)
const result = z([1, []])(
(x, xs = [[]]) => xs
)

result.should.deep.equal([[]])
})
Expand Down
2 changes: 1 addition & 1 deletion src/matchObject.spec.js → tests/matchObject.spec.js
Expand Up @@ -2,7 +2,7 @@ require('chai').should()
const {
getFlattenedKeysFromArgs,
objectAndArgsDestructureMatches
} = require('./matchObject')
} = require('../src/matchObject')

// declared as a single string for readability here
// but the reflection lib will hand it over as the
Expand Down
25 changes: 19 additions & 6 deletions tests/types.spec.js
Expand Up @@ -13,13 +13,17 @@ describe('types', () => {
})

it('should match string type', () => {
const result = z('string')((x = String) => true)
const result = z('string')(
(x = String) => true
)

result.should.equal(true)
})

it('should match bool type', () => {
const result = z(true)((x = Boolean) => true)
const result = z(true)(
(x = Boolean) => true
)

result.should.equal(true)
})
Expand All @@ -35,25 +39,34 @@ describe('types', () => {
})

it('should match object type', () => {
const result = z({ a: 1 })((x = Object) => true, (x = Object) => false)
const result = z({ a: 1 })(
(x = Object) => true,
(x = Object) => false
)

result.should.equal(true)
})

it('should match instance', () => {
const result = z(new Date())((x = Date) => true)
const result = z(new Date())(
(x = Date) => true
)

result.should.equal(true)
})

it('should match single item array', () => {
const result = z([1])((x = [1]) => x)
const result = z([1])(
(x = [1]) => x
)

result.should.deep.equal([1])
})

it('should match array', () => {
const result = z([1])((x = Array) => x)
const result = z([1])(
(x = Array) => x
)

result.should.deep.equal([1])
})
Expand Down

0 comments on commit 0e17d4b

Please sign in to comment.