Dictionary.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.collections.Dictionary"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.collections.Dictionary"] = true;
  8. dojo.provide("dojox.collections.Dictionary");
  9. dojo.require("dojox.collections._base");
  10. dojox.collections.Dictionary=function(/* dojox.collections.Dictionary? */dictionary){
  11. // summary
  12. // Returns an object of type dojox.collections.Dictionary
  13. var items={};
  14. this.count=0;
  15. // comparator for property addition and access.
  16. var testObject={};
  17. this.add=function(/* string */k, /* object */v){
  18. // summary
  19. // Add a new item to the Dictionary.
  20. var b=(k in items);
  21. items[k]=new dojox.collections.DictionaryEntry(k,v);
  22. if(!b){
  23. this.count++;
  24. }
  25. };
  26. this.clear=function(){
  27. // summary
  28. // Clears the internal dictionary.
  29. items={};
  30. this.count=0;
  31. };
  32. this.clone=function(){
  33. // summary
  34. // Returns a new instance of dojox.collections.Dictionary; note the the dictionary is a clone but items might not be.
  35. return new dojox.collections.Dictionary(this); // dojox.collections.Dictionary
  36. };
  37. this.contains=this.containsKey=function(/* string */k){
  38. // summary
  39. // Check to see if the dictionary has an entry at key "k".
  40. if(testObject[k]){
  41. return false; // bool
  42. }
  43. return (items[k]!=null); // bool
  44. };
  45. this.containsValue=function(/* object */v){
  46. // summary
  47. // Check to see if the dictionary has an entry with value "v".
  48. var e=this.getIterator();
  49. while(e.get()){
  50. if(e.element.value==v){
  51. return true; // bool
  52. }
  53. }
  54. return false; // bool
  55. };
  56. this.entry=function(/* string */k){
  57. // summary
  58. // Accessor method; similar to dojox.collections.Dictionary.item but returns the actual Entry object.
  59. return items[k]; // dojox.collections.DictionaryEntry
  60. };
  61. this.forEach=function(/* function */ fn, /* object? */ scope){
  62. // summary
  63. // functional iterator, following the mozilla spec.
  64. var a=[]; // Create an indexing array
  65. for(var p in items) {
  66. if(!testObject[p]){
  67. a.push(items[p]); // fill it up
  68. }
  69. }
  70. dojo.forEach(a, fn, scope);
  71. };
  72. this.getKeyList=function(){
  73. // summary
  74. // Returns an array of the keys in the dictionary.
  75. return (this.getIterator()).map(function(entry){
  76. return entry.key;
  77. }); // array
  78. };
  79. this.getValueList=function(){
  80. // summary
  81. // Returns an array of the values in the dictionary.
  82. return (this.getIterator()).map(function(entry){
  83. return entry.value;
  84. }); // array
  85. };
  86. this.item=function(/* string */k){
  87. // summary
  88. // Accessor method.
  89. if(k in items){
  90. return items[k].valueOf(); // object
  91. }
  92. return undefined; // object
  93. };
  94. this.getIterator=function(){
  95. // summary
  96. // Gets a dojox.collections.DictionaryIterator for iteration purposes.
  97. return new dojox.collections.DictionaryIterator(items); // dojox.collections.DictionaryIterator
  98. };
  99. this.remove=function(/* string */k){
  100. // summary
  101. // Removes the item at k from the internal collection.
  102. if(k in items && !testObject[k]){
  103. delete items[k];
  104. this.count--;
  105. return true; // bool
  106. }
  107. return false; // bool
  108. };
  109. if (dictionary){
  110. var e=dictionary.getIterator();
  111. while(e.get()) {
  112. this.add(e.element.key, e.element.value);
  113. }
  114. }
  115. };
  116. }