Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

extras: Moved into extras namespace

  • Loading branch information...
commit 1c6a9a164af042e05dfe2154f65b3057e962ce29 1 parent a10b9ce
@koichik authored
View
1  CHANGELOG.md
@@ -3,6 +3,7 @@
- 0.0.2 (Not yet released)
- core: Added logging for debug.
- core: Fixed case where `functions` or `array` is empty.
+ - extras: Moved into `extras` namespace.
- extras: Added `flattenFirst()`, `flattenSecond()` and `flattenThird()`.
- 0.0.1 (2012/02/25)
View
42 README.md
@@ -196,6 +196,10 @@ For example, the first example of this page, it is output log as follows:
## Core API
+```javascript
+var flowless = require('flowless');
+```
+
### flowless.seq(functions)
Returns an asynchronous style function which runs functions in sequential.
@@ -440,7 +444,11 @@ flowless.seq([
## Extra API
-### flowless.array
+```javascript
+var extras = require('flowless').extras;
+```
+
+### extras.array
Provides asynchronous style functions to wrap a method of `Array`.
It takes the first argument as `this`.
@@ -450,7 +458,7 @@ Example:
```javascript
flowless.seq([
...
- flowless.array.join(':'),
+ extras.array.join(':'),
...
]);
```
@@ -467,7 +475,7 @@ flowless.seq([
]);
```
-### flowless.string
+### extras.string
Provides asynchronous style functions to wrap a method of `String`.
It takes the first argument as `this`.
@@ -477,7 +485,7 @@ Example:
```javascript
flowless.seq([
...
- flowless.string.split(':'),
+ extras.string.split(':'),
...
]);
```
@@ -494,7 +502,7 @@ flowless.seq([
]);
```
-### flowless.generate(args...)
+### extras.generate(args...)
Returns a new asynchronous style function which passes a given arguments
to a callback.
@@ -508,7 +516,7 @@ Example:
```javascript
flowless.seq([
- flowless.generate(1, 2, 3),
+ extras.generate(1, 2, 3),
...
]);
```
@@ -524,9 +532,9 @@ flowless.seq([
]);
```
-### flowless.bindFirst(target, name)
-### flowless.bindSecond(target, name)
-### flowless.bindThird(target, name)
+### extras.bindFirst(target, name)
+### extras.bindSecond(target, name)
+### extras.bindThird(target, name)
Returns a new asynchronous style function which binds an argument of the
specific position (first, second or third) to a property of a target object.
@@ -544,7 +552,7 @@ Example:
var context = {};
flowless.seq([
...
- flowless.bindFirst(context, 'name'),
+ extras.bindFirst(context, 'name'),
...
]);
```
@@ -563,9 +571,9 @@ flowless.seq([
]);
```
-### flowless.flattenFirst()
-### flowless.flattenSecond()
-### flowless.flattenThird()
+### extras.flattenFirst()
+### extras.flattenSecond()
+### extras.flattenThird()
Returns a new asynchronous style function which flatten an argument of the
specific position (first, second or third).
@@ -579,15 +587,15 @@ Example:
```javascript
var context = {};
flowless.seq([
- flowless.generate(['foo', 'bar', 'baz']),
- flowless.flattenFirst(),
+ extras.generate(['foo', 'bar', 'baz']),
+ extras.flattenFirst(),
function(foo, bar, baz, cb) { // foo: 'foo', bar: 'bar', baz: 'baz'
...
}
]);
```
-### flowless.makeAsync(fn)
+### extras.makeAsync(fn)
Returns a new asynchronous style function which invokes a synchronous function
and passes the return value to a callback.
@@ -602,7 +610,7 @@ Example:
```javascript
flowless.seq([
...
- flowless.makeAsync(encodeURI),
+ extras.makeAsync(encodeURI),
...
]);
```
View
3  examples/ex1.js
@@ -3,6 +3,7 @@
var fs = require('fs');
var path = require('path');
var flowless = require('../index');
+var extras = flowless.extras;
var path1 = path.join(__dirname, 'path1');
var path2 = path.join(__dirname, 'path2');
@@ -13,7 +14,7 @@ flowless.runSeq([
[fs.readFile, path1, 'utf8'],
[fs.readFile, path2, 'utf8']
]),
- flowless.array.join(''),
+ extras.array.join(''),
[fs.writeFile, path3, flowless.first],
[fs.readFile, path3, 'utf8']
], function(err, result) {
View
5 examples/ex2.js
@@ -2,13 +2,14 @@
var fs = require('fs');
var flowless = require('../index');
+var extras = flowless.extras;
flowless.runSeq([
[fs.readdir, __dirname],
- flowless.array.filter(function(filename) {
+ extras.array.filter(function(filename) {
return /\.js$/.test(filename);
}),
- flowless.array.map(function(filename) {
+ extras.array.map(function(filename) {
return __dirname + '/' + filename;
}),
flowless.map([fs.readFile, flowless.first, 'utf8'])
View
5 examples/statAll.js
@@ -2,15 +2,16 @@
var fs = require('fs');
var flowless = require('../index');
+var extras = flowless.extras;
var context = {};
flowless.runSeq([
[fs.readdir, __dirname],
- flowless.bindFirst(context, 'fileNames'),
+ extras.bindFirst(context, 'fileNames'),
flowless.map(function(file, cb) {
fs.stat(__dirname + '/' + file, cb);
}),
- flowless.array.reduce(function(sizes, stat, i) {
+ extras.array.reduce(function(sizes, stat, i) {
sizes[context.fileNames[i]] = stat.size;
return sizes;
}, {})
View
7 index.js
@@ -2,12 +2,7 @@
module.exports = exports = require('./lib/flowless');
-var extras = require('./lib/extras');
-var keys = Object.keys(extras);
-for (var i = 0, len = keys.length; i < len; ++i) {
- var key = keys[i];
- exports[key] = extras[key];
-}
+exports.extras = require('./lib/extras');
var utils = require('./lib/utils');
exports.first = utils.first;
View
9 lib/extras.js
@@ -1,8 +1,5 @@
'use strict';
-var assert = require('assert');
-var utils = require('./utils');
-
exports.generate = generate;
exports.bindFirst = bindFirst;
@@ -17,6 +14,12 @@ exports.makeAsync = makeAsync;
exports.array = makeWrappers(Array.prototype);
exports.string = makeWrappers(String.prototype);
+var assert = require('assert');
+var utils = require('./utils');
+
+var first = exports.first = utils.first;
+var second = exports.second = utils.second;
+var third = exports.third = utils.third;
function generate() {
var args = Array.prototype.slice.call(arguments);
View
7 lib/flowless.js
@@ -28,13 +28,12 @@ function runPar(functions, cb) {
setupPar(utils.getLocation(runPar), functions)(cb || noop);
}
-function map(concurrency, array, fn) {
+function map(concurrency, fn) {
if (typeof concurrency !== 'number') {
- fn = array;
- array = concurrency;
+ fn = concurrency;
concurrency = DEFAULT_CONCURRENCY;
}
- return setupMap(utils.getLocation(map), concurrency, functions, fn);
+ return setupMap(utils.getLocation(map), concurrency, fn);
}
function runMap(concurrency, array, fn, cb) {
View
13 test/extras.js
@@ -2,10 +2,11 @@
var should = require('should');
var flowless = require('../index');
+var extras = flowless.extras;
describe('extras', function() {
it('should generate data', function(done) {
- var fn = flowless.generate('foo', 'bar');
+ var fn = extras.generate('foo', 'bar');
fn(function(err, foo, bar) {
should.not.exist(err);
foo.should.equal('foo');
@@ -16,7 +17,7 @@ describe('extras', function() {
it('should bind first argument', function(done) {
var context = {};
- var fn = flowless.bindFirst(context, 'name');
+ var fn = extras.bindFirst(context, 'name');
fn('hoge', 'moge', function(err, hoge, moge) {
should.not.exist(err);
context.name.should.equal('hoge');
@@ -27,7 +28,7 @@ describe('extras', function() {
});
it('should flatten first argument', function(done) {
- var fn = flowless.flattenFirst();
+ var fn = extras.flattenFirst();
fn([1, 2, 3, 4, 5], function(err, a, b, c, d, e) {
should.not.exist(err);
a.should.equal(1);
@@ -40,7 +41,7 @@ describe('extras', function() {
});
it('should call method of Array', function(done) {
- var fn = flowless.array.join(':');
+ var fn = extras.array.join(':');
fn(['foo', 'bar'], function(err, result) {
should.not.exist(err);
result.should.equal('foo:bar');
@@ -49,7 +50,7 @@ describe('extras', function() {
});
it('should call method of String', function(done) {
- var fn = flowless.string.split(':');
+ var fn = extras.string.split(':');
fn('foo:bar', function(err, result) {
should.not.exist(err);
result.should.have.lengthOf(2);
@@ -60,7 +61,7 @@ describe('extras', function() {
});
it('should convert placeholder to actual argument', function(done) {
- var fn = flowless.string.concat(flowless.second);
+ var fn = extras.string.concat(flowless.second);
fn('foo', 'bar', function(err, result) {
should.not.exist(err);
result.should.equal('foobar');
View
3  test/makeAsync.js
@@ -3,6 +3,7 @@
var path = require('path');
var should = require('should');
var flowless = require('../index');
+var extras = flowless.extras;
function minus(a, b) {
return a - b;
@@ -10,7 +11,7 @@ function minus(a, b) {
describe('makeAsync', function() {
it('should finish without error', function(done) {
- var func = flowless.makeAsync(minus);
+ var func = extras.makeAsync(minus);
func.should.be.a('function');
func(1, 2, function(err, result) {
should.not.exist(err);
View
12 test/map.js
@@ -2,13 +2,14 @@
var should = require('should');
var flowless = require('../index');
+var extras = flowless.extras;
describe('map', function() {
it('should finish without error', function(done) {
flowless.runMap([
['aaa', 'bbb'],
['xxx', 'yyy', 'zzz']
- ], flowless.makeAsync(function(array) {
+ ], extras.makeAsync(function(array) {
return array.join(', ');
}), function(err, results) {
should.not.exist(err);
@@ -40,9 +41,7 @@ describe('map', function() {
it('should run in sequential', function(done) {
var par = 0;
- flowless.runMap(1, [
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
- ], function(n, cb) {
+ var map = flowless.map(1, function(n, cb) {
++par;
par.should.equal(1);
process.nextTick(function() {
@@ -50,7 +49,10 @@ describe('map', function() {
--par;
cb(null, n);
});
- }, function(err, results) {
+ });
+ map([
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+ ], function(err, results) {
should.not.exist(err);
results.should.have.lengthOf(10);
done();
Please sign in to comment.
Something went wrong with that request. Please try again.