Browse Source

Merge pull request #1 from Floofies/master

Added `forField`, `subsetOf`, `is`, Refactor
develop
Muthu Kumar 7 years ago
committed by GitHub
parent
commit
9438fe265b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 184
      Select.js
  2. 19
      spec/SelectArraySpec.js
  3. 24
      spec/SelectSpec.js

184
Select.js

@ -1,12 +1,11 @@
'use strict';
/**
* Return a function based on a condition.
* Functional alternative to switch-case.
* @projectname Select-Return
* @version 1.0.0
* @author Muthu Kumar (@MKRhere)
*/
* Return a function based on a condition.
* Functional alternative to switch-case.
* @projectname Select-Return
* @version 1.0.0
* @author Muthu Kumar (MKRhere)
*/
/**
* Creates a SelectValue instance with a value and optional resolve function.
@ -23,7 +22,6 @@ class SelectValue {
this.value = value;
if (resolve) this.resolve = (...args) => resolve(...args, value);
}
/**
* Default resolve prototype. Returns null when called.
* Used in case a resolve is never set.
@ -36,61 +34,94 @@ class SelectValue {
}
/**
* Creates a SelectIterable instance from an array.
* Created internally from Select.prototype.for, and not exported.
* @class SelectIterable
*/
* Creates a SelectIterable instance from an array.
* Created internally from Select.prototype.for, and not exported.
* @class SelectIterable
* @param {array} values - array created from Select.prototype.for
* @param {Array<function>} tests - array of { test, consequent } objects
* @param {function} tests[].test - test function
* @param {function} tests[].consequent - consequent function
* @constructs SelectIterable
*/
class SelectIterable {
/**
* @param {array} values - array created from Select.prototype.for
* @param {Array<function>} tests - array of { test, consequent } objects
* @param {function} tests[].test - test function
* @param {function} tests[].consequent - consequent function
* @constructs SelectIterable
*/
constructor(values, tests) {
this.values = values
.map(x => x instanceof SelectValue
? x
: new SelectValue(x)
);
this.tests = tests;
constructor(values, conditionals) {
this.values = values.map(value => value instanceof SelectValue ? value : new SelectValue(value));
this.conditionals = conditionals;
}
/**
* Accepts a test and consequent function each and returns a new
* SelectIterable instance.
* @param {Test} test - test callback function
* @param {function} consequent - consequent callback function
* @returns {SelectIterable} - an instance of SelectIterable
* @memberof SelectIterable
*/
for(test, consequent) {
/* SelectIterable.prototype.for works a little
differently than Select.prototype.for,
by accumulating the tests and resolving
all the values when .resolve() is called */
* Accepts a test and consequent function each and returns a new
* SelectIterable instance.
* SelectIterable.prototype.for works a little differently than
* Select.prototype.for, by accumulating the tests and resolving all the
* values when .resolve() is called.
* @param {callback} predicate A test callback function.
* @callback predicate
* @param {any} value The Selected value.
* @returns {boolean} The Boolean result of the test.
* @param {callback} consequent consequent callback function
* @callback consequent
* @param {Array} args An arbitrary Array of arguments.
* @param {any} value The Selected value.
* @returns {any}
* @returns {SelectIterable} an instance of SelectIterable
* @memberof SelectIterable
*/
if(predicate, consequent) {
return new SelectIterable(this.values, [
...this.conditionals,
{predicate, consequent}
]);
}
forField(...pairs) {
var pair;
var callback;
var even;
pairs.forEach((pair, index) => {
even = index % 2 === 0;
pair = this.conditionals[this.conditionals.length];
if (pair.length === 2) {
this.conditionals.push({});
};
callback = conditionals[loc];
if (typeof callback === "object") {
if (Array.isArray(callback)) {
pair.predicate = callback[0];
pair.consequent = callback[0];
} else {
Object.assign(pair, {
predicate: callback.predicate,
consequent: callback.consequent
});
}
continue;
}
if (even) pair.predicate = callback;
if (!even) pair.consequent = callback;
}, this);
return new SelectIterable(
this.values,
[ ...this.tests, { test, consequent } ]
[...this.conditionals, ...conditionals]
);
}
resolve(...args) {
/* When .resolve() is called, a resolved value
is generated for each value in the array */
return this.values.map(item => {
const resolver = this
.tests
.find(x => x.test(item.value)
? x.consequent
: null);
return resolver
? resolver.consequent(...args, item.value)
: null;
});
subset(value, consequent) {
return this.if(this.value.every(element => element in value))
}
superset(value, consequent) {
return this.if(value.every(element => element in this.value));
}
}
SelectIterable.prototype.resolve = function (...args) {
return this.values.map(item => {
const resolver = this.conditionals.find(conditional =>
conditional.predicate(item.value)
? conditional.consequent
: null
);
return resolver
? resolver.consequent(...args, this.value)
: null;
}, this);
};
/**
* Creates a new Select instance.
@ -107,33 +138,46 @@ class Select extends SelectValue {
super(value, resolve);
this.iterable = typeof value === "object" && Symbol.iterator in value;
}
/**
* Accepts a test and consequent function each and returns a new
* Select or SelectIterable instance.
* @param {function} test - test callback function
* @param {function} consequent - consequent callback function
* @returns {Select|SelectIterable} - Returns a SelectIterable instance
* if value was array, or a Select instance otherwise
* @memberof Select
*/
for(test, consequent) {
* Accepts a test and consequent function each and returns a new
* Select or SelectIterable instance.
* @param {callback} predicate A test callback function.
* @callback predicate
* @param {any} value The Selected value.
* @returns {boolean} The Boolean result of the test.
* @param {callback} consequent consequent callback function
* @callback consequent
* @param {Array} args An arbitrary Array of arguments.
* @param {any} value The Selected value.
* @returns {any}
* @returns {(Select|SelectIterable)} - Returns a SelectIterable instance
* if value was array, or a Select instance otherwise
* @memberof Select
*/
if(predicate, consequent) {
if (this.iterable) {
/* If the value passed to the constructor is
an array, initialise a new SelectIterable
with the array and { test, consequent } pair
and return */
/* If the value passed to the constructor is an
iterable, return a new SelectIterable using the
iterable and { test, consequent } pair.*/
return new SelectIterable(
Array.from(this.value),
[ { test, consequent } ],
[{ predicate, consequent }]
);
}
if (test(this.value)) return new Select(this.value, consequent);
if (predicate(this.value)) {
return new Select(this.value, consequent);
}
/* If the test doesn't pass, just pass the Select
instance along the chain until a test passes,
or .resolve() is called */
return this;
}
is(value, consequent) {
return this.if(value === this.value, consequent);
}
isNot(value, consequent) {
return this.if(value !== this.value, consequent)
}
}
module.exports = Select;

19
spec/SelectArraySpec.js

@ -11,14 +11,15 @@ describe("Select", () => {
const a = [ 10, 20, 0, 'UnexpectedString' ];
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',
null ]);
.if(x => x > 10, () => 'Greater than 10')
.if(x => x < 10, () => 'Lesser than 10')
.if(x => x === 10, () => `Is 10`);
console.log(result.resolve());
expect(result.resolve()).toEqual([
'Is 10',
'Greater than 10',
'Lesser than 10',
null
]);
});
});

