Guest User

Untitled

a guest
Jan 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. const cookieStorage = {
  2.  
  3. get COOKIE() { return window.document.cookie; },
  4. set COOKIE(v: string) { window.document.cookie = v; },
  5.  
  6. get ITEMS(): { [key: string]: string } {
  7. return this.COOKIE
  8. .split(/;\s*/)
  9. .filter(Boolean)
  10. .map(s => s.match(/^([^=]+)=(.*)$/))
  11. .reduce((mem, [str, key, value]) => {
  12. mem[key] = decodeURIComponent(value);
  13. return mem;
  14. }, {});
  15. },
  16.  
  17. get DEFAULT_OPTIONS() {
  18. const nextYear = new Date();
  19. nextYear.setFullYear(nextYear.getFullYear() + 1);
  20.  
  21. return {
  22. path: '/',
  23. domain: window.location.hostname,
  24. expires: nextYear,
  25. };
  26. },
  27.  
  28. get DEFAULT_CHUNK() {
  29. return {
  30. size: 4000,
  31. maxCount: 10,
  32. };
  33. },
  34.  
  35.  
  36. _getOptionsString(options: { path?: string, domain?: string, expires?: Date|number } = {}) {
  37. const opts = Object.assign(this.DEFAULT_OPTIONS, options) as {
  38. path: string,
  39. domain: string,
  40. expires?: Date|number|string,
  41. };
  42.  
  43. if (typeof opts.expires === 'number')
  44. opts.expires = (opts.expires !== 0) ? new Date(opts.expires) : undefined;
  45. if (opts.expires instanceof Date)
  46. opts.expires = opts.expires.toUTCString();
  47.  
  48. return Object.keys(opts)
  49. .filter(key => Boolean(opts[key]))
  50. .map(key => `${key}=${opts[key]}`)
  51. .join('; ');
  52. },
  53.  
  54.  
  55. getItem(key: string) {
  56. return this.ITEMS[key];
  57. },
  58.  
  59. setItem(
  60. key: string,
  61. value: string,
  62. options: { path?: string, domain?: string, expires?: Date|number } = {}
  63. ) {
  64. const optionsString = this._getOptionsString(options);
  65.  
  66. const encoded = encodeURIComponent(value);
  67. if (encoded.length > this.DEFAULT_CHUNK.size) {
  68. console.warn('Cookie limit is 4 KB.');
  69. }
  70.  
  71. this.COOKIE = `${key}=${encodeURIComponent(value)}; ${optionsString}`;
  72. },
  73.  
  74. removeItem(key: string) {
  75. this.setItem(key, '', { expires: -1 });
  76. },
  77.  
  78.  
  79. setItemByChunks(
  80. key: string,
  81. value: string,
  82. options: { path?: string, domain?: string, expires?: Date|number } = {},
  83. chunkOptions: { size?: number, maxCount?: number } = {}
  84. ) {
  85. const { size: chunkSize, maxCount: chunkMaxCount } = Object.assign(this.DEFAULT_CHUNK, chunkOptions) as {
  86. size: number,
  87. maxCount: number,
  88. };
  89.  
  90. const optionsString = this._getOptionsString(options);
  91.  
  92. encodeURIComponent(value)
  93. .match(/(%.{2}|.)/g)
  94. .reduce((chunks, symbol) => {
  95. let lastIndex = chunks.length - 1;
  96. let lastChunk = chunks[lastIndex];
  97.  
  98. if ((lastChunk.length + symbol.length) > chunkSize) {
  99. lastIndex += 1;
  100. lastChunk = '';
  101. }
  102.  
  103. chunks[lastIndex] = lastChunk + symbol;
  104. return chunks;
  105. }, [''])
  106. .forEach((chunk, index) => {
  107. this.COOKIE = `${key}-${index}=${chunk}; ${optionsString}`;
  108. });
  109. },
  110.  
  111. getItemByChunks(key: string) {
  112. const items = this.ITEMS;
  113. return Object
  114. .keys(items)
  115. .map(k => k.match(/(^.*?)\-(\d+$)/))
  116. .filter(Boolean)
  117. .filter(([k, name, index]) => name === key)
  118. .map(([k, name, index]) => index)
  119. .map(Number)
  120. .sort()
  121. .reduce((result, index) => result + items[`${key}-${index}`], '');
  122. },
  123.  
  124. removeItemByChunks(key: string) {
  125. Object
  126. .keys(this.ITEMS)
  127. .map(k => k.match(/(^.*?)\-(\d+$)/))
  128. .filter(Boolean)
  129. .filter(([k, name]) => name === key)
  130. .forEach(([k]) => this.removeItem(k));
  131. },
  132.  
  133.  
  134. clear() {
  135. Object
  136. .keys(this.ITEMS)
  137. .forEach(key => this.removeItem(key));
  138. },
  139. }
Add Comment
Please, Sign In to add comment