/* 
Name:         fibonacci
Description:  This function calculates fibonacci numbers for one or endless iterations.
Source:       https://github.com/fvdm/nodejs-fibonacci
Contact:      https://github.com/fvdm/nodejs-fibonacci/issues
License:      Copyleft - You can do whatever you want, just don't copyright it.

It returns an object with these elements:

* number     - the number as string
* length     - the number of digits
* iterations - how many iterations it took
* ms         - duration in milliseconds

Usage:
var fibonacci = require('fibonacci');
var bigNumber = fibonacci.iterate( 3000 );
console.log( bigNumber );

Get all numbers:
WARNING: THIS CONTINUES FOREVER !!!
         Kill with fibonacci.kill();

fibonacci.on( 'result', function( result ) {
	console.log( result.iterations +') '+ result.number );
	if( result.ms > 10000 ) {
		fibonacci.kill();
	}
});
fibonacci.iterate();
*/

var	bignum = require('bignum'),
	EventEmitter = require('events').EventEmitter;

var app = new EventEmitter();

app.iterate = function( limit ) {
	var	next = bignum(1),
		cur = bignum(-1),
		last = bignum(0),
		loop = bignum(0),
		start = new Date().getTime();
	
	app.doWhile = true;
	
	while( app.doWhile ) {
		last = cur;	// prev cur -> now last
		cur = next;	// prev next -> now cur
		next = cur.add(last);
		
		var result = {
			number:		next.toString(),
			length:		next.toString().length,
			iterations:	loop.toString(),
			ms:		new Date().getTime() - start
		}
		
		app.emit( 'result', result );
		
		// found the one
		if( limit !== undefined && loop == limit ) {
			app.doWhile = false;
			app.emit( 'done', result );
			return result;
		}
		
		// catch infinity
		if( next == 'Infinity' ) {
			app.doWhile = false;
			app.emit( 'stop', {
				reason:		'infinity',
				max_limit:	Number.MAX_LIMIT.toString(),
				last_result:	result,
				iterations:	loop.toString(),
				intended:	limit ? limit : false
			});
			break;
		}
		
		// count
		loop = loop.add(1);
	}
}

app.kill = function() {
	app.doWhile = false;
}

// ready
module.exports = app;

@@ -922,19 +922,22 @@
 bignum')
-,%0A%09
+;%0Avar 
 EventEmi
@@ -1062,20 +1062,24 @@
 ignum(1)
-,%0A%09%09
+;%0A  var 
 cur = bi
@@ -1086,20 +1086,24 @@
 gnum(-1)
-,%0A%09%09
+;%0A  var 
 last = b
@@ -1110,20 +1110,24 @@
 ignum(0)
-,%0A%09%09
+;%0A  var 
 loop = b
@@ -1134,20 +1134,24 @@
 ignum(0)
-,%0A%09%09
+;%0A  var 
 start = 
@@ -1351,18 +1351,17 @@
 %09number:
-%09%09
+ 
 next.toS
@@ -1379,18 +1379,17 @@
 %09length:
-%09%09
+ 
 next.toS
@@ -1410,33 +1410,33 @@
 ,%0A%09%09%09iterations:
-%09
+ 
 loop.toString(),
@@ -1442,18 +1442,17 @@
 ,%0A%09%09%09ms:
-%09%09
+ 
 new Date
@@ -1767,18 +1767,17 @@
 %09reason:
-%09%09
+ 
 'infinit
@@ -1794,17 +1794,17 @@
 x_limit:
-%09
+ 
 Number.M
@@ -1840,17 +1840,17 @@
 _result:
-%09
+ 
 result,%0A
@@ -1864,17 +1864,17 @@
 rations:
-%09
+ 
 loop.toS
@@ -1895,17 +1895,17 @@
 ntended:
-%09
+ 
 limit ? 

Clean up whitespace