From cdc08ac78d7f1f1c27ec51ffc7d36eb2c14ca685 Mon Sep 17 00:00:00 2001 From: Stefano Vozza Date: Thu, 11 Feb 2016 21:08:07 +0000 Subject: [PATCH] modularise includes, init, last and length --- includes.js | 3 +++ index.js | 12 ++++++++---- init.js | 1 + last.js | 1 + length.js | 1 + test/includes.js | 18 ++++++++++++++++++ test/init.js | 16 ++++++++++++++++ test/last.js | 10 ++++++++++ test/length.js | 10 ++++++++++ 9 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 includes.js create mode 100644 init.js create mode 100644 last.js create mode 100644 length.js create mode 100644 test/includes.js create mode 100644 test/init.js create mode 100644 test/last.js create mode 100644 test/length.js diff --git a/includes.js b/includes.js new file mode 100644 index 0000000..07ac139 --- /dev/null +++ b/includes.js @@ -0,0 +1,3 @@ +const curry = require('./curry') + +module.exports = curry((e, xs) => xs.indexOf(e) !== -1) diff --git a/index.js b/index.js index f40fdce..443fa8c 100644 --- a/index.js +++ b/index.js @@ -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) @@ -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)) diff --git a/init.js b/init.js new file mode 100644 index 0000000..431992b --- /dev/null +++ b/init.js @@ -0,0 +1 @@ +module.exports = xs => xs.slice(0, xs.length - 1) diff --git a/last.js b/last.js new file mode 100644 index 0000000..ef92881 --- /dev/null +++ b/last.js @@ -0,0 +1 @@ +module.exports = xs => xs[xs.length - 1] \ No newline at end of file diff --git a/length.js b/length.js new file mode 100644 index 0000000..aaf614f --- /dev/null +++ b/length.js @@ -0,0 +1 @@ +module.exports = xs => xs.length \ No newline at end of file diff --git a/test/includes.js b/test/includes.js new file mode 100644 index 0000000..69743bd --- /dev/null +++ b/test/includes.js @@ -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) + }) + +}) \ No newline at end of file diff --git a/test/init.js b/test/init.js new file mode 100644 index 0000000..a3dad23 --- /dev/null +++ b/test/init.js @@ -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]) + }) + +}) \ No newline at end of file diff --git a/test/last.js b/test/last.js new file mode 100644 index 0000000..c9dc81b --- /dev/null +++ b/test/last.js @@ -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) + }) + +}) \ No newline at end of file diff --git a/test/length.js b/test/length.js new file mode 100644 index 0000000..8e9f441 --- /dev/null +++ b/test/length.js @@ -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) + }) + +}) \ No newline at end of file