PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/jsdoc_toolkit-2.3.2/jsdoc-toolkit/app/frame/Chain.js

http://jsdoc-toolkit.googlecode.com/
JavaScript | 102 lines | 59 code | 13 blank | 30 comment | 15 complexity | 647aff22569137e866ec2062b3936bfd MD5 | raw file
  1. /**@constructor*/
  2. function ChainNode(object, link) {
  3. this.value = object;
  4. this.link = link; // describes this node's relationship to the previous node
  5. }
  6. /**@constructor*/
  7. function Chain(valueLinks) {
  8. this.nodes = [];
  9. this.cursor = -1;
  10. if (valueLinks && valueLinks.length > 0) {
  11. this.push(valueLinks[0], "//");
  12. for (var i = 1, l = valueLinks.length; i < l; i+=2) {
  13. this.push(valueLinks[i+1], valueLinks[i]);
  14. }
  15. }
  16. }
  17. Chain.prototype.push = function(o, link) {
  18. if (this.nodes.length > 0 && link) this.nodes.push(new ChainNode(o, link));
  19. else this.nodes.push(new ChainNode(o));
  20. }
  21. Chain.prototype.unshift = function(o, link) {
  22. if (this.nodes.length > 0 && link) this.nodes[0].link = link;
  23. this.nodes.unshift(new ChainNode(o));
  24. this.cursor++;
  25. }
  26. Chain.prototype.get = function() {
  27. if (this.cursor < 0 || this.cursor > this.nodes.length-1) return null;
  28. return this.nodes[this.cursor];
  29. }
  30. Chain.prototype.first = function() {
  31. this.cursor = 0;
  32. return this.get();
  33. }
  34. Chain.prototype.last = function() {
  35. this.cursor = this.nodes.length-1;
  36. return this.get();
  37. }
  38. Chain.prototype.next = function() {
  39. this.cursor++;
  40. return this.get();
  41. }
  42. Chain.prototype.prev = function() {
  43. this.cursor--;
  44. return this.get();
  45. }
  46. Chain.prototype.toString = function() {
  47. var string = "";
  48. for (var i = 0, l = this.nodes.length; i < l; i++) {
  49. if (this.nodes[i].link) string += " -("+this.nodes[i].link+")-> ";
  50. string += this.nodes[i].value.toString();
  51. }
  52. return string;
  53. }
  54. Chain.prototype.joinLeft = function() {
  55. var result = "";
  56. for (var i = 0, l = this.cursor; i < l; i++) {
  57. if (result && this.nodes[i].link) result += this.nodes[i].link;
  58. result += this.nodes[i].value.toString();
  59. }
  60. return result;
  61. }
  62. /* USAGE:
  63. var path = "one/two/three.four/five-six";
  64. var pathChain = new Chain(path.split(/([\/.-])/));
  65. print(pathChain);
  66. var lineage = new Chain();
  67. lineage.push("Port");
  68. lineage.push("Les", "son");
  69. lineage.push("Dawn", "daughter");
  70. lineage.unshift("Purdie", "son");
  71. print(lineage);
  72. // walk left
  73. for (var node = lineage.last(); node !== null; node = lineage.prev()) {
  74. print("< "+node.value);
  75. }
  76. // walk right
  77. var node = lineage.first()
  78. while (node !== null) {
  79. print(node.value);
  80. node = lineage.next();
  81. if (node && node.link) print("had a "+node.link+" named");
  82. }
  83. */