|
|
@ -1,49 +1,75 @@ |
|
|
|
'use strict'; |
|
|
|
|
|
|
|
/** |
|
|
|
* SelectValue - Wraps and pairs an arbitrary value with a reducer. |
|
|
|
* @class SelectValue |
|
|
|
* @param {any} value An arbitrary value to store within. |
|
|
|
* @param {(Function|null)} resolve The reducer. |
|
|
|
*/ |
|
|
|
class SelectValue { |
|
|
|
constructor(value, resolve) { |
|
|
|
constructor(value, resolve = null) { |
|
|
|
this.value = value; |
|
|
|
if (resolve) this.resolve = resolve; |
|
|
|
if (resolve !== null) 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; |
|
|
|
constructor(values, conditionals) { |
|
|
|
this.values = values.map(value => value instanceof SelectValue ? value : new SelectValue(value)); |
|
|
|
this.conditionals = conditionals; |
|
|
|
} |
|
|
|
|
|
|
|
for(test, consequent) { |
|
|
|
const self = this; |
|
|
|
return new SelectIterable( |
|
|
|
self.values, |
|
|
|
[ ...self.tests, { test, consequent } ] |
|
|
|
); |
|
|
|
for(predicate, consequent) { |
|
|
|
return new SelectIterable(this.values, [...this.conditionals, { predicate, consequent }]); |
|
|
|
} |
|
|
|
forField(...pairs) { |
|
|
|
var pair; |
|
|
|
var callback; |
|
|
|
var even; |
|
|
|
for ( |
|
|
|
// beforeBegin:
|
|
|
|
var loc = 0; |
|
|
|
// beforeEvery:
|
|
|
|
even = loc % 2 === 0, |
|
|
|
// breakPredicate:
|
|
|
|
loc < pairs.length; |
|
|
|
// afterEvery:
|
|
|
|
loc++ |
|
|
|
) { |
|
|
|
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; |
|
|
|
} |
|
|
|
return new SelectIterable(this.values, [...this.conditionals, ...conditionals]); |
|
|
|
} |
|
|
|
subsetOf(value, consequent) { |
|
|
|
return this.for(this.value.every(element => element in this.value)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/* 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; |
|
|
|
}); |
|
|
|
return this.values.map(item => { |
|
|
|
const resolve = this.conditionals.find(tuple => tuple.predicate(item.value) ? tuple.consequent : null); |
|
|
|
return resolve ? resolve.consequent(...args, this.value) : () => null; |
|
|
|
}, this); |
|
|
|
}; |
|
|
|
|
|
|
|
class Select extends SelectValue { |
|
|
@ -51,17 +77,16 @@ class Select extends SelectValue { |
|
|
|
super(value, resolve); |
|
|
|
this.iterable = typeof value === "object" && Symbol.iterator in value; |
|
|
|
} |
|
|
|
|
|
|
|
for(test, consequent) { |
|
|
|
for(predicate, consequent) { |
|
|
|
if (this.iterable) { |
|
|
|
return new SelectIterable( |
|
|
|
Array.from(this.value), |
|
|
|
[ { test, consequent } ], |
|
|
|
); |
|
|
|
return new SelectIterable(Array.from(this.value), [{ predicate, consequent }]); |
|
|
|
} |
|
|
|
if (test(this.value)) return new Select(this.value, consequent); |
|
|
|
if (predicate(this.value)) return new Select(this.value, consequent); |
|
|
|
return this; |
|
|
|
} |
|
|
|
is(value, consequent) { |
|
|
|
return this.for(value === this.value, consequent); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
module.exports = Select; |
|
|
|