Skip to content

Commit 9c58153

Browse files
committed
update run-callback
1 parent d2bcbda commit 9c58153

File tree

12 files changed

+311
-231
lines changed

12 files changed

+311
-231
lines changed

.eslintrc

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,60 +13,59 @@ rules:
1313
no-dupe-keys: 2
1414
no-duplicate-case: 2
1515
no-empty-character-class: 2
16-
no-ex-assign: 1
17-
no-extra-boolean-cast: 1
18-
no-extra-parens: 1
19-
no-extra-semi: 1
20-
no-func-assign: 1
21-
no-inner-declarations: 1
22-
no-invalid-regexp: 1
23-
no-irregular-whitespace: 1
24-
no-negated-in-lhs: 1
25-
no-obj-calls: 1
26-
no-regex-spaces: 1
27-
no-sparse-arrays: 1
28-
no-unreachable: 1
29-
use-isnan: 1
16+
no-ex-assign: 2
17+
no-extra-boolean-cast: 2
18+
no-extra-parens: 2
19+
no-extra-semi: 2
20+
no-func-assign: 2
21+
no-inner-declarations: 2
22+
no-invalid-regexp: 2
23+
no-irregular-whitespace: 2
24+
no-negated-in-lhs: 2
25+
no-obj-calls: 2
26+
no-regex-spaces: 2
27+
no-sparse-arrays: 2
28+
no-unreachable: 2
29+
use-isnan: 2
3030
valid-typeof: 2
31-
no-unexpected-multiline: 1
32-
no-cond-assign: 1
33-
no-constant-condition: 1
34-
no-control-regex: 1
35-
no-debugger: 1
31+
no-unexpected-multiline: 2
32+
no-cond-assign: 2
33+
no-constant-condition: 2
34+
no-control-regex: 2
35+
no-debugger: 2
3636
# code style
37-
consistent-return: 1
38-
curly: 1
39-
default-case: 1
40-
dot-notation: 1
41-
dot-location: [1, "property"]
42-
eqeqeq: 1
43-
no-else-return: 1
44-
no-lone-blocks: 1
45-
no-loop-func: 1
46-
no-multi-spaces: 1
47-
no-multi-str: 1
48-
no-proto: 1
49-
no-redeclare: 1
50-
no-return-assign: 1
51-
no-sequences: 1
52-
no-throw-literal: 1
53-
no-unused-expressions: 1
54-
no-void: 1
55-
no-warning-comments: [1, { "terms": ["todo", "fixme", "xxx"], "location": "start" }]
56-
no-with: 1
57-
radix: 1
58-
no-delete-var: 1
59-
no-shadow-restricted-names: 1
60-
no-shadow: 1
37+
consistent-return: 0
38+
curly: 2
39+
default-case: 2
40+
dot-notation: 2
41+
dot-location: [2, "property"]
42+
eqeqeq: [2, "allow-null"]
43+
no-else-return: 2
44+
no-lone-blocks: 2
45+
no-loop-func: 2
46+
no-multi-spaces: 2
47+
no-multi-str: 2
48+
no-proto: 2
49+
no-redeclare: 2
50+
no-return-assign: 2
51+
no-sequences: 2
52+
no-throw-literal: 2
53+
no-unused-expressions: 2
54+
no-void: 2
55+
no-warning-comments: [2, { "terms": ["todo", "fixme", "xxx"], "location": "start" }]
56+
no-with: 2
57+
radix: 2
58+
no-delete-var: 2
59+
no-shadow-restricted-names: 2
60+
no-shadow: 2
6161
no-undef: 2
62-
no-unused-vars: 1
63-
brace-style: [1, "1tbs", { "allowSingleLine": true }]
64-
comma-spacing: 1
65-
comma-style: 1
66-
consistent-this: [2, "self"]
62+
no-unused-vars: 2
63+
brace-style: [2, "1tbs", { "allowSingleLine": true }]
64+
comma-spacing: 2
65+
comma-style: 2
6766
indent: [2, 2]
68-
key-spacing: 1
69-
max-nested-callbacks: [2, 7]
67+
key-spacing: 2
68+
max-nested-callbacks: [2, 5]
7069
no-lonely-if: 2
7170
no-mixed-spaces-and-tabs: 2
7271
no-nested-ternary: 2
@@ -76,7 +75,7 @@ rules:
7675
operator-linebreak: 2
7776
quote-props: [2, "as-needed"]
7877
quotes: [2, "single", "avoid-escape"]
79-
semi: [2, "always"]
78+
semi: [2, "never"]
8079
space-after-keywords: 2
8180
space-before-blocks: 2
8281
space-infix-ops: 2

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/node_modules/
22
.DS_Store
33
npm-debug.log
4+
/build/
5+
/coverage/

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: node_js
2+
node_js:
3+
- "4.1"
4+
- "4.0"
5+
- "0.12"
6+
- "0.11"
7+
- "iojs"

