PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/src/Iterator.js

http://github.com/polygonplanet/Pot.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

  1. //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
  2. // Define iteration methods. (internal)
  3. (function() {
  4. var LightIterator,
  5. QuickIteration,
  6. createLightIterateConstructor,
  7. createSyncIterator;
  8. update(PotInternal, {
  9. /**
  10. * @lends Pot.Internal
  11. */
  12. /**
  13. * LightIterator.
  14. *
  15. * Async/Sync iterator.
  16. *
  17. * @class
  18. * @private
  19. * @constructor
  20. * @ignore
  21. */
  22. LightIterator : update(function(object, callback, options) {
  23. return new LightIterator.fn.doit(
  24. object, callback, options
  25. );
  26. }, {
  27. /**@ignore*/
  28. speeds : {
  29. limp : -1,
  30. doze : 0,
  31. slow : 2,
  32. normal : 5,
  33. fast : 12,
  34. rapid : 36,
  35. ninja : 60
  36. },
  37. /**@ignore*/
  38. delays : {
  39. limp : 1000,
  40. doze : 100,
  41. slow : 13,
  42. normal : 0,
  43. fast : 0,
  44. rapid : 0,
  45. ninja : 0
  46. },
  47. /**@ignore*/
  48. types : {
  49. forLoop : 0x01,
  50. forInLoop : 0x02,
  51. repeat : 0x04,
  52. forEver : 0x08,
  53. iterate : 0x10,
  54. items : 0x20,
  55. zip : 0x40
  56. }
  57. })
  58. });
  59. // Refer the Pot properties/functions.
  60. PotInternalLightIterator = LightIterator = PotInternal.LightIterator;
  61. update(LightIterator, {
  62. /**@ignore*/
  63. defaults : {
  64. speed : LightIterator.speeds.normal
  65. },
  66. /**@ignore*/
  67. revSpeeds : {}
  68. });
  69. each(LightIterator.speeds, function(v, k) {
  70. LightIterator.revSpeeds[v] = k;
  71. });
  72. LightIterator.fn = LightIterator.prototype =
  73. update(LightIterator.prototype, {
  74. /**
  75. * @lends Pot.Internal.LightIterator.prototype
  76. */
  77. /**
  78. * @ignore
  79. */
  80. constructor : LightIterator,
  81. /**
  82. * @private
  83. * @ignore
  84. */
  85. interval : LightIterator.defaults.speed,
  86. /**
  87. * @private
  88. * @ignore
  89. */
  90. iter : null,
  91. /**
  92. * @private
  93. * @ignore
  94. */
  95. result : null,
  96. /**
  97. * @private
  98. * @ignore
  99. */
  100. deferred : null,
  101. /**
  102. * @private
  103. * @ignore
  104. */
  105. revDeferred : null,
  106. /**
  107. * @private
  108. * @ignore
  109. */
  110. isDeferStopIter : false,
  111. /**
  112. * @private
  113. * @ignore
  114. */
  115. time : {},
  116. /**
  117. * @private
  118. * @ignore
  119. */
  120. waiting : false,
  121. /**
  122. * @private
  123. * @ignore
  124. */
  125. restable : false,
  126. /**
  127. * @private
  128. * @ignore
  129. */
  130. async : false,
  131. /**
  132. * @private
  133. * @ignore
  134. */
  135. options : null,
  136. /**
  137. * @private
  138. * @ignore
  139. */
  140. doit : function(object, callback, options) {
  141. this.setOptions(options);
  142. this.execute(object, callback);
  143. this.watch();
  144. return this;
  145. },
  146. /**
  147. * Set the options.
  148. *
  149. * @private
  150. * @ignore
  151. */
  152. setOptions : function(options) {
  153. this.options = options || {};
  154. this.setInterval();
  155. this.setAsync();
  156. },
  157. /**
  158. * Set the interval option.
  159. *
  160. * @private
  161. * @ignore
  162. */
  163. setInterval : function() {
  164. var n = null;
  165. if (isNumeric(this.options.interval)) {
  166. n = this.options.interval - 0;
  167. } else if (this.options.interval in LightIterator.speeds) {
  168. n = LightIterator.speeds[this.options.interval] - 0;
  169. }
  170. if (n !== null && !isNaN(n)) {
  171. this.interval = n;
  172. }
  173. if (!isNumeric(this.interval)) {
  174. this.interval = LightIterator.defaults.speed;
  175. }
  176. },
  177. /**
  178. * Set the async option.
  179. *
  180. * @private
  181. * @ignore
  182. */
  183. setAsync : function() {
  184. var a = null;
  185. if (this.options.async !== void 0) {
  186. a = !!this.options.async;
  187. }
  188. if (a !== null) {
  189. this.async = !!a;
  190. }
  191. if (!isBoolean(this.async)) {
  192. this.async = !!this.async;
  193. }
  194. },
  195. /**
  196. * Create a new Deferred.
  197. *
  198. * @private
  199. * @ignore
  200. */
  201. createDeferred : function() {
  202. return new Deferred({ async : false });
  203. },
  204. /**
  205. * Watch the process.
  206. *
  207. * @private
  208. * @ignore
  209. */
  210. watch : function() {
  211. var that = this;
  212. if (!this.async && this.waiting === true && PotSystem.isWaitable) {
  213. Pot.XPCOM.throughout(function() {
  214. return that.waiting !== true;
  215. });
  216. }
  217. },
  218. /**
  219. * Execute process.
  220. *
  221. * @private
  222. * @ignore
  223. */
  224. execute : function(object, callback) {
  225. var d, that = this;
  226. this.waiting = true;
  227. if (!object) {
  228. this.result = {};
  229. this.waiting = false;
  230. } else {
  231. this.waiting = true;
  232. this.restable = true;
  233. this.time = {
  234. start : now(),
  235. total : null,
  236. loop : null,
  237. diff : null,
  238. risk : null,
  239. axis : null,
  240. count : 1,
  241. rest : 100,
  242. limit : 255
  243. };
  244. this.setIter(object, callback);
  245. if (!this.async && !PotSystem.isWaitable) {
  246. this.revback();
  247. this.waiting = false;
  248. } else {
  249. d = this.createDeferred();
  250. d.then(function() {
  251. var d1 = that.createDeferred(),
  252. d2 = that.createDeferred();
  253. d1.then(function() {
  254. return that.revolve().then(function() {
  255. d2.begin();
  256. });
  257. }).begin();
  258. return d2;
  259. }).ensure(function() {
  260. that.waiting = false;
  261. });
  262. if (this.async) {
  263. this.deferred = d.then(function() {
  264. if (isDeferred(that.result) &&
  265. isStopIter(Deferred.lastError(that.result))) {
  266. that.result = Deferred.lastResult(that.result);
  267. }
  268. return that.result;
  269. });
  270. }
  271. this.flush(d);
  272. }
  273. }
  274. },
  275. /**
  276. * @private
  277. * @ignore
  278. */
  279. setIter : function(object, callback) {
  280. var type = this.options.type,
  281. types = LightIterator.types,
  282. context = this.options.context;
  283. switch (true) {
  284. case ((type & types.iterate) === types.iterate):
  285. this.result = null;
  286. this.iter = this.iterate(object, callback, context);
  287. break;
  288. case ((type & types.forEver) === types.forEver):
  289. this.result = {};
  290. this.iter = this.forEver(object, context);
  291. break;
  292. case ((type & types.repeat) === types.repeat):
  293. this.result = {};
  294. this.iter = this.repeat(object, callback, context);
  295. break;
  296. case ((type & types.items) === types.items):
  297. this.result = [];
  298. this.iter = this.items(object, callback, context);
  299. break;
  300. case ((type & types.zip) === types.zip):
  301. this.result = [];
  302. this.iter = this.zip(object, callback, context);
  303. break;
  304. default:
  305. if (isArrayLike(object)) {
  306. this.result = object;
  307. this.iter = this.forLoop(object, callback, context);
  308. } else {
  309. this.result = object;
  310. this.iter = this.forInLoop(object, callback, context);
  311. }
  312. }
  313. },
  314. /**
  315. * @private
  316. * @ignore
  317. */
  318. revback : function() {
  319. var that = this, result, err, cutback = false, time;
  320. this.time.loop = now();
  321. REVOLVE: {
  322. do {
  323. try {
  324. if (this.isDeferStopIter) {
  325. this.isDeferStopIter = false;
  326. throw PotStopIteration;
  327. }
  328. result = this.iter.next();
  329. } catch (e) {
  330. err = e;
  331. if (isStopIter(err)) {
  332. break REVOLVE;
  333. }
  334. throw err;
  335. }
  336. if (this.async && isDeferred(result)) {
  337. return result.ensure(function(res) {
  338. if (res !== void 0) {
  339. if (isError(res)) {
  340. if (isStopIter(res)) {
  341. that.isDeferStopIter = true;
  342. if (isDeferred(that.result) &&
  343. isStopIter(Deferred.lastError(that.result))) {
  344. that.result = Deferred.lastResult(that.result);
  345. }
  346. } else {
  347. Deferred.lastError(this, res);
  348. }
  349. } else {
  350. Deferred.lastResult(this, res);
  351. }
  352. }
  353. that.flush(that.revback, true);
  354. });
  355. }
  356. time = now();
  357. if (PotSystem.isWaitable) {
  358. if (this.time.total === null) {
  359. this.time.total = time;
  360. } else if (time - this.time.total >= this.time.rest) {
  361. Pot.XPCOM.throughout(0);
  362. this.time.total = now();
  363. }
  364. } else if (!this.async) {
  365. if (this.restable && this.time.count >= this.time.limit) {
  366. this.restable = false;
  367. }
  368. }
  369. this.time.risk = time - this.time.start;
  370. this.time.diff = time - this.time.loop;
  371. if (this.time.diff >= this.interval) {
  372. if (this.async &&
  373. this.interval < LightIterator.speeds.normal) {
  374. cutback = true;
  375. } else if (this.async || this.restable || PotSystem.isWaitable) {
  376. if (this.time.diff < this.interval + 8) {
  377. this.time.axis = 2;
  378. } else if (this.time.diff < this.interval + 36) {
  379. this.time.axis = 5;
  380. } else if (this.time.diff < this.interval + 48) {
  381. this.time.axis = 7;
  382. } else {
  383. this.time.axis = 10;
  384. }
  385. if (this.time.axis >= 10 ||
  386. (Math.random() * 10 < this.time.axis)) {
  387. cutback = true;
  388. }
  389. }
  390. }
  391. } while (!cutback);
  392. if (this.time.count <= this.time.limit) {
  393. this.time.count++;
  394. }
  395. return this.flush(this.revback, true);
  396. }
  397. if (isDeferred(this.revDeferred)) {
  398. this.revDeferred.begin();
  399. }
  400. },
  401. /**
  402. * Revolve the process.
  403. *
  404. * @private
  405. * @ignore
  406. */
  407. revolve : function() {
  408. var that = this,
  409. d = this.createDeferred(),
  410. de = this.createDeferred();
  411. d.then(function() {
  412. var dd = that.createDeferred();
  413. that.revDeferred = that.createDeferred();
  414. dd.then(function() {
  415. return that.revback();
  416. }).begin();
  417. return that.revDeferred;
  418. }).ensure(function(er) {
  419. de.begin();
  420. if (isError(er)) {
  421. throw er;
  422. }
  423. });
  424. this.flush(d);
  425. return de;
  426. },
  427. /**
  428. * Flush the callback.
  429. *
  430. * @private
  431. * @ignore
  432. */
  433. flush : function(callback, useSpeed) {
  434. var that = this, d, lazy = false, speed, speedKey;
  435. if (this.async || PotSystem.isWaitable) {
  436. lazy = true;
  437. }
  438. if (!lazy && isFunction(callback)) {
  439. return callback.call(this);
  440. } else {
  441. d = this.createDeferred();
  442. d.then(function() {
  443. if (isDeferred(callback)) {
  444. callback.begin();
  445. } else {
  446. callback.call(that);
  447. }
  448. });
  449. if (lazy) {
  450. speed = 0;
  451. if (useSpeed) {
  452. speedKey = LightIterator.revSpeeds[this.interval];
  453. if (speedKey &&
  454. isNumeric(LightIterator.delays[speedKey])) {
  455. speed = LightIterator.delays[speedKey];
  456. }
  457. if (Math.random() * 10 < Math.max(2, (this.time.axis || 2) / 2.75)) {
  458. speed += Math.min(
  459. this.time.rest,
  460. Math.max(1,
  461. Math.ceil(
  462. (this.time.risk / (this.time.rest + this.time.diff)) +
  463. this.time.diff
  464. )
  465. )
  466. );
  467. }
  468. }
  469. PotInternalSetTimeout(function() {
  470. d.begin();
  471. }, speed);
  472. } else {
  473. d.begin();
  474. }
  475. }
  476. },
  477. /**
  478. * Return noop function.
  479. *
  480. * @private
  481. * @ignore
  482. */
  483. noop : function() {
  484. return {
  485. /**@ignore*/
  486. next : function() {
  487. throw PotStopIteration;
  488. }
  489. };
  490. },
  491. /**
  492. * forEver.
  493. *
  494. * @private
  495. * @ignore
  496. */
  497. forEver : function(callback, context) {
  498. var i = 0;
  499. if (!isFunction(callback)) {
  500. return this.noop();
  501. }
  502. return {
  503. /**@ignore*/
  504. next : function() {
  505. var result = callback.call(context, i);
  506. try {
  507. if (!isFinite(++i) || i >= Number.MAX_VALUE) {
  508. throw 0;
  509. }
  510. } catch (ex) {
  511. i = 0;
  512. }
  513. return result;
  514. }
  515. };
  516. },
  517. /**
  518. * repeat.
  519. *
  520. * @private
  521. * @ignore
  522. */
  523. repeat : function(max, callback, context) {
  524. var i, loops, n, last;
  525. if (!isFunction(callback)) {
  526. return this.noop();
  527. }
  528. if (!max || max == null) {
  529. n = 0;
  530. } else if (isNumeric(max)) {
  531. n = max - 0;
  532. } else {
  533. n = max || {};
  534. if (isNumeric(n.start)) {
  535. n.begin = n.start;
  536. }
  537. if (isNumeric(n.stop)) {
  538. n.end = n.stop;
  539. }
  540. }
  541. loops = {
  542. begin : isNumeric(n.begin) ? n.begin - 0 : 0,
  543. end : isNumeric(n.end) ? n.end - 0 : (n || 0) - 0,
  544. step : isNumeric(n.step) ? n.step - 0 : 1,
  545. last : false,
  546. prev : null
  547. };
  548. i = loops.step ? loops.begin : loops.end;
  549. last = loops.end - loops.step;
  550. return {
  551. /**@ignore*/
  552. next : function() {
  553. var result;
  554. if (i < loops.end) {
  555. loops.last = (i >= last);
  556. result = callback.call(context, i, loops.last, loops);
  557. loops.prev = result;
  558. } else {
  559. throw PotStopIteration;
  560. }
  561. i += loops.step;
  562. return result;
  563. }
  564. };
  565. },
  566. /**
  567. * forLoop.
  568. *
  569. * @private
  570. * @ignore
  571. */
  572. forLoop : function(object, callback, context) {
  573. var copy, i = 0;
  574. if (!object || !object.length || !isFunction(callback)) {
  575. return this.noop();
  576. }
  577. copy = arrayize(object);
  578. return {
  579. /**@ignore*/
  580. next : function() {
  581. var val, result;
  582. while (true) {
  583. if (i >= copy.length) {
  584. throw PotStopIteration;
  585. }
  586. if (!(i in copy)) {
  587. i++;
  588. continue;
  589. }
  590. try {
  591. val = copy[i];
  592. } catch (e) {
  593. i++;
  594. continue;
  595. }
  596. result = callback.call(context, val, i, object);
  597. i++;
  598. return result;
  599. }
  600. }
  601. };
  602. },
  603. /**
  604. * forInLoop.
  605. *
  606. * @private
  607. * @ignore
  608. */
  609. forInLoop : function(object, callback, context) {
  610. var copy, i = 0;
  611. //XXX: Should use "yield" for duplicate loops.
  612. if (isFunction(callback)) {
  613. copy = [];
  614. each(object, function(value, prop) {
  615. copy[copy.length] = [value, prop];
  616. });
  617. }
  618. if (!copy || !copy.length) {
  619. return this.noop();
  620. }
  621. return {
  622. /**@ignore*/
  623. next : function() {
  624. var result, c, key, val;
  625. while (true) {
  626. if (i >= copy.length) {
  627. throw PotStopIteration;
  628. }
  629. if (!(i in copy)) {
  630. i++;
  631. continue;
  632. }
  633. try {
  634. c = copy[i];
  635. val = c[0];
  636. key = c[1];
  637. } catch (e) {
  638. i++;
  639. continue;
  640. }
  641. result = callback.call(context, val, key, object);
  642. i++;
  643. return result;
  644. }
  645. }
  646. };
  647. },
  648. /**
  649. * iterate.
  650. *
  651. * @private
  652. * @ignore
  653. */
  654. iterate : function(object, callback, context) {
  655. var that = this, iterable;
  656. if (Pot.isIterable(object) && !Pot.isIter(object)) {
  657. // using "yield" generator.
  658. if (isFunction(callback)) {
  659. return {
  660. /**@ignore*/
  661. next : function() {
  662. var res = object.next();
  663. that.result = callback.apply(context, arrayize(res));
  664. return that.result;
  665. }
  666. };
  667. } else {
  668. return {
  669. /**@ignore*/
  670. next : function() {
  671. that.result = object.next();
  672. return that.result;
  673. }
  674. };
  675. }
  676. } else {
  677. iterable = Iter.toIter(object);
  678. if (!isIter(iterable)) {
  679. return this.noop();
  680. }
  681. if (isFunction(callback)) {
  682. return {
  683. /**@ignore*/
  684. next : function() {
  685. var results = iterable.next();
  686. results = arrayize(results);
  687. while (results.length < 2) {
  688. results.push((void 0));
  689. }
  690. results.push(object);
  691. that.result = callback.apply(context, results);
  692. return that.result;
  693. }
  694. };
  695. } else {
  696. return {
  697. /**@ignore*/
  698. next : function() {
  699. that.result = iterable.next();
  700. return that.result;
  701. }
  702. };
  703. }
  704. }
  705. },
  706. /**
  707. * items format loop.
  708. *
  709. * @private
  710. * @ignore
  711. */
  712. items : function(object, callback, context) {
  713. var that = this, copy, i = 0, isPair;
  714. if (isObject(object)) {
  715. copy = [];
  716. each(object, function(ov, op) {
  717. copy[copy.length] = [op, ov];
  718. });
  719. isPair = true;
  720. } else if (isArrayLike(object)) {
  721. copy = arrayize(object);
  722. }
  723. if (!copy || !copy.length) {
  724. return this.noop();
  725. }
  726. if (isFunction(callback)) {
  727. return {
  728. /**@ignore*/
  729. next : function() {
  730. var result, c, key, val;
  731. while (true) {
  732. if (i >= copy.length) {
  733. throw PotStopIteration;
  734. }
  735. if (!(i in copy)) {
  736. i++;
  737. continue;
  738. }
  739. try {
  740. c = copy[i];
  741. if (isPair) {
  742. key = c[0];
  743. val = c[1];
  744. } else {
  745. key = i;
  746. val = c;
  747. }
  748. } catch (e) {
  749. i++;
  750. continue;
  751. }
  752. result = callback.call(context, [key, val], object);
  753. i++;
  754. that.result[that.result.length] = result;
  755. return result;
  756. }
  757. }
  758. };
  759. } else {
  760. return {
  761. /**@ignore*/
  762. next : function() {
  763. var r, t, k, v;
  764. while (true) {
  765. if (i >= copy.length) {
  766. throw PotStopIteration;
  767. }
  768. if (!(i in copy)) {
  769. i++;
  770. continue;
  771. }
  772. try {
  773. t = copy[i];
  774. if (isPair) {
  775. k = t[0];
  776. v = t[1];
  777. } else {
  778. k = i;
  779. v = t;
  780. }
  781. } catch (e) {
  782. i++;
  783. continue;
  784. }
  785. i++;
  786. r = [k, v];
  787. that.result[that.result.length] = r;
  788. return r;
  789. }
  790. }
  791. };
  792. }
  793. },
  794. /**
  795. * zip iteration.
  796. *
  797. * @private
  798. * @ignore
  799. */
  800. zip : function(object, callback, context) {
  801. var that = this, copy, i = 0, max;
  802. if (isArrayLike(object)) {
  803. copy = arrayize(object);
  804. max = copy.length;
  805. }
  806. if (!max || !copy || !copy.length) {
  807. return this.noop();
  808. }
  809. if (isFunction(callback)) {
  810. return {
  811. /**@ignore*/
  812. next : function() {
  813. var result, zips = [], j, item;
  814. for (j = 0; j < max; j++) {
  815. item = arrayize(copy[j]);
  816. if (!item || !item.length || i >= item.length) {
  817. throw PotStopIteration;
  818. }
  819. zips[zips.length] = item[i];
  820. }
  821. result = callback.call(context, zips, object);
  822. that.result[that.result.length] = result;
  823. i++;
  824. return result;
  825. }
  826. };
  827. } else {
  828. return {
  829. /**@ignore*/
  830. next : function() {
  831. var z = [], k, t;
  832. for (k = 0; k < max; k++) {
  833. t = arrayize(copy[k]);
  834. if (!t || !t.length || i >= t.length) {
  835. throw PotStopIteration;
  836. }
  837. z[z.length] = t[i];
  838. }
  839. that.result[that.result.length] = z;
  840. i++;
  841. return z;
  842. }
  843. };
  844. }
  845. }
  846. });
  847. LightIterator.fn.doit.prototype = LightIterator.fn;
  848. // Update internal synchronous iteration.
  849. update(LightIterator, {
  850. /**
  851. * @lends Pot.Internal.LightIterator
  852. */
  853. /**
  854. * Quick iteration for synchronous.
  855. *
  856. * @type Object
  857. * @class
  858. * @private
  859. * @ignore
  860. */
  861. QuickIteration : {
  862. /**
  863. * @lends Pot.Internal.LightIterator.QuickIteration
  864. */
  865. /**
  866. * @private
  867. * @ignore
  868. */
  869. resolve : function(iter) {
  870. var err;
  871. try {
  872. while (true) {
  873. iter.next();
  874. }
  875. } catch (e) {
  876. err = e;
  877. if (!isStopIter(err)) {
  878. throw err;
  879. }
  880. }
  881. },
  882. /**
  883. * @private
  884. * @ignore
  885. */
  886. forEach : function(object, callback, context) {
  887. var result, iter, that = LightIterator.fn;
  888. if (!object) {
  889. result = {};
  890. } else {
  891. result = object;
  892. if (isArrayLike(object)) {
  893. iter = that.forLoop(object, callback, context);
  894. } else {
  895. iter = that.forInLoop(object, callback, context);
  896. }
  897. QuickIteration.resolve(iter);
  898. }
  899. return result;
  900. },
  901. /**
  902. * @private
  903. * @ignore
  904. */
  905. repeat : function(max, callback, context) {
  906. var result = {}, iter, that = LightIterator.fn;
  907. if (max) {
  908. iter = that.repeat(max, callback, context);
  909. QuickIteration.resolve(iter);
  910. }
  911. return result;
  912. },
  913. /**
  914. * @private
  915. * @ignore
  916. */
  917. forEver : function(callback, context) {
  918. var result = {}, iter, that = LightIterator.fn;
  919. if (callback) {
  920. iter = that.forEver(callback, context);
  921. QuickIteration.resolve(iter);
  922. }
  923. return result;
  924. },
  925. /**
  926. * @private
  927. * @ignore
  928. */
  929. iterate : function(object, callback, context) {
  930. var result, iter, o, that = LightIterator.fn;
  931. if (!object) {
  932. result = {};
  933. } else {
  934. result = null;
  935. o = {
  936. noop : that.noop,
  937. result : null
  938. };
  939. iter = that.iterate.call(o, object, callback, context);
  940. QuickIteration.resolve(iter);
  941. result = o.result;
  942. }
  943. return result;
  944. },
  945. /**
  946. * @private
  947. * @ignore
  948. */
  949. items : function(object, callback, context) {
  950. var result = [], iter, o, that = LightIterator.fn;
  951. if (object) {
  952. o = {
  953. noop : that.noop,
  954. result : []
  955. };
  956. iter = that.items.call(o, object, callback, context);
  957. QuickIteration.resolve(iter);
  958. result = o.result;
  959. }
  960. return result;
  961. },
  962. /**
  963. * @private
  964. * @ignore
  965. */
  966. zip : function(object, callback, context) {
  967. var result = [], iter, o, that = LightIterator.fn;
  968. if (object) {
  969. o = {
  970. noop : that.noop,
  971. result : []
  972. };
  973. iter = that.zip.call(o, object, callback, context);
  974. QuickIteration.resolve(iter);
  975. result = o.result;
  976. }
  977. return result;
  978. }
  979. }
  980. });
  981. QuickIteration = LightIterator.QuickIteration;
  982. //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
  983. // Define the main iterators.
  984. // Temporary creation function.
  985. update(PotTmp, {
  986. /**
  987. * @lends Pot.tmp
  988. */
  989. /**
  990. * @private
  991. * @ignore
  992. */
  993. createLightIterateConstructor : function(creator) {
  994. var
  995. name,
  996. /**@ignore*/
  997. create = function(speed) {
  998. var interval;
  999. if (LightIterator.speeds[speed] === void 0) {
  1000. interval = LightIterator.defaults.speed;
  1001. } else {
  1002. interval = LightIterator.speeds[speed];
  1003. }
  1004. return creator(interval);
  1005. },
  1006. methods = {},
  1007. construct = create();
  1008. for (name in LightIterator.speeds) {
  1009. methods[name] = create(name);
  1010. }
  1011. return update(construct, methods);
  1012. }
  1013. });
  1014. createLightIterateConstructor = PotTmp.createLightIterateConstructor;
  1015. // Define the iterator functions to evaluate as synchronized.
  1016. Pot.update({
  1017. /**
  1018. * @lends Pot
  1019. */
  1020. /**
  1021. * Iterates as "for each" loop.
  1022. *
  1023. *
  1024. * @desc
  1025. * <pre>
  1026. * Unlike Deferred, speed options affect to cutback count in loop.
  1027. * Options append to after the forEach and execute it.
  1028. *
  1029. * e.g. Pot.forEach.slow(obj, function() {...})
  1030. *
  1031. * The available methods are below.
  1032. * ------------------------------------
  1033. * method name | speed
  1034. * ------------------------------------
  1035. * limp : slowest
  1036. * doze : slower
  1037. * slow : slow
  1038. * normal : normal (default)
  1039. * fast : fast
  1040. * rapid : faster
  1041. * ninja : fastest
  1042. * ------------------------------------
  1043. * </pre>
  1044. *
  1045. *
  1046. * @example
  1047. * var a = 0;
  1048. * Pot.forEach([1, 2, 3], function(value) {
  1049. * a += value;
  1050. * });
  1051. * debug(a);
  1052. * // @results 6
  1053. *
  1054. *
  1055. * @example
  1056. * var a = '';
  1057. * Pot.forEach({a:'foo', b:'bar'}, function(value, key) {
  1058. * a += key + '=' + value + ',';
  1059. * });
  1060. * debug(a);
  1061. * // @results 'a=foo,b=bar,'
  1062. *
  1063. *
  1064. * @param {Array|Object} object A target object.
  1065. * @param {Function} callback An iterable function.
  1066. * function(value, key, object)
  1067. * this == `context`.
  1068. * Throw Pot.StopIteration
  1069. * if you want to stop the loop.
  1070. * @param {*} (context) Optionally, context object. (i.e. this)
  1071. * @result {Object} Return the object.
  1072. * @class
  1073. * @function
  1074. * @static
  1075. * @name Pot.forEach
  1076. * @property {Function} limp Iterates "for each" loop with slowest speed.
  1077. * @property {Function} doze Iterates "for each" loop with slower speed.
  1078. * @property {Function} slow Iterates "for each" loop with slow speed.
  1079. * @property {Function} normal Iterates "for each" loop with default speed.
  1080. * @property {Function} fast Iterates "for each" loop with fast speed.
  1081. * @property {Function} rapid Iterates "for each" loop with faster speed.
  1082. * @property {Function} ninja Iterates "for each" loop with fastest speed.
  1083. */
  1084. forEach : createLightIterateConstructor(function(interval) {
  1085. if (PotSystem.isWaitable &&
  1086. interval < LightIterator.speeds.normal) {
  1087. return function(object, callback, context) {
  1088. var opts = {};
  1089. opts.type = LightIterator.types.forLoop |
  1090. LightIterator.types.forInLoop;
  1091. opts.interval = interval;
  1092. opts.async = false;
  1093. opts.context = context;
  1094. return (new LightIterator(object, callback, opts)).result;
  1095. };
  1096. } else {
  1097. return function(object, callback, context) {
  1098. return QuickIteration.forEach(
  1099. object, callback, context
  1100. );
  1101. };
  1102. }
  1103. }),
  1104. /**
  1105. * "repeat" loop iterates a specified number.
  1106. *
  1107. * The second argument of the callback function is
  1108. * passed the value to true only for the end of the loop.
  1109. *
  1110. * The first argument can pass as object
  1111. * that gives names "begin, end, step" any keys.
  1112. *
  1113. *
  1114. * @example
  1115. * var a = [];
  1116. * Pot.repeat(10, function(i) {
  1117. * a.push(i);
  1118. * });
  1119. * debug(a);
  1120. * // @results [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  1121. *
  1122. *
  1123. * @example
  1124. * //
  1125. * // The second argument of the callback function is
  1126. * // passed the value to true only for the end of the loop.
  1127. * //
  1128. * var s = '', a = 'abcdef'.split('');
  1129. * Pot.repeat(a.length, function(i, last) {
  1130. * s += a[i] + '=' + i + (last ? ';' : ',');
  1131. * });
  1132. * debug(s);
  1133. * // @results 'a=0,b=1,c=2,d=3,e=4,f=5;'
  1134. *
  1135. *
  1136. * @example
  1137. * //
  1138. * // The first argument can pass as object
  1139. * // that gives names "begin, end, step" any keys.
  1140. * //
  1141. * var a = [];
  1142. * Pot.repeat({begin: 0, end: 100, step: 10}, function(i) {
  1143. * a.push(i);
  1144. * });
  1145. * debug(a);
  1146. * // @results [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
  1147. *
  1148. *
  1149. * @param {Number|Object} max The maximum number of times to loop,
  1150. * or object.
  1151. * @param {Function} callback An iterable function.
  1152. * Throw Pot.StopIteration
  1153. * if you want to stop the loop.
  1154. * @param {*} (context) Optionally, context object. (i.e. this)
  1155. * @class
  1156. * @function
  1157. * @static
  1158. * @name Pot.repeat
  1159. * @property {Function} limp Iterates "repeat" loop with slowest speed.
  1160. * @property {Function} doze Iterates "repeat" loop with slower speed.
  1161. * @property {Function} slow Iterates "repeat" loop with slow speed.
  1162. * @property {Function} normal Iterates "repeat" loop with default speed.
  1163. * @property {Function} fast Iterates "repeat" loop with fast speed.
  1164. * @property {Function} rapid Iterates "repeat" loop with faster speed.
  1165. * @property {Function} ninja Iterates "repeat" loop with fastest speed.
  1166. */
  1167. repeat : createLightIterateConstructor(function(interval) {
  1168. if (PotSystem.isWaitable &&
  1169. interval < LightIterator.speeds.normal) {
  1170. return function(max, callback, context) {
  1171. var opts = {};
  1172. opts.type = LightIterator.types.repeat;
  1173. opts.interval = interval;
  1174. opts.async = false;
  1175. opts.context = context;
  1176. return (new LightIterator(max, callback, opts)).result;
  1177. };
  1178. } else {
  1179. return function(max, callback, context) {
  1180. return QuickIteration.repeat(
  1181. max, callback, context
  1182. );
  1183. };
  1184. }
  1185. }),
  1186. /**
  1187. * Iterates indefinitely until "Pot.StopIteration" is thrown.
  1188. *
  1189. *
  1190. * @example
  1191. * var s = '', a = 'abc*';
  1192. * Pot.forEver(function(i) {
  1193. * s += i + ':' + a;
  1194. * if (s.length > 50) {
  1195. * throw Pot.StopIteration;
  1196. * }
  1197. * });
  1198. * debug(s);
  1199. * // @results
  1200. * // '0:abc*1:abc*2:abc*3:abc*4:abc*5:abc*6:abc*7:abc*8:abc*'
  1201. *
  1202. *
  1203. * @param {Function} callback An iterable function.
  1204. * Throw Pot.StopIteration
  1205. * if you want to stop the loop.
  1206. * @param {*} (context) Optionally, context object. (i.e. this)
  1207. * @class
  1208. * @function
  1209. * @static
  1210. * @name Pot.forEver
  1211. * @property {Function} limp Iterates "forEver" loop with slowest speed.
  1212. * @property {Function} doze Iterates "forEver" loop with slower speed.
  1213. * @property {Function} slow Iterates "forEver" loop with slow speed.
  1214. * @property {Function} normal Iterates "forEver" loop with default speed.
  1215. * @property {Function} fast Iterates "forEver" loop with fast speed.
  1216. * @property {Function} rapid Iterates "forEver" loop with faster speed.
  1217. * @property {Function} ninja Iterates "forEver" loop with fastest speed.
  1218. */
  1219. forEver : createLightIterateConstructor(function(interval) {
  1220. if (PotSystem.isWaitable &&
  1221. interval < LightIterator.speeds.normal) {
  1222. return function(callback, context) {
  1223. var opts = {};
  1224. opts.type = LightIterator.types.forEver;
  1225. opts.interval = interval;
  1226. opts.async = false;
  1227. opts.context = context;
  1228. return (new LightIterator(callback, null, opts)).result;
  1229. };
  1230. } else {
  1231. return function(callback, context) {
  1232. return QuickIteration.forEver(
  1233. callback, context
  1234. );
  1235. };
  1236. }
  1237. }),
  1238. /**
  1239. * Iterate an iterable object. (using Pot.Iter)
  1240. *
  1241. * @param {*} object An iterable object.
  1242. * @param {Function} callback An iterable function.
  1243. * function(value, key, object)
  1244. * this == `context`.
  1245. * Throw Pot.StopIteration
  1246. * if you want to stop the loop.
  1247. * @param {Object} (context) Optionally, context object. (i.e. this)
  1248. * @return {*} Result of iteration.
  1249. * @class
  1250. * @function
  1251. * @static
  1252. * @name Pot.iterate
  1253. * @property {Function} limp Iterates "iterate" loop with slowest speed.
  1254. * @property {Function} doze Iterates "iterate" loop with slower speed.
  1255. * @property {Function} slow Iterates "iterate" loop with slow speed.
  1256. * @property {Function} normal Iterates "iterate" loop with default speed.
  1257. * @property {Function} fast Iterates "iterate" loop with fast speed.
  1258. * @property {Function} rapid Iterates "iterate" loop with faster speed.
  1259. * @property {Function} ninja Iterates "iterate" loop with fastest speed.
  1260. */
  1261. iterate : createLightIterateConstructor(function(interval) {
  1262. if (PotSystem.isWaitable &&
  1263. interval < LightIterator.speeds.normal) {
  1264. return function(object, callback, context) {
  1265. var opts = {};
  1266. opts.type = LightIterator.types.iterate;
  1267. opts.interval = interval;
  1268. opts.async = false;
  1269. opts.context = context;
  1270. return (new LightIterator(object, callback, opts)).result;
  1271. };
  1272. } else {
  1273. return function(object, callback, context) {
  1274. return QuickIteration.iterate(
  1275. object, callback, context
  1276. );
  1277. };
  1278. }
  1279. }),
  1280. /**
  1281. * Collect the object key and value and make array as items format.
  1282. *
  1283. *
  1284. * @example
  1285. * var obj = {foo: 1, bar: 2, baz: 3};
  1286. * debug(items(obj));
  1287. * // @results [['foo', 1], ['bar', 2], ['baz', 3]]
  1288. *
  1289. *
  1290. * @example
  1291. * var array = ['foo', 'bar', 'baz'];
  1292. * debug(items(array));
  1293. * // @results [[0, 'foo'], [1, 'bar'], [2, 'baz']]
  1294. *
  1295. *
  1296. * @example
  1297. * // Example for using callback.
  1298. * var arr = ['foo', 'bar', 'baz'];
  1299. * var func = function(item) {
  1300. * return '(' + item[0] + ')' + item[1];
  1301. * };
  1302. * debug(items(arr, func));
  1303. * // @results ['(0)foo', '(1)bar', '(2)baz']
  1304. *
  1305. *
  1306. * @example
  1307. * // Example for using callback.
  1308. * var obj = {foo: 1, bar: 2, baz: 3};
  1309. * var func = function(item) {
  1310. * return [item[0] + '::' + item[1]];
  1311. * };
  1312. * debug(items(obj, func));
  1313. * // @results [['foo::1'], ['bar::2'], ['baz::3']]
  1314. *
  1315. *
  1316. * @param {Object|Array} object The target object or an array.
  1317. * @param {Function} (callback) (Optional) Callback function.
  1318. * function({Array} item[, object])
  1319. * this == `context`.
  1320. * @param {*} (context) (Optional) Object to use
  1321. * as `this` when executing callback.
  1322. * @return {Array} The collected items as an array.
  1323. *
  1324. * @class
  1325. * @function
  1326. * @static
  1327. * @name Pot.items
  1328. *
  1329. * @property {Function} limp Iterates "items" loop with slowest speed.
  1330. * @property {Function} doze Iterates "items" loop with slower speed.
  1331. * @property {Function} slow Iterates "items" loop with slow speed.
  1332. * @property {Function} normal Iterates "items" loop with default speed.
  1333. * @property {Function} fast Iterates "items" loop with fast speed.
  1334. * @property {Function} rapid Iterates "items" loop with faster speed.
  1335. * @property {Function} ninja Iterates "items" loop with fastest speed.
  1336. */
  1337. items : createLightIterateConstructor(function(interval) {
  1338. if (PotSystem.isWaitable &&
  1339. interval < LightIterator.speeds.normal) {
  1340. return function(object, callback, context) {
  1341. var opts = {};
  1342. opts.type = LightIterator.types.items;
  1343. opts.interval = interval;
  1344. opts.async = false;
  1345. opts.context = context;
  1346. return (new LightIterator(object, callback, opts)).result;
  1347. };
  1348. } else {
  1349. return function(object, callback, context) {
  1350. return QuickIteration.items(
  1351. object, callback, context
  1352. );
  1353. };
  1354. }
  1355. }),
  1356. /**
  1357. * Create a new array which has the elements at
  1358. * position ith of the provided arrays.
  1359. * This function is handled as seen from the longitudinal for array
  1360. * that is similar to the zip() function in Python.
  1361. *
  1362. * <pre>
  1363. * Example:
  1364. *
  1365. * arguments: [[1, 2, 3],
  1366. * [4, 5, 6]]
  1367. *
  1368. * results: [[1, 4],
  1369. * [2, 5],
  1370. * [3, 6]]
  1371. * </pre>
  1372. *
  1373. *
  1374. * @link http://docs.python.org/library/functions.html#zip
  1375. *
  1376. *
  1377. * @example
  1378. * var result = zip([[1, 2, 3], [4, 5, 6]]);
  1379. * debug(result);
  1380. * // @results
  1381. * // [[1, 4], [2, 5], [3, 6]]
  1382. * //
  1383. *
  1384. *
  1385. * @example
  1386. * var result = zip([[1, 2, 3], [1, 2, 3, 4, 5]]);
  1387. * debug(result);
  1388. * // @results
  1389. * // [[1, 1], [2, 2], [3, 3]]
  1390. * //
  1391. *
  1392. *
  1393. * @example
  1394. * var result = zip([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]);
  1395. * debug(result);
  1396. * // @results
  1397. * // [[1, 4, 7, 10], [2, 5, 8, 11]]
  1398. * //
  1399. *
  1400. *
  1401. * @example
  1402. * var result = zip(['hoge']);
  1403. * debug(result);
  1404. * // @results
  1405. * // [['hoge']]
  1406. * //
  1407. *
  1408. *
  1409. * @example
  1410. * var result = zip([[1], [2], [3]]);
  1411. * debug(result);
  1412. * // @results
  1413. * // [[1, 2, 3]]
  1414. * //
  1415. *
  1416. *
  1417. * @example
  1418. * var result = zip([[1, 2, 3], ['foo', 'bar', 'baz'], [4, 5]]);
  1419. * debug(result);
  1420. * // @results
  1421. * // [[1, 'foo', 4], [2, 'bar', 5]]
  1422. * //
  1423. *
  1424. *
  1425. * @example
  1426. * var callback = function(items) { return items[0] + items[1]; };
  1427. * var result = zip([[1, 2, 3], [4, 5, 6]], callback);
  1428. * debug(result);
  1429. * // @results [5, 7, 9]
  1430. *
  1431. *
  1432. * @param {Array} object An array to be combined.
  1433. * @param {Function} (callback) (Optional) Callback function.
  1434. * function({Array} items[, {*} object])
  1435. * this == `context`.
  1436. * @param {*} (context) (Optional) Object to use
  1437. * as `this` when executing callback.
  1438. * @return {Array} A new array of arrays created from
  1439. * provided objects.
  1440. *
  1441. * @class
  1442. * @function
  1443. * @static
  1444. * @name Pot.zip
  1445. *
  1446. * @property {Function} limp Iterates "zip" loop with slowest speed.
  1447. * @property {Function} doze Iterates "zip" loop with slower speed.
  1448. * @property {Function} slow Iterates "zip" loop with slow speed.
  1449. * @property {Function} normal Iterates "zip" loop with default speed.
  1450. * @property {Function} fast Iterates "zip" loop with fast speed.
  1451. * @property {Function} rapid Iterates "zip" loop with faster speed.
  1452. * @property {Function} ninja Iterates "zip" loop with fastest speed.
  1453. */
  1454. zip : createLightIterateConstructor(function(interval) {
  1455. if (PotSystem.isWaitable &&
  1456. interval < LightIterator.speeds.normal) {
  1457. return function(object, callback, context) {
  1458. var opts = {};
  1459. opts.type = LightIterator.types.zip;
  1460. opts.interval = interval;
  1461. opts.async = false;
  1462. opts.context = context;
  1463. return (new LightIterator(object, callback, opts)).result;
  1464. };
  1465. } else {
  1466. return function(object, callback, context) {
  1467. return QuickIteration.zip(
  1468. object, callback, context
  1469. );
  1470. };
  1471. }
  1472. })
  1473. });
  1474. // Define iterators for Deferred (Asynchronous)
  1475. update(Deferred, {
  1476. /**
  1477. * Iterates as "for each" loop. (Asynchronous)
  1478. *
  1479. * @desc
  1480. * <pre>
  1481. * Unlike Deferred, speed options affect to cutback count in loop.
  1482. * Options append to after the forEach and execute it.
  1483. *
  1484. * e.g. Pot.Deferred.forEach.fast(obj, function() {...})
  1485. *
  1486. * The available methods are below.
  1487. * ------------------------------------
  1488. * method name | speed
  1489. * ------------------------------------
  1490. * limp : slowest
  1491. * doze : slower
  1492. * slow : slow
  1493. * normal : normal (default)
  1494. * fast : fast
  1495. * rapid : faster
  1496. * ninja : fastest
  1497. * ------------------------------------
  1498. * </pre>
  1499. *
  1500. * @param {Array|Object} object A target object.
  1501. * @param {Function} callback An iterable function.
  1502. * function(value, key, object)
  1503. * this == `context`.
  1504. * Throw Pot.StopIteration
  1505. * if you want to stop the loop.
  1506. * @param {*} (context) Optionally, context object. (i.e. this)
  1507. * @result {Deferred} Return the Deferred.
  1508. * @class
  1509. * @function
  1510. * @public
  1511. * @type Function
  1512. * @name Pot.Deferred.forEach
  1513. * @property {Function} limp Iterates "for each" loop with slowest speed.
  1514. * @property {Function} doze Iterates "for each" loop with slower speed.
  1515. * @property {Function} slow Iterates "for each" loop with slow speed.
  1516. * @property {Function} normal Iterates "for each" loop with default speed.
  1517. * @property {Function} fast Iterates "for each" loop with fast speed.
  1518. * @property {Function} rapid Iterates "for each" loop with faster speed.
  1519. * @property {Function} ninja Iterates "for each" loop with fastest speed.
  1520. */
  1521. forEach : createLightIterateConstructor(function(interval) {
  1522. return function(object, callback, context) {
  1523. var opts = {};
  1524. opts.type = LightIterator.types.forLoop |
  1525. LightIterator.types.forInLoop;
  1526. opts.interval = interval;
  1527. opts.async = true;
  1528. opts.context = context;
  1529. return (new LightIterator(object, callback, opts)).deferred;
  1530. };
  1531. }),
  1532. /**
  1533. * "repeat" loop iterates a specified number. (Asynchronous)
  1534. *
  1535. * @param {Number|Object} max The maximum number of times to loop,
  1536. * or object.
  1537. * @param {Function} callback An iterable function.
  1538. * Throw Pot.StopIteration
  1539. * if you want to stop the loop.
  1540. * @param {*} (context) Optionally, context object. (i.e. this)
  1541. * @return {Deferred} Return the Deferred.
  1542. * @class
  1543. * @function
  1544. * @public
  1545. * @type Function
  1546. * @name Pot.Deferred.repeat
  1547. * @property {Function} limp Iterates "repeat" loop with slowest speed.
  1548. * @property {Function} doze Iterates "repeat" loop with slower speed.
  1549. * @property {Function} slow Iterates "repeat" loop with slow speed.
  1550. * @property {Function} normal Iterates "repeat" loop with default speed.
  1551. * @property {Function} fast Iterates "repeat" loop with fast speed.
  1552. * @property {Function} rapid Iterates "repeat" loop with faster speed.
  1553. * @property {Function} ninja Iterates "repeat" loop with fastest speed.
  1554. */
  1555. repeat : createLightIterateConstructor(function(interval) {
  1556. return function(max, callback, context) {
  1557. var opts = {};
  1558. opts.type = LightIterator.types.repeat;
  1559. opts.interval = interval;
  1560. opts.async = true;
  1561. opts.context = context;
  1562. return (new LightIterator(max, callback, opts)).deferred;
  1563. };
  1564. }),
  1565. /**
  1566. * Iterates indefinitely until "Pot.StopIteration" is thrown. (Asynchronous)
  1567. *
  1568. * @param {Function} callback An iterable function.
  1569. * Throw Pot.StopIteration
  1570. * if you want to stop the loop.
  1571. * @param {*} (context) Optionally, context object. (i.e. this)
  1572. * @return {Deferred} Return the Deferred.
  1573. * @class
  1574. * @function
  1575. * @public
  1576. * @type Function
  1577. * @name Pot.Deferred.forEver
  1578. * @property {Function} limp Iterates "forEver" loop with slowest speed.
  1579. * @property {Function} doze Iterates "forEver" loop with slower speed.
  1580. * @property {Function} slow Iterates "forEver" loop with slow speed.
  1581. * @property {Function} normal Iterates "forEver" loop with default speed.
  1582. * @property {Function} fast Iterates "forEver" loop with fast speed.
  1583. * @property {Function} rapid Iterates "forEver" loop with faster speed.
  1584. * @property {Function} ninja Iterates "forEver" loop with fastest speed.
  1585. */
  1586. forEver : createLightIterateConstructor(function(interval) {
  1587. return function(callback, context) {
  1588. var opts = {};
  1589. opts.type = LightIterator.types.forEver;
  1590. opts.interval = interval;
  1591. opts.async = true;
  1592. opts.context = context;
  1593. return (new LightIterator(callback, null, opts)).deferred;
  1594. };
  1595. }),
  1596. /**
  1597. * Iterate an iterable object. (using Pot.Iter)
  1598. *
  1599. * @param {*} object An iterable object.
  1600. * @param {Function} callback An iterable function.
  1601. * function(value, key, object)
  1602. * this == `context`.
  1603. * Throw Pot.StopIteration
  1604. * if you want to stop the loop.
  1605. * @param {Object} (context) Optionally, context object. (i.e. this)
  1606. * @return {Deferred} Return the Deferred.
  1607. * @class
  1608. * @function
  1609. * @public
  1610. * @type Function
  1611. * @name Pot.Deferred.iterate
  1612. * @property {Function} limp Iterates "iterate" loop with slowest speed.
  1613. * @property {Function} doze Iterates "iterate" loop with slower speed.
  1614. * @property {Function} slow Iterates "iterate" loop with slow speed.
  1615. * @property {Function} normal Iterates "iterate" loop with default speed.
  1616. * @property {Function} fast Iterates "iterate" loop with fast speed.
  1617. * @property {Function} rapid Iterates "iterate" loop with faster speed.
  1618. * @property {Function} ninja Iterates "iterate" loop with fastest speed.
  1619. */
  1620. iterate : createLightIterateConstructor(function(interval) {
  1621. return function(object, callback, context) {
  1622. var opts = {};
  1623. opts.type = LightIterator.types.iterate;
  1624. opts.interval = interval;
  1625. opts.async = true;
  1626. opts.context = context;
  1627. return (new LightIterator(object, callback, opts)).deferred;
  1628. };
  1629. }),
  1630. /**
  1631. * Collect the object key and value and make array as items format.
  1632. *
  1633. * @param {Object|Array} object The target object or an array.
  1634. * @param {Function} (callback) (Optional) Callback function.
  1635. * function({Array} item[, object])
  1636. * this == `context`.
  1637. * @param {*} (context) (Optional) Object to use
  1638. * as `this` when executing callback.
  1639. * @return {Deferred} Return a new instance of Deferred that
  1640. * has the collected items as an array.
  1641. *
  1642. * @class
  1643. * @function
  1644. * @public
  1645. * @type Function
  1646. * @name Pot.Deferred.items
  1647. *
  1648. * @property {Function} limp Iterates "items" loop with slowest speed.
  1649. * @property {Function} doze Iterates "items" loop with slower speed.
  1650. * @property {Function} slow Iterates "items" loop with slow speed.
  1651. * @property {Function} normal Iterates "items" loop with default speed.
  1652. * @property {Function} fast Iterates "items" loop with fast speed.
  1653. * @property {Function} rapid Iterates "items" loop with faster speed.
  1654. * @property {Function} ninja Iterates "items" loop with fastest speed.
  1655. */
  1656. items : createLightIterateConstructor(function(interval) {
  1657. return function(object, callback, context) {
  1658. var opts = {};
  1659. opts.type = LightIterator.types.items;
  1660. opts.interval = interval;
  1661. opts.async = true;
  1662. opts.context = context;
  1663. return (new LightIterator(object, callback, opts)).deferred;
  1664. };
  1665. }),
  1666. /**
  1667. * Create a new array which has the elements at
  1668. * position ith of the provided arrays.
  1669. * This function is handled as seen from the longitudinal for array
  1670. * that is similar to the zip() function in Python.
  1671. *
  1672. * <pre>
  1673. * Example:
  1674. *
  1675. * arguments: [[1, 2, 3],
  1676. * [4, 5, 6]]
  1677. *
  1678. * results: [[1, 4],
  1679. * [2, 5],
  1680. * [3, 6]]
  1681. * </pre>
  1682. *
  1683. * @link http://docs.python.org/library/functions.html#zip
  1684. *
  1685. * @param {Array} object Objects to be combined.
  1686. * @param {Function} (callback) (Optional) Callback function.
  1687. * function({Array} items[, {*} object])
  1688. * this == `context`.
  1689. * @param {*} (context) (Optional) Object to use
  1690. * as `this` when executing callback.
  1691. * @return {Deferred} Return a new instance of Deferred that has
  1692. * a new array of arrays created from
  1693. * provided objects.
  1694. * @class
  1695. * @function
  1696. * @public
  1697. * @type Function
  1698. * @name Pot.Deferred.zip
  1699. *
  1700. * @property {Function} limp Iterates "zip" loop with slowest speed.
  1701. * @property {Function} doze Iterates "zip" loop with slower speed.
  1702. * @property {Function} slow Iterates "zip" loop with slow speed.
  1703. * @property {Function} normal Iterates "zip" loop with default speed.
  1704. * @property {Function} fast Iterates "zip" loop with fast speed.
  1705. * @property {Function} rapid Iterates "zip" loop with faster speed.
  1706. * @property {Function} ninja Iterates "zip" loop with fastest speed.
  1707. */
  1708. zip : createLightIterateConstructor(function(interval) {
  1709. return function(object, callback, context) {
  1710. var opts = {};
  1711. opts.type = LightIterator.types.zip;
  1712. opts.interval = interval;
  1713. opts.async = true;
  1714. opts.context = context;
  1715. return (new LightIterator(object, callback, opts)).deferred;
  1716. };
  1717. })
  1718. });
  1719. delete PotTmp.createLightIterateConstructor;
  1720. //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
  1721. // Definition of Iter.
  1722. Pot.update({
  1723. /**
  1724. * @lends Pot
  1725. */
  1726. /**
  1727. * Iter.
  1728. *
  1729. * A Simple iterator.
  1730. * Constructor.
  1731. *
  1732. * @param {*} Options.
  1733. * @return {Pot.Iter} Returns an instance of Pot.Iter
  1734. *
  1735. * @name Pot.Iter
  1736. * @class
  1737. * @constructor
  1738. * @public
  1739. */
  1740. Iter : function() {
  1741. return isIter(this) ? this.init(arguments)
  1742. : new Iter.fn.init(arguments);
  1743. }
  1744. });
  1745. // Refer the Pot properties/functions.
  1746. Iter = Pot.Iter;
  1747. // Definition of the prototype
  1748. Iter.fn = Iter.prototype = update(Iter.prototype, {
  1749. /**
  1750. * @lends Pot.Iter.…

Large files files are truncated, but you can click here to view the full file