set-array.umd.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.setArray = {}));
  5. })(this, (function (exports) { 'use strict';
  6. /**
  7. * Gets the index associated with `key` in the backing array, if it is already present.
  8. */
  9. exports.get = void 0;
  10. /**
  11. * Puts `key` into the backing array, if it is not already present. Returns
  12. * the index of the `key` in the backing array.
  13. */
  14. exports.put = void 0;
  15. /**
  16. * Pops the last added item out of the SetArray.
  17. */
  18. exports.pop = void 0;
  19. /**
  20. * SetArray acts like a `Set` (allowing only one occurrence of a string `key`), but provides the
  21. * index of the `key` in the backing array.
  22. *
  23. * This is designed to allow synchronizing a second array with the contents of the backing array,
  24. * like how in a sourcemap `sourcesContent[i]` is the source content associated with `source[i]`,
  25. * and there are never duplicates.
  26. */
  27. class SetArray {
  28. constructor() {
  29. this._indexes = { __proto__: null };
  30. this.array = [];
  31. }
  32. }
  33. (() => {
  34. exports.get = (strarr, key) => strarr._indexes[key];
  35. exports.put = (strarr, key) => {
  36. // The key may or may not be present. If it is present, it's a number.
  37. const index = exports.get(strarr, key);
  38. if (index !== undefined)
  39. return index;
  40. const { array, _indexes: indexes } = strarr;
  41. return (indexes[key] = array.push(key) - 1);
  42. };
  43. exports.pop = (strarr) => {
  44. const { array, _indexes: indexes } = strarr;
  45. if (array.length === 0)
  46. return;
  47. const last = array.pop();
  48. indexes[last] = undefined;
  49. };
  50. })();
  51. exports.SetArray = SetArray;
  52. Object.defineProperty(exports, '__esModule', { value: true });
  53. }));
  54. //# sourceMappingURL=set-array.umd.js.map