sanex3339 vor 9 Jahren
Ursprung
Commit
c20239be58
2 geänderte Dateien mit 19 neuen und 4 gelöschten Zeilen
  1. 7 2
      dist/src/Utils.js
  2. 12 2
      src/Utils.ts

+ 7 - 2
dist/src/Utils.js

@@ -21,7 +21,11 @@ class Utils {
         return newArray;
     }
     static decToHex(dec) {
-        return (dec + Math.pow(16, 6)).toString(16).substr(-6).replace(Utils.hexRepetitiveZerosRegExp, '');
+        const decToHexSliceValue = -6, exponent = 6, radix = 16;
+        return (dec + Math.pow(radix, exponent))
+            .toString(radix)
+            .substr(decToHexSliceValue)
+            .replace(Utils.hexRepetitiveZerosRegExp, '');
     }
     static getRandomInteger(min, max) {
         return Math.floor(Math.random() * (max - min + 1)) + min;
@@ -37,8 +41,9 @@ class Utils {
         return obj;
     }
     static stringToUnicode(string) {
+        const radix = 16, unicodeSliceValue = -4;
         return `'${string.replace(/[\s\S]/g, (escape) => {
-            return `\\u${('0000' + escape.charCodeAt(0).toString(16)).slice(-4)}`;
+            return `\\u${('0000' + escape.charCodeAt(0).toString(radix)).slice(unicodeSliceValue)}`;
         })}'`;
     }
 }

+ 12 - 2
src/Utils.ts

@@ -45,7 +45,14 @@ export class Utils {
      * @returns {string}
      */
     public static decToHex(dec: number): string {
-        return (dec + Math.pow(16, 6)).toString(16).substr(-6).replace(Utils.hexRepetitiveZerosRegExp, '');
+        const decToHexSliceValue: number = -6,
+            exponent: number = 6,
+            radix: number = 16;
+
+        return (dec + Math.pow(radix, exponent))
+            .toString(radix)
+            .substr(decToHexSliceValue)
+            .replace(Utils.hexRepetitiveZerosRegExp, '');
     }
 
     /**
@@ -90,8 +97,11 @@ export class Utils {
      * @returns {string}
      */
     public static stringToUnicode (string: string): string {
+        const radix: number = 16,
+            unicodeSliceValue: number = -4;
+
         return `'${string.replace(/[\s\S]/g, (escape: string): string => {
-            return `\\u${('0000' + escape.charCodeAt(0).toString(16)).slice(-4)}`;
+            return `\\u${('0000' + escape.charCodeAt(0).toString(radix)).slice(unicodeSliceValue)}`;
         })}'`;
     }
 }