From ffc5ceae8cdecd2f0d213e06d43980182f1f975e Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Wed, 11 Apr 2018 11:03:10 +0530 Subject: [PATCH] [docs] Generated JSDoc --- docs/Fuse.html | 840 ++++++++++++++++++++++++++++++++++++++++++++++++ docs/FuseItem.html | 593 ++++++++++++++++++++++++++++++++++ docs/FuseIterable.html | 844 +++++++++++++++++++++++++++++++++++++++++++++++++ docs/global.html | 474 +++++++++++++++++++++++++++ docs/index.html | 29 +- docs/index.js.html | 233 ++++++++++++++ 6 files changed, 2999 insertions(+), 14 deletions(-) create mode 100644 docs/Fuse.html create mode 100644 docs/FuseItem.html create mode 100644 docs/FuseIterable.html create mode 100644 docs/global.html create mode 100644 docs/index.js.html diff --git a/docs/Fuse.html b/docs/Fuse.html new file mode 100644 index 0000000..56e66f0 --- /dev/null +++ b/docs/Fuse.html @@ -0,0 +1,840 @@ + + + + + JSDoc: Class: Fuse + + + + + + + + + + +
+ +

Class: Fuse

+ + + + + + +
+ +
+ +

Fuse()

+ + +
+ +
+
+ + + + + + +

new Fuse()

+ + + + + + +
+ Creates a new Fuse instance. +
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + +

Extends

+ + + + + + + + + + + + + + + + + + + + + + +

Methods

+ + + + + + + +

is(value, consequent) → {Fuse}

+ + + + + + +
+ Accepts a value instead of a test function, and checks for strict +equality with this.value. +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
value + + +any + + + + Any value to check against this.value
consequent + + +function + + + + Consequent callback function
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+ An instance of Fuse +
+ + + +
+
+ Type +
+
+ +Fuse + + +
+
+ + + + + + + + + + + + + +

not(value, consequent) → {Fuse}

+ + + + + + +
+ Accepts a value instead of a test function, and checks for strict +inequality with this.value. +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
value + + +any + + + + Any value to check against this.value
consequent + + +function + + + + Consequent callback function
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+ An instance of Fuse +
+ + + +
+
+ Type +
+
+ +Fuse + + +
+
+ + + + + + + + + + + + + +

on(predicate, consequent) → {Fuse}

+ + + + + + +
+ Accepts a test and consequent function each and returns a new +Fuse or FuseIterable instance. +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
predicate + + +callback + + + + A test callback function
consequent + + +callback + + + + Consequent callback function
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+ Returns a new Fuse instance +
+ + + +
+
+ Type +
+
+ +Fuse + + +
+
+ + + + + + + + + + + + + +

resolve() → {null}

+ + + + + + +
+ Fallback resolve prototype. Returns null when called. +Used in case a resolve is never found. +
+ + + + + + + + + + + + + +
+ + + + + + +
Inherited From:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+ null +
+ + + +
+
+ Type +
+
+ +null + + +
+
+ + + + + + + + + + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/docs/FuseItem.html b/docs/FuseItem.html new file mode 100644 index 0000000..c41b9c6 --- /dev/null +++ b/docs/FuseItem.html @@ -0,0 +1,593 @@ + + + + + JSDoc: Class: FuseItem + + + + + + + + + + +
+ +

Class: FuseItem

+ + + + + + +
+ +
+ +

FuseItem()

+ + +
+ +
+
+ + + + + + +

new FuseItem()

+ + + + + + +
+ Creates a FuseItem instance with a value and optional resolve function. +FuseIterable constructor uses it internally, and Fuse extends FuseItem. +Not exposed. +
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + +

Methods

+ + + + + + + +

resolve() → {null}

+ + + + + + +
+ Fallback resolve prototype. Returns null when called. +Used in case a resolve is never found. +
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+ null +
+ + + +
+
+ Type +
+
+ +null + + +
+
+ + + + + + + + + + + + + +
+ +
+ + + + + + + +
+ +
+ +

