stringTools.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* eslint-disable @typescript-eslint/naming-convention */
  2. /**
  3. * Checks for a matching suffix at the end of a string (for ES5 and lower)
  4. * @param str Source string
  5. * @param suffix Suffix to search for in the source string
  6. * @returns Boolean indicating whether the suffix was found (true) or not (false)
  7. * @deprecated Please use native string function instead
  8. */
  9. export const EndsWith = (str, suffix) => {
  10. return str.endsWith(suffix);
  11. };
  12. /**
  13. * Checks for a matching suffix at the beginning of a string (for ES5 and lower)
  14. * @param str Source string
  15. * @param suffix Suffix to search for in the source string
  16. * @returns Boolean indicating whether the suffix was found (true) or not (false)
  17. * @deprecated Please use native string function instead
  18. */
  19. export const StartsWith = (str, suffix) => {
  20. if (!str) {
  21. return false;
  22. }
  23. return str.startsWith(suffix);
  24. };
  25. /**
  26. * Decodes a buffer into a string
  27. * @param buffer The buffer to decode
  28. * @returns The decoded string
  29. */
  30. export const Decode = (buffer) => {
  31. if (typeof TextDecoder !== "undefined") {
  32. return new TextDecoder().decode(buffer);
  33. }
  34. let result = "";
  35. for (let i = 0; i < buffer.byteLength; i++) {
  36. result += String.fromCharCode(buffer[i]);
  37. }
  38. return result;
  39. };
  40. /**
  41. * Encode a buffer to a base64 string
  42. * @param buffer defines the buffer to encode
  43. * @returns the encoded string
  44. */
  45. export const EncodeArrayBufferToBase64 = (buffer) => {
  46. const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  47. let output = "";
  48. let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  49. let i = 0;
  50. const bytes = ArrayBuffer.isView(buffer) ? new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength) : new Uint8Array(buffer);
  51. while (i < bytes.length) {
  52. chr1 = bytes[i++];
  53. chr2 = i < bytes.length ? bytes[i++] : Number.NaN;
  54. chr3 = i < bytes.length ? bytes[i++] : Number.NaN;
  55. enc1 = chr1 >> 2;
  56. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  57. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  58. enc4 = chr3 & 63;
  59. if (isNaN(chr2)) {
  60. enc3 = enc4 = 64;
  61. }
  62. else if (isNaN(chr3)) {
  63. enc4 = 64;
  64. }
  65. output += keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
  66. }
  67. return output;
  68. };
  69. /**
  70. * Converts a given base64 string as an ASCII encoded stream of data
  71. * @param base64Data The base64 encoded string to decode
  72. * @returns Decoded ASCII string
  73. */
  74. export const DecodeBase64ToString = (base64Data) => {
  75. return atob(base64Data);
  76. };
  77. /**
  78. * Converts a given base64 string into an ArrayBuffer of raw byte data
  79. * @param base64Data The base64 encoded string to decode
  80. * @returns ArrayBuffer of byte data
  81. */
  82. export const DecodeBase64ToBinary = (base64Data) => {
  83. const decodedString = DecodeBase64ToString(base64Data);
  84. const bufferLength = decodedString.length;
  85. const bufferView = new Uint8Array(new ArrayBuffer(bufferLength));
  86. for (let i = 0; i < bufferLength; i++) {
  87. bufferView[i] = decodedString.charCodeAt(i);
  88. }
  89. return bufferView.buffer;
  90. };
  91. /**
  92. * Converts a number to string and pads with preceding zeroes until it is of specified length.
  93. * @param num the number to convert and pad
  94. * @param length the expected length of the string
  95. * @returns the padded string
  96. */
  97. export const PadNumber = (num, length) => {
  98. let str = String(num);
  99. while (str.length < length) {
  100. str = "0" + str;
  101. }
  102. return str;
  103. };
  104. /**
  105. * Helper to manipulate strings
  106. */
  107. export const StringTools = {
  108. EndsWith,
  109. StartsWith,
  110. Decode,
  111. EncodeArrayBufferToBase64,
  112. DecodeBase64ToString,
  113. DecodeBase64ToBinary,
  114. PadNumber,
  115. };
  116. //# sourceMappingURL=stringTools.js.map