From f8a5159da94ba517ada08521706d85310d9951d9 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Sun, 8 Apr 2018 20:14:13 +0530 Subject: [PATCH] [select] select-return now works for arrays and iterables. //TODO: * Documentation --- .eslintrc.js | 1 - Select.js | 60 +++++++++++++++++++++++++++++++++++++++++++------ spec/SelectArraySpec.js | 20 +++++++++++++++++ 3 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 spec/SelectArraySpec.js diff --git a/.eslintrc.js b/.eslintrc.js index 532dad6..5f62d38 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -29,7 +29,6 @@ module.exports = { camelcase: 'warn', 'capitalized-comments': 'error', 'class-methods-use-this': 'error', - 'comma-dangle': 'error', 'comma-spacing': 'error', 'comma-style': 'error', complexity: 'error', diff --git a/Select.js b/Select.js index f1588fb..d0c9baf 100644 --- a/Select.js +++ b/Select.js @@ -1,21 +1,67 @@ 'use strict'; -const isIterable = val => typeof val === "object" && Symbol.iterator in val; - -class Select { +class SelectValue { constructor(value, resolve) { this.value = value; - this.iterable = isIterable(value); if (resolve) this.resolve = resolve; } +} + +SelectValue.prototype.resolve = () => null; + +class SelectIterable { + constructor(values, tests) { + this.values = values + .map(x => x instanceof SelectValue + ? x + : new SelectValue(x) + ); + this.tests = tests; + } + + for(test, consequent) { + const self = this; + return new SelectIterable( + self.values, + [ ...self.tests, { test, consequent } ] + ); + } +} + +/* eslint-disable-next-line func-names */ +SelectIterable.prototype.resolve = function (...args) { + const self = this; + return self.values.map(item => { + const resolve = self + .tests + /* eslint-disable-next-line */ + .find(x => { + return x.test(item.value) + ? x.consequent + : null; + }); + return resolve + ? resolve.consequent(...args, self.value) + : () => null; + }); +}; + +class Select extends SelectValue { + constructor(value, resolve) { + super(value, resolve); + this.iterable = typeof value === "object" && Symbol.iterator in value; + } for(test, consequent) { + if (this.iterable) { + return new SelectIterable( + Array.from(this.value), + [ { test, consequent } ], + ); + } if (test(this.value)) return new Select(this.value, consequent); - if (this.resolve) return this; return this; } } -Select.prototype.resolve = () => null; - module.exports = Select; diff --git a/spec/SelectArraySpec.js b/spec/SelectArraySpec.js new file mode 100644 index 0000000..8aad699 --- /dev/null +++ b/spec/SelectArraySpec.js @@ -0,0 +1,20 @@ +'use strict'; + +const Select = require('../Select'); + +/* global describe it expect */ +describe("Select", () => { + + it("Should return array [ 'Is 10', 'Greater than 10', 'Lesser than 10' ]", + () => { + const a = [ 10, 20, 0 ]; + + const result = new Select(a) + .for(x => x > 10, () => 'Greater than 10') + .for(x => x < 10, () => 'Lesser than 10') + .for(x => x === 10, () => `Is 10`); + + expect(result.resolve()) + .toEqual([ 'Is 10', 'Greater than 10', 'Lesser than 10' ]); + }); +});