/src/Iterator.js
JavaScript | 5493 lines | 2780 code | 27 blank | 2686 comment | 585 complexity | 8d9d63e74320019e6aa96e877f271dcb MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
- // Define iteration methods. (internal)
- (function() {
- var LightIterator,
- QuickIteration,
- createLightIterateConstructor,
- createSyncIterator;
- update(PotInternal, {
- /**
- * @lends Pot.Internal
- */
- /**
- * LightIterator.
- *
- * Async/Sync iterator.
- *
- * @class
- * @private
- * @constructor
- * @ignore
- */
- LightIterator : update(function(object, callback, options) {
- return new LightIterator.fn.doit(
- object, callback, options
- );
- }, {
- /**@ignore*/
- speeds : {
- limp : -1,
- doze : 0,
- slow : 2,
- normal : 5,
- fast : 12,
- rapid : 36,
- ninja : 60
- },
- /**@ignore*/
- delays : {
- limp : 1000,
- doze : 100,
- slow : 13,
- normal : 0,
- fast : 0,
- rapid : 0,
- ninja : 0
- },
- /**@ignore*/
- types : {
- forLoop : 0x01,
- forInLoop : 0x02,
- repeat : 0x04,
- forEver : 0x08,
- iterate : 0x10,
- items : 0x20,
- zip : 0x40
- }
- })
- });
- // Refer the Pot properties/functions.
- PotInternalLightIterator = LightIterator = PotInternal.LightIterator;
- update(LightIterator, {
- /**@ignore*/
- defaults : {
- speed : LightIterator.speeds.normal
- },
- /**@ignore*/
- revSpeeds : {}
- });
- each(LightIterator.speeds, function(v, k) {
- LightIterator.revSpeeds[v] = k;
- });
- LightIterator.fn = LightIterator.prototype =
- update(LightIterator.prototype, {
- /**
- * @lends Pot.Internal.LightIterator.prototype
- */
- /**
- * @ignore
- */
- constructor : LightIterator,
- /**
- * @private
- * @ignore
- */
- interval : LightIterator.defaults.speed,
- /**
- * @private
- * @ignore
- */
- iter : null,
- /**
- * @private
- * @ignore
- */
- result : null,
- /**
- * @private
- * @ignore
- */
- deferred : null,
- /**
- * @private
- * @ignore
- */
- revDeferred : null,
- /**
- * @private
- * @ignore
- */
- isDeferStopIter : false,
- /**
- * @private
- * @ignore
- */
- time : {},
- /**
- * @private
- * @ignore
- */
- waiting : false,
- /**
- * @private
- * @ignore
- */
- restable : false,
- /**
- * @private
- * @ignore
- */
- async : false,
- /**
- * @private
- * @ignore
- */
- options : null,
- /**
- * @private
- * @ignore
- */
- doit : function(object, callback, options) {
- this.setOptions(options);
- this.execute(object, callback);
- this.watch();
- return this;
- },
- /**
- * Set the options.
- *
- * @private
- * @ignore
- */
- setOptions : function(options) {
- this.options = options || {};
- this.setInterval();
- this.setAsync();
- },
- /**
- * Set the interval option.
- *
- * @private
- * @ignore
- */
- setInterval : function() {
- var n = null;
- if (isNumeric(this.options.interval)) {
- n = this.options.interval - 0;
- } else if (this.options.interval in LightIterator.speeds) {
- n = LightIterator.speeds[this.options.interval] - 0;
- }
- if (n !== null && !isNaN(n)) {
- this.interval = n;
- }
- if (!isNumeric(this.interval)) {
- this.interval = LightIterator.defaults.speed;
- }
- },
- /**
- * Set the async option.
- *
- * @private
- * @ignore
- */
- setAsync : function() {
- var a = null;
- if (this.options.async !== void 0) {
- a = !!this.options.async;
- }
- if (a !== null) {
- this.async = !!a;
- }
- if (!isBoolean(this.async)) {
- this.async = !!this.async;
- }
- },
- /**
- * Create a new Deferred.
- *
- * @private
- * @ignore
- */
- createDeferred : function() {
- return new Deferred({ async : false });
- },
- /**
- * Watch the process.
- *
- * @private
- * @ignore
- */
- watch : function() {
- var that = this;
- if (!this.async && this.waiting === true && PotSystem.isWaitable) {
- Pot.XPCOM.throughout(function() {
- return that.waiting !== true;
- });
- }
- },
- /**
- * Execute process.
- *
- * @private
- * @ignore
- */
- execute : function(object, callback) {
- var d, that = this;
- this.waiting = true;
- if (!object) {
- this.result = {};
- this.waiting = false;
- } else {
- this.waiting = true;
- this.restable = true;
- this.time = {
- start : now(),
- total : null,
- loop : null,
- diff : null,
- risk : null,
- axis : null,
- count : 1,
- rest : 100,
- limit : 255
- };
- this.setIter(object, callback);
- if (!this.async && !PotSystem.isWaitable) {
- this.revback();
- this.waiting = false;
- } else {
- d = this.createDeferred();
- d.then(function() {
- var d1 = that.createDeferred(),
- d2 = that.createDeferred();
- d1.then(function() {
- return that.revolve().then(function() {
- d2.begin();
- });
- }).begin();
- return d2;
- }).ensure(function() {
- that.waiting = false;
- });
- if (this.async) {
- this.deferred = d.then(function() {
- if (isDeferred(that.result) &&
- isStopIter(Deferred.lastError(that.result))) {
- that.result = Deferred.lastResult(that.result);
- }
- return that.result;
- });
- }
- this.flush(d);
- }
- }
- },
- /**
- * @private
- * @ignore
- */
- setIter : function(object, callback) {
- var type = this.options.type,
- types = LightIterator.types,
- context = this.options.context;
- switch (true) {
- case ((type & types.iterate) === types.iterate):
- this.result = null;
- this.iter = this.iterate(object, callback, context);
- break;
- case ((type & types.forEver) === types.forEver):
- this.result = {};
- this.iter = this.forEver(object, context);
- break;
- case ((type & types.repeat) === types.repeat):
- this.result = {};
- this.iter = this.repeat(object, callback, context);
- break;
- case ((type & types.items) === types.items):
- this.result = [];
- this.iter = this.items(object, callback, context);
- break;
- case ((type & types.zip) === types.zip):
- this.result = [];
- this.iter = this.zip(object, callback, context);
- break;
- default:
- if (isArrayLike(object)) {
- this.result = object;
- this.iter = this.forLoop(object, callback, context);
- } else {
- this.result = object;
- this.iter = this.forInLoop(object, callback, context);
- }
- }
- },
- /**
- * @private
- * @ignore
- */
- revback : function() {
- var that = this, result, err, cutback = false, time;
- this.time.loop = now();
- REVOLVE: {
- do {
- try {
- if (this.isDeferStopIter) {
- this.isDeferStopIter = false;
- throw PotStopIteration;
- }
- result = this.iter.next();
- } catch (e) {
- err = e;
- if (isStopIter(err)) {
- break REVOLVE;
- }
- throw err;
- }
- if (this.async && isDeferred(result)) {
- return result.ensure(function(res) {
- if (res !== void 0) {
- if (isError(res)) {
- if (isStopIter(res)) {
- that.isDeferStopIter = true;
- if (isDeferred(that.result) &&
- isStopIter(Deferred.lastError(that.result))) {
- that.result = Deferred.lastResult(that.result);
- }
- } else {
- Deferred.lastError(this, res);
- }
- } else {
- Deferred.lastResult(this, res);
- }
- }
- that.flush(that.revback, true);
- });
- }
- time = now();
- if (PotSystem.isWaitable) {
- if (this.time.total === null) {
- this.time.total = time;
- } else if (time - this.time.total >= this.time.rest) {
- Pot.XPCOM.throughout(0);
- this.time.total = now();
- }
- } else if (!this.async) {
- if (this.restable && this.time.count >= this.time.limit) {
- this.restable = false;
- }
- }
- this.time.risk = time - this.time.start;
- this.time.diff = time - this.time.loop;
- if (this.time.diff >= this.interval) {
- if (this.async &&
- this.interval < LightIterator.speeds.normal) {
- cutback = true;
- } else if (this.async || this.restable || PotSystem.isWaitable) {
- if (this.time.diff < this.interval + 8) {
- this.time.axis = 2;
- } else if (this.time.diff < this.interval + 36) {
- this.time.axis = 5;
- } else if (this.time.diff < this.interval + 48) {
- this.time.axis = 7;
- } else {
- this.time.axis = 10;
- }
- if (this.time.axis >= 10 ||
- (Math.random() * 10 < this.time.axis)) {
- cutback = true;
- }
- }
- }
- } while (!cutback);
- if (this.time.count <= this.time.limit) {
- this.time.count++;
- }
- return this.flush(this.revback, true);
- }
- if (isDeferred(this.revDeferred)) {
- this.revDeferred.begin();
- }
- },
- /**
- * Revolve the process.
- *
- * @private
- * @ignore
- */
- revolve : function() {
- var that = this,
- d = this.createDeferred(),
- de = this.createDeferred();
- d.then(function() {
- var dd = that.createDeferred();
- that.revDeferred = that.createDeferred();
- dd.then(function() {
- return that.revback();
- }).begin();
- return that.revDeferred;
- }).ensure(function(er) {
- de.begin();
- if (isError(er)) {
- throw er;
- }
- });
- this.flush(d);
- return de;
- },
- /**
- * Flush the callback.
- *
- * @private
- * @ignore
- */
- flush : function(callback, useSpeed) {
- var that = this, d, lazy = false, speed, speedKey;
- if (this.async || PotSystem.isWaitable) {
- lazy = true;
- }
- if (!lazy && isFunction(callback)) {
- return callback.call(this);
- } else {
- d = this.createDeferred();
- d.then(function() {
- if (isDeferred(callback)) {
- callback.begin();
- } else {
- callback.call(that);
- }
- });
- if (lazy) {
- speed = 0;
- if (useSpeed) {
- speedKey = LightIterator.revSpeeds[this.interval];
- if (speedKey &&
- isNumeric(LightIterator.delays[speedKey])) {
- speed = LightIterator.delays[speedKey];
- }
- if (Math.random() * 10 < Math.max(2, (this.time.axis || 2) / 2.75)) {
- speed += Math.min(
- this.time.rest,
- Math.max(1,
- Math.ceil(
- (this.time.risk / (this.time.rest + this.time.diff)) +
- this.time.diff
- )
- )
- );
- }
- }
- PotInternalSetTimeout(function() {
- d.begin();
- }, speed);
- } else {
- d.begin();
- }
- }
- },
- /**
- * Return noop function.
- *
- * @private
- * @ignore
- */
- noop : function() {
- return {
- /**@ignore*/
- next : function() {
- throw PotStopIteration;
- }
- };
- },
- /**
- * forEver.
- *
- * @private
- * @ignore
- */
- forEver : function(callback, context) {
- var i = 0;
- if (!isFunction(callback)) {
- return this.noop();
- }
- return {
- /**@ignore*/
- next : function() {
- var result = callback.call(context, i);
- try {
- if (!isFinite(++i) || i >= Number.MAX_VALUE) {
- throw 0;
- }
- } catch (ex) {
- i = 0;
- }
- return result;
- }
- };
- },
- /**
- * repeat.
- *
- * @private
- * @ignore
- */
- repeat : function(max, callback, context) {
- var i, loops, n, last;
- if (!isFunction(callback)) {
- return this.noop();
- }
- if (!max || max == null) {
- n = 0;
- } else if (isNumeric(max)) {
- n = max - 0;
- } else {
- n = max || {};
- if (isNumeric(n.start)) {
- n.begin = n.start;
- }
- if (isNumeric(n.stop)) {
- n.end = n.stop;
- }
- }
- loops = {
- begin : isNumeric(n.begin) ? n.begin - 0 : 0,
- end : isNumeric(n.end) ? n.end - 0 : (n || 0) - 0,
- step : isNumeric(n.step) ? n.step - 0 : 1,
- last : false,
- prev : null
- };
- i = loops.step ? loops.begin : loops.end;
- last = loops.end - loops.step;
- return {
- /**@ignore*/
- next : function() {
- var result;
- if (i < loops.end) {
- loops.last = (i >= last);
- result = callback.call(context, i, loops.last, loops);
- loops.prev = result;
- } else {
- throw PotStopIteration;
- }
- i += loops.step;
- return result;
- }
- };
- },
- /**
- * forLoop.
- *
- * @private
- * @ignore
- */
- forLoop : function(object, callback, context) {
- var copy, i = 0;
- if (!object || !object.length || !isFunction(callback)) {
- return this.noop();
- }
- copy = arrayize(object);
- return {
- /**@ignore*/
- next : function() {
- var val, result;
- while (true) {
- if (i >= copy.length) {
- throw PotStopIteration;
- }
- if (!(i in copy)) {
- i++;
- continue;
- }
- try {
- val = copy[i];
- } catch (e) {
- i++;
- continue;
- }
- result = callback.call(context, val, i, object);
- i++;
- return result;
- }
- }
- };
- },
- /**
- * forInLoop.
- *
- * @private
- * @ignore
- */
- forInLoop : function(object, callback, context) {
- var copy, i = 0;
- //XXX: Should use "yield" for duplicate loops.
- if (isFunction(callback)) {
- copy = [];
- each(object, function(value, prop) {
- copy[copy.length] = [value, prop];
- });
- }
- if (!copy || !copy.length) {
- return this.noop();
- }
- return {
- /**@ignore*/
- next : function() {
- var result, c, key, val;
- while (true) {
- if (i >= copy.length) {
- throw PotStopIteration;
- }
- if (!(i in copy)) {
- i++;
- continue;
- }
- try {
- c = copy[i];
- val = c[0];
- key = c[1];
- } catch (e) {
- i++;
- continue;
- }
- result = callback.call(context, val, key, object);
- i++;
- return result;
- }
- }
- };
- },
- /**
- * iterate.
- *
- * @private
- * @ignore
- */
- iterate : function(object, callback, context) {
- var that = this, iterable;
- if (Pot.isIterable(object) && !Pot.isIter(object)) {
- // using "yield" generator.
- if (isFunction(callback)) {
- return {
- /**@ignore*/
- next : function() {
- var res = object.next();
- that.result = callback.apply(context, arrayize(res));
- return that.result;
- }
- };
- } else {
- return {
- /**@ignore*/
- next : function() {
- that.result = object.next();
- return that.result;
- }
- };
- }
- } else {
- iterable = Iter.toIter(object);
- if (!isIter(iterable)) {
- return this.noop();
- }
- if (isFunction(callback)) {
- return {
- /**@ignore*/
- next : function() {
- var results = iterable.next();
- results = arrayize(results);
- while (results.length < 2) {
- results.push((void 0));
- }
- results.push(object);
- that.result = callback.apply(context, results);
- return that.result;
- }
- };
- } else {
- return {
- /**@ignore*/
- next : function() {
- that.result = iterable.next();
- return that.result;
- }
- };
- }
- }
- },
- /**
- * items format loop.
- *
- * @private
- * @ignore
- */
- items : function(object, callback, context) {
- var that = this, copy, i = 0, isPair;
- if (isObject(object)) {
- copy = [];
- each(object, function(ov, op) {
- copy[copy.length] = [op, ov];
- });
- isPair = true;
- } else if (isArrayLike(object)) {
- copy = arrayize(object);
- }
- if (!copy || !copy.length) {
- return this.noop();
- }
- if (isFunction(callback)) {
- return {
- /**@ignore*/
- next : function() {
- var result, c, key, val;
- while (true) {
- if (i >= copy.length) {
- throw PotStopIteration;
- }
- if (!(i in copy)) {
- i++;
- continue;
- }
- try {
- c = copy[i];
- if (isPair) {
- key = c[0];
- val = c[1];
- } else {
- key = i;
- val = c;
- }
- } catch (e) {
- i++;
- continue;
- }
- result = callback.call(context, [key, val], object);
- i++;
- that.result[that.result.length] = result;
- return result;
- }
- }
- };
- } else {
- return {
- /**@ignore*/
- next : function() {
- var r, t, k, v;
- while (true) {
- if (i >= copy.length) {
- throw PotStopIteration;
- }
- if (!(i in copy)) {
- i++;
- continue;
- }
- try {
- t = copy[i];
- if (isPair) {
- k = t[0];
- v = t[1];
- } else {
- k = i;
- v = t;
- }
- } catch (e) {
- i++;
- continue;
- }
- i++;
- r = [k, v];
- that.result[that.result.length] = r;
- return r;
- }
- }
- };
- }
- },
- /**
- * zip iteration.
- *
- * @private
- * @ignore
- */
- zip : function(object, callback, context) {
- var that = this, copy, i = 0, max;
- if (isArrayLike(object)) {
- copy = arrayize(object);
- max = copy.length;
- }
- if (!max || !copy || !copy.length) {
- return this.noop();
- }
- if (isFunction(callback)) {
- return {
- /**@ignore*/
- next : function() {
- var result, zips = [], j, item;
- for (j = 0; j < max; j++) {
- item = arrayize(copy[j]);
- if (!item || !item.length || i >= item.length) {
- throw PotStopIteration;
- }
- zips[zips.length] = item[i];
- }
- result = callback.call(context, zips, object);
- that.result[that.result.length] = result;
- i++;
- return result;
- }
- };
- } else {
- return {
- /**@ignore*/
- next : function() {
- var z = [], k, t;
- for (k = 0; k < max; k++) {
- t = arrayize(copy[k]);
- if (!t || !t.length || i >= t.length) {
- throw PotStopIteration;
- }
- z[z.length] = t[i];
- }
- that.result[that.result.length] = z;
- i++;
- return z;
- }
- };
- }
- }
- });
- LightIterator.fn.doit.prototype = LightIterator.fn;
- // Update internal synchronous iteration.
- update(LightIterator, {
- /**
- * @lends Pot.Internal.LightIterator
- */
- /**
- * Quick iteration for synchronous.
- *
- * @type Object
- * @class
- * @private
- * @ignore
- */
- QuickIteration : {
- /**
- * @lends Pot.Internal.LightIterator.QuickIteration
- */
- /**
- * @private
- * @ignore
- */
- resolve : function(iter) {
- var err;
- try {
- while (true) {
- iter.next();
- }
- } catch (e) {
- err = e;
- if (!isStopIter(err)) {
- throw err;
- }
- }
- },
- /**
- * @private
- * @ignore
- */
- forEach : function(object, callback, context) {
- var result, iter, that = LightIterator.fn;
- if (!object) {
- result = {};
- } else {
- result = object;
- if (isArrayLike(object)) {
- iter = that.forLoop(object, callback, context);
- } else {
- iter = that.forInLoop(object, callback, context);
- }
- QuickIteration.resolve(iter);
- }
- return result;
- },
- /**
- * @private
- * @ignore
- */
- repeat : function(max, callback, context) {
- var result = {}, iter, that = LightIterator.fn;
- if (max) {
- iter = that.repeat(max, callback, context);
- QuickIteration.resolve(iter);
- }
- return result;
- },
- /**
- * @private
- * @ignore
- */
- forEver : function(callback, context) {
- var result = {}, iter, that = LightIterator.fn;
- if (callback) {
- iter = that.forEver(callback, context);
- QuickIteration.resolve(iter);
- }
- return result;
- },
- /**
- * @private
- * @ignore
- */
- iterate : function(object, callback, context) {
- var result, iter, o, that = LightIterator.fn;
- if (!object) {
- result = {};
- } else {
- result = null;
- o = {
- noop : that.noop,
- result : null
- };
- iter = that.iterate.call(o, object, callback, context);
- QuickIteration.resolve(iter);
- result = o.result;
- }
- return result;
- },
- /**
- * @private
- * @ignore
- */
- items : function(object, callback, context) {
- var result = [], iter, o, that = LightIterator.fn;
- if (object) {
- o = {
- noop : that.noop,
- result : []
- };
- iter = that.items.call(o, object, callback, context);
- QuickIteration.resolve(iter);
- result = o.result;
- }
- return result;
- },
- /**
- * @private
- * @ignore
- */
- zip : function(object, callback, context) {
- var result = [], iter, o, that = LightIterator.fn;
- if (object) {
- o = {
- noop : that.noop,
- result : []
- };
- iter = that.zip.call(o, object, callback, context);
- QuickIteration.resolve(iter);
- result = o.result;
- }
- return result;
- }
- }
- });
- QuickIteration = LightIterator.QuickIteration;
- //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
- // Define the main iterators.
- // Temporary creation function.
- update(PotTmp, {
- /**
- * @lends Pot.tmp
- */
- /**
- * @private
- * @ignore
- */
- createLightIterateConstructor : function(creator) {
- var
- name,
- /**@ignore*/
- create = function(speed) {
- var interval;
- if (LightIterator.speeds[speed] === void 0) {
- interval = LightIterator.defaults.speed;
- } else {
- interval = LightIterator.speeds[speed];
- }
- return creator(interval);
- },
- methods = {},
- construct = create();
- for (name in LightIterator.speeds) {
- methods[name] = create(name);
- }
- return update(construct, methods);
- }
- });
- createLightIterateConstructor = PotTmp.createLightIterateConstructor;
- // Define the iterator functions to evaluate as synchronized.
- Pot.update({
- /**
- * @lends Pot
- */
- /**
- * Iterates as "for each" loop.
- *
- *
- * @desc
- * <pre>
- * Unlike Deferred, speed options affect to cutback count in loop.
- * Options append to after the forEach and execute it.
- *
- * e.g. Pot.forEach.slow(obj, function() {...})
- *
- * The available methods are below.
- * ------------------------------------
- * method name | speed
- * ------------------------------------
- * limp : slowest
- * doze : slower
- * slow : slow
- * normal : normal (default)
- * fast : fast
- * rapid : faster
- * ninja : fastest
- * ------------------------------------
- * </pre>
- *
- *
- * @example
- * var a = 0;
- * Pot.forEach([1, 2, 3], function(value) {
- * a += value;
- * });
- * debug(a);
- * // @results 6
- *
- *
- * @example
- * var a = '';
- * Pot.forEach({a:'foo', b:'bar'}, function(value, key) {
- * a += key + '=' + value + ',';
- * });
- * debug(a);
- * // @results 'a=foo,b=bar,'
- *
- *
- * @param {Array|Object} object A target object.
- * @param {Function} callback An iterable function.
- * function(value, key, object)
- * this == `context`.
- * Throw Pot.StopIteration
- * if you want to stop the loop.
- * @param {*} (context) Optionally, context object. (i.e. this)
- * @result {Object} Return the object.
- * @class
- * @function
- * @static
- * @name Pot.forEach
- * @property {Function} limp Iterates "for each" loop with slowest speed.
- * @property {Function} doze Iterates "for each" loop with slower speed.
- * @property {Function} slow Iterates "for each" loop with slow speed.
- * @property {Function} normal Iterates "for each" loop with default speed.
- * @property {Function} fast Iterates "for each" loop with fast speed.
- * @property {Function} rapid Iterates "for each" loop with faster speed.
- * @property {Function} ninja Iterates "for each" loop with fastest speed.
- */
- forEach : createLightIterateConstructor(function(interval) {
- if (PotSystem.isWaitable &&
- interval < LightIterator.speeds.normal) {
- return function(object, callback, context) {
- var opts = {};
- opts.type = LightIterator.types.forLoop |
- LightIterator.types.forInLoop;
- opts.interval = interval;
- opts.async = false;
- opts.context = context;
- return (new LightIterator(object, callback, opts)).result;
- };
- } else {
- return function(object, callback, context) {
- return QuickIteration.forEach(
- object, callback, context
- );
- };
- }
- }),
- /**
- * "repeat" loop iterates a specified number.
- *
- * The second argument of the callback function is
- * passed the value to true only for the end of the loop.
- *
- * The first argument can pass as object
- * that gives names "begin, end, step" any keys.
- *
- *
- * @example
- * var a = [];
- * Pot.repeat(10, function(i) {
- * a.push(i);
- * });
- * debug(a);
- * // @results [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- *
- *
- * @example
- * //
- * // The second argument of the callback function is
- * // passed the value to true only for the end of the loop.
- * //
- * var s = '', a = 'abcdef'.split('');
- * Pot.repeat(a.length, function(i, last) {
- * s += a[i] + '=' + i + (last ? ';' : ',');
- * });
- * debug(s);
- * // @results 'a=0,b=1,c=2,d=3,e=4,f=5;'
- *
- *
- * @example
- * //
- * // The first argument can pass as object
- * // that gives names "begin, end, step" any keys.
- * //
- * var a = [];
- * Pot.repeat({begin: 0, end: 100, step: 10}, function(i) {
- * a.push(i);
- * });
- * debug(a);
- * // @results [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
- *
- *
- * @param {Number|Object} max The maximum number of times to loop,
- * or object.
- * @param {Function} callback An iterable function.
- * Throw Pot.StopIteration
- * if you want to stop the loop.
- * @param {*} (context) Optionally, context object. (i.e. this)
- * @class
- * @function
- * @static
- * @name Pot.repeat
- * @property {Function} limp Iterates "repeat" loop with slowest speed.
- * @property {Function} doze Iterates "repeat" loop with slower speed.
- * @property {Function} slow Iterates "repeat" loop with slow speed.
- * @property {Function} normal Iterates "repeat" loop with default speed.
- * @property {Function} fast Iterates "repeat" loop with fast speed.
- * @property {Function} rapid Iterates "repeat" loop with faster speed.
- * @property {Function} ninja Iterates "repeat" loop with fastest speed.
- */
- repeat : createLightIterateConstructor(function(interval) {
- if (PotSystem.isWaitable &&
- interval < LightIterator.speeds.normal) {
- return function(max, callback, context) {
- var opts = {};
- opts.type = LightIterator.types.repeat;
- opts.interval = interval;
- opts.async = false;
- opts.context = context;
- return (new LightIterator(max, callback, opts)).result;
- };
- } else {
- return function(max, callback, context) {
- return QuickIteration.repeat(
- max, callback, context
- );
- };
- }
- }),
- /**
- * Iterates indefinitely until "Pot.StopIteration" is thrown.
- *
- *
- * @example
- * var s = '', a = 'abc*';
- * Pot.forEver(function(i) {
- * s += i + ':' + a;
- * if (s.length > 50) {
- * throw Pot.StopIteration;
- * }
- * });
- * debug(s);
- * // @results
- * // '0:abc*1:abc*2:abc*3:abc*4:abc*5:abc*6:abc*7:abc*8:abc*'
- *
- *
- * @param {Function} callback An iterable function.
- * Throw Pot.StopIteration
- * if you want to stop the loop.
- * @param {*} (context) Optionally, context object. (i.e. this)
- * @class
- * @function
- * @static
- * @name Pot.forEver
- * @property {Function} limp Iterates "forEver" loop with slowest speed.
- * @property {Function} doze Iterates "forEver" loop with slower speed.
- * @property {Function} slow Iterates "forEver" loop with slow speed.
- * @property {Function} normal Iterates "forEver" loop with default speed.
- * @property {Function} fast Iterates "forEver" loop with fast speed.
- * @property {Function} rapid Iterates "forEver" loop with faster speed.
- * @property {Function} ninja Iterates "forEver" loop with fastest speed.
- */
- forEver : createLightIterateConstructor(function(interval) {
- if (PotSystem.isWaitable &&
- interval < LightIterator.speeds.normal) {
- return function(callback, context) {
- var opts = {};
- opts.type = LightIterator.types.forEver;
- opts.interval = interval;
- opts.async = false;
- opts.context = context;
- return (new LightIterator(callback, null, opts)).result;
- };
- } else {
- return function(callback, context) {
- return QuickIteration.forEver(
- callback, context
- );
- };
- }
- }),
- /**
- * Iterate an iterable object. (using Pot.Iter)
- *
- * @param {*} object An iterable object.
- * @param {Function} callback An iterable function.
- * function(value, key, object)
- * this == `context`.
- * Throw Pot.StopIteration
- * if you want to stop the loop.
- * @param {Object} (context) Optionally, context object. (i.e. this)
- * @return {*} Result of iteration.
- * @class
- * @function
- * @static
- * @name Pot.iterate
- * @property {Function} limp Iterates "iterate" loop with slowest speed.
- * @property {Function} doze Iterates "iterate" loop with slower speed.
- * @property {Function} slow Iterates "iterate" loop with slow speed.
- * @property {Function} normal Iterates "iterate" loop with default speed.
- * @property {Function} fast Iterates "iterate" loop with fast speed.
- * @property {Function} rapid Iterates "iterate" loop with faster speed.
- * @property {Function} ninja Iterates "iterate" loop with fastest speed.
- */
- iterate : createLightIterateConstructor(function(interval) {
- if (PotSystem.isWaitable &&
- interval < LightIterator.speeds.normal) {
- return function(object, callback, context) {
- var opts = {};
- opts.type = LightIterator.types.iterate;
- opts.interval = interval;
- opts.async = false;
- opts.context = context;
- return (new LightIterator(object, callback, opts)).result;
- };
- } else {
- return function(object, callback, context) {
- return QuickIteration.iterate(
- object, callback, context
- );
- };
- }
- }),
- /**
- * Collect the object key and value and make array as items format.
- *
- *
- * @example
- * var obj = {foo: 1, bar: 2, baz: 3};
- * debug(items(obj));
- * // @results [['foo', 1], ['bar', 2], ['baz', 3]]
- *
- *
- * @example
- * var array = ['foo', 'bar', 'baz'];
- * debug(items(array));
- * // @results [[0, 'foo'], [1, 'bar'], [2, 'baz']]
- *
- *
- * @example
- * // Example for using callback.
- * var arr = ['foo', 'bar', 'baz'];
- * var func = function(item) {
- * return '(' + item[0] + ')' + item[1];
- * };
- * debug(items(arr, func));
- * // @results ['(0)foo', '(1)bar', '(2)baz']
- *
- *
- * @example
- * // Example for using callback.
- * var obj = {foo: 1, bar: 2, baz: 3};
- * var func = function(item) {
- * return [item[0] + '::' + item[1]];
- * };
- * debug(items(obj, func));
- * // @results [['foo::1'], ['bar::2'], ['baz::3']]
- *
- *
- * @param {Object|Array} object The target object or an array.
- * @param {Function} (callback) (Optional) Callback function.
- * function({Array} item[, object])
- * this == `context`.
- * @param {*} (context) (Optional) Object to use
- * as `this` when executing callback.
- * @return {Array} The collected items as an array.
- *
- * @class
- * @function
- * @static
- * @name Pot.items
- *
- * @property {Function} limp Iterates "items" loop with slowest speed.
- * @property {Function} doze Iterates "items" loop with slower speed.
- * @property {Function} slow Iterates "items" loop with slow speed.
- * @property {Function} normal Iterates "items" loop with default speed.
- * @property {Function} fast Iterates "items" loop with fast speed.
- * @property {Function} rapid Iterates "items" loop with faster speed.
- * @property {Function} ninja Iterates "items" loop with fastest speed.
- */
- items : createLightIterateConstructor(function(interval) {
- if (PotSystem.isWaitable &&
- interval < LightIterator.speeds.normal) {
- return function(object, callback, context) {
- var opts = {};
- opts.type = LightIterator.types.items;
- opts.interval = interval;
- opts.async = false;
- opts.context = context;
- return (new LightIterator(object, callback, opts)).result;
- };
- } else {
- return function(object, callback, context) {
- return QuickIteration.items(
- object, callback, context
- );
- };
- }
- }),
- /**
- * Create a new array which has the elements at
- * position ith of the provided arrays.
- * This function is handled as seen from the longitudinal for array
- * that is similar to the zip() function in Python.
- *
- * <pre>
- * Example:
- *
- * arguments: [[1, 2, 3],
- * [4, 5, 6]]
- *
- * results: [[1, 4],
- * [2, 5],
- * [3, 6]]
- * </pre>
- *
- *
- * @link http://docs.python.org/library/functions.html#zip
- *
- *
- * @example
- * var result = zip([[1, 2, 3], [4, 5, 6]]);
- * debug(result);
- * // @results
- * // [[1, 4], [2, 5], [3, 6]]
- * //
- *
- *
- * @example
- * var result = zip([[1, 2, 3], [1, 2, 3, 4, 5]]);
- * debug(result);
- * // @results
- * // [[1, 1], [2, 2], [3, 3]]
- * //
- *
- *
- * @example
- * var result = zip([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]);
- * debug(result);
- * // @results
- * // [[1, 4, 7, 10], [2, 5, 8, 11]]
- * //
- *
- *
- * @example
- * var result = zip(['hoge']);
- * debug(result);
- * // @results
- * // [['hoge']]
- * //
- *
- *
- * @example
- * var result = zip([[1], [2], [3]]);
- * debug(result);
- * // @results
- * // [[1, 2, 3]]
- * //
- *
- *
- * @example
- * var result = zip([[1, 2, 3], ['foo', 'bar', 'baz'], [4, 5]]);
- * debug(result);
- * // @results
- * // [[1, 'foo', 4], [2, 'bar', 5]]
- * //
- *
- *
- * @example
- * var callback = function(items) { return items[0] + items[1]; };
- * var result = zip([[1, 2, 3], [4, 5, 6]], callback);
- * debug(result);
- * // @results [5, 7, 9]
- *
- *
- * @param {Array} object An array to be combined.
- * @param {Function} (callback) (Optional) Callback function.
- * function({Array} items[, {*} object])
- * this == `context`.
- * @param {*} (context) (Optional) Object to use
- * as `this` when executing callback.
- * @return {Array} A new array of arrays created from
- * provided objects.
- *
- * @class
- * @function
- * @static
- * @name Pot.zip
- *
- * @property {Function} limp Iterates "zip" loop with slowest speed.
- * @property {Function} doze Iterates "zip" loop with slower speed.
- * @property {Function} slow Iterates "zip" loop with slow speed.
- * @property {Function} normal Iterates "zip" loop with default speed.
- * @property {Function} fast Iterates "zip" loop with fast speed.
- * @property {Function} rapid Iterates "zip" loop with faster speed.
- * @property {Function} ninja Iterates "zip" loop with fastest speed.
- */
- zip : createLightIterateConstructor(function(interval) {
- if (PotSystem.isWaitable &&
- interval < LightIterator.speeds.normal) {
- return function(object, callback, context) {
- var opts = {};
- opts.type = LightIterator.types.zip;
- opts.interval = interval;
- opts.async = false;
- opts.context = context;
- return (new LightIterator(object, callback, opts)).result;
- };
- } else {
- return function(object, callback, context) {
- return QuickIteration.zip(
- object, callback, context
- );
- };
- }
- })
- });
- // Define iterators for Deferred (Asynchronous)
- update(Deferred, {
- /**
- * Iterates as "for each" loop. (Asynchronous)
- *
- * @desc
- * <pre>
- * Unlike Deferred, speed options affect to cutback count in loop.
- * Options append to after the forEach and execute it.
- *
- * e.g. Pot.Deferred.forEach.fast(obj, function() {...})
- *
- * The available methods are below.
- * ------------------------------------
- * method name | speed
- * ------------------------------------
- * limp : slowest
- * doze : slower
- * slow : slow
- * normal : normal (default)
- * fast : fast
- * rapid : faster
- * ninja : fastest
- * ------------------------------------
- * </pre>
- *
- * @param {Array|Object} object A target object.
- * @param {Function} callback An iterable function.
- * function(value, key, object)
- * this == `context`.
- * Throw Pot.StopIteration
- * if you want to stop the loop.
- * @param {*} (context) Optionally, context object. (i.e. this)
- * @result {Deferred} Return the Deferred.
- * @class
- * @function
- * @public
- * @type Function
- * @name Pot.Deferred.forEach
- * @property {Function} limp Iterates "for each" loop with slowest speed.
- * @property {Function} doze Iterates "for each" loop with slower speed.
- * @property {Function} slow Iterates "for each" loop with slow speed.
- * @property {Function} normal Iterates "for each" loop with default speed.
- * @property {Function} fast Iterates "for each" loop with fast speed.
- * @property {Function} rapid Iterates "for each" loop with faster speed.
- * @property {Function} ninja Iterates "for each" loop with fastest speed.
- */
- forEach : createLightIterateConstructor(function(interval) {
- return function(object, callback, context) {
- var opts = {};
- opts.type = LightIterator.types.forLoop |
- LightIterator.types.forInLoop;
- opts.interval = interval;
- opts.async = true;
- opts.context = context;
- return (new LightIterator(object, callback, opts)).deferred;
- };
- }),
- /**
- * "repeat" loop iterates a specified number. (Asynchronous)
- *
- * @param {Number|Object} max The maximum number of times to loop,
- * or object.
- * @param {Function} callback An iterable function.
- * Throw Pot.StopIteration
- * if you want to stop the loop.
- * @param {*} (context) Optionally, context object. (i.e. this)
- * @return {Deferred} Return the Deferred.
- * @class
- * @function
- * @public
- * @type Function
- * @name Pot.Deferred.repeat
- * @property {Function} limp Iterates "repeat" loop with slowest speed.
- * @property {Function} doze Iterates "repeat" loop with slower speed.
- * @property {Function} slow Iterates "repeat" loop with slow speed.
- * @property {Function} normal Iterates "repeat" loop with default speed.
- * @property {Function} fast Iterates "repeat" loop with fast speed.
- * @property {Function} rapid Iterates "repeat" loop with faster speed.
- * @property {Function} ninja Iterates "repeat" loop with fastest speed.
- */
- repeat : createLightIterateConstructor(function(interval) {
- return function(max, callback, context) {
- var opts = {};
- opts.type = LightIterator.types.repeat;
- opts.interval = interval;
- opts.async = true;
- opts.context = context;
- return (new LightIterator(max, callback, opts)).deferred;
- };
- }),
- /**
- * Iterates indefinitely until "Pot.StopIteration" is thrown. (Asynchronous)
- *
- * @param {Function} callback An iterable function.
- * Throw Pot.StopIteration
- * if you want to stop the loop.
- * @param {*} (context) Optionally, context object. (i.e. this)
- * @return {Deferred} Return the Deferred.
- * @class
- * @function
- * @public
- * @type Function
- * @name Pot.Deferred.forEver
- * @property {Function} limp Iterates "forEver" loop with slowest speed.
- * @property {Function} doze Iterates "forEver" loop with slower speed.
- * @property {Function} slow Iterates "forEver" loop with slow speed.
- * @property {Function} normal Iterates "forEver" loop with default speed.
- * @property {Function} fast Iterates "forEver" loop with fast speed.
- * @property {Function} rapid Iterates "forEver" loop with faster speed.
- * @property {Function} ninja Iterates "forEver" loop with fastest speed.
- */
- forEver : createLightIterateConstructor(function(interval) {
- return function(callback, context) {
- var opts = {};
- opts.type = LightIterator.types.forEver;
- opts.interval = interval;
- opts.async = true;
- opts.context = context;
- return (new LightIterator(callback, null, opts)).deferred;
- };
- }),
- /**
- * Iterate an iterable object. (using Pot.Iter)
- *
- * @param {*} object An iterable object.
- * @param {Function} callback An iterable function.
- * function(value, key, object)
- * this == `context`.
- * Throw Pot.StopIteration
- * if you want to stop the loop.
- * @param {Object} (context) Optionally, context object. (i.e. this)
- * @return {Deferred} Return the Deferred.
- * @class
- * @function
- * @public
- * @type Function
- * @name Pot.Deferred.iterate
- * @property {Function} limp Iterates "iterate" loop with slowest speed.
- * @property {Function} doze Iterates "iterate" loop with slower speed.
- * @property {Function} slow Iterates "iterate" loop with slow speed.
- * @property {Function} normal Iterates "iterate" loop with default speed.
- * @property {Function} fast Iterates "iterate" loop with fast speed.
- * @property {Function} rapid Iterates "iterate" loop with faster speed.
- * @property {Function} ninja Iterates "iterate" loop with fastest speed.
- */
- iterate : createLightIterateConstructor(function(interval) {
- return function(object, callback, context) {
- var opts = {};
- opts.type = LightIterator.types.iterate;
- opts.interval = interval;
- opts.async = true;
- opts.context = context;
- return (new LightIterator(object, callback, opts)).deferred;
- };
- }),
- /**
- * Collect the object key and value and make array as items format.
- *
- * @param {Object|Array} object The target object or an array.
- * @param {Function} (callback) (Optional) Callback function.
- * function({Array} item[, object])
- * this == `context`.
- * @param {*} (context) (Optional) Object to use
- * as `this` when executing callback.
- * @return {Deferred} Return a new instance of Deferred that
- * has the collected items as an array.
- *
- * @class
- * @function
- * @public
- * @type Function
- * @name Pot.Deferred.items
- *
- * @property {Function} limp Iterates "items" loop with slowest speed.
- * @property {Function} doze Iterates "items" loop with slower speed.
- * @property {Function} slow Iterates "items" loop with slow speed.
- * @property {Function} normal Iterates "items" loop with default speed.
- * @property {Function} fast Iterates "items" loop with fast speed.
- * @property {Function} rapid Iterates "items" loop with faster speed.
- * @property {Function} ninja Iterates "items" loop with fastest speed.
- */
- items : createLightIterateConstructor(function(interval) {
- return function(object, callback, context) {
- var opts = {};
- opts.type = LightIterator.types.items;
- opts.interval = interval;
- opts.async = true;
- opts.context = context;
- return (new LightIterator(object, callback, opts)).deferred;
- };
- }),
- /**
- * Create a new array which has the elements at
- * position ith of the provided arrays.
- * This function is handled as seen from the longitudinal for array
- * that is similar to the zip() function in Python.
- *
- * <pre>
- * Example:
- *
- * arguments: [[1, 2, 3],
- * [4, 5, 6]]
- *
- * results: [[1, 4],
- * [2, 5],
- * [3, 6]]
- * </pre>
- *
- * @link http://docs.python.org/library/functions.html#zip
- *
- * @param {Array} object Objects to be combined.
- * @param {Function} (callback) (Optional) Callback function.
- * function({Array} items[, {*} object])
- * this == `context`.
- * @param {*} (context) (Optional) Object to use
- * as `this` when executing callback.
- * @return {Deferred} Return a new instance of Deferred that has
- * a new array of arrays created from
- * provided objects.
- * @class
- * @function
- * @public
- * @type Function
- * @name Pot.Deferred.zip
- *
- * @property {Function} limp Iterates "zip" loop with slowest speed.
- * @property {Function} doze Iterates "zip" loop with slower speed.
- * @property {Function} slow Iterates "zip" loop with slow speed.
- * @property {Function} normal Iterates "zip" loop with default speed.
- * @property {Function} fast Iterates "zip" loop with fast speed.
- * @property {Function} rapid Iterates "zip" loop with faster speed.
- * @property {Function} ninja Iterates "zip" loop with fastest speed.
- */
- zip : createLightIterateConstructor(function(interval) {
- return function(object, callback, context) {
- var opts = {};
- opts.type = LightIterator.types.zip;
- opts.interval = interval;
- opts.async = true;
- opts.context = context;
- return (new LightIterator(object, callback, opts)).deferred;
- };
- })
- });
- delete PotTmp.createLightIterateConstructor;
- //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
- // Definition of Iter.
- Pot.update({
- /**
- * @lends Pot
- */
- /**
- * Iter.
- *
- * A Simple iterator.
- * Constructor.
- *
- * @param {*} Options.
- * @return {Pot.Iter} Returns an instance of Pot.Iter
- *
- * @name Pot.Iter
- * @class
- * @constructor
- * @public
- */
- Iter : function() {
- return isIter(this) ? this.init(arguments)
- : new Iter.fn.init(arguments);
- }
- });
- // Refer the Pot properties/functions.
- Iter = Pot.Iter;
- // Definition of the prototype
- Iter.fn = Iter.prototype = update(Iter.prototype, {
- /**
- * @lends Pot.Iter.…
Large files files are truncated, but you can click here to view the full file