24
spec/SelectSpec.js

@ -9,9 +9,9 @@ describe("Select", () => {
const a = 10;
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');
.if(x => x > 10, () => 'Greater than 10')
.if(x => x < 10, () => 'Lesser than 10')
.if(x => x === 10, () => 'Is 10');
expect(result.resolve()).toBe('Is 10');
});
@ -20,9 +20,9 @@ describe("Select", () => {
const a = 1;
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`);
.if(x => x > 10, () => 'Greater than 10')
.if(x => x < 10, () => 'Lesser than 10')
.if(x => x === 10, () => `Is 10`);
expect(result.resolve()).toBe('Lesser than 10');
});
@ -31,9 +31,9 @@ describe("Select", () => {
const a = 100;
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`);
.if(x => x > 10, () => 'Greater than 10')
.if(x => x < 10, () => 'Lesser than 10')
.if(x => x === 10, () => `Is 10`);
expect(result.resolve()).toBe('Greater than 10');
});
@ -42,9 +42,9 @@ describe("Select", () => {
const a = 'UnexpectedString';
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`);
.if(x => x > 10, () => 'Greater than 10')
.if(x => x < 10, () => 'Lesser than 10')
.if(x => x === 10, () => `Is 10`);
expect(result.resolve()).toBe(null);
});

Loading…
Cancel
Save