This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from mstade/and
And
- Loading branch information
Showing
5 changed files
with
129 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module.exports = require('./variadic')(and) | ||
|
||
function and(x, rest) { | ||
if (x === undefined && isEmpty(rest)) return true | ||
|
||
var result = val(x) | ||
|
||
if (isnt(result) || isnt(rest)) return result | ||
|
||
rest.every(function(x) { | ||
return is(result = val(x)) | ||
}) | ||
|
||
return result | ||
} | ||
|
||
const isEmpty = require('./isEmpty') | ||
, isnt = require('./isnt') | ||
, val = require('./val') | ||
, is = require('./is') |
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,50 @@ | ||
const constantly = require('../lib/constantly') | ||
, expect = require('must') | ||
, and = require('../lib/and') | ||
|
||
describe('`and`', function() { | ||
describe('given zero arguments', function() { | ||
it('should return `true`', function() { | ||
expect(and()).to.equal(true) | ||
}) | ||
}) | ||
|
||
describe('given one argument', function() { | ||
it('should return the value of that argument', function() { | ||
expect(and(1)).to.equal(1) | ||
expect(and(0)).to.equal(0) | ||
expect(and(false)).to.equal(false) | ||
}) | ||
|
||
describe('and when that argument is a function', function() { | ||
it('should return the value of calling that function', function() { | ||
expect(and(constantly('wibble'))).to.equal('wibble') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('given two or more arguments', function() { | ||
describe('when the values of all arguments are logically true', function() { | ||
it('should return the last supplied value', function() { | ||
expect(and(0, 1, 2, 3)).to.equal(3) | ||
}) | ||
}) | ||
|
||
describe('when any one argument is logically false', function() { | ||
it('should return the value that was logically false', function() { | ||
expect(and(0, null, 1)).to.equal(null) | ||
expect(and(false, true, 1)).to.equal(false) | ||
expect(and(0, true, undefined)).to.equal(undefined) | ||
}) | ||
}) | ||
|
||
describe('when an argument is a function', function() { | ||
it('should call the function and evaluate its return value', function() { | ||
var called = false | ||
|
||
expect(and(0, function() { return (called = true) }, 1, 'yup')).to.equal('yup') | ||
expect(called).to.be.true | ||
}) | ||
}) | ||
}) | ||
}) |
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