FuseItem(value, resolve)

+ + +
+ +
+
+ + + + + + +

new FuseItem(value, resolve)

+ + + + + + + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
value + + +any + + + + The input value
resolve + + +function + + + + Optional resolve function
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + +

Methods

+ + + + + + + +

resolve() → {null}

+ + + + + + +
+ Fallback resolve prototype. Returns null when called. +Used in case a resolve is never found. +
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+ null +
+ + + +
+
+ Type +
+
+ +null + + +
+
+ + + + + + + + + + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/docs/FuseIterable.html b/docs/FuseIterable.html new file mode 100644 index 0000000..b905ca3 --- /dev/null +++ b/docs/FuseIterable.html @@ -0,0 +1,844 @@ + + + + + JSDoc: Class: FuseIterable + + + + + + + + + + +
+ +

Class: FuseIterable

+ + + + + + +
+ +
+ +

FuseIterable(values, tests)

+ +
FuseIterable
+ + +
+ +
+
+ + + + +

Constructor

+ + + +

new FuseIterable(values, tests)

+ + + + + + + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
values + + +iterable + + + + An iterable expression as the switch
tests + + +Array.<function()> + + + + Array of { test, consequent } objects +
Properties
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
predicate + + +function + + + + Test function
consequent + + +function + + + + Consequent function
+ +
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + +

Methods

+ + + + + + + +

on(predicate, consequent) → {FuseIterable}

+ + + + + + +
+ Accepts a test and consequent function each and returns a new +FuseIterable instance. +FuseIterable.prototype.for works a little differently than +Fuse.prototype.for, by lazy accumulating the tests and +resolving all the values when .resolve() is called. +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
predicate + + +callback + + + + A test callback function
consequent + + +callback + + + + Consequent callback function
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+ An instance of FuseIterable +
+ + + +
+
+ Type +
+
+ +FuseIterable + + +
+
+ + + + + + + + + + + + + +

onField(…tuples) → {FuseIterable}

+ + + + + + +
+ Accepts a list of tuples as arguments and returns a new +FuseIterable instance. An alternative to chaining multiple +FuseIterable.prototype.on methods. +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
tuples + + +Array.<function()> + + + + + + + + + + <repeatable>
+ +
Array of [ predicate, consequent ] pairs
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+ An instance of FuseIterable +
+ + + +
+
+ Type +
+
+ +FuseIterable + + +
+
+ + + + + + + + + + + + + +

resolve(…args) → {any|null}

+ + + + + + +
+ Accepts parameters during resolve time and passes them along with +each value into the winning consequent function. +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
args + + +any + + + + + + + + + + <repeatable>
+ +
Any number of arguments
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+ Resolved value or null if it was unresolved +
+ + + +
+
+ Type +
+
+ +any +| + +null + + +
+
+ + + + + + + + + + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/docs/global.html b/docs/global.html new file mode 100644 index 0000000..3f21817 --- /dev/null +++ b/docs/global.html @@ -0,0 +1,474 @@ + + + + + JSDoc: Global + + + + + + + + + + +
+ +

Global

+ + + + + + +
+ +
+ +

+ + +
+ +
+
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + +
+ + + + + + + + + + + + + + + + + + +

Type Definitions

+ + + + + + + +

consequent(value, …args) → {any}

+ + + + + + + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
value + + +any + + + + + + + + + + The selected Fuse value
args + + +any + + + + + + + + + + <repeatable>
+ +
An arbitrary array of arguments
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + + + +
+
+ Type +
+
+ +any + + +
+
+ + + + + + + + + + + + + +

predicate(value) → {boolean}

+ + + + + + + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
value + + +any + + + + The selected Fuse value
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+ The Boolean result of the test +
+ + + +
+
+ Type +
+
+ +boolean + + +
+
+ + + + + + + + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/docs/index.html b/docs/index.html index 2f6aba1..8f67fba 100644 --- a/docs/index.html +++ b/docs/index.html @@ -43,31 +43,32 @@
-

