dictionary.js

  1. /**
  2. * Dictionary function constructor.
  3. *
  4. * @example const Dictionary = require('dstructures').Dictionary;
  5. * const myDictionary = new Dictionary();
  6. * @description In computer science, an associative array, map, symbol table, or
  7. * dictionary is an abstract data type composed of a collection of (key, value) pairs,
  8. * such that each possible key appears at most once in the collection. Full wikipedia article at:
  9. * {@link https://en.wikipedia.org/wiki/Associative_array}
  10. * @public
  11. * @constructor
  12. */
  13. class Dictionary {
  14. constructor(){
  15. let container = [];
  16. this.setContainer = (value) => container = value;
  17. this.getContainer = () => container;
  18. }
  19. /**
  20. * Adds key value pair in a dictionary.
  21. *
  22. * @param {any} key Given key.
  23. * @param {any} value Given value.
  24. * @example Dictionary.add('dog', 1); // ['dog': 1]
  25. * Dictionary.add('dog', 2); // ['dog': 2]
  26. * Dictionary.add('cat', 1); // ['dog': 2, 'cat': 1]
  27. */
  28. add (key, value) {
  29. if (key === undefined || key === null) {
  30. return false;
  31. }
  32. if (value === undefined || value === null) {
  33. return false;
  34. }
  35. this.getContainer()[key] = value;
  36. }
  37. /**
  38. * Returns the value for the given key.
  39. *
  40. * @param {any} key Given key.
  41. * @returns Returns the value for the given key.
  42. * @example Dictionary.add('dog', 1); // ['dog': 1]
  43. * Dictionary.find('dog'); // 1
  44. * Dictionary.add('cat', 3); // ['dog': 1, 'cat': 3]
  45. * Dictionary.find('cat'); // 3
  46. */
  47. find (key) {
  48. return this.getContainer()[key];
  49. }
  50. /**
  51. * Removes the value associated to the given key.
  52. *
  53. * @param {any} key Given key.
  54. * @example Dictionary.add('dog', 2); // ['dog': 2]
  55. * Dictionary.add('cat', 1); // ['dog': 2, 'cat': 1]
  56. * Dictionary.remove('dog'); // [cat: 1]
  57. */
  58. remove (key) {
  59. delete this.getContainer()[key];
  60. }
  61. /**
  62. * Returns the underlying array.
  63. *
  64. * @returns {Array} Returns the underlying array.
  65. * @example Dictionary.add('dog', 2); // ['dog': 2]
  66. * Dictionary.add('cat', 1); // ['dog': 2, 'cat': 1]
  67. * Dictionary.display(); // ['dog': 2, 'cat': 1]
  68. */
  69. display () {
  70. return this.getContainer();
  71. }
  72. /**
  73. * Counts all key value pairs.
  74. *
  75. * @returns {Number} Returns number of all key value pairs.
  76. * @example Dictionary.display(); // ['dog': 2, 'cat': 1]
  77. * Dictionary.count(); // 2
  78. * Dictionary.display(); // ['dog': 2, 'cat': 1, 'pig': 4]
  79. * Dictionary.count(); // 3
  80. */
  81. count () {
  82. let number = 0;
  83. for (const key in Object.keys(this.getContainer())) {
  84. number++;
  85. }
  86. return number;
  87. }
  88. /**
  89. * Deletes all key value pairs.
  90. *
  91. * @example Dictionary.display(); // ['dog': 2, 'cat': 1]
  92. * Dictionary.clear(); // []
  93. *
  94. */
  95. clear () {
  96. this.setContainer([]);
  97. }
  98. }
  99. module.exports = Dictionary;