stringInternPool.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.StringInternPool = exports.JsonStringInternalizer = void 0;
  6. /**
  7. * Copyright (c) Microsoft Corporation.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. class StringInternPool {
  22. constructor() {
  23. this._stringCache = new Map();
  24. }
  25. internString(s) {
  26. let result = this._stringCache.get(s);
  27. if (!result) {
  28. this._stringCache.set(s, s);
  29. result = s;
  30. }
  31. return result;
  32. }
  33. }
  34. exports.StringInternPool = StringInternPool;
  35. class JsonStringInternalizer {
  36. constructor(pool) {
  37. this._pool = void 0;
  38. this._pool = pool;
  39. }
  40. traverse(value) {
  41. if (typeof value !== 'object') return;
  42. if (Array.isArray(value)) {
  43. for (let i = 0; i < value.length; i++) {
  44. if (typeof value[i] === 'string') value[i] = this.intern(value[i]);else this.traverse(value[i]);
  45. }
  46. } else {
  47. for (const name in value) {
  48. if (typeof value[name] === 'string') value[name] = this.intern(value[name]);else this.traverse(value[name]);
  49. }
  50. }
  51. }
  52. intern(value) {
  53. return this._pool.internString(value);
  54. }
  55. }
  56. exports.JsonStringInternalizer = JsonStringInternalizer;