Skip to content

Commit

Permalink
fixed bug for jump, add reset function to Flow object
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoazhen committed Dec 3, 2013
1 parent 09ab91d commit 605003e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
55 changes: 30 additions & 25 deletions src/Flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,33 @@
var _ = require('underscore');

var undef = undefined;
var ArrayProto = Array.prototype;
var ObjProto = Object.prototype;
var FuncProto = Function.prototype;
var slice = Array.prototype.slice;
var ArrayProto = Array.prototype;
var ObjProto = Object.prototype;
var FuncProto = Function.prototype;
var slice = Array.prototype.slice;
var hasOwnProperty = ObjProto.hasOwnProperty;
var nativeSome = ArrayProto.some;
var nativeBind = FuncProto.bind;
var nativeForEach = ArrayProto.forEach;
var nativeSome = ArrayProto.some;
var nativeBind = FuncProto.bind;
var nativeForEach = ArrayProto.forEach;
var noop = function () {
};

var isFunction = function(obj) {
return typeof obj === 'function';
};
var isFunction = function (obj) {
return typeof obj === 'function';
};

// Establish the object that gets returned to break out of a loop iteration.
var breaker = {};
// Reusable constructor function for prototype setting.
var ctor = function(){};
var ctor = function () {
};

var bind = function(func, context){
var bind = function (func, context) {
var bound, args;
if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
if (!isFunction(func)) throw new TypeError;
args = slice.call(arguments, 2);
return bound = function() {
return bound = function () {
if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
ctor.prototype = func.prototype;
var self = new ctor;
Expand All @@ -45,17 +46,17 @@
// The cornerstone, an `each` implementation, aka `forEach`.
// Handles objects with the built-in `forEach`, arrays, and raw objects.
// Delegates to **ECMAScript 5**'s native `forEach` if available.
var each = function(obj, iterator, context){
if(obj == null) return;
if(nativeForEach && obj.forEach === nativeForEach){
var each = function (obj, iterator, context) {
if (obj == null) return;
if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context);
}
else if(obj.length === +obj.length){
else if (obj.length === +obj.length) {
for (var i = 0, l = obj.length; i < l; i++) {
if (iterator.call(context, obj[i], i, obj) === breaker) return;
}
}
else{
else {
for (var key in obj) {
if (hasOwnProperty.call(obj, key)) {
if (iterator.call(context, obj[key], key, obj) === breaker) return;
Expand All @@ -66,21 +67,21 @@

// Determine if at least one element in the object matches a truth test.
// Delegates to **ECMAScript 5**'s native `some` if available.
var some = function(obj, iterator, context){
var some = function (obj, iterator, context) {
var result = false;
if (obj == null) return result;
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);

each(obj, function(value, index, list) {
each(obj, function (value, index, list) {
if (result || (result = iterator.call(context, value, index, list))) return breaker;
});
return !!result;
};

// Return the first value which passes a truth test.
var find = function(obj, iterator, context) {
var find = function (obj, iterator, context) {
var result;
some(obj, function(value, index, list) {
some(obj, function (value, index, list) {
if (iterator.call(context, value, index, list)) {
result = value;
return true;
Expand Down Expand Up @@ -319,6 +320,10 @@
return this;
};

p.reset = function () {
this.index = -1;
};

// Run this step and keep current process
p.action = function () {

Expand Down Expand Up @@ -366,10 +371,10 @@

// Go to special process with index
p.goto = p.jump = function (index) {
index--;

if (index >= -1 && index < this.steps.length) {
if (index > -1 && index < this.steps.length) {
this.index = index;
this.action.apply(this, arguments);
}

return this;
Expand Down Expand Up @@ -410,7 +415,7 @@
var mapping = {"underscore": "_"};

factory(function (value) {
if(mapping[value]){
if (mapping[value]) {
value = mapping[value];
}
return window[value];
Expand Down
10 changes: 7 additions & 3 deletions src/Flow_underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@
return this;
};

p.reset = function () {
this.index = -1;
};

// Run this step and keep current process
p.action = function () {

Expand Down Expand Up @@ -290,10 +294,10 @@

// Go to special process with index
p.goto = p.jump = function (index) {
index--;

if (index >= -1 && index < this.steps.length) {
if (index > -1 && index < this.steps.length) {
this.index = index;
this.action.apply(this, arguments);
}

return this;
Expand Down Expand Up @@ -333,7 +337,7 @@
var mapping = {"underscore": "_"};

factory(function (value) {
if(mapping[value]){
if (mapping[value]) {
value = mapping[value];
}
return window[value];
Expand Down

0 comments on commit 605003e

Please sign in to comment.