Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #23 from mstade/inc-dec
Browse files Browse the repository at this point in the history
Add inc/dev functions
  • Loading branch information
mstade committed Jan 31, 2015
2 parents e2130c9 + 2159e07 commit 61cac6a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 6 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ module.exports =
, compose : require('./lib/compose')
, constantly : require('./lib/constantly')
, count : require('./lib/count')
, dec : require('./lib/dec')
, each : require('./lib/each')
, eq : require('./lib/eq')
, gt : require('./lib/gt')
, get : require('./lib/get')
, identity : require('./lib/identity')
, inc : require('./lib/inc')
, is : require('./lib/is')
, isEmpty : require('./lib/isEmpty')
, isnt : require('./lib/isnt')
Expand Down
13 changes: 13 additions & 0 deletions lib/dec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = dec

function dec(x) {
var n = val(x)

assert(is(Number, n), "Expected number, got: " + x)

return n - 1
}

var assert = require('./assert')
, val = require('./val')
, is = require('./is')
13 changes: 13 additions & 0 deletions lib/inc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = inc

function inc(x) {
var n = val(x)

assert(is(Number, n), "Expected number, got: " + x)

return n + 1
}

var assert = require('./assert')
, val = require('./val')
, is = require('./is')
31 changes: 31 additions & 0 deletions test/dec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var constantly = require('../lib/constantly')
, expect = require('must')
, range = require('../lib/range')
, each = require('../lib/each')
, dec = require('../lib/dec')
, $ = require('../lib/partial')

describe('dec', function() {
describe('when given a number `n`', function() {
it('should decrement that number by one and return the result', function() {
each(range(100), function(n) {
expect(dec(n)).to.equal(n - 1)
})
})
})

describe('when given a function', function() {
it('should call the function and try to decrement the return value', function() {
var three = constantly(3)
expect(dec(three)).to.equal(2)
})
})

describe('when given a value that is not a number', function() {
it('should throw', function() {
each([ 'foo', true, false, null, undefined, NaN ], function(x) {
expect($(dec, x)).to.throw()
})
})
})
})
14 changes: 8 additions & 6 deletions test/funkis.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ var fs = require('fs')
, path = require('path')
, each = require('../lib/each')
, funkis = require('../')
, expect = require('expect.js')
, expect = require('must')

describe('funkis', function() {
it('should export every function in lib/', function() {
var names = fs.readdirSync(path.join(__dirname, '../lib'))
.map(function(file) {
var module = path.basename(file, '.js')
expect(funkis).to.have.key(module)
})
var names = fs.readdirSync(path.join(__dirname, '../lib')).map(function(file) {
return path.basename(file, '.js')
})

each(names, function(name) {
expect(funkis).to.have.property(name)
})
})
})
31 changes: 31 additions & 0 deletions test/inc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var constantly = require('../lib/constantly')
, expect = require('must')
, range = require('../lib/range')
, each = require('../lib/each')
, inc = require('../lib/inc')
, $ = require('../lib/partial')

describe('inc', function() {
describe('when given a number `n`', function() {
it('should increment that number by one and return the result', function() {
each(range(100), function(n) {
expect(inc(n)).to.equal(n + 1)
})
})
})

describe('when given a function', function() {
it('should call the function and try to increment the return value', function() {
var three = constantly(3)
expect(inc(three)).to.equal(4)
})
})

describe('when given a value that is not a number', function() {
it('should throw', function() {
each([ 'foo', true, false, null, undefined, NaN ], function(x) {
expect($(inc, x)).to.throw()
})
})
})
})

0 comments on commit 61cac6a

Please sign in to comment.