short-string-pool.ts 778 B

123456789101112131415161718192021222324
  1. /*
  2. * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * from https://github.com/dsehnal/CIFTools.js
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. /**
  8. * This ensures there is only 1 instance of a short string.
  9. * Also known as string interning, see https://en.wikipedia.org/wiki/String_interning
  10. */
  11. interface ShortStringPool { [key: string]: string }
  12. namespace ShortStringPool {
  13. export function create(): ShortStringPool { return Object.create(null); }
  14. export function get(pool: ShortStringPool, str: string) {
  15. if (str.length > 6) return str;
  16. const value = pool[str];
  17. if (value !== void 0) return value;
  18. pool[str] = str;
  19. return str;
  20. }
  21. }
  22. export default ShortStringPool;