gulpfile.babel.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import gulp from 'gulp'
2+
3+
gulp.task('clean', () => {
4+
let del = require('del')
5+
return del('build')
6+
})
7+
8+
gulp.task('scripts', ['clean'], () => {
9+
let babel = require('gulp-babel')
10+
return gulp.src(['lib/*.js', 'bin/*.js'], { base: process.cwd() })
11+
.pipe(babel())
12+
.pipe(gulp.dest('build'))
13+
})
14+
15+
gulp.task('docs', ['clean'], () => {
16+
return gulp.src(['README.md', 'LICENSE'])
17+
.pipe(gulp.dest('build'))
18+
})
19+
20+
gulp.task('package', ['clean'], () => {
21+
let editor = require('gulp-json-editor')
22+
return gulp.src('./package.json')
23+
.pipe(editor( (p) => {
24+
p.main = 'lib/main'
25+
return p
26+
}))
27+
.pipe(gulp.dest('build'))
28+
})
29+
30+
gulp.task('build', ['scripts', 'package', 'docs'])
31+
32+
gulp.task('lint', () => {
33+
let eslint = require('gulp-eslint')
34+
return gulp.src(['*.js', 'bin/*.js', 'lib/*.js', 'test/*.js'])
35+
.pipe(eslint())
36+
.pipe(eslint.format())
37+
.pipe(eslint.failAfterError())
38+
})
39+
40+
gulp.task('test', ['lint'], function test() {
41+
require('task-tape')
42+
let tape = require('gulp-tape')
43+
let reporter = require('tap-spec')
44+
return gulp.src('test/*.js')
45+
.pipe(tape({
46+
reporter: reporter(),
47+
}))
48+
})
49+
gulp.task('default', ['test'])
50+

index.js

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,3 @@
1-
var bindAsync = require('run-callback').bindAsync;
2-
3-
var noop = function () {};
4-
var undef;
5-
6-
module.exports = sequence;
7-
module.exports.run = run;
8-
module.exports.last = {};
9-
10-
function sequence() {
11-
return run.bind(null, arguments);
12-
}
13-
14-
function run(things, initial, done) {
15-
var res = [];
16-
if (arguments.length < 3) {
17-
if (typeof initial === 'function') {
18-
done = initial;
19-
initial = undef;
20-
}
21-
}
22-
done = done || noop;
23-
24-
(function NEXT(i) {
25-
if (i >= things.length) {
26-
return done(null, res);
27-
}
28-
bind(things[i], res, initial)(function (err, r) {
29-
if (err) {
30-
return done(err, res);
31-
}
32-
res.push(r);
33-
NEXT(++i);
34-
});
35-
}(0));
36-
}
37-
38-
function bind(fn, res, initial) {
39-
var args = [].concat(fn).map(function (j) {
40-
if (j === sequence.last) {
41-
return res.length === 0 ? initial : res[res.length - 1];
42-
}
43-
return j;
44-
});
45-
return bindAsync.apply(null, args);
46-
}
1+
require('babel-core/register')()
2+
module.exports = require('./lib/main')
473

