1.0.3 • Published 8 years ago

genit v1.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

genit

Generator based utility belt

Build Status npm Dependencies Join the chat at https://gitter.im/fleekjs/genit

$ npm install genit

Key

Usage

Direct

  'use strict'

  let genit  = require('genit');
  let fooFunc = function *(someSet) {
     return yield genit.map(someSet, function *(value, index) {
      return yield someGeneratorFunction('foo', 'bar', value);  
    });
  }

Module Extension

  'use strict'

  let _       = require('lodash');
  let genit  = require('genit');

  genit.inject(_);

  _.each(someSet, function () {  }); //existing
  _.isGenerator(mightBeAGenerator) // injected
   function *() {
    return yield _.genit.map(someSet, function *(value, index) {   }); // injected to .genit property (name collision)
  }

Utilities

Synchronous

Mixed Synchronous

each

  • Parameters
  • Functionality
    • Iterates over every item in the collection (objec tor array)
    • Executes the operation on each item
  • Returns
    • Undefined
yield genit.each([one, two, three], function *(value, index) {
  console.log(index + ' = ' + this + ' ~ ' + value);
});
// console:
// 1 = one ~ one
// 2 = two ~ two
// 3 = three ~ three

yield genit.each({ one:'foo', two:'bar', three:'biz'  }, function *(value, key) {
  console.log(key + ' = ' + this + ' ~ ' + value);
});
// console:
// one = foo ~ foo
// two = bar ~ bar
// three = biz ~ biz

map

  • Parameters
  • Functionality
    • Iterates over every item in the collection (objec tor array)
    • Executes the operation on each item
    • Returned value is added to the resulting array
  • Returns
    • Array of values returned
let result = yield genit.each(['one', 'two', 'three'], function *(value, index) {
  console.log(index + ' = ' + this + ' ~ ' + value);
  return index * 2;
});
// console:
// 1 = one ~ one
// 2 = two ~ two
// 3 = three ~ three
console.log(result);
// console:
// [0, 1, 2]

let result = yield genit.each({ one:'foo', two:'bar', three:'biz'  }, function *(value, key) {
  console.log(key + ' = ' + this + ' ~ ' + value);
  return value;
});
// console:
// one = foo ~ foo
// two = bar ~ bar
// three = biz ~ biz
console.log(result);
// console:
// ['foo', 'bar', 'biz']

filter

  • Parameters
  • Functionality
    • Iterates over every item in the collection (object or array)
    • Reserve only the item that passed the checking condition
    • Returned value is added to the resulting array or object
  • Returns
    • Array or object values returned
let result = yield genit.filter([-1, -2, -3, 4, 5], function *(value) {
  return value > 0;
});
console.log(result);
// console:
// [4,5]

let result = yield genit.filter({ one:'foo', two:'bar', three:'biz'  }, function *(value, key) {
  return value == "biz" || value == "bar";
});
console.log(result);
// console:
// {two : 'bar', three: 'biz'}

Asynchronous

Coming Soon

Miscellaneous

isGenerator

  • Parameters
  • Functionality
    • Test whether or not a function is a generator
  • Returns
    • Boolean of generator status
let result = yield genit.isGenerator(function () {});
console.log(result); // => false

let result = yield genit.isGenerator(function *() {});
console.log(result); // => true

inject

  • Parameters
    • Required - object to inject properties to
  • Functionality
    • Inject properties into target Object
    • Collision in property name move genit property to the .genit property
  • Returns
    • target object with injected properties
'use strict'

  let test   = { each : function () { return 'Original Each';  }  };
  let genit = require('genit');

  genit.inject(test);

  console.log(test.each()); // => "Original Each"
  console.log(test.isGenerator(function () {})) // => "false"
  console.log(test.isGenerator(test.genit.each)) // => "true"

Authors

  • John Hofrichter
  • Contributor Lan Nguyen