shared-strings.js 596 B

1234567891011121314151617181920212223242526272829303132333435
  1. class SharedStrings {
  2. constructor() {
  3. this._values = [];
  4. this._totalRefs = 0;
  5. this._hash = Object.create(null);
  6. }
  7. get count() {
  8. return this._values.length;
  9. }
  10. get values() {
  11. return this._values;
  12. }
  13. get totalRefs() {
  14. return this._totalRefs;
  15. }
  16. getString(index) {
  17. return this._values[index];
  18. }
  19. add(value) {
  20. let index = this._hash[value];
  21. if (index === undefined) {
  22. index = this._hash[value] = this._values.length;
  23. this._values.push(value);
  24. }
  25. this._totalRefs++;
  26. return index;
  27. }
  28. }
  29. module.exports = SharedStrings;