lib/main.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { bindAsync } from 'run-callback'
2+
3+
export var last = {}
4+
export var first = {}
5+
6+
export default function () {
7+
return run.bind(null, arguments)
8+
}
9+
10+
export function run(things, initial, done) {
11+
if (arguments.length < 3) {
12+
if (typeof initial === 'function') {
13+
done = initial
14+
initial = null
15+
}
16+
}
17+
18+
next(things, initial, done || function () {}, [], 0)
19+
}
20+
21+
function next(things, initial, done, res, i) {
22+
if (i >= things.length) {
23+
return done(null, res)
24+
}
25+
bind(things[i], res, initial)((err, r) => {
26+
if (err) {
27+
return done(err, res)
28+
}
29+
res.push(r)
30+
next(things, initial, done, res, ++i)
31+
})
32+
}
33+
34+
function bind(fn, res, initial) {
35+
return bindAsync.apply(null, [].concat(fn).map((j) => {
36+
if (j === last) {
37+
return res.length === 0 ? initial : res[res.length - 1]
38+
}
39+
if (j === first) {
40+
return initial == null ? res[0] : initial
41+
}
42+
return j
43+
}))
44+
}
45+

package.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Make a new callback to run input callbacks in sequence",
55
"main": "index.js",
66
"scripts": {
7-
"test": "eslint . && tap test/*.js"
7+
"test": "gulp"
88
},
99
"repository": {
1010
"type": "git",
@@ -24,10 +24,18 @@
2424
},
2525
"homepage": "https://github.com/zoubin/callback-sequence#readme",
2626
"dependencies": {
27-
"run-callback": "^1.0.0"
27+
"run-callback": "^2.0.0"
2828
},
2929
"devDependencies": {
30-
"eslint": "^1.1.0",
31-
"tap": "^1.3.1"
30+
"babel-core": "^5.8.25",
31+
"del": "^2.0.2",
32+
"gulp": "^3.9.0",
33+
"gulp-babel": "^5.2.1",
34+
"gulp-eslint": "^1.0.0",
35+
"gulp-json-editor": "^2.2.1",
36+
"gulp-tape": "0.0.4",
37+
"tap-spec": "^4.1.0",
38+
"tape": "^4.2.0",
39+
"task-tape": "^0.1.0"
3240
}
3341
}

test/args.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
1-
var test = require('tap').test;
2-
var sequence = require('..');
3-
var run = sequence.run;
1+
import test from 'tape'
2+
import { first, last, run } from '..'
43

5-
test('arguments', function(t) {
6-
t.plan(1);
4+
test('last', function(t) {
5+
t.plan(1)
76
function sum(a, b, next) {
87
process.nextTick(function () {
9-
next(null, a + b);
10-
});
8+
next(null, a + b)
9+
})
1110
}
1211
run(
1312
[
14-
[sum, sequence.last, 1],
15-
[sum, sequence.last, 1],
16-
[sum, sequence.last, 1],
13+
[sum, last, 1],
14+
[sum, last, 1],
15+
[sum, last, 1],
1716
],
1817
1,
19-
function (err, res) {
20-
t.same(res, [2, 3, 4]);
18+
(err, res) => {
19+
t.same(res, [2, 3, 4])
2120
}
22-
);
23-
});
21+
)
22+
})
23+
24+
test('first', function(t) {
25+
t.plan(1)
26+
function order(a, b, next) {
27+
process.nextTick(() => {
28+
next(null, a + b)
29+
})
30+
}
31+
run(
32+
[
33+
[order, first, 1],
34+
[order, first, 2],
35+
[order, first, 3],
36+
],
37+
10,
38+
(err, res) => {
39+
t.same(res, [11, 12, 13])
40+
}
41+
)
42+
})
2443

0 commit comments

Comments
 (0)