genData
An iteration utility with a rich callback environment.
(1/10/13) version 3.1.0 by Bemi Faison
Usage
genData iterates a queue, beginning with the first parameter, then it's non-inherited members, then their non-inherited members, and so on - recursively and in depth-first order. Below is what genData outputs when a callback simply returns the current iteration value.
;// [[1,[2,3]], 1, [2,3], 2, 3]
Note how the first element is the original value, passed to genData. Also, note that genData returns an array, which is the default container for each callback's return value (except undefined
).
Callbacks
Callbacks are functions passed to genData, after the first parameter. genData executes them in order, and scoped to a uniquely generated "data" objects - representing the name and value of the current iteration. Your callback can use either the first two arguments, or the scope object in it's logic, as follows.
;// ['hello', 'world!']
Callback Signature
Callbacks receive four arguments per iterated value:
- The name (or key) corresponding this value. This is an empty string for the first iteration.
- The value being iterated. This is the first parameter passed to genData, for the first iteration.
- The data object which sourced the current iteration. This is
undefined
, for the first iteration. - The iteration object, with various flags to use in your logic.
Iteration Flags
flags.allowUndefined
Set this flag to a truthy value, if you want genData to capture the result of callbacks that return undefined
(which includes callbacks with no return
statement). This flag is reset to 0 before executing each callback. Below, a callback allows undefined values, which are then captured in the returned array.
;// ['foo', undefined, 'bar', undefined]
flags.args
A simple array of non-function values passed to genData, after the first parameter (the value to iterate). Below, callbacks use arguments to build the array results.
;// ['Hello John. How are you?', 'Hello Sally. How are you?']
flags.breaks
Similar to the JavaScript break
statement, genData will abort processing it's queue when this flag is truthy. This flag persists between callbacks, but is reset (to 0) at the beginning of each iteration. Below, a callback tells genData to stop and return the first number found.
;// [3]
flags.continues
Similar to the JavaScript continue
statement, set this flag to a truthy value when you want to end the current iteration and begin the next one. Below, a callback uses this flag to avoid runtime errors.
;// ['1','3']
flags.loop
This is a number, reflecting genData's iteration count. The value is 0 initially and increments per iteration.
flags.params
A simple array of what was passed to genData. Mutating this array has no impact on genData's iteration or other flags. Below, demonstrates how to use this flag, along with flag.continues
, in order to custom invoke functions that genData would otherwise assume a callback.
{ console;} ; // (console output)// hello// world!
flags.queued
This is a number, reflecting genData's queue of pending iterations. Because genData adds to the queue after each iteration, a value of 0 does not mean the end of genData's process.
flags.returns
This flag is an array by default, and is what genData returns. This flag persists between callbacks, until genData completes processing a given value. Note that genData will ignore returned callback values, when this flag is not an Array. Below, demonstrates how this flag can change genData's return value.
;// 'echo'
flags.source
This value is scanned by genData (at the end of every iteration), in order to add more non-inherited members to genData's queue. By default, this flag reflects the second argument of the callback signature, or the value
property of the callback scope. Below, this flag is used to make a non-object value iterable.
;// ['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D', '!']
Spawning genData Generators
The static .spawn()
method expands upon the concept of currying functions, by also extending genData's prototype chain. Functions spawned by genData are called "generators", which also host the .spawn()
method for further spawning. Below, two generators are spawned, in order to preserve callback configurations and isolate additions to genData's prototype.
var nums = genData calc = nums; calcprototype { return thisvalue * thisvalue;}; ; // [4, 100]
Generators accept the same argument signature as genData. Continuing with the example above, the calc()
generator is refined to prune numbers below 100.
;// [100]
Note: While spawning does allow you to curry values to the iteration flag "args", this practice is not recommended.
Installation
Node
Use npm to install the "genData" package, with npm install genData
. Then, simply require and use the function directly.
var genData = ; /* genData(stuff, callbacks, arguments, ...) */
AMD
genData anonymously registers itself as an AMD function. Assuming you configured your loader to map the module id "genData" (recommended), then you could include genData in either a module or a top-level script, as follows:
// module; // top-level logic;
Browser
If you include genData directly in a web page - via the <script>
tag - it will be available in the global scope. For these kind of static deployments, the minified is recommended.
Files
- gendata-min.js - genData source file (minified with UglifyJS)
- LICENSE - The legal terms and conditions under which this software may be used
- README.md - This readme file
- src/ - Directory containing the genData source code
- test/ - Directory containing test files written in the Mocha framework
License
genData is available under the terms of the MIT-License.
Copyright, Bemi Faison