
Research
/Security News
Miasma Mini Shai-Hulud Hits ImmobiliareLabs npm Packages
Miasma Mini Shai-Hulud hits @immobiliarelabs Backstage plugins, targeting GitLab and LDAP auth packages on npm.
An iteration utility with a rich callback environment.
(1/8/13) version 3.0.0 by Bemi Faison
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.
genData(
[1,[2,3]],
function (name, value) {
return 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 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.
genData(
[1,2],
function () {
if (this.value === 2) {
return 'world!';
}
},
function (name, value) {
if (value === 1) {
return 'hello';
}
}
);
// ['hello', 'world!']
Callbacks receive four arguments per iterated value:
undefined, for the first iteration.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.
genData(
['foo', 'bar'],
function (name, value) {
return value;
},
function (name, value, parent, flags) {
flags.allowUndefined = 1;
}
);
// ['foo', undefined, 'bar', undefined]
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.
genData(
['John', 'Sally'],
'Hello ',
function (name, value, parent, flags) {
if (typeof value === 'string') {
return flags.args[0] + value + flags.args[1];
}
},
'. How are you?'
);
// ['Hello John. How are you?', 'Hello Sally. How are you?']
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.
genData(
['a', 3, 'b', 'c', 'd'],
function (name, value, parent, flags) {
if (typeof value === 'number') {
flags.breaks = 1;
return value;
}
}
);
// [3]
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.
genData(
[1.1, 'a', 2.5],
function (name, value, parent, flags) {
if (typeof value !== 'number') {
flags.callbacks = 1;
}
},
function (name, value) {
return value.toFixed(0);
}
);
// ['1','3']
This is a number, reflecting genData's iteration count. The value is 0 initially and increments per iteration.
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.
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.
genData(
['e', 'c', 'h', 'o'],
function (name, value, parent, flags) {
if (!flags.loop) {
flags.returns = '';
}
if (typeof value === 'string') {
flags.returns += value;
}
}
);
// 'echo'
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.
genData(
'hello world!',
function (name, value, parent, flags) {
if (typeof value === 'string' && value.length > 1) {
flags.source = value.split('');
flags.callbacks = false;
}
},
function (name, value) {
if (typeof value === 'string') {
return value.toUpperCase();
}
}
);
// ['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D', '!']
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.spawn(function (name, value, parent, flags) {
if (typeof value !== 'number') {
flags.callbacks = false;
}
}),
calc = nums.spawn(function () {
return this.square();
})
;
calc.prototype.square = function () {
return this.value * this.value;
};
calc(['foo', 2, 'bar', 10]); // [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.
calc(,
['foo', 2, 'bar', 10],
function (name, value, parent, flags) {
flags.returns = flags.returns.filter(function (value) {
return value >= 100;
});
}
);
// [100]
Note: While spawning does allow you to curry values to the iteration flag "args", this practice is not recommended.
Use npm to install the "genData" package, with npm install genData. Then, simply require and use the function directly.
var genData = require('genData');
/* genData(stuff, callbacks, arguments, ...) */
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
define(function (require, module, exports) {
var genData = require('genData');
/* genData(stuff, callbacks, arguments, ...) */
});
// top-level logic
require(['genData'], function (genData) {
/* genData(stuff, callbacks, arguments, ...) */
});
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.
<script type="text/javascript" src="somepath/gendata-min.js"></script>
<script type="text/javascript">
/* genData(stuff, callbacks, arguments, ...) */
</script>
genData is available under the terms of the MIT-License.
Copyright, Bemi Faison
FAQs
A normalization pattern to build, query, and manipulate everything.
The npm package genData receives a total of 19 weekly downloads. As such, genData popularity was classified as not popular.
We found that genData demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Research
/Security News
Miasma Mini Shai-Hulud hits @immobiliarelabs Backstage plugins, targeting GitLab and LDAP auth packages on npm.

Security News
Rolldown paused Rust React Compiler integration after a 5MB binary size increase raised concerns about shipping React-specific code to all Vite users.

Security News
/Research
Mini Shai-Hulud expands into the Go ecosystem after hitting LeoPlatform npm packages and targeting GitHub Actions workflows.