stringDictionary.d.ts 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import type { Nullable } from "../types";
  2. /**
  3. * This class implement a typical dictionary using a string as key and the generic type T as value.
  4. * The underlying implementation relies on an associative array to ensure the best performances.
  5. * The value can be anything including 'null' but except 'undefined'
  6. */
  7. export declare class StringDictionary<T> {
  8. /**
  9. * This will clear this dictionary and copy the content from the 'source' one.
  10. * If the T value is a custom object, it won't be copied/cloned, the same object will be used
  11. * @param source the dictionary to take the content from and copy to this dictionary
  12. */
  13. copyFrom(source: StringDictionary<T>): void;
  14. /**
  15. * Get a value based from its key
  16. * @param key the given key to get the matching value from
  17. * @returns the value if found, otherwise undefined is returned
  18. */
  19. get(key: string): T | undefined;
  20. /**
  21. * Get a value from its key or add it if it doesn't exist.
  22. * This method will ensure you that a given key/data will be present in the dictionary.
  23. * @param key the given key to get the matching value from
  24. * @param factory the factory that will create the value if the key is not present in the dictionary.
  25. * The factory will only be invoked if there's no data for the given key.
  26. * @returns the value corresponding to the key.
  27. */
  28. getOrAddWithFactory(key: string, factory: (key: string) => T): T;
  29. /**
  30. * Get a value from its key if present in the dictionary otherwise add it
  31. * @param key the key to get the value from
  32. * @param val if there's no such key/value pair in the dictionary add it with this value
  33. * @returns the value corresponding to the key
  34. */
  35. getOrAdd(key: string, val: T): T;
  36. /**
  37. * Check if there's a given key in the dictionary
  38. * @param key the key to check for
  39. * @returns true if the key is present, false otherwise
  40. */
  41. contains(key: string): boolean;
  42. /**
  43. * Add a new key and its corresponding value
  44. * @param key the key to add
  45. * @param value the value corresponding to the key
  46. * @returns true if the operation completed successfully, false if we couldn't insert the key/value because there was already this key in the dictionary
  47. */
  48. add(key: string, value: T): boolean;
  49. /**
  50. * Update a specific value associated to a key
  51. * @param key defines the key to use
  52. * @param value defines the value to store
  53. * @returns true if the value was updated (or false if the key was not found)
  54. */
  55. set(key: string, value: T): boolean;
  56. /**
  57. * Get the element of the given key and remove it from the dictionary
  58. * @param key defines the key to search
  59. * @returns the value associated with the key or null if not found
  60. */
  61. getAndRemove(key: string): Nullable<T>;
  62. /**
  63. * Remove a key/value from the dictionary.
  64. * @param key the key to remove
  65. * @returns true if the item was successfully deleted, false if no item with such key exist in the dictionary
  66. */
  67. remove(key: string): boolean;
  68. /**
  69. * Clear the whole content of the dictionary
  70. */
  71. clear(): void;
  72. /**
  73. * Gets the current count
  74. */
  75. get count(): number;
  76. /**
  77. * Execute a callback on each key/val of the dictionary.
  78. * Note that you can remove any element in this dictionary in the callback implementation
  79. * @param callback the callback to execute on a given key/value pair
  80. */
  81. forEach(callback: (key: string, val: T) => void): void;
  82. /**
  83. * Execute a callback on every occurrence of the dictionary until it returns a valid TRes object.
  84. * If the callback returns null or undefined the method will iterate to the next key/value pair
  85. * Note that you can remove any element in this dictionary in the callback implementation
  86. * @param callback the callback to execute, if it return a valid T instanced object the enumeration will stop and the object will be returned
  87. * @returns the first item
  88. */
  89. first<TRes>(callback: (key: string, val: T) => TRes): NonNullable<TRes> | null;
  90. private _count;
  91. private _data;
  92. }