Dictionary.js 3.1 KB

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