StringArrayBase64DecodeTemplate.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { IRandomGenerator } from '../../../../interfaces/utils/IRandomGenerator';
  2. /**
  3. * @param {IRandomGenerator} randomGenerator
  4. * @returns {string}
  5. * @constructor
  6. */
  7. export function StringArrayBase64DecodeTemplate (
  8. randomGenerator: IRandomGenerator
  9. ): string {
  10. const identifierLength: number = 6;
  11. const initializedIdentifier: string = randomGenerator.getRandomString(identifierLength);
  12. const base64DecodeFunctionIdentifier: string = randomGenerator.getRandomString(identifierLength);
  13. const dataIdentifier: string = randomGenerator.getRandomString(identifierLength);
  14. return `
  15. if ({stringArrayCallsWrapperName}.${initializedIdentifier} === undefined) {
  16. {atobPolyfill}
  17. {stringArrayCallsWrapperName}.${base64DecodeFunctionIdentifier} = function (str) {
  18. const string = {atobFunctionName}(str);
  19. let newStringChars = [];
  20. for (let i = 0, length = string.length; i < length; i++) {
  21. newStringChars += '%' + ('00' + string.charCodeAt(i).toString(16)).slice(-2);
  22. }
  23. return decodeURIComponent(newStringChars);
  24. };
  25. {stringArrayCallsWrapperName}.${dataIdentifier} = {};
  26. {stringArrayCallsWrapperName}.${initializedIdentifier} = true;
  27. }
  28. const cachedValue = {stringArrayCallsWrapperName}.${dataIdentifier}[index];
  29. if (cachedValue === undefined) {
  30. {selfDefendingCode}
  31. value = {stringArrayCallsWrapperName}.${base64DecodeFunctionIdentifier}(value);
  32. {stringArrayCallsWrapperName}.${dataIdentifier}[index] = value;
  33. } else {
  34. value = cachedValue;
  35. }
  36. `;
  37. }