select-return

Return a function based on a condition. Functional alternative to switch-case.

-

Usage

const a = 100;
+        

Fuse

FunctionSelect

+

Fuse selects a function that passes a condition.

+

Usage

const Fuse = require('@codefeathers/fuse');
+const { FuseIterable } = require('@codefeathers/fuse');
 
-const result = new Select(a)
-    .for(x => x>10,
+const a = 100;
+
+const result = new Fuse(a)
+    .on(x => x>10,
         a => `${a} is greater than 10.`)
-    .for(x => x<10,
+    .on(x => x<10,
         a => `${a} is lesser than 10.`)
-    .for(x => x===10,
+    .on(x => x===10,
         a => `${a} is 10.`)
 
 console.log(result.resolve()); // -> "100 is greater than 10."

Docs

Docs exist in /docs directory. Will be served soon.

Development

-

If you find any mistakes in code/documentation, or if you feel something could be done better, do PR :)

-
-
-

I appreciate it.

+

If you find any mistakes in code/documentation, or if you feel something could be done better, do PR :) I appreciate it.

  • Always write test spec for any code you add. Make sure they run as intended.
  • Add/update JSDoc comments as needed.
  • -
  • Use npm scripts for linting, debugging, building docs.
  • +
  • Use npm scripts for linting, tests, debugging, building docs.

Place your test file as testscript.js in root.

-

The following npm scripts are available: npm run lint, npm run debug, npm run docs.

+

The following npm scripts are available: npm run lint, npm test, npm run debug, npm run docs.

Credits

@Floofies was of huge help during development of this module.

Also, @TRGWII was a source of inspiration and guidance.

@@ -80,13 +81,13 @@ console.log(result.resolve()); // -> "100 is greater than 10."<
diff --git a/docs/index.js.html b/docs/index.js.html new file mode 100644 index 0000000..c47cbdd --- /dev/null +++ b/docs/index.js.html @@ -0,0 +1,233 @@ + + + + + JSDoc: Source: index.js + + + + + + + + + + +
+ +

Source: index.js

+ + + + + + +
+
+
'use strict';
+
+/**
+ * FUSE
+ * FunctionSelect: Return a function based on a condition.
+ * @version 0.10.0
+ * @author Muthu Kumar (MKRhere)
+ */
+
+/**
+ * @callback predicate
+ * @param {any} value The selected Fuse value
+ * @returns {boolean} The Boolean result of the test
+ */
+
+/**
+ * @callback consequent
+ * @param {any} value The selected Fuse value
+ * @param {...any} args An arbitrary array of arguments
+ * @returns {any}
+ */
+
+/**
+ * Creates a FuseItem instance with a value and optional resolve function.
+ * FuseIterable constructor uses it internally, and Fuse extends FuseItem.
+ * Not exposed.
+ * @class FuseItem
+ */
+class FuseItem {
+	/**
+	 * @param {any} value The input value
+	 * @param {function} resolve Optional resolve function
+	 * @constructs FuseItem
+	 */
+	constructor(value, resolve) {
+		this.value = value;
+		if (resolve) {
+			this.resolve = (...args) => resolve(this.value, ...args);
+			this.resolved = true;
+		}
+	}
+
+	/**
+	 * Fallback resolve prototype. Returns null when called.
+	 * Used in case a resolve is never found.
+	 * @returns {null} null
+	 * @memberof FuseItem
+	 */
+	resolve() {
+		return null;
+	}
+}
+
+/**
+ * Creates a FuseIterable instance from an array or iterable.
+ * @class FuseIterable
+ * @param {iterable} values An iterable expression as the switch
+ * @param {Array<function>} tests Array of { test, consequent } objects
+ * @param {function} tests[].predicate Test function
+ * @param {function} tests[].consequent Consequent function
+ * @constructs FuseIterable
+ */
+class FuseIterable {
+	constructor(values, conditionals) {
+		this.values = Array.from(values).map(value =>
+			value instanceof FuseItem
+				? value
+				: new FuseItem(value));
+		this.conditionals = conditionals || [];
+	}
+
+	/**
+	 * Accepts a test and consequent function each and returns a new
+	 * FuseIterable instance.
+	 * FuseIterable.prototype.for works a little differently than
+	 * Fuse.prototype.for, by lazy accumulating the tests and
+	 * resolving all the values when .resolve() is called.
+	 * @param {callback} predicate A test callback function
+	 * @param {callback} consequent Consequent callback function
+	 * @returns {FuseIterable} An instance of FuseIterable
+	 * @memberof FuseIterable
+	 */
+	on(predicate, consequent) {
+		return new FuseIterable(this.values, [
+			...this.conditionals,
+			{ predicate, consequent }
+		]);
+	}
+
+	/**
+	 * Accepts a list of tuples as arguments and returns a new
+	 * FuseIterable instance. An alternative to chaining multiple
+	 * FuseIterable.prototype.on methods.
+	 * @memberOf FuseIterable
+	 * @param {...Array.<function>} tuples -
+	 * Array of [ predicate, consequent ] pairs
+	 * @returns {FuseIterable} An instance of FuseIterable
+	 */
+	onField(...tuples) {
+		const conditionals = tuples.map(conditional =>
+			({
+				predicate: conditional[0],
+				consequent: conditional[1]
+			}));
+		return new FuseIterable(this.values, conditionals);
+	}
+
+	/**
+	 * Accepts parameters during resolve time and passes them along with
+	 * each value into the winning consequent function.
+	 * @param {...any} args Any number of arguments
+	 * @returns {(any|null)} Resolved value or null if it was unresolved
+	 */
+	resolve(...args) {
+		return this.values.map(item => {
+			const resolver = this.conditionals.find(conditional =>
+				conditional.predicate(item.value)
+					? conditional.consequent
+					: null
+			);
+			return resolver
+				? resolver.consequent(item.value, ...args)
+				: null;
+		});
+	}
+}
+
+/**
+ * Creates a new Fuse instance.
+ * @class Fuse
+ * @extends {FuseItem}
+ */
+class Fuse extends FuseItem {
+
+	/**
+	 * Accepts a test and consequent function each and returns a new
+	 * Fuse or FuseIterable instance.
+	 * @param {callback} predicate A test callback function
+	 * @param {callback} consequent Consequent callback function
+	 * @returns {Fuse} Returns a new Fuse instance
+	 * @memberof Fuse
+	 */
+	on(predicate, consequent) {
+
+		/* If a resolve exists, just pass on the instance
+			until .resolve() is called */
+		if (this.resolved) return this;
+
+		if (predicate(this.value)) return new Fuse(this.value, consequent);
+
+		/* If the test doesn't pass, just pass the Fuse
+			instance along the chain until a test passes,
+			or .resolve() is called */
+		return this;
+	}
+
+	/**
+	 * Accepts a value instead of a test function, and checks for strict
+	 * equality with this.value.
+	 * @param {any} value Any value to check against this.value
+	 * @param {function} consequent Consequent callback function
+	 * @returns {Fuse} An instance of Fuse
+	 */
+	is(value, consequent) {
+		return this.on(() => value === this.value, consequent);
+	}
+
+
+	/**
+	 * Accepts a value instead of a test function, and checks for strict
+	 * inequality with this.value.
+	 * @param {any} value Any value to check against this.value
+	 * @param {function} consequent Consequent callback function
+	 * @returns {Fuse} An instance of Fuse
+	 */
+	not(value, consequent) {
+		return this.on(() => value !== this.value, consequent);
+	}
+}
+
+module.exports = Fuse;
+module.exports.FuseIterable = FuseIterable;
+
+
+
+ + + + +
+ + + +
+ + + + + + +