FunctionSelect: Select a function that passes a condition. A functional alternative to switch-case.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

190 lines
4.9 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: Select.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: Select.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>'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)
*/
/**
* Creates a SelectValue instance with a value and optional resolve function.
* Created internally from SelectIterable constructor, and not exported.
* @class SelectValue
*/
class SelectValue {
/**
* @param {any} value - the input value
* @param {function} resolve - optional resolve function
* @constructs SelectIterable
*/
constructor(value, resolve) {
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.
* @returns {object} null
* @memberof SelectValue
*/
resolve() {
return null;
}
}
/**
* Creates a SelectIterable instance from an array.
* Created internally from Select.prototype.for, and not exported.
* @class SelectIterable
*/
class SelectIterable {
/**
* @param {array} values - array created from Select.prototype.for
* @param {Array&lt;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;
}
/**
* 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 */
return new SelectIterable(
this.values,
[ ...this.tests, { test, consequent } ]
);
}
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;
});
}
}
/**
* Creates a new Select instance.
* @class Select
* @extends {SelectValue}
*/
class Select extends SelectValue {
/**
* @param {any|array} value - the value or array of values to check against
* @param {function} resolve - optional resolve function
* @constructs Select
*/
constructor(value, resolve) {
super(value, resolve);
this.iterable = typeof value === "object" &amp;&amp; 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) {
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 */
return new SelectIterable(
Array.from(this.value),
[ { test, consequent } ],
);
}
if (test(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;
}
}
module.exports = Select;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Select.html">Select</a></li><li><a href="SelectIterable.html">SelectIterable</a></li><li><a href="SelectValue.html">SelectValue</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Apr 10 2018 04:36:07 GMT+0530 (IST)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>