staticRegister.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.default = staticRegister;
  4. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  5. var collection = exports.collection = new Map();
  6. function staticRegister() {
  7. var namespace = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'common';
  8. if (!collection.has(namespace)) {
  9. collection.set(namespace, new Map());
  10. }
  11. var subCollection = collection.get(namespace);
  12. /**
  13. * Register an item to the collection. If the item under the same was exist earlier then this item will be replaced with new one.
  14. *
  15. * @param {String} name Identification of the item.
  16. * @param {*} item Item to save in the collection.
  17. */
  18. function register(name, item) {
  19. subCollection.set(name, item);
  20. }
  21. /**
  22. * Retrieve the item from the collection.
  23. *
  24. * @param {String} name Identification of the item.
  25. * @returns {*} Returns item which was saved in the collection.
  26. */
  27. function getItem(name) {
  28. return subCollection.get(name);
  29. }
  30. /**
  31. * Check if item under specyfied name is exists.
  32. *
  33. * @param {String} name Identification of the item.
  34. * @returns {Boolean} Returns `true` or `false` depends on if element exists in the collection.
  35. */
  36. function hasItem(name) {
  37. return subCollection.has(name);
  38. }
  39. /**
  40. * Retrieve list of names registered from the collection.
  41. *
  42. * @returns {Array} Returns an array of strings with all names under which objects are stored.
  43. */
  44. function getNames() {
  45. return [].concat(_toConsumableArray(subCollection.keys()));
  46. }
  47. /**
  48. * Retrieve all registered values from the collection.
  49. *
  50. * @returns {Array} Returns an array with all values stored in the collection.
  51. */
  52. function getValues() {
  53. return [].concat(_toConsumableArray(subCollection.values()));
  54. }
  55. return {
  56. register: register,
  57. getItem: getItem,
  58. hasItem: hasItem,
  59. getNames: getNames,
  60. getValues: getValues
  61. };
  62. }