service-utilities
Version:
Utility Package for FIORI UI5
62 lines (56 loc) • 1.39 kB
JavaScript
/**
* @module NumberBase64
* @description Utilities to create a Number in Base 64
* @author jpanti
* @version 1.0.0
* @created 2025-08-01
* @lastModified 2025-08-01
* @license ISC
*/
sap.ui.define([], () => {
"use strict";
const BASE64_CHARS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
class NumberBase64 {
#valueInteger;
#valueBase64;
constructor(value) {
this.setValue(value);
}
isNumeric(value) {
return !isNaN(value) && !isNaN(parseFloat(value));
}
setValue(value) {
if (!this.isNumeric(value)) {
this.#valueInteger = value;
this.#valueBase64 = this.encode(value);
} else {
this.#valueInteger = this.decode(value);
this.#valueBase64 = value;
}
}
getInteger() {
return this.#valueInteger;
}
getValue() {
return this.#valueBase64;
}
encode(integer) {
if (integer === 0) return BASE64_CHARS[0];
var base64 = "";
while (integer > 0) {
base64 = BASE64_CHARS[integer % 64] + base64;
integer = Math.floor(integer / 64);
}
return base64;
}
decode(base64) {
var integer = 0;
for (var i = 0; i < base64.length; i++) {
integer = integer * 64 + BASE64_CHARS.indexOf(base64[i]);
}
return integer;
}
}
return NumberBase64;
});