Skip to content

Commit

Permalink
Merge pull request #13 from svozza/includes-init-last-length
Browse files Browse the repository at this point in the history
Modularise includes, init, last and length
  • Loading branch information
svozza committed Feb 12, 2016
2 parents ef6c7e7 + cdc08ac commit 3874f03
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 4 deletions.
3 changes: 3 additions & 0 deletions includes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const curry = require('./curry')

module.exports = curry((e, xs) => xs.indexOf(e) !== -1)
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const curry = require('./curry')
const append = require('./append')
const drop = require('./drop')
const head = require('./head')
const includes = require('./includes')
const init = require('./init')
const last = require('./last')
const length = require('./length')

const f2 = curry((name, arg, coll) => {
return coll[name](arg)
Expand All @@ -23,10 +27,10 @@ arg2.forEach(name => ex[name] = f3(name))
ex.append = append
ex.drop = drop
ex.head = head
ex.includes = curry((e, list) => list.indexOf(e) !== -1)
ex.init = list => list.slice(0, list.length - 1)
ex.last = list => list[list.length -1]
ex.length = list => list.length
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))
Expand Down
1 change: 1 addition & 0 deletions init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = xs => xs.slice(0, xs.length - 1)
1 change: 1 addition & 0 deletions last.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = xs => xs[xs.length - 1]
1 change: 1 addition & 0 deletions length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = xs => xs.length
18 changes: 18 additions & 0 deletions test/includes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const assert = require('assert')
const includes = require('../includes.js')

describe('includes', () => {

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

it('should indicate if the element is in the list', () => {
assert.strictEqual(includes(1, [1, 2, 3, 4]), true)
})

it('should indicate if the element is not in the list', () => {
assert.strictEqual(includes(5, [1, 2, 3, 4]), false)
})

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

describe('init', () => {

it('does not mutate the original list', () => {
const xs = [1, 2, 3, 4]
const initxs = init(xs)
assert.notStrictEqual(xs, initxs)
})

it('should remove the last item of the list', () => {
assert.deepEqual(init([1, 2, 3, 4, 5]), [1, 2, 3, 4])
})

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

describe('last', () => {

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

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

describe('length', () => {

it('should return the length of a list', () => {
assert.strictEqual(length([1, 2, 3, 4, 5]), 5)
})

})

0 comments on commit 3874f03

Please sign in to comment.