Skip to content

Commit

Permalink
Modularise nth, pluck, range and reverse functions
Browse files Browse the repository at this point in the history
  • Loading branch information
svozza committed Feb 12, 2016
1 parent 3874f03 commit 4dc3c0a
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 11 deletions.
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const includes = require('./includes')
const init = require('./init')
const last = require('./last')
const length = require('./length')
const nth = require('./nth')
const pluck = require('./pluck')
const range = require('./range')
const reverse = require('./reverse')

const f2 = curry((name, arg, coll) => {
return coll[name](arg)
Expand All @@ -31,10 +35,10 @@ ex.includes = includes
ex.init = init
ex.last = last
ex.length = length
ex.nth = curry((i, list) => list[i])
ex.pluck = curry((key, list) => list.map(o => o[key]))
ex.range = curry((start, end) => Array(end - start).fill(1).map((e, i) => i + start))
ex.reverse = list => [].concat(list).reverse()
ex.nth = nth
ex.pluck = pluck
ex.range = range
ex.reverse = reverse
ex.sort = list => [].concat(list).sort()
ex.sortBy = curry((f, list) => [].concat(list).sort(f))
ex.sum = list => list.reduce((sum, e) => sum + e, 0)
Expand Down
3 changes: 3 additions & 0 deletions nth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const curry = require('./curry')

module.exports = curry((i, xs) => xs[i])
3 changes: 3 additions & 0 deletions pluck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const curry = require('./curry')

module.exports = curry((key, xs) => xs.map(o => o[key]).filter(x => x != null))
3 changes: 3 additions & 0 deletions range.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const curry = require('./curry')

module.exports = curry((start, end) => Array(end - start).fill(1).map((e, i) => i + start))
1 change: 1 addition & 0 deletions reverse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = xs => [].concat(xs).reverse()
5 changes: 5 additions & 0 deletions test/append.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const assert = require('assert')
const append = require('../append.js')
const bf = require('../index.js')

describe('append', () => {

Expand All @@ -17,4 +18,8 @@ describe('append', () => {
assert.deepEqual(append(5, [1, 2, 3, 4]), [1, 2, 3, 4 ,5])
})

it('is exported from index', () => {
assert.deepEqual(bf.append(5, [1, 2, 3, 4]), [1, 2, 3, 4 ,5])
})

})
15 changes: 15 additions & 0 deletions test/nth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const assert = require('assert')
const nth = require('../nth.js')
const bf = require('../index.js')

describe('nth', () => {

it('is curried', () => {
assert.strictEqual(nth(2)([1, 2, 3, 4]), 3);
})

it('should retrieve third item in a list', () => {
assert.strictEqual(nth(2, [1, 2, 3, 4, 5]), 3)
})

})
16 changes: 13 additions & 3 deletions test/pluck.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
const assert = require('assert')
const pluck = require('../pluck.js')
const bf = require('../index.js')

describe('pluck', () => {

it('is curried', () => {
assert.deepEqual(pluck('a')([{a: 1}, {a: 2}, {a: 3}]), [1, 2, 3])
})

it('should pluck values from list of objects', () => {
const list = [{a: 1}, {a: 2}, {a: 3}]
assert.deepEqual(bf.pluck('a', list), [1, 2, 3])
const list = [{a: 1}, {b: 2}, {a: 3}]
assert.deepEqual(pluck('a', list), [1, 3])
})

it('is exported from the index file', () => {
const list = [{a: 1}, {b: 2}, {a: 3}]
assert.deepEqual(bf.pluck('a', list), [1, 3])
})

})
})
19 changes: 19 additions & 0 deletions test/range.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const assert = require('assert')
const range = require('../range.js')
const bf = require('../index.js')

describe('range', () => {

it('is curried', () => {
assert.deepEqual(range(1)(5), [1, 2, 3, 4])
})

it('should create a range from 1 to 5 exclusive', () => {
assert.deepEqual(range(1, 5), [1, 2, 3, 4])
})

it('is exported from index', () => {
assert.deepEqual(bf.range(1, 5), [1, 2, 3, 4])
})

})
14 changes: 10 additions & 4 deletions test/reverse.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
const assert = require('assert')
const reverse = require('../reverse.js')
const bf = require('../index.js')

describe('reverse', () => {

it('should reverse list', () => {
it('should not mutate the list', () => {
const list = [1, 2, 3, 4, 5]
assert.deepEqual(bf.reverse(list), [5, 4, 3, 2, 1])
assert.notEqual(reverse(list), list)
})

it('should not mutate argument', () => {
it('should reverse the list', () => {
const list = [1, 2, 3, 4, 5]
assert.notEqual(bf.reverse(list), list)
assert.deepEqual(reverse(list), [5, 4, 3, 2, 1])
})

it('should be exported from index', () => {
const list = [1, 2, 3, 4, 5]
assert.deepEqual(bf.reverse(list), [5, 4, 3, 2, 1])
})
})

0 comments on commit 4dc3c0a

Please sign in to comment.