-
- public static isObject(item: unknown): boolean {
- return (
- Utils.isNullOrUndefined(item) === false &&
- typeof item === 'object' &&
- Array.isArray(item) === false
- );
- }
-
- public static cloneObject<T extends object>(object: T): T {
- return clone<T>(object);
- }
-
- public static hasOwnProp(object: unknown, property: PropertyKey): boolean {
- return Utils.isObject(object) && Object.hasOwn(object as object, property);
- }
-
- public static isCFEnvironment(): boolean {
- return !Utils.isNullOrUndefined(process.env.VCAP_APPLICATION);
- }
-
- public static isIterable<T>(obj: T): boolean {
- return !Utils.isNullOrUndefined(obj) ? typeof obj[Symbol.iterator] === 'function' : false;
- }
-
- public static isString(value: unknown): boolean {
- return typeof value === 'string';
- }
-
- public static isEmptyString(value: unknown): boolean {
- return (
- Utils.isNullOrUndefined(value) ||
- (Utils.isString(value) && (value as string).trim().length === 0)
- );
- }
-
- public static isNotEmptyString(value: unknown): boolean {
- return Utils.isString(value) && (value as string).trim().length > 0;
- }
-
- public static isUndefined(value: unknown): boolean {
- return value === undefined;
- }
-
- public static isNullOrUndefined(value: unknown): boolean {
- // eslint-disable-next-line eqeqeq, no-eq-null
- return value == null;
- }
-
- public static isEmptyArray(object: unknown): boolean {
- return Array.isArray(object) && object.length === 0;
- }
-
- public static isNotEmptyArray(object: unknown): boolean {
- return Array.isArray(object) && object.length > 0;
- }
-
- public static isEmptyObject(obj: object): boolean {
- if (obj?.constructor !== Object) {
- return false;
- }
- // Iterates over the keys of an object, if
- // any exist, return false.
- for (const _ in obj) {
- return false;
- }
- return true;
- }
-
- public static insertAt = (str: string, subStr: string, pos: number): string =>
- `${str.slice(0, pos)}${subStr}${str.slice(pos)}`;
-
- /**
- * @param retryNumber - the number of retries that have already been attempted
- * @returns delay in milliseconds
- */
- public static exponentialDelay(retryNumber = 0, maxDelayRatio = 0.2): number {
- const delay = Math.pow(2, retryNumber) * 100;
- const randomSum = delay * maxDelayRatio * Utils.secureRandom(); // 0-20% of the delay
- return delay + randomSum;
- }
-
- public static isPromisePending(promise: Promise<unknown>): boolean {
- return util.inspect(promise).includes('pending');
- }
-
- public static async promiseWithTimeout<T>(
- promise: Promise<T>,
- timeoutMs: number,
- timeoutError: Error,
- timeoutCallback: () => void = () => {
- /* This is intentional */
- }
- ): Promise<T> {
- // Create a timeout promise that rejects in timeout milliseconds
- const timeoutPromise = new Promise<never>((_, reject) => {
- setTimeout(() => {
- if (Utils.isPromisePending(promise)) {
- timeoutCallback();
- // FIXME: The original promise shall be canceled
- }
- reject(timeoutError);
- }, timeoutMs);
- });
-
- // Returns a race between timeout promise and the passed promise
- return Promise.race<T>([promise, timeoutPromise]);
- }
-
- /**
- * Generate a cryptographically secure random number in the [0,1[ range
- *
- * @returns
- */
- public static secureRandom(): number {
- return crypto.randomBytes(4).readUInt32LE() / 0x100000000;
- }
-
- public static JSONStringifyWithMapSupport(
- obj: Record<string, unknown> | Record<string, unknown>[] | Map<unknown, unknown>,
- space?: number
- ): string {
- return JSON.stringify(
- obj,
- (key, value: Record<string, unknown>) => {
- if (value instanceof Map) {
- return {
- dataType: 'Map',
- value: [...value],
- };
- }
- return value;
- },
- space
- );
- }
-
- /**
- * Convert websocket error code to human readable string message
- *
- * @param code - websocket error code
- * @returns human readable string message
- */
- public static getWebSocketCloseEventStatusString(code: number): string {
- if (code >= 0 && code <= 999) {
- return '(Unused)';
- } else if (code >= 1016) {
- if (code <= 1999) {
- return '(For WebSocket standard)';
- } else if (code <= 2999) {
- return '(For WebSocket extensions)';
- } else if (code <= 3999) {
- return '(For libraries and frameworks)';
- } else if (code <= 4999) {
- return '(For applications)';
+ if (fluctuationPercent === 0) {
+ return roundTo(staticValue, scale);
+ }
+ const fluctuationRatio = fluctuationPercent / 100;
+ return getRandomFloatRounded(
+ staticValue * (1 + fluctuationRatio),
+ staticValue * (1 - fluctuationRatio),
+ scale,
+ );
+};
+
+export const extractTimeSeriesValues = (timeSeries: Array<TimestampedData>): number[] => {
+ return timeSeries.map((timeSeriesItem) => timeSeriesItem.value);
+};
+
+export const isObject = (item: unknown): boolean => {
+ return (
+ isNullOrUndefined(item) === false && typeof item === 'object' && Array.isArray(item) === false
+ );
+};
+
+export const cloneObject = <T extends object>(object: T): T => {
+ return clone<T>(object);
+};
+
+export const hasOwnProp = (object: unknown, property: PropertyKey): boolean => {
+ return isObject(object) && Object.hasOwn(object as object, property);
+};
+
+export const isCFEnvironment = (): boolean => {
+ return !isNullOrUndefined(process.env.VCAP_APPLICATION);
+};
+
+export const isIterable = <T>(obj: T): boolean => {
+ return !isNullOrUndefined(obj) ? typeof obj[Symbol.iterator as keyof T] === 'function' : false;
+};
+
+const isString = (value: unknown): boolean => {
+ return typeof value === 'string';
+};
+
+export const isEmptyString = (value: unknown): boolean => {
+ return isNullOrUndefined(value) || (isString(value) && (value as string).trim().length === 0);
+};
+
+export const isNotEmptyString = (value: unknown): boolean => {
+ return isString(value) && (value as string).trim().length > 0;
+};
+
+export const isUndefined = (value: unknown): boolean => {
+ return value === undefined;
+};
+
+export const isNullOrUndefined = (value: unknown): boolean => {
+ // eslint-disable-next-line eqeqeq, no-eq-null
+ return value == null;
+};
+
+export const isEmptyArray = (object: unknown): boolean => {
+ return Array.isArray(object) && object.length === 0;
+};
+
+export const isNotEmptyArray = (object: unknown): boolean => {
+ return Array.isArray(object) && object.length > 0;
+};
+
+export const isEmptyObject = (obj: object): boolean => {
+ if (obj?.constructor !== Object) {
+ return false;
+ }
+ // Iterates over the keys of an object, if
+ // any exist, return false.
+ for (const _ in obj) {
+ return false;
+ }
+ return true;
+};
+
+export const insertAt = (str: string, subStr: string, pos: number): string =>
+ `${str.slice(0, pos)}${subStr}${str.slice(pos)}`;
+
+/**
+ * Computes the retry delay in milliseconds using an exponential backoff algorithm.
+ *
+ * @param retryNumber - the number of retries that have already been attempted
+ * @returns delay in milliseconds
+ */
+export const exponentialDelay = (retryNumber = 0, maxDelayRatio = 0.2): number => {
+ const delay = Math.pow(2, retryNumber) * 100;
+ const randomSum = delay * maxDelayRatio * secureRandom(); // 0-20% of the delay
+ return delay + randomSum;
+};
+
+const isPromisePending = (promise: Promise<unknown>): boolean => {
+ return inspect(promise).includes('pending');
+};
+
+export const promiseWithTimeout = async <T>(
+ promise: Promise<T>,
+ timeoutMs: number,
+ timeoutError: Error,
+ timeoutCallback: () => void = () => {
+ /* This is intentional */
+ },
+): Promise<T> => {
+ // Create a timeout promise that rejects in timeout milliseconds
+ const timeoutPromise = new Promise<never>((_, reject) => {
+ setTimeout(() => {
+ if (isPromisePending(promise)) {
+ timeoutCallback();
+ // FIXME: The original promise shall be canceled