Skip to content

Sample Generators

bemson edited this page Aug 1, 2011 · 10 revisions

This page lists sample generators, demonstrating the many different ways to use genData.

The list is divided between standard and custom generators. Standard generators return a dataset; an array of normalized data objects - each having the same structure and prototype. Custom generators return an array whose contents may vary. Read the generator's description, to understand what object(s) may be returned.

Notice:

  • All code samples is available license free.
  • Use of these examples is at your own risk.
  • By default, genData will scan objects recursively.

Standard Generators

flagArrays(stuff)

by bemson

Structures the dataset.

Data Object:

  • Adds .isArray - Boolean, Indicates when the value of this data object is an array.
var flagArrays = new genData(function (name, value) {
  this.isArray = !!~{}.toString.call(value).indexOf('y');
});

genFncs(stuff)

by bemson

Filters non-function values, and structures the dataset. Spawned from flagArrays (bemson).

Data Object:

  • Adds .scope - Object, The original scope for the function. The scope is window by default.
var genFncs = new flagArrays(function (name, value, parent, dataset, flags) {
  if (typeof value === 'function') {
    this.scope = (parent && !parent.isArray) ? parent.value : window;
    if (parent && parent.isArray && value.name) {
      this.name = value.name;
    }
  } else {
    flags.omit = 1;
  }
});

genRelations(stuff)

by bemson

Adds members that reference related data objects. Prototype methods to this generator, for context-sensitive actions per data object.

Data Object:

  • Adds .parent - Object, The data objectIndicates when the value of this data object is an array.
  • Adds .children - Array, Contains any and all data objects derived from this value.
  • Adds .childIndex - Integer, The index of this data object within it's parent's .children array.
  • Adds .nextSibling - Object, References the data object following this one, within the parent's .children array.
  • Adds .previousSibling - Object, References the data object preceding this one, within the parent's .children array.
  • Adds .firstChild - Object, References the first data object within the .children array.
  • Adds .lastChild - Object, References the last data object within the .children array.
var genRelations = new genData(function (name, value, parent) {
  var data = this;
  data.parent = parent;
  data.children = [];
  if (parent) {
    if (parent.children.length) {
      data.previousSibling = parent.children[parent.children.length - 1];
      data.previousSibling.nextSibling = data;
    } else {
      parent.firstChild = data;
    }
    data.childIndex = parent.children.push(data); 
    parent.lastChild = data;
  }
});

Custom Generators

grepFncs(stuff)

by bemson

Returns all found functions.

var grepFncs = new genData(function (name, value, parent, dataset, flags) {
  flags.omit = 1;
  if (typeof value === 'function') {
    dataset.push(value);
  }
});

mapArray([mapFunction, arrayTomap])

by bemson

Returns an array of values returned by passing elements from arrayMap to mapFunction. Returns nothing when mapFunction is not a function.

var mapArray = new genData(function (name, value, parent, dataset, flags, shared) {
  flags.omit = 1;
  if (parent) {
    if (!shared.mapFunction) {
      if (typeof value === 'function') {
        shared.mapFunction = value;
      } else {
        flags.exit = 1;
      }
    } else if (!shared.inArrayToMap) {
      shared.inArrayToMap = 1;
      shared.idx = 0;
    } else {
      flags.scan = 0;
      dataset.push(shared.mapFunction(value, shared.idx++));
    }
  }
});

flattenArray(stuff)

by bemson

Returns an array of values, from nested arrays. Arrays within objects are not flattened.

var flattenArray = new genData(function (name, value, parent, dataset, flags) {
  flags.omit = 1;
  if (!~{}.toString.call(value).indexOf('y')) {
    flags.scan = 0;
    dataset.push(value);
  }
});

getValuesWithKey([needle, objectToSearch1, objectToSearch2, ..., objectToSearchN])

by bemson

Returns those elements whose key matches the given needle. The needle may be a string, regular expression or function (to test the data's name). Matches properties are not further scanned.

var getValuesWithKey = new genData(function (name, value, parent, dataset, flags, shared) {
  flags.omit = 1;
  if (shared.parsedArgs) {
    if (shared.findFnc(name)) {
	  dataset.push(value);
      flags.scan = 0;
    }
  } else if (parent) {
	shared.parsedArgs = 1;
	switch (typeof value) {
      case 'function' :
        shared.findFnc = value;
      break;
      case 'string' :
        shared.findFnc = function (hay) {
          return ~hay.indexOf(value);
        };
      break;
      case 'object' :
        if (value.constructor === RegExp) {
          shared.findFnc = function (hay) {
            return value.test(hay);
          };
        }
      break;
    }
    if (!shared.findFnc) {
      flags.exit = 1;
    }
  }
});

===

If you'd like to share your generator here, send me a message on github! Your generator may use an existing name and reference your favorite library. Licensing is not allowed, but your name will be noted in the description.

Clone this